From 6252e51595151039271270bc3ce70c3450f92591 Mon Sep 17 00:00:00 2001 From: wrvsrx <42770726+wrvsrx@users.noreply.github.com> Date: Thu, 18 May 2023 20:04:21 +0800 Subject: [PATCH] Fix LSP positions using UTF-16 offsets (#317) --- internal/adapter/lsp/document.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/internal/adapter/lsp/document.go b/internal/adapter/lsp/document.go index fbe6d3d..05233fb 100644 --- a/internal/adapter/lsp/document.go +++ b/internal/adapter/lsp/document.go @@ -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(`\[?\[\[(.+?)(?: *\| *(.+?))?\]\]`)