Skip to content

Commit

Permalink
feat(lsp): fix bug when module found up to workspace and we see its p…
Browse files Browse the repository at this point in the history
…roblems
  • Loading branch information
emil14 committed Dec 10, 2024
1 parent 6383565 commit ffb7907
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 12 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/cmd/neva",
"cwd": "${workspaceFolder}/examples",
"args": ["run", "--trace", "filter_list"]
"cwd": "${workspaceFolder}/e2e/filter_list",
"args": ["run", "--trace", "main"]
},
{
"name": "DEBUG CODEGEN",
Expand Down
36 changes: 32 additions & 4 deletions cmd/lsp/indexer/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package indexer

import (
"context"
"path/filepath"
"strings"

"github.com/nevalang/neva/internal/builder"
"github.com/nevalang/neva/internal/compiler"
Expand All @@ -15,12 +17,38 @@ type Indexer struct {
analyzer analyzer.Analyzer
}

func (i Indexer) FullIndex(ctx context.Context, path string) (src.Build, *compiler.Error) {
feResult, err := i.fe.Process(ctx, path)
func (i Indexer) FullScan(
ctx context.Context,
workspacePath string,
) (src.Build, bool, *compiler.Error) {
feResult, err := i.fe.Process(ctx, workspacePath)
if err != nil {
return src.Build{}, err
return src.Build{}, false, err
}
return i.analyzer.AnalyzeBuild(feResult.ParsedBuild)

// if nevalang module is found, but it's not part of the workspace
if isParentPath(workspacePath, feResult.Path) {

Check failure on line 30 in cmd/lsp/indexer/indexer.go

View workflow job for this annotation

GitHub Actions / build_and_test

feResult.Path undefined (type compiler.FrontendResult has no field or method Path)

Check failure on line 30 in cmd/lsp/indexer/indexer.go

View workflow job for this annotation

GitHub Actions / lint

feResult.Path undefined (type compiler.FrontendResult has no field or method Path) (typecheck)

Check failure on line 30 in cmd/lsp/indexer/indexer.go

View workflow job for this annotation

GitHub Actions / lint

feResult.Path undefined (type compiler.FrontendResult has no field or method Path)) (typecheck)

Check failure on line 30 in cmd/lsp/indexer/indexer.go

View workflow job for this annotation

GitHub Actions / lint

feResult.Path undefined (type compiler.FrontendResult has no field or method Path)) (typecheck)
return src.Build{}, false, nil
}

aBuild, err := i.analyzer.AnalyzeBuild(feResult.ParsedBuild)
if err != nil {
return src.Build{}, false, err
}

return aBuild, true, nil
}

func isParentPath(parent, child string) bool {
parent = filepath.Clean(parent)
child = filepath.Clean(child)

rel, err := filepath.Rel(parent, child)
if err != nil {
return false
}

return !strings.HasPrefix(rel, "..")
}

func New(
Expand Down
1 change: 1 addition & 0 deletions cmd/lsp/server/general_messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

func (s *Server) Initialize(glspCtx *glsp.Context, params *protocol.InitializeParams) (any, error) {
s.workspacePath = *params.RootPath

return protocol.InitializeResult{
Capabilities: s.handler.CreateServerCapabilities(),
ServerInfo: &protocol.InitializeResultServerInfo{
Expand Down
21 changes: 15 additions & 6 deletions cmd/lsp/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,29 @@ type Server struct {
index *src.Build

problemsMutex *sync.Mutex
problemFiles map[string]struct{}
problemFiles map[string]struct{} // we only need to store file urls but not their problems

activeFile string
activeFileMutex *sync.Mutex
}

// indexAndNotifyProblems does full scan of the workspace
// and sends diagnostics if there are any problems
func (s *Server) indexAndNotifyProblems(notify glsp.NotifyFunc) error {
build, err := s.indexer.FullIndex(context.Background(), s.workspacePath)
build, found, proplems := s.indexer.FullScan(
context.Background(),
s.workspacePath,
)

if !found {
return nil
}

s.indexMutex.Lock()
s.index = &build
s.indexMutex.Unlock()

if err == nil {
if proplems == nil {
// clear problems
s.problemsMutex.Lock()
for uri := range s.problemFiles {
Expand All @@ -60,13 +69,13 @@ func (s *Server) indexAndNotifyProblems(notify glsp.NotifyFunc) error {

// remember problem and send diagnostic
s.problemsMutex.Lock()
uri := filepath.Join(s.workspacePath, err.Meta.Location.String())
uri := filepath.Join(s.workspacePath, proplems.Meta.Location.String())
s.problemFiles[uri] = struct{}{}
notify(
protocol.ServerTextDocumentPublishDiagnostics,
s.createDiagnostics(*err, uri),
s.createDiagnostics(*proplems, uri),
)
s.logger.Info("diagnostic sent:", "err", err)
s.logger.Info("diagnostic sent:", "err", proplems)
s.problemsMutex.Unlock()

return nil
Expand Down

0 comments on commit ffb7907

Please sign in to comment.