Friday, October 14, 2022

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

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/264f2b7f-3f8d-453a-a38b-5b20e5c45023n%40googlegroups.com.

No comments:

Post a Comment