~maxgyver83/hare-jump.vim

83214fe4a47c7294e5879b4cc6613eb7bb4a02ce — Max Schillinger 3 months ago d5417b6 main
Fix wrong jumps when searching for enum types

Add function s:SearchTypeDefinition to search for type definitions only.
1 files changed, 26 insertions(+), 9 deletions(-)

M plugin/harejump.vim
M plugin/harejump.vim => plugin/harejump.vim +26 -9
@@ 34,10 34,10 @@ function! harejump#JumpToDefinition(identifier = '')
  endif

  " search symbol in same module
  if len(tokens) == 1 && SearchSymbol(tokens[0], [expand("%:h")], 0)
  if len(tokens) == 1 && s:SearchAnyDefinition(tokens[0], [expand("%:h")], 0)
    return 1
  " in case token[-1] is an enum type value:
  elseif len(tokens) == 2 && SearchSymbol(tokens[-2], [expand("%:h")], 0)
  elseif len(tokens) == 2 && s:SearchTypeDefinition(tokens[-2], [expand("%:h")], 0)
    call search(tokens[-1], 'W')
    return 1
  end


@@ 46,7 46,7 @@ function! harejump#JumpToDefinition(identifier = '')
  if len(tokens) >= 2
    let [module, symbol] = s:IsFullyQualified(tokens)
    if module != '' && symbol != ''
      if SearchSymbol(symbol, [module], 1)
      if s:SearchAnyDefinition(symbol, [module], 1)
        return 1
      endif
    endif


@@ 62,7 62,7 @@ function! harejump#JumpToDefinition(identifier = '')
    let is_enum = 1
  endif
  if reldir != ''
    if SearchSymbol(symbol, [reldir], 1)
    if s:SearchAnyDefinition(symbol, [reldir], 1)
      if is_enum
        call search(tokens[-1], 'W')
      endif


@@ 76,9 76,16 @@ function! harejump#JumpToDefinition(identifier = '')
    let reldir = s:FindModuleImport(module)
    if reldir != ''
      let symbol = join(tokens[1:], '::')
      if SearchSymbol(symbol, [reldir], 1)
      if s:SearchAnyDefinition(symbol, [reldir], 1)
        return 1
      endif
      if len(tokens) >= 3
        let symbol = join(tokens[1:-2], '::')
        if s:SearchTypeDefinition(symbol, [reldir], 1)
          call search(tokens[-1], 'W')
          return 1
        endif
      endif
    endif
  endif



@@ 86,7 93,7 @@ function! harejump#JumpToDefinition(identifier = '')
  let star_imports = s:FindStarImports()
  if len(star_imports)
    let symbol = join(tokens, '::')
    return SearchSymbol(symbol, star_imports, 1)
    return s:SearchAnyDefinition(symbol, star_imports, 1)
  endif

  return 0


@@ 239,8 246,18 @@ function! s:GetIdentifierUnderCursor()
  return split(str, "::")
endfunction

function! SearchSymbol(fn, reldirs, searchharepath = 0)
  let pattern = '(^|export )(fn|type|let|const|def) ' .. a:fn .. '\b'
function! s:SearchAnyDefinition(symbol, reldirs, searchharepath = 0)
  let pattern = '(^|export )(fn|type|let|const|def) ' .. a:symbol .. '\b'
  return SearchDefinition(pattern, a:reldirs, a:searchharepath)
endfunction

function! s:SearchTypeDefinition(symbol, reldirs, searchharepath = 0)
  let pattern = '(^|export )type ' .. a:symbol .. '\b'
  return SearchDefinition(pattern, a:reldirs, a:searchharepath)
endfunction

function! SearchDefinition(pattern, reldirs, searchharepath = 0)
  " let pattern = '(^|export )(fn|type|let|const|def) ' .. a:fn .. '\b'
  let reldirs = a:reldirs
  if empty(reldirs)
    echo "Error: reldirs shouldn't be an empty list!"


@@ 266,7 283,7 @@ function! SearchSymbol(fn, reldirs, searchharepath = 0)
    endfor
  endif

  return JumpToDefinition(shellescape(pattern), dirs)
  return JumpToDefinition(shellescape(a:pattern), dirs)
endfunction

function! JumpToDefinition(pattern, dirs)