This repository has been archived by the owner on Dec 6, 2023. It is now read-only.
/
workspace.go
392 lines (340 loc) · 9.97 KB
/
workspace.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
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
package z
import (
"encoding/json"
"os"
"path/filepath"
"sync"
"time"
"github.com/go-leap/dev/go"
"github.com/go-leap/str"
"github.com/go-leap/sys"
)
type IWorkspace interface {
iDispatcher
IObjSnap
json.Marshaler
sync.Locker
Dirs() WorkspaceDirs
Files() WorkspaceFiles
PrettyPath(string, ...string) string
}
type WorkspaceDir struct {
Path string
}
type WorkspaceDirs map[string]*WorkspaceDir
type WorkspaceFile struct {
Path string
IsOpen bool `json:",omitempty"`
Diags struct {
AutoLintUpToDate bool
Problems diags
Lintishs diags
}
existsAtLastCheck bool
modTime int64
}
func (me *WorkspaceFile) updateModTime() (hasChanged bool) {
fileinfo, err := os.Stat(me.Path)
if me.existsAtLastCheck = (err == nil) && fileinfo.Mode().IsRegular(); me.existsAtLastCheck {
modtime := fileinfo.ModTime().UnixNano()
me.modTime, hasChanged = modtime, modtime > me.modTime
}
return
}
func (me *WorkspaceFile) resetDiags() {
me.Diags.Problems.forget(nil)
me.Diags.Lintishs.forget(nil)
me.Diags.AutoLintUpToDate = false
}
type WorkspaceFiles map[string]*WorkspaceFile
func (me WorkspaceFiles) ensure(fpath string) (file *WorkspaceFile) {
if file = me[fpath]; file == nil {
file = &WorkspaceFile{Path: fpath}
if file.updateModTime(); file.existsAtLastCheck {
me[fpath] = file
}
}
return
}
func (me WorkspaceFiles) Has(fpath string) bool { return me[fpath] != nil }
func (me WorkspaceFiles) OrFromCurImplIfNil() (ret WorkspaceFiles) {
if ret = me; ret == nil {
ret = Lang.Workspace.Files()
}
return
}
func (me WorkspaceFiles) IsOpen(fpath string) bool {
f := me[fpath]
return f != nil && f.IsOpen
}
type diagsSummary struct {
numBuild int
numLint int
files map[*WorkspaceFile]bool
}
func (me WorkspaceFiles) diagsSummary() *diagsSummary {
s := &diagsSummary{files: make(map[*WorkspaceFile]bool, len(me))}
for _, f := range me {
if nb, nl := len(f.Diags.Problems.Items), len(f.Diags.Lintishs.Items); nb > 0 || nl > 0 {
s.numBuild, s.numLint, s.files[f] = s.numBuild+nb, s.numLint+nl, true
}
}
if s.numBuild == 0 && s.numLint == 0 {
return nil
}
return s
}
func (me WorkspaceFiles) haveAnyDiags(probDiags bool, lintDiags bool) bool {
for _, f := range me {
if lb, ll := len(f.Diags.Problems.Items), len(f.Diags.Lintishs.Items); (probDiags && lb > 0) || (lintDiags && ll > 0) {
return true
}
}
return false
}
func (me WorkspaceFiles) HasProbDiags(filePath string) (has bool) {
if f := me[filePath]; f != nil {
has = len(f.Diags.Problems.Items) > 0
}
return
}
func (me WorkspaceFiles) exists(fpath string) bool {
return me[fpath] != nil
}
func (me WorkspaceFiles) filePathsOpened() (all []string) {
all = make([]string, 0, len(me))
for _, f := range me {
if f.IsOpen {
all = append(all, f.Path)
}
}
return
}
func (me WorkspaceFiles) filePathsKnown() (all []string) {
var i int
all = make([]string, len(me))
for fp := range me {
all[i], i = fp, i+1
}
return
}
func (me WorkspaceFiles) numDirs(incl func(*WorkspaceFile) bool) int {
filedirs := make(map[string]bool, len(me))
for _, f := range me {
if incl == nil || incl(f) {
filedirs[filepath.Dir(f.Path)] = true
}
}
return len(filedirs)
}
func (me *WorkspaceChanges) HasDirChanges() bool {
return len(me.AddedDirs) > 0 || len(me.RemovedDirs) > 0
}
func (me *WorkspaceChanges) hasFileIn(filePath string, slices ...[]string) bool {
if len(slices) == 0 {
slices = [][]string{me.ClosedFiles, me.OpenedFiles, me.WrittenFiles}
}
for _, eventfiles := range slices {
for _, fpath := range eventfiles {
if fpath == filePath {
return true
}
}
}
return false
}
func (me *WorkspaceChanges) hasFileChanges() bool {
return len(me.OpenedFiles) > 0 || len(me.ClosedFiles) > 0 || len(me.WrittenFiles) > 0 || (Lang.Live && len(me.LiveFiles) > 0)
}
type WorkspaceChangesBefore func(upd *WorkspaceChanges, freshFiles []string, willAutoLint bool)
type WorkspaceChangesAfter func(upd *WorkspaceChanges)
type WorkspaceBase struct {
mutex sync.Mutex
Impl IWorkspace `json:"-"`
// raised before updating zentient-internal workspaceFolders/openedFiles and then requesting new diags
OnBeforeChanges WorkspaceChangesBefore `json:"-"`
// raised after updating zentient-internal workspaceFolders/openedFiles and then requesting new diags
OnAfterChanges WorkspaceChangesAfter `json:"-"`
dirs WorkspaceDirs
files WorkspaceFiles
pollingStarted bool
pollingPaused bool
}
func (me *WorkspaceBase) Init() { me.dirs, me.files = WorkspaceDirs{}, WorkspaceFiles{} }
func (me *WorkspaceBase) Dirs() (dirs WorkspaceDirs) { dirs = me.dirs; return }
func (me *WorkspaceBase) Files() (files WorkspaceFiles) { files = me.files; return }
func (me *WorkspaceBase) Lock() {
me.pollingPaused = true
me.mutex.Lock()
}
func (me *WorkspaceBase) Unlock() {
me.mutex.Unlock()
me.pollingPaused = false
}
func (me *WorkspaceBase) MarshalJSON() ([]byte, error) {
var obj struct {
Dirs WorkspaceDirs
Files WorkspaceFiles
}
obj.Dirs, obj.Files = me.dirs, me.files
return json.Marshal(&obj)
}
func (me *WorkspaceBase) dispatch(req *IpcReq, resp *IpcResp) bool {
switch req.IpcID {
case IPCID_PROJ_CHANGED:
me.onChanges(req.ProjUpd)
default:
return false
}
return true
}
func (*WorkspaceBase) analyzeChanges(files WorkspaceFiles, upd *WorkspaceChanges) (freshFiles []string, hasFreshFiles bool, hasDiedFiles bool, dirsChanged bool, needsFreshAutoLints bool) {
for _, eventfiles := range [][]string{upd.OpenedFiles, upd.ClosedFiles, upd.WrittenFiles} {
for _, fpath := range eventfiles {
if !files.exists(fpath) {
freshFiles = append(freshFiles, fpath)
}
}
}
for fpath := range upd.LiveFiles {
if !files.exists(fpath) {
freshFiles = append(freshFiles, fpath)
}
}
for _, file := range files { // we really only check all-known-files for changes seemingly-somewhat-redudantly here to cover changes outside the editor (from code-gens etc)
if haschanged := file.updateModTime(); !file.existsAtLastCheck {
hasDiedFiles = true
} else if haschanged && !upd.hasFileIn(file.Path, upd.WrittenFiles) {
upd.WrittenFiles = append(upd.WrittenFiles, file.Path)
}
}
hasFreshFiles, dirsChanged = len(freshFiles) > 0, upd.HasDirChanges()
needsFreshAutoLints = hasFreshFiles || len(upd.WrittenFiles) > 0 || len(upd.OpenedFiles) > 0
return
}
func (me *WorkspaceBase) onChanges(upd *WorkspaceChanges) {
if upd != nil && (upd.HasDirChanges() || upd.hasFileChanges()) {
dirs, files := me.dirs, me.files
haslivefiles := Lang.Live && len(upd.LiveFiles) > 0
freshfiles, hasfreshfiles, hasdiedfiles, dirschanged, needsfreshautolints := me.analyzeChanges(files, upd)
needsfreshautolints = needsfreshautolints || haslivefiles
if needsfreshautolints || hasfreshfiles || hasdiedfiles || dirschanged || me.OnBeforeChanges != nil || me.OnAfterChanges != nil {
me.Lock()
defer me.Unlock()
}
if me.OnBeforeChanges != nil {
me.OnBeforeChanges(upd, freshfiles, needsfreshautolints)
}
if dirschanged {
dirs = make(WorkspaceDirs, len(me.dirs))
for k, v := range me.dirs {
dirs[k] = v
}
for _, gonedirpath := range upd.RemovedDirs {
delete(dirs, gonedirpath)
}
for _, newdirpath := range upd.AddedDirs {
if dir := dirs[newdirpath]; dir == nil {
dir = &WorkspaceDir{Path: ustr.TrimR(newdirpath, "/\\")}
dirs[newdirpath] = dir
}
}
}
if hasfreshfiles || hasdiedfiles {
files = make(WorkspaceFiles, len(me.files))
for k, v := range me.files {
if v.existsAtLastCheck {
files[k] = v
}
}
}
for _, gonefilepath := range upd.ClosedFiles {
files.ensure(gonefilepath).IsOpen = false
}
for _, freshfilepath := range upd.OpenedFiles {
files.ensure(freshfilepath).IsOpen = true
}
for _, modfilepath := range upd.WrittenFiles {
files.ensure(modfilepath).resetDiags()
}
if Lang.Live {
for srcfilepath := range upd.LiveFiles {
files.ensure(srcfilepath).resetDiags()
}
}
if Lang.Diag != nil {
if len(upd.WrittenFiles) > 0 || (Lang.Live && needsfreshautolints) {
Lang.Diag.UpdateProbDiagsAsNeeded(files, upd.WrittenFiles)
}
if needsfreshautolints && !files.haveAnyDiags(true, false) {
Lang.Diag.UpdateLintDiagsIfAndAsNeeded(files, true)
}
}
me.dirs, me.files = dirs, files
if me.OnAfterChanges != nil {
me.OnAfterChanges(upd)
}
}
if !me.pollingStarted {
me.pollingStarted = true
go me.pollFileEventsForever()
}
}
func (me *WorkspaceBase) ObjSnap(string) interface{} { return me.Impl }
func (*WorkspaceBase) ObjSnapPrefix() string { return Lang.ID + ".proj." }
func (me *WorkspaceBase) pollFileEventsForever() {
interval := 321 * time.Millisecond
msgraw, _ := json.Marshal(&IpcResp{IpcID: IPCID_PROJ_POLLEVTS})
msgraw = append(msgraw, '\n')
for {
if time.Sleep(interval); !me.pollingPaused {
if !canSend() {
return
} else if err := sendRaw(msgraw); err != nil {
return
}
}
}
}
func (*WorkspaceBase) prettyPathRel(path string, fsPath string) string {
if path != "" {
if rp, err := filepath.Rel(path, fsPath); err == nil && rp != "" && !ustr.Pref(rp, ".") {
return rp
}
}
return ""
}
func (me *WorkspaceBase) PrettyPath(fsPath string, otherEnvs ...string) string {
if fsPath != "" {
rel := func(path string) string { return me.prettyPathRel(path, fsPath) }
candidates := []string{}
for _, d := range me.dirs {
if rp := rel(d.Path); rp != "" {
candidates = append(candidates, filepath.Join("...", filepath.Base(d.Path), rp))
}
}
if len(candidates) > 0 {
return ustr.Shortest(candidates)
}
for _, gopath := range udevgo.Gopaths() {
if rp := rel(gopath); rp != "" {
return filepath.Join("$GOPATH", rp)
}
}
for _, envname := range otherEnvs {
if envval := os.Getenv(envname); envval != "" {
if envpaths := filepath.SplitList(envval); len(envpaths) > 0 {
for _, envpath := range envpaths {
if rp := rel(envpath); rp != "" {
return filepath.Join("$"+envname, rp)
}
}
}
}
}
if rp := rel(usys.UserHomeDirPath()); rp != "" {
return filepath.Join("~", rp)
}
}
return fsPath
}