Fix LSP link recognition with unicode (#238)

pull/237/head^2
Mickaël Menu 2 years ago committed by GitHub
parent 53aabce1ed
commit 8bfafe5dab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -26,9 +26,9 @@ All notable changes to this project will be documented in this file.
### Fixed
* [#233](https://github.com/mickael-menu/zk/issues/233) Hide index progress in non-interactive shells.
* [#235](https://github.com/mickael-menu/zk/issues/235) Fix LSP link recognition with unicode (contributed by [@zkbpkp](https://github.com/mickael-menu/zk/issues/235)).
* [#239](https://github.com/mickael-menu/zk/discussions/239) Support standard input via shell redirection with `zk new`.
## 0.10.1
### Changed

@ -189,6 +189,10 @@ func (d *document) DocumentLinks() ([]documentLink, error) {
return
}
// Go regexes work with bytes, but the LSP client expects character indexes.
start = strutil.ByteIndexToRuneIndex(line, start)
end = strutil.ByteIndexToRuneIndex(line, end)
links = append(links, documentLink{
Href: href,
Range: protocol.Range{

@ -148,3 +148,14 @@ func CopyList(list []string) []string {
copy(out, list)
return out
}
func ByteIndexToRuneIndex(s string, i int) int {
res := 0
for j, _ := range s {
if j >= i {
break
}
res += 1
}
return res
}

@ -151,3 +151,28 @@ func TestWordAt(t *testing.T) {
test("one\ttwo\tthree", 5, "two")
test("one @:~two three", 5, "@:~two")
}
func TestByteIndexToRuneIndex(t *testing.T) {
test := func(s string, index int, expected int) {
assert.Equal(t, ByteIndexToRuneIndex(s, index), expected)
}
test("", 0, 0)
source := "une étoile bleuâtre"
test(source, -2, 0)
test(source, -1, 0)
test(source, 0, 0)
test(source, 1, 1)
test(source, 4, 4)
test(source, 5, 5)
test(source, 6, 5)
test(source, 7, 6)
test(source, 16, 15)
test(source, 17, 16)
test(source, 18, 16)
test(source, 19, 17)
test(source, 20, 18)
test(source, 21, 19)
test(source, 22, 19)
}

Loading…
Cancel
Save