mirror of
https://github.com/mickael-menu/zk
synced 2024-11-11 07:10:25 +00:00
Fix LSP positions using UTF-16 offsets (#317)
This commit is contained in:
parent
e26ac5133e
commit
6252e51595
@ -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…
Reference in New Issue
Block a user