From 2e30a3fe7d172fdb0a5e395a837a667e4620b4a1 Mon Sep 17 00:00:00 2001
From: Billie Cleek <bhcleek@gmail.com>
Date: Tue, 5 Dec 2023 07:02:56 -0800
Subject: [PATCH] sameids: fix trailing identifier highlighting

Fix same id highlighting of identifiers that are at the very end of a
line.
---
 autoload/go/lsp/lsp.vim      | 12 ++++++------
 autoload/go/lsp/lsp_test.vim | 17 +++++++++++++++++
 2 files changed, 23 insertions(+), 6 deletions(-)

diff --git a/autoload/go/lsp/lsp.vim b/autoload/go/lsp/lsp.vim
index c0cf3e082c..7138155623 100644
--- a/autoload/go/lsp/lsp.vim
+++ b/autoload/go/lsp/lsp.vim
@@ -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
@@ -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
diff --git a/autoload/go/lsp/lsp_test.vim b/autoload/go/lsp/lsp_test.vim
index 24a2a7474e..49503f6a6a 100644
--- a/autoload/go/lsp/lsp_test.vim
+++ b/autoload/go/lsp/lsp_test.vim
@@ -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