Skip to content

Commit

Permalink
Merge pull request #3606 from bhcleek/sameids/off-by-one
Browse files Browse the repository at this point in the history
sameids: fix trailing identifier highlighting
  • Loading branch information
bhcleek authored Dec 5, 2023
2 parents 9732792 + c5b51a0 commit bbdfb6b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 11 deletions.
4 changes: 2 additions & 2 deletions autoload/go/config_test.vim
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func! Test_SetBuildTags() abort

let l:expectedfilename = printf('%s/foo.go', l:dir)

let l:expected = [0, 5, 1, 0]
let l:expected = [0, 5, 6, 0]
call assert_notequal(l:expected, l:jumpstart)

call go#def#Jump('', 0)
Expand Down Expand Up @@ -57,7 +57,7 @@ func! Test_SetBuildTags() abort
call assert_equal(l:jumpstart, getpos('.'))

let l:expectedfilename = printf('%s/constrainedfoo.go', l:dir)
let l:expected = [0, 6, 1, 0]
let l:expected = [0, 6, 6, 0]
call assert_notequal(l:expected, l:jumpstart)

call go#def#Jump('', 0)
Expand Down
24 changes: 21 additions & 3 deletions autoload/go/lsp.vim
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,14 @@ function! s:definitionHandler(next, msg) abort dict

" gopls returns a []Location; just take the first one.
let l:msg = a:msg[0]
let l:args = [[printf('%s:%d:%d: %s', go#path#FromURI(l:msg.uri), l:msg.range.start.line+1, go#lsp#lsp#PositionOf(getline(l:msg.range.start.line+1), l:msg.range.start.character), 'lsp does not supply a description')]]

let l:line = s:lineinfile(go#path#FromURI(l:msg.uri), l:msg.range.start.line+1)
if l:line is -1
call go#util#Warn('could not find definition')
return
endif

let l:args = [[printf('%s:%d:%d: %s', go#path#FromURI(l:msg.uri), l:msg.range.start.line+1, go#lsp#lsp#PositionOf(l:line, l:msg.range.start.character), 'lsp does not supply a description')]]
call call(a:next, l:args)
endfunction

Expand All @@ -641,7 +648,14 @@ function! s:typeDefinitionHandler(next, msg) abort dict

" gopls returns a []Location; just take the first one.
let l:msg = a:msg[0]
let l:args = [[printf('%s:%d:%d: %s', go#path#FromURI(l:msg.uri), l:msg.range.start.line+1, go#lsp#lsp#PositionOf(getline(l:msg.range.start.line+1), l:msg.range.start.character), 'lsp does not supply a description')]]

let l:line = s:lineinfile(go#path#FromURI(l:msg.uri), l:msg.range.start.line+1)
if l:line is -1
call go#util#Warn('could not find definition')
return
endif

let l:args = [[printf('%s:%d:%d: %s', go#path#FromURI(l:msg.uri), l:msg.range.start.line+1, go#lsp#lsp#PositionOf(l:line, l:msg.range.start.character), 'lsp does not supply a description')]]
call call(a:next, l:args)
endfunction

Expand Down Expand Up @@ -2035,7 +2049,11 @@ function! s:lineinfile(fname, line) abort
if l:bufnr == -1 || len(l:bufinfo) == 0 || l:bufinfo[0].loaded == 0
let l:filecontents = readfile(a:fname, '', a:line)
else
let l:filecontents = getbufline(a:fname, a:line)
if exists('*getbufoneline')
let l:filecontents = [getbufoneline(a:fname, a:line)]
else
let l:filecontents = getbufline(a:fname, a:line)
endif
endif

if len(l:filecontents) == 0
Expand Down
12 changes: 6 additions & 6 deletions autoload/go/lsp/lsp.vim
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ function! s:character(line, col) abort
endfunction

" go#lsp#PositionOf returns len(content[0:units]) where units is utf-16 code
" units. This is mostly useful for converting LSP text position to vim
" position.
" units. This is mostly useful for converting zero-based LSP text position to
" vim one-based position.
function! go#lsp#lsp#PositionOf(content, units, ...) abort
if a:units == 0
return 1
if len(a:content) is 0
return 0
endif

let l:remaining = a:units
let l:str = ''
for l:rune in split(a:content, '\zs')
if l:remaining < 0
if l:remaining <= 0
break
endif
let l:remaining -= 1
Expand All @@ -48,7 +48,7 @@ function! go#lsp#lsp#PositionOf(content, units, ...) abort
let l:str = l:str . l:rune
endfor

return len(l:str)
return len(l:str) + 1
endfunction

function! go#lsp#lsp#SeverityToErrorType(severity) abort
Expand Down
17 changes: 17 additions & 0 deletions autoload/go/lsp/lsp_test.vim
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,23 @@ function! Test_PositionOf_Simple()
call assert_equal(4, l:actual)
endfunc

function! Test_PositionOf_Start()
let l:str = 'abcd'
let l:actual = go#lsp#lsp#PositionOf(l:str, 0)
call assert_equal(l:actual, 1)
" subtract one, because PositionOf returns a one-based cursor position and
" while string indices are zero based.
call assert_equal(l:str[l:actual-1], 'a')
endfunc

function! Test_PositionOf_End()
let l:str = 'abcd'
let l:actual = go#lsp#lsp#PositionOf(l:str, 3)
call assert_equal(l:actual, 4)
" subtract one, because PositionOf returns a one-based cursor position and
" while string indices are zero based.
call assert_equal(l:str[l:actual-1], 'd')
endfunc

function! Test_PositionOf_MultiByte()
" ⌘ is U+2318, which encodes to three bytes in utf-8 and 1 code unit in
Expand Down

0 comments on commit bbdfb6b

Please sign in to comment.