Saturday, October 15, 2022

Re: E122 Def Function Vim9 already exists due to reentrancy autocommand side effect

Hum, seems to be not fixed, I got this error

 _vimrc: 
  autocmd BufEnter *.cpp,*.hpp,*.h,*.c  call that.UnderLineHeaders()


_autoload/myhelper/myhelper.vim: 
export def UnderLineHeaders(): void

  echomsg 'UnderLineHeaders called for buffer ' .. expand("%:p")

   some stuff....
  
  if !exists('*g:OpeningHeader')    # not enough to prevent the error above
    def g:OpeningHeader(): void
      some other stuff
  enddef
enddef


How to prevent 




Le vendredi 14 octobre 2022 à 22:34:07 UTC+2, N V a écrit :
Solved by this prevent conditionnal test

if !exists('*MyFunction') fun MyFunction ... endfun endif as described https://stackoverflow.com/questions/31663750/function-already-exists-vim

Le vendredi 14 octobre 2022 à 22:25:29 UTC+2, N V a écrit :
Hi,

I got reentrancy side effect with a bufenter autocommand on header considering these feature.

  • First def func1 is processing current buffer (cpp, h) to add text_prop on header file and underline them. => works Fin
  • Inside this   func1  declare  func2 to search and open underlined header double cliked.

The vim9script that do the job :
export def UnderLineHeaders(): void

  var headers: dict<string> = {}
      .. some stuff
      .. some stuff


  def g:OpeningHeader(): void
      .. some stuff 
      .. some stuff
  enddef

  nnoremap <buffer> <2-Leftmouse> :call g:OpeningHeader()<CR>
enddef



The error I got when double clik on header :
Error detected while processing function OpeningHeader[21]..BufEnter Autocommands for "*.h"..function nvhelper#UnderLineHeaders:
line   28:
E122: Function OpeningHeader already exists, add ! to replace it         


How can I force the g:OpeningHeader def func ?
Thank you
Nicolas


The complete code 
export def UnderLineHeaders(): void

  var patterns: dict<string> = { 'header': '\w\+\.h' }
  var headers: dict<string> = {}

  var list_of_line: list<string> = getline(1, '$')
  var lineno: number = 1
  var res: string = ''

  # identify headers' lines
  for line in list_of_line
    res = matchstr(line, patterns.header)
    if res != ""
      headers[lineno] = res
    endif
    lineno = lineno + 1
  endfor

  # some declarations before adding text prop
  call prop_type_delete('header')
  hi Underlined_Header    ctermbg=NONE ctermfg=NONE  cterm=UNDERLINE guibg=NONE    guifg=NONE      gui=UNDERLINE
  call prop_type_add('header', {'highlight': 'Underlined_Header'})

  # add prop on headers
  var col: number = -1
  for key in keys(headers)
    lineno = key->str2nr()
    col = stridx(getline(lineno), headers[key]) + 1
    call prop_add(lineno, col, {'length': headers[key]->len(), 'type': 'header'})
  endfor

  def g:OpeningHeader(): void
    var curlnum: number = getcurpos()[1]
    var key: string = getcurpos()[1]->string()
    if has_key(headers, key)
      var searchedfile: string = headers[getcurpos()[1]->string()]
      echomsg 'File to search for is ' .. searchedfile

      var currentpath: list<string> = expand("%:p:h")->split('\\')

      var idxfolder: number = 1
      var maxfolders: number = currentpath->len()
      var foundedfile: string = ''
      while  idxfolder < maxfolders
        foundedfile = globpath( currentpath[ : -1 * idxfolder ]->join('\'), "**/" .. searchedfile )
        if foundedfile != ''
          break
        endif
        idxfolder += 1
      endwhile

      echomsg foundedfile
      exe 'e ' .. foundedfile
    else
      echomsg 'Sorry no File to search for.'
    endif
  enddef
  # add double clickable feature => opening header
  nnoremap <buffer> <2-Leftmouse> :call g:OpeningHeader()<CR>
enddef

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/b0f9a9c9-7f78-4c9b-82d6-71fe67a52801n%40googlegroups.com.

No comments: