Skip to content

Commit 9e6d88d

Browse files
committed
enhanced diagnostics to not always reparse by saving errors on each parse
1 parent 698c6e9 commit 9e6d88d

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

documents/documents.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,20 @@ import (
1313

1414
// represents the state of a single document
1515
type DocumentState struct {
16-
Content string // the content of the document
17-
Uri uri.URI // the uri from the client
18-
Path string // the filepath as parsed from the uri
19-
Module *ast.Module // the corresponding ddp module
20-
NeedReparse atomic.Bool // whether the document needs to be reparsed
21-
parseMutex sync.Mutex // the mutex used for parsing
16+
Content string // the content of the document
17+
Uri uri.URI // the uri from the client
18+
Path string // the filepath as parsed from the uri
19+
Module *ast.Module // the corresponding ddp module
20+
NeedReparse atomic.Bool // whether the document needs to be reparsed
21+
LatestErrors []ddperror.Error // the errors from the last parsing
22+
parseMutex sync.Mutex // the mutex used for parsing
23+
}
24+
25+
func (d *DocumentState) newErrorCollector() ddperror.Handler {
26+
d.LatestErrors = make([]ddperror.Error, 0, 10)
27+
return func(err ddperror.Error) {
28+
d.LatestErrors = append(d.LatestErrors, err)
29+
}
2230
}
2331

2432
func (d *DocumentState) reParseInContext(modules map[string]*ast.Module, errorHandler ddperror.Handler) (err error) {
@@ -74,10 +82,10 @@ func (dm *DocumentManager) AddAndParse(vscURI, content string) error {
7482
dm.documentStates[docURI] = docState
7583
dm.mu.Unlock()
7684

77-
return dm.ReParse(docURI, ddperror.EmptyHandler)
85+
return dm.reParse(docURI, docState.newErrorCollector())
7886
}
7987

80-
func (dm *DocumentManager) ReParse(docUri uri.URI, errHndl ddperror.Handler) error {
88+
func (dm *DocumentManager) reParse(docUri uri.URI, errHndl ddperror.Handler) error {
8189
dm.mu.RLock()
8290
defer dm.mu.RUnlock()
8391

@@ -107,7 +115,7 @@ func (dm *DocumentManager) Get(vscURI string) (*DocumentState, bool) {
107115
// it was not being parsed, so we unlock the mutex
108116
// which will be locked again by ReParse
109117
doc.parseMutex.Unlock()
110-
ok = dm.ReParse(doc.Uri, ddperror.EmptyHandler) == nil
118+
ok = dm.reParse(doc.Uri, doc.newErrorCollector()) == nil
111119
} else {
112120
// if it is being currently reparsed we wait for it to finish
113121
// by aquiring the mutex and then immediately unlock and return it

handlers/diagnostics.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,22 @@ func CreateSendDiagnostics() DiagnosticSender {
3434
var (
3535
docMod *ast.Module
3636
docUri uri.URI
37+
errs []ddperror.Error
3738
)
3839
if doc, ok := dm.Get(vscURI); !ok {
3940
log.Warningf("Could not retrieve document %s", vscURI)
4041
return
4142
} else {
4243
docMod = doc.Module
4344
docUri = doc.Uri
45+
errs = doc.LatestErrors
4446
}
4547
path := docUri.Filepath()
4648

4749
visitor := &diagnosticVisitor{path: path, diagnostics: make([]protocol.Diagnostic, 0)}
4850

49-
if err := dm.ReParse(docUri, func(err ddperror.Error) {
50-
visitor.add(err)
51-
}); err != nil {
52-
log.Errorf("parser error: %s", err)
53-
return
51+
for i := range errs {
52+
visitor.add(errs[i])
5453
}
5554

5655
ast.VisitModuleRec(docMod, visitor)

0 commit comments

Comments
 (0)