Skip to content

Commit

Permalink
Merge pull request #3644 from bhcleek/lsp/multi-byte-edit-handling
Browse files Browse the repository at this point in the history
lsp: fix text edit handling
  • Loading branch information
bhcleek authored Feb 25, 2024
2 parents 0e97556 + a3e0e9c commit 99533a6
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 5 deletions.
6 changes: 3 additions & 3 deletions autoload/go/lsp.vim
Original file line number Diff line number Diff line change
Expand Up @@ -1942,15 +1942,15 @@ function s:applyTextEdits(bufnr, msg) abort
let l:startcontent = ''
if l:msg.range.start.character > 0
let l:startcontent = getline(l:startline)
let l:preSliceEnd = go#lsp#lsp#PositionOf(l:startcontent, l:msg.range.start.character-1) - 1
let l:startcontent = l:startcontent[:l:preSliceEnd]
let l:preSliceEnd = go#lsp#lsp#PositionOf(l:startcontent, l:msg.range.start.character-1)
let l:startcontent = strcharpart(l:startcontent, 0, l:preSliceEnd) "l:startcontent[:l:preSliceEnd]
endif

let l:endcontent = getline(l:endline)
let l:postSliceStart = 0
if l:msg.range.end.character > 0
let l:postSliceStart = go#lsp#lsp#PositionOf(l:endcontent, l:msg.range.end.character-1)
let l:endcontent = l:endcontent[(l:postSliceStart):]
let l:endcontent = strcharpart(l:endcontent, l:postSliceStart) "l:endcontent[(l:postSliceStart):]
endif

" There isn't an easy way to replace the text in a byte or character
Expand Down
4 changes: 2 additions & 2 deletions autoload/go/lsp/lsp_test.vim
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ 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.
" subtract one, because PositionOf returns a one-based cursor position while
" string indices are zero based.
call assert_equal(l:str[l:actual-1], 'a')
endfunc

Expand Down
18 changes: 18 additions & 0 deletions autoload/go/lsp_test.vim
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,24 @@ func! Test_Format_SingleNewline() abort
endtry
endfunc

func! Test_Format_Multibyte() abort
let l:wd = getcwd()
try
let expected = join(readfile("test-fixtures/lsp/fmt/multibyte_golden.go"), "\n")
let l:tmp = gotest#load_fixture('lsp/fmt/multibyte.go')

call go#lsp#Format()

" this should now contain the formatted code
let actual = join(go#util#GetLines(), "\n")

call assert_equal(expected, actual)
finally
call go#util#Chdir(l:wd)
call delete(l:tmp, 'rf')
endtry
endfunc

func! Test_Imports() abort
let l:wd = getcwd()
try
Expand Down
29 changes: 29 additions & 0 deletions autoload/go/test-fixtures/lsp/fmt/multibyte.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import "fmt"

func main() {
fmt.Println("vim-go") // 中 asdfasdf
fmt.Println("vim-go") // 中
fmt.Println("vim-go") // 中a
fmt.Println("vim-go") // 中 a
fmt.Println("vim-go") // 中

fmt.Println("vim-go") // ⌘ asdfasdf
fmt.Println("vim-go") // ⌘
fmt.Println("vim-go") // ⌘ a
fmt.Println("vim-go") // ⌘ a
fmt.Println("vim-go") // ⌘

fmt.Println("vim-go") // é asdfasdf
fmt.Println("vim-go") // é
fmt.Println("vim-go") // é a
fmt.Println("vim-go") // é a
fmt.Println("vim-go") // é






}
24 changes: 24 additions & 0 deletions autoload/go/test-fixtures/lsp/fmt/multibyte_golden.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import "fmt"

func main() {
fmt.Println("vim-go") // 中 asdfasdf
fmt.Println("vim-go") // 中
fmt.Println("vim-go") // 中a
fmt.Println("vim-go") // 中 a
fmt.Println("vim-go") // 中

fmt.Println("vim-go") // ⌘ asdfasdf
fmt.Println("vim-go") // ⌘
fmt.Println("vim-go") // ⌘ a
fmt.Println("vim-go") // ⌘ a
fmt.Println("vim-go") // ⌘

fmt.Println("vim-go") // é asdfasdf
fmt.Println("vim-go") // é
fmt.Println("vim-go") // é a
fmt.Println("vim-go") // é a
fmt.Println("vim-go") // é

}

0 comments on commit 99533a6

Please sign in to comment.