Skip to content

Commit

Permalink
lsp/diagnostics: fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
GraphR00t committed May 17, 2024
1 parent 9ab0810 commit fa49f0e
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 33 deletions.
13 changes: 9 additions & 4 deletions internal/projectserver/debug_methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -658,15 +658,20 @@ func handleDebugLaunch(callCtx context.Context, req interface{}) (interface{}, e
}
}()

session.lock.Lock()
lastCodeBaseAnalysis := session.lastCodebaseAnalysis
session.lock.Unlock()

defer computeNotifyDocumentDiagnostics(diagnosticNotificationParams{
rpcSession: rpcSession,
docURI: debugSession.programURI,
usingInoxFS: debugSession.inProjectMode,

fls: fls,
project: project,
memberAuthToken: memberAuthToken,
inoxChunkCache: chunkCache,
fls: fls,
project: project,
memberAuthToken: memberAuthToken,
inoxChunkCache: chunkCache,
lastCodebaseAnalysis: lastCodeBaseAnalysis,
})
defer removeDebugSession(debugSession, rpcSession)

Expand Down
40 changes: 27 additions & 13 deletions internal/projectserver/document_diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (
"encoding/json"
"errors"
"path/filepath"
"slices"
"strings"
"sync"
"time"

"github.com/go-git/go-billy/v5/util"
"github.com/inoxlang/inox/internal/codebase/analysis"
"github.com/inoxlang/inox/internal/core"
"github.com/inoxlang/inox/internal/core/symbolic"
"github.com/inoxlang/inox/internal/hyperscript/hsanalysis"
Expand Down Expand Up @@ -51,6 +52,7 @@ func handleDocumentDiagnostic(ctx context.Context, req *defines.DocumentDiagnost
project := session.project
fls := session.filesystem
memberAuthToken := session.memberAuthToken
lastCodebaseAnalysis := session.lastCodebaseAnalysis
session.lock.Unlock()
//----------------------------------------------------------

Expand All @@ -74,10 +76,11 @@ func handleDocumentDiagnostic(ctx context.Context, req *defines.DocumentDiagnost
docURI: uri,
usingInoxFS: projectMode,

rpcSession: rpcSession,
fls: fls,
project: project,
memberAuthToken: memberAuthToken,
rpcSession: rpcSession,
fls: fls,
project: project,
memberAuthToken: memberAuthToken,
lastCodebaseAnalysis: lastCodebaseAnalysis,
})
}()

Expand Down Expand Up @@ -123,6 +126,7 @@ type diagnosticNotificationParams struct {
memberAuthToken string
inoxChunkCache *parse.ChunkCache
hyperscriptParseCache *hscode.ParseCache
lastCodebaseAnalysis *analysis.Result //may be nil
}

// computeNotifyDocumentDiagnostics diagnostics a document and notifies the LSP client (textDocument/publishDiagnostics).
Expand Down Expand Up @@ -150,14 +154,23 @@ func computeNotifyDocumentDiagnostics(params diagnosticNotificationParams) error
return err
}

otherDocumentDiagnostics := diagnostics.otherDocumentDiagnostics
//Note: the locking of $diagnostics is not necessary because otherDocumentDiagnostics are never updated.

go func() {
defer utils.Recover()
for otherDocURI, otherDocDiagnostics := range diagnostics.otherDocumentDiagnostics {
for otherDocURI, otherDocDiagnostics := range otherDocumentDiagnostics {
sendDocumentDiagnostics(params.rpcSession, otherDocURI, otherDocDiagnostics)
}
}()

return sendDocumentDiagnostics(params.rpcSession, params.docURI, diagnostics.items)
items := slices.Clone(diagnostics.items)

if params.lastCodebaseAnalysis != nil {
addErrorsAndWarningsAboutFileFromCodebaseAnalysis(params.lastCodebaseAnalysis, diagnostics.filePath, &items)
}

return sendDocumentDiagnostics(params.rpcSession, params.docURI, items)
}

// computes prepares a source file, constructs a list of defines.Diagnostic from errors at different phases
Expand Down Expand Up @@ -543,13 +556,13 @@ func MakeDocDiagnosticId(absPath absoluteFilePath) DocDiagnosticId {
return DocDiagnosticId(ulid.Make().String() + "-" + string(absPath))
}

// A singleDocumentDiagnostics contains the diagnostics of a single Inox or Hyperscript document.
// A singleDocumentDiagnostics contains the diagnostics of a single Inox or Hyperscript document,
// it does not contain workspace diagnostics. This struct is never modified.
type singleDocumentDiagnostics struct {
id DocDiagnosticId
startTime time.Time
items []defines.Diagnostic
containsWorkspaceDiagnostics bool
lock sync.Mutex
id DocDiagnosticId
filePath absoluteFilePath
startTime time.Time
items []defines.Diagnostic

//Fields specific to Inox files.

Expand All @@ -559,6 +572,7 @@ type singleDocumentDiagnostics struct {

func (d *singleDocumentDiagnostics) finalize(fpath absoluteFilePath, computeStart time.Time, rpcSession *jsonrpc.Session) {
d.id = MakeDocDiagnosticId(fpath)
d.filePath = fpath
d.startTime = computeStart

if d.items == nil {
Expand Down
5 changes: 1 addition & 4 deletions internal/projectserver/hover.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package projectserver
import (
"bytes"
"fmt"
"slices"
"strings"

"github.com/inoxlang/inox/internal/codebase/analysis"
Expand Down Expand Up @@ -310,9 +309,7 @@ func writeReformattedSymbolicErrors(w *strings.Builder, cursorIndex int32, param
return false, nil
}

diagnostics.lock.Lock()
checkErrors := slices.Clone(diagnostics.symbolicErrors[params.docURI])
diagnostics.lock.Unlock()
checkErrors := diagnostics.symbolicErrors[params.docURI]

isHeaderPrinted := false

Expand Down
6 changes: 6 additions & 0 deletions internal/projectserver/standard_lsp_methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,7 @@ func handleDidOpenDocument(callCtx context.Context, req *defines.DidOpenTextDocu
chunkCache := session.inoxChunkCache
hyperscriptFileCache := session.hyperscriptFileCache
memberAuthToken := session.memberAuthToken
lastCodeBaseAnalysis := session.lastCodebaseAnalysis
session.lock.Unlock()
//----------------------------------------

Expand Down Expand Up @@ -605,6 +606,7 @@ func handleDidOpenDocument(callCtx context.Context, req *defines.DidOpenTextDocu
inoxChunkCache: chunkCache,
hyperscriptParseCache: hyperscriptFileCache,
memberAuthToken: memberAuthToken,
lastCodebaseAnalysis: lastCodeBaseAnalysis,
})
}

Expand All @@ -619,6 +621,7 @@ func handleDidSaveDocument(callCtx context.Context, req *defines.DidSaveTextDocu
chunkCache := session.inoxChunkCache
hyperscriptFileCache := session.hyperscriptFileCache
memberAuthToken := session.memberAuthToken
lastCodeBaseAnalysis := session.lastCodebaseAnalysis

uri := normalizeURI(req.TextDocument.Uri)
fpath, err := getSupportedFilePath(uri, projectMode)
Expand Down Expand Up @@ -708,6 +711,7 @@ func handleDidSaveDocument(callCtx context.Context, req *defines.DidSaveTextDocu
hyperscriptParseCache: hyperscriptFileCache,
fls: fls,
memberAuthToken: memberAuthToken,
lastCodebaseAnalysis: lastCodeBaseAnalysis,
})
}

Expand Down Expand Up @@ -768,6 +772,8 @@ func handleDidChangeDocument(callCtx context.Context, req *defines.DidChangeText
inoxChunkCache: chunkCache,
hyperscriptParseCache: hyperscriptFileCache,
memberAuthToken: memberAuthToken,
//The last codebase analysis is not passed because a new analysis will likely be
//performed.
})
})

Expand Down
12 changes: 0 additions & 12 deletions internal/projectserver/workspace_diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,6 @@ func publishWorkspaceDiagnostics(projSession *Session, lastAnalysis *analysis.Re
continue
}

{
diagnostics.lock.Lock()
containsWorkspaceDiagnostics := diagnostics.containsWorkspaceDiagnostics

if containsWorkspaceDiagnostics {
diagnostics.lock.Unlock()
continue
}
diagnostics.containsWorkspaceDiagnostics = true
diagnostics.lock.Unlock()
}

items := slices.Clone(diagnostics.items)

go func(absPath absoluteFilePath, uri defines.DocumentUri, items *[]defines.Diagnostic) {
Expand Down

0 comments on commit fa49f0e

Please sign in to comment.