Fix LSP positions using UTF-16 offsets (#317)

pull/322/head
wrvsrx 12 months ago committed by GitHub
parent e26ac5133e
commit 6252e51595
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -5,6 +5,7 @@ import (
"path/filepath"
"regexp"
"strings"
"unicode/utf16"
"github.com/mickael-menu/zk/internal/core"
"github.com/mickael-menu/zk/internal/util"
@ -131,30 +132,32 @@ func (d *document) GetLines() []string {
// LookBehind returns the n characters before the given position, on the same line.
func (d *document) LookBehind(pos protocol.Position, length int) string {
line, ok := d.GetLine(int(pos.Line))
utf16Bytes := utf16.Encode([]rune(line))
if !ok {
return ""
}
charIdx := int(pos.Character)
if length > charIdx {
return line[0:charIdx]
return string(utf16.Decode(utf16Bytes[0:charIdx]))
}
return line[(charIdx - length):charIdx]
return string(utf16.Decode(utf16Bytes[(charIdx - length):charIdx]))
}
// LookForward returns the n characters after the given position, on the same line.
func (d *document) LookForward(pos protocol.Position, length int) string {
line, ok := d.GetLine(int(pos.Line))
utf16Bytes := utf16.Encode([]rune(line))
if !ok {
return ""
}
lineLength := len(line)
lineLength := len(utf16Bytes)
charIdx := int(pos.Character)
if lineLength <= charIdx+length {
return line[charIdx:]
return string(utf16.Decode(utf16Bytes[charIdx:]))
}
return line[charIdx:(charIdx + length)]
return string(utf16.Decode(utf16Bytes[charIdx:(charIdx + length)]))
}
var wikiLinkRegex = regexp.MustCompile(`\[?\[\[(.+?)(?: *\| *(.+?))?\]\]`)

Loading…
Cancel
Save