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

pull/322/head
wrvsrx 1 year 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" "path/filepath"
"regexp" "regexp"
"strings" "strings"
"unicode/utf16"
"github.com/mickael-menu/zk/internal/core" "github.com/mickael-menu/zk/internal/core"
"github.com/mickael-menu/zk/internal/util" "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. // LookBehind returns the n characters before the given position, on the same line.
func (d *document) LookBehind(pos protocol.Position, length int) string { func (d *document) LookBehind(pos protocol.Position, length int) string {
line, ok := d.GetLine(int(pos.Line)) line, ok := d.GetLine(int(pos.Line))
utf16Bytes := utf16.Encode([]rune(line))
if !ok { if !ok {
return "" return ""
} }
charIdx := int(pos.Character) charIdx := int(pos.Character)
if length > charIdx { 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. // LookForward returns the n characters after the given position, on the same line.
func (d *document) LookForward(pos protocol.Position, length int) string { func (d *document) LookForward(pos protocol.Position, length int) string {
line, ok := d.GetLine(int(pos.Line)) line, ok := d.GetLine(int(pos.Line))
utf16Bytes := utf16.Encode([]rune(line))
if !ok { if !ok {
return "" return ""
} }
lineLength := len(line) lineLength := len(utf16Bytes)
charIdx := int(pos.Character) charIdx := int(pos.Character)
if lineLength <= charIdx+length { 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(`\[?\[\[(.+?)(?: *\| *(.+?))?\]\]`) var wikiLinkRegex = regexp.MustCompile(`\[?\[\[(.+?)(?: *\| *(.+?))?\]\]`)

Loading…
Cancel
Save