Repository navigation
Expand file tree
/
Copy patheditor_workspace.go
More file actions
343 lines (331 loc) · 9.95 KB
/
Copy patheditor_workspace.go
File metadata and controls
343 lines (331 loc) · 9.95 KB
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package driver
import (
"fmt"
"maps"
"path/filepath"
"slices"
"strings"
"github.com/GiGurra/bork/internal/check"
"github.com/GiGurra/bork/internal/diag"
"github.com/GiGurra/bork/internal/modcache"
"github.com/GiGurra/bork/internal/syntax"
)
// EditorWorkspace owns current checked snapshots of local workspace packages.
// Dependencies remain readable for navigation, but never editable.
type EditorWorkspace struct {
analyses []*EditorAnalysis
paths []string
overlays map[string]string
}
func (w *EditorWorkspace) Analyses() []*EditorAnalysis { return slices.Clone(w.analyses) }
func (w *EditorWorkspace) Sources() map[string]string {
out := map[string]string{}
for _, a := range w.analyses {
maps.Copy(out, a.Sources())
}
return out
}
func (w *EditorWorkspace) References(def diag.Pos) []check.SourceReference {
seen := map[check.SourceReference]bool{}
for _, a := range w.analyses {
for _, ref := range a.References(def) {
seen[ref] = true
}
}
out := make([]check.SourceReference, 0, len(seen))
for ref := range seen {
out = append(out, ref)
}
slices.SortFunc(out, func(a, b check.SourceReference) int {
if n := strings.Compare(a.Start.File, b.Start.File); n != 0 {
return n
}
if a.Start.Line != b.Start.Line {
return a.Start.Line - b.Start.Line
}
return a.Start.Col - b.Start.Col
})
return out
}
// WorkspacePackages inventories local packages through the compiler source
// reader, including new buffers. Hidden/vendor directories and nested modules
// are excluded unless supplied as explicit workspace roots.
func WorkspacePackages(path string, roots []string, overlays map[string]string) ([]string, error) {
reader := overlaySources{files: overlays}
cache := modcache.Root()
absolute, err := filepath.Abs(path)
if err != nil {
return nil, err
}
dir := absolute
if yes, err := reader.isDirectory(dir); err != nil {
return nil, err
} else if !yes {
dir = filepath.Dir(dir)
}
owner, err := findModuleFrom(dir, reader)
if err != nil {
return nil, err
}
if owner.path == "" {
if text, err := reader.readFile(absolute); err == nil && strings.HasPrefix(string(text), "#!") {
return []string{absolute}, nil
}
return []string{dir}, nil
}
roots = append(slices.Clone(roots), owner.root)
packages := map[string]bool{}
visited := map[string]bool{}
var walk func(string, bool) error
walk = func(dir string, root bool) error {
if visited[dir] || modcache.Contains(cache, dir) {
return nil
}
if !root {
if _, err := reader.readFile(filepath.Join(dir, ModFile)); err == nil {
return nil
}
}
visited[dir] = true
entries, err := reader.directory(dir)
if err != nil {
return err
}
for _, entry := range entries {
if entry.directory {
if strings.HasPrefix(entry.name, ".") || entry.name == "vendor" || entry.name == "node_modules" {
continue
}
if err := walk(filepath.Join(dir, entry.name), false); err != nil {
return err
}
} else if filepath.Ext(entry.name) == ".bork" {
file := filepath.Join(dir, entry.name)
text, err := reader.readFile(file)
if err != nil {
return err
}
if strings.HasPrefix(string(text), "#!") {
packages[file] = true
} else {
packages[dir] = true
}
}
}
return nil
}
for _, root := range roots {
root, err = filepath.Abs(root)
if err != nil {
return nil, err
}
if err := walk(root, true); err != nil {
return nil, err
}
}
out := slices.Sorted(maps.Keys(packages))
return out, nil
}
// AnalyzeWorkspace checks closed importers as well as open buffers. An invalid
// local package prevents edits: returning a partial reference set is unsafe.
func AnalyzeWorkspace(path string, roots []string, overlays map[string]string) (*EditorWorkspace, error) {
return AnalyzeWorkspaceWithSessions(path, roots, overlays, nil)
}
// AnalyzeWorkspaceWithSessions retains checked snapshots for repeated workspace
// queries while Session validation still checks overlays, disk inputs and tools.
func AnalyzeWorkspaceWithSessions(path string, roots []string, overlays map[string]string, sessions map[string]*Session) (*EditorWorkspace, error) {
paths, err := WorkspacePackages(path, roots, overlays)
if err != nil {
return nil, err
}
return analyzeWorkspacePackages(paths, overlays, sessions)
}
func analyzeWorkspacePackages(paths []string, overlays map[string]string, sessions map[string]*Session) (*EditorWorkspace, error) {
w := &EditorWorkspace{paths: paths, overlays: maps.Clone(overlays)}
for _, path := range paths {
session := sessions[path]
if session == nil {
session = NewSession()
if sessions != nil {
sessions[path] = session
}
}
analysis, err := session.Analyze(path, overlays)
if err != nil {
return nil, fmt.Errorf("workspace package %s does not check: %w", path, err)
}
w.analyses = append(w.analyses, analysis)
}
return w, nil
}
// Rename constructs and checks edits without writing files. Rechecking every
// package also verifies that references still bind to the renamed declaration.
func (w *EditorWorkspace) Rename(def diag.Pos, name string) ([]diag.TextEdit, error) {
return w.RenameWithSessions(def, name, nil)
}
// RenameWithSessions keeps verification caches separate from current-source
// snapshots. Sessions still validate every dependency, buffer and Go input.
func (w *EditorWorkspace) RenameWithSessions(def diag.Pos, name string, sessions map[string]*Session) ([]diag.TextEdit, error) {
cache := modcache.Root()
tokens, _ := syntax.Lex("", []byte(name), &diag.List{})
if len(tokens) < 2 || tokens[0].Kind != syntax.TIdent || tokens[0].Text != name {
return nil, fmt.Errorf("new name must be an identifier, not a keyword")
}
refs := w.References(def)
if len(refs) == 0 {
return nil, fmt.Errorf("no checked source references")
}
sources := w.Sources()
if _, ok := sources[def.File]; !ok || modcache.Contains(cache, def.File) {
return nil, fmt.Errorf("dependency and standard library sources are read-only")
}
supported := false
for _, analysis := range w.analyses {
for _, symbol := range analysis.Symbols() {
if symbol.Definition != def {
continue
}
switch symbol.Kind {
case "variable", "parameter", "typeParameter", "function", "predicate", "method", "type", "field", "variant", "value":
supported = true
}
}
}
if !supported {
return nil, fmt.Errorf("rename does not yet support this declaration kind")
}
edits := []diag.TextEdit{}
changed := map[string]bool{}
for _, ref := range refs {
if modcache.Contains(cache, ref.Start.File) {
return nil, fmt.Errorf("dependency sources are read-only")
}
if _, ok := sources[ref.Start.File]; !ok {
return nil, fmt.Errorf("source outside editable workspace")
}
edits = append(edits, diag.TextEdit{Start: ref.Start, End: ref.End, Replacement: ref.Prefix + name + ref.Suffix})
changed[ref.Start.File] = true
}
// Raw Go is opaque to the checker. Reject affected graphs rather than claim
// to verify bindings inside Go bodies.
for _, a := range w.analyses {
affected := false
for path := range a.Sources() {
affected = affected || changed[path]
}
if !affected {
continue
}
for path, text := range a.Sources() {
if modcache.Contains(cache, path) {
continue
}
tokens, _ := syntax.Lex(path, []byte(text), &diag.List{})
for _, token := range tokens {
if token.Kind == syntax.TGoCode {
return nil, fmt.Errorf("rename cannot verify references inside unsafe Go bodies")
}
}
}
}
if refs[0].Name == name {
return []diag.TextEdit{}, nil
}
overlays := maps.Clone(w.overlays)
if overlays == nil {
overlays = map[string]string{}
}
for file := range changed {
text := sources[file]
for i := len(edits) - 1; i >= 0; i-- {
edit := edits[i]
if edit.Start.File != file {
continue
}
start, end := sourceOffset(text, edit.Start), sourceOffset(text, edit.End)
if start < 0 || end < start || end > len(text) {
return nil, fmt.Errorf("invalid rename range")
}
text = text[:start] + edit.Replacement + text[end:]
}
overlays[file] = text
}
translate := func(pos, identity diag.Pos) diag.Pos {
out := renamePosition(pos, edits)
for _, ref := range refs {
if ref.Start != pos {
continue
}
if identity == def {
out.Col += len(ref.Prefix)
}
if identity != def && identity == pos && ref.Suffix != "" {
out.Col += len(name) + 2
}
}
return out
}
beforeRefs := map[check.SourceReference]bool{}
for _, before := range w.analyses {
for _, ref := range before.AllReferences() {
beforeRefs[ref] = true
}
}
for _, path := range w.paths {
session := sessions[path]
if session == nil {
session = NewSession()
if sessions != nil {
sessions[path] = session
}
}
after, err := session.Analyze(path, overlays)
if err != nil {
return nil, fmt.Errorf("proposed rename does not check: %w", err)
}
afterSources := after.Sources()
afterRefs := map[diag.Pos]map[diag.Pos]bool{}
for _, ref := range after.AllReferences() {
if afterRefs[ref.Start] == nil {
afterRefs[ref.Start] = map[diag.Pos]bool{}
}
afterRefs[ref.Start][ref.Definition] = true
}
for ref := range beforeRefs {
if _, ok := afterSources[ref.Start.File]; !ok {
continue
}
expected := translate(ref.Definition, ref.Definition)
if !afterRefs[translate(ref.Start, ref.Definition)][expected] {
return nil, fmt.Errorf("proposed rename changes the binding of %s at %s", ref.Name, ref.Start)
}
}
}
return edits, nil
}
func sourceOffset(text string, pos diag.Pos) int {
offset := 0
for line := 1; line < pos.Line; line++ {
i := strings.IndexByte(text[offset:], '\n')
if i < 0 {
return -1
}
offset += i + 1
}
if pos.Col < 1 || offset+pos.Col-1 > len(text) {
return -1
}
return offset + pos.Col - 1
}
func renamePosition(pos diag.Pos, edits []diag.TextEdit) diag.Pos {
out := pos
for _, edit := range edits {
if edit.Start.File != pos.File || edit.Start.Line != pos.Line {
continue
}
if edit.End.Col <= pos.Col {
out.Col += len(edit.Replacement) - (edit.End.Col - edit.Start.Col)
}
}
return out
}