Skip to content

Commit bbdfb6b

Browse files
authored
Merge pull request #3606 from bhcleek/sameids/off-by-one
sameids: fix trailing identifier highlighting
2 parents 9732792 + c5b51a0 commit bbdfb6b

File tree

4 files changed

+46
-11
lines changed

4 files changed

+46
-11
lines changed

autoload/go/config_test.vim

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func! Test_SetBuildTags() abort
2121

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

24-
let l:expected = [0, 5, 1, 0]
24+
let l:expected = [0, 5, 6, 0]
2525
call assert_notequal(l:expected, l:jumpstart)
2626

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

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

6363
call go#def#Jump('', 0)

autoload/go/lsp.vim

+21-3
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,14 @@ function! s:definitionHandler(next, msg) abort dict
615615

616616
" gopls returns a []Location; just take the first one.
617617
let l:msg = a:msg[0]
618-
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')]]
618+
619+
let l:line = s:lineinfile(go#path#FromURI(l:msg.uri), l:msg.range.start.line+1)
620+
if l:line is -1
621+
call go#util#Warn('could not find definition')
622+
return
623+
endif
624+
625+
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')]]
619626
call call(a:next, l:args)
620627
endfunction
621628

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

642649
" gopls returns a []Location; just take the first one.
643650
let l:msg = a:msg[0]
644-
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')]]
651+
652+
let l:line = s:lineinfile(go#path#FromURI(l:msg.uri), l:msg.range.start.line+1)
653+
if l:line is -1
654+
call go#util#Warn('could not find definition')
655+
return
656+
endif
657+
658+
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')]]
645659
call call(a:next, l:args)
646660
endfunction
647661

@@ -2035,7 +2049,11 @@ function! s:lineinfile(fname, line) abort
20352049
if l:bufnr == -1 || len(l:bufinfo) == 0 || l:bufinfo[0].loaded == 0
20362050
let l:filecontents = readfile(a:fname, '', a:line)
20372051
else
2038-
let l:filecontents = getbufline(a:fname, a:line)
2052+
if exists('*getbufoneline')
2053+
let l:filecontents = [getbufoneline(a:fname, a:line)]
2054+
else
2055+
let l:filecontents = getbufline(a:fname, a:line)
2056+
endif
20392057
endif
20402058

20412059
if len(l:filecontents) == 0

autoload/go/lsp/lsp.vim

+6-6
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,17 @@ function! s:character(line, col) abort
2828
endfunction
2929

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

3838
let l:remaining = a:units
3939
let l:str = ''
4040
for l:rune in split(a:content, '\zs')
41-
if l:remaining < 0
41+
if l:remaining <= 0
4242
break
4343
endif
4444
let l:remaining -= 1
@@ -48,7 +48,7 @@ function! go#lsp#lsp#PositionOf(content, units, ...) abort
4848
let l:str = l:str . l:rune
4949
endfor
5050

51-
return len(l:str)
51+
return len(l:str) + 1
5252
endfunction
5353

5454
function! go#lsp#lsp#SeverityToErrorType(severity) abort

autoload/go/lsp/lsp_test.vim

+17
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,23 @@ function! Test_PositionOf_Simple()
99
call assert_equal(4, l:actual)
1010
endfunc
1111

12+
function! Test_PositionOf_Start()
13+
let l:str = 'abcd'
14+
let l:actual = go#lsp#lsp#PositionOf(l:str, 0)
15+
call assert_equal(l:actual, 1)
16+
" subtract one, because PositionOf returns a one-based cursor position and
17+
" while string indices are zero based.
18+
call assert_equal(l:str[l:actual-1], 'a')
19+
endfunc
20+
21+
function! Test_PositionOf_End()
22+
let l:str = 'abcd'
23+
let l:actual = go#lsp#lsp#PositionOf(l:str, 3)
24+
call assert_equal(l:actual, 4)
25+
" subtract one, because PositionOf returns a one-based cursor position and
26+
" while string indices are zero based.
27+
call assert_equal(l:str[l:actual-1], 'd')
28+
endfunc
1229

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

0 commit comments

Comments
 (0)