This repository has been archived by the owner on May 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocument.go
145 lines (127 loc) · 3.52 KB
/
document.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"net/url"
"runtime"
"strings"
"github.com/pkg/errors"
"github.com/tliron/glsp"
protocol "github.com/tliron/glsp/protocol_3_16"
)
func (s *Lsp) DidOpen(params protocol.DidOpenTextDocumentParams, notify glsp.NotifyFunc) (*document, error) {
uri := params.TextDocument.URI
path, err := normalizePath(uri)
if err != nil {
return nil, err
}
doc := &document{
URI: uri,
Path: path,
Content: params.TextDocument.Text,
}
s.documents[path] = doc
return doc, nil
}
func (s *Lsp) Get(pathOrURI string) (*document, bool) {
path, err := normalizePath(pathOrURI)
if err != nil {
return nil, false
}
d, ok := s.documents[path]
return d, ok
}
func uriToPath(uri string) (string, error) {
s := strings.ReplaceAll(uri, "%5C", "/")
parsed, err := url.Parse(s)
if err != nil {
return "", err
}
if parsed.Scheme != "file" {
return "", errors.New("URI was not a file:// URI")
}
if runtime.GOOS == "windows" {
return "", errors.New("Windows is not supported")
}
return parsed.Path, nil
}
func normalizePath(pathOrUri string) (string, error) {
path, err := uriToPath(pathOrUri)
if err != nil {
return "", errors.Wrapf(err, "unable to parse URI: %s", pathOrUri)
}
return path, nil
}
// document represents an opened file.
type document struct {
URI protocol.DocumentUri
Path string
NeedsRefreshDiagnostics bool
Content string
lines []string
}
// ApplyChanges updates the content of the document from LSP textDocument/didChange events.
func (d *document) ApplyChanges(changes []interface{}) {
for _, change := range changes {
switch c := change.(type) {
case protocol.TextDocumentContentChangeEvent:
startIndex, endIndex := c.Range.IndexesIn(d.Content)
d.Content = d.Content[:startIndex] + c.Text + d.Content[endIndex:]
case protocol.TextDocumentContentChangeEventWhole:
d.Content = c.Text
}
}
d.lines = nil
}
// WordAt returns the word found at the given location.
func (d *document) WordAt(pos protocol.Position) string {
line, ok := d.GetLine(int(pos.Line))
if !ok {
return ""
}
return WordAt(line, int(pos.Character))
}
// ContentAtRange returns the document text at given range.
func (d *document) ContentAtRange(rng protocol.Range) string {
startIndex, endIndex := rng.IndexesIn(d.Content)
return d.Content[startIndex:endIndex]
}
// GetLine returns the line at the given index.
func (d *document) GetLine(index int) (string, bool) {
lines := d.GetLines()
if index < 0 || index > len(lines) {
return "", false
}
return lines[index], true
}
// GetLines returns all the lines in the document.
func (d *document) GetLines() []string {
if d.lines == nil {
// We keep \r on purpose, to avoid messing up position conversions.
d.lines = strings.Split(d.Content, "\n")
}
return d.lines
}
// 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))
if !ok {
return ""
}
charIdx := int(pos.Character)
if length > charIdx {
return line[0:charIdx]
}
return line[(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))
if !ok {
return ""
}
lineLength := len(line)
charIdx := int(pos.Character)
if lineLength <= charIdx+length {
return line[charIdx:]
}
return line[charIdx:(charIdx + length)]
}