-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
271 lines (227 loc) · 6.9 KB
/
file.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package tekton
import (
"fmt"
"regexp"
"strings"
"sync"
"github.com/cezarguimaraes/tekton-ls/internal/file"
"github.com/goccy/go-yaml"
"github.com/goccy/go-yaml/ast"
"github.com/goccy/go-yaml/parser"
protocol "github.com/tliron/glsp/protocol_3_16"
)
type StringMap = map[string]interface{}
// Meta is the root interface for any given Tekton object understood by this
// language server.
type Meta interface {
// Name returns the name of the given Tekton object.
Name() string
// Documentation returns the Markdown documentation of the given Tekton
// object.
Documentation() string
// Completions returns a list of possible completion suggestions
// associated with the Tekton object.
Completions() []completion
}
// File provides operation on a YAML file containing any number of
// Tekton Documents - separated by `---` as defined by the YAML spec.
type File struct {
// TextDocument of this file.
file.TextDocument
// Workspace containing this file.
workspace *Workspace
// uri is the TextDocument URI of this file.
uri string
// ast is the abstract syntax tree of this YAML file.
ast *ast.File
parseError error
// docs is the list of tekton.Document contained in this file.
docs []*Document
// danglingRefs is a set containing the name of any reference to which
// no identifier could be mapped. This set is used to identify which
// files have to be re-linted whenever a file in the workspace is changed.
danglingRefs map[string]struct{}
}
// Document provides operation on a single YAML document containing a Tekton
// resource.
type Document struct {
// TextDocument is the LSP TextDocument which contains this document.
file.TextDocument
// file containing this document.
file *File
// offset is the starting position of the Document in its containing File.
offset int
// size of the document in its containing File.
size int
// ast is the abstract syntax tree of this YAML document.
ast *ast.DocumentNode
// identifiers is the list of identifiers (i.e definitions) in this file.
identifiers []*identifier
// references is the list of possible references to identifiers in this file.
references []reference
}
// helmSanitizerRegexp is the regular expression used to identify Helm template
// language directives. These directives are filtered out of the file contents
// to prevent YAML syntax errors.
var helmSanitizerRegexp = regexp.MustCompile(`{{.*?}}`)
// NewFile sanitizes a TextDocument, parses its YAML contents into an AST,
// and calculates the offset and size of every YAML document in the file.
func NewFile(f file.TextDocument) *File {
r := &File{
TextDocument: f,
}
sanitized := helmSanitizerRegexp.ReplaceAllFunc(f.Bytes(), func(src []byte) []byte {
return []byte(strings.Repeat("x", len(src)))
})
r.ast, r.parseError = parser.ParseBytes(sanitized, 0)
// document separator -- is not considered parse error
if r.parseError != nil {
return r
}
for i, doc := range r.ast.Docs {
d := &Document{
TextDocument: f,
file: r,
ast: doc,
offset: r.LineOffset(doc.Body.GetToken().Position.Line - 1),
references: []reference{},
identifiers: []*identifier{},
}
if i > 0 {
r.docs[i-1].size = d.offset - r.docs[i-1].offset
}
r.docs = append(r.docs, d)
}
if len(r.docs) > 0 {
lst := r.docs[len(r.docs)-1]
lst.size = len(r.Bytes()) - lst.offset
}
return r
}
// deprecated: used only for texts
func parseFile(f file.TextDocument) *File {
ws := NewWorkspace()
uri := "file://test.yaml"
ws.UpsertFile(uri, string(f))
ws.Lint()
return ws.File(uri)
}
func (f *File) solveReferences() {
if f.parseError != nil {
return
}
f.danglingRefs = map[string]struct{}{}
for _, d := range f.docs {
d.solveReferences()
}
}
func (f *File) solveIdentifiers() {
if f.parseError != nil {
return
}
for _, d := range f.docs {
d.parseIdentifiers()
}
}
// getIdent accepts an identLocator and returns a pointer to an identifier
// in this File matched by the locator, or nil if none matches.
func (f *File) getIdent(l identLocator) *identifier {
ids := []*identifier{}
for _, d := range f.docs {
if id := d.getIdent(l); id != nil {
ids = append(ids, id)
}
}
if len(ids) > 1 {
panic(
fmt.Errorf(
"file.getIdent(%v) returned more than one identifier",
l,
),
)
}
if len(ids) == 0 {
return nil
}
return ids[0]
}
// findDoc returns the Document containing the given position.
func (f *File) findDoc(pos protocol.Position) *Document {
for _, d := range f.docs {
st := d.OffsetPosition(d.offset)
en := d.OffsetPosition(d.offset + d.size)
if inRange(pos, protocol.Range{Start: st, End: en}) {
return d
}
}
return nil
}
// Hover returns a pointer to a string containing the Documentation of the
// Tekton object in the given position, or nil if no reference is found.
func (f *File) Hover(pos protocol.Position) *string {
return f.findDoc(pos).hover(pos)
}
// Rename returns the Workspace changes required to rename the identifier
// in the given position to the newName argument.
func (f *File) Rename(pos protocol.Position, newName string) (*protocol.WorkspaceEdit, error) {
return f.findDoc(pos).rename(pos, newName)
}
// PrepareRename returns the Location (File, Range) that should be edited
// if a Rename request is made at the given position. It should return
// nil if it's not a valid position for a rename request, that is, if no
// identifier is found at the given position.
func (f *File) PrepareRename(pos protocol.Position) *protocol.Location {
return f.findDoc(pos).prepareRename(pos)
}
// Definition returns the Location where the identifier in the given position
// is defined, or nil if no identifier is found.
func (f *File) Definition(pos protocol.Position) *protocol.Location {
return f.findDoc(pos).definition(pos)
}
// FindReferences returns a list of all Locations which refer to the identifier
// in the given position, or nil if no identifier is found.
func (f *File) FindReferences(pos protocol.Position) []protocol.Location {
return f.findDoc(pos).findReferences(pos)
}
// Completions returns a list of completion suggestions for the given
// position.
func (f *File) Completions(pos protocol.Position) []fmt.Stringer {
res := []fmt.Stringer{}
if f.parseError != nil {
return res
}
return f.findDoc(pos).completions(pos)
}
// Diagnostics returns a list of Diagnostics issues found in this File.
func (f *File) Diagnostics() []protocol.Diagnostic {
if f.parseError != nil {
if d := syntaxErrorDiagnostic(f.parseError); d != nil {
return []protocol.Diagnostic{*d}
}
}
dgs := make(chan *protocol.Diagnostic, len(f.docs))
var wg sync.WaitGroup
for _, d := range f.docs {
wg.Add(1)
go func() {
defer wg.Done()
d.diagnostics(dgs)
}()
}
go func() {
wg.Wait()
close(dgs)
}()
rs := make([]protocol.Diagnostic, 0, len(f.docs))
for dg := range dgs {
rs = append(rs, *dg)
}
return rs
}
func mustPathString(path string) *yaml.Path {
p, err := yaml.PathString(path)
if err != nil {
panic(err)
}
return p
}