-
Notifications
You must be signed in to change notification settings - Fork 12
Add lsp index #328
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+418
−32
Merged
Add lsp index #328
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| package lsp | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestLoadMixinsStoresAndIndexesMixinDocuments(t *testing.T) { | ||
| dir := t.TempDir() | ||
|
|
||
| mainPath := filepath.Join(dir, "lets.yaml") | ||
| baseMixinPath := filepath.Join(dir, "lets.base.yaml") | ||
| localMixinPath := filepath.Join(dir, "lets.local.yaml") | ||
|
|
||
| baseMixinDoc := `commands: | ||
| build: | ||
| cmd: echo build` | ||
|
|
||
| localMixinDoc := `commands: | ||
| test: | ||
| cmd: echo test` | ||
|
|
||
| if err := os.WriteFile(baseMixinPath, []byte(baseMixinDoc), 0o644); err != nil { | ||
| t.Fatalf("WriteFile(%s) error = %v", baseMixinPath, err) | ||
| } | ||
|
|
||
| if err := os.WriteFile(localMixinPath, []byte(localMixinDoc), 0o644); err != nil { | ||
| t.Fatalf("WriteFile(%s) error = %v", localMixinPath, err) | ||
| } | ||
|
|
||
| mainDoc := `mixins: | ||
| - lets.base.yaml | ||
| - -lets.local.yaml | ||
| commands: | ||
| release: | ||
| depends: [build, test] | ||
| cmd: echo release` | ||
|
|
||
| server := &lspServer{ | ||
| storage: newStorage(), | ||
| parser: newParser(logger), | ||
| index: newIndex(logger), | ||
| log: logger, | ||
| } | ||
|
|
||
| mainURI := pathToURI(mainPath) | ||
| server.storage.AddDocument(mainURI, mainDoc) | ||
| server.loadMixins(mainURI) | ||
|
|
||
| baseMixinURI := pathToURI(baseMixinPath) | ||
| localMixinURI := pathToURI(localMixinPath) | ||
|
|
||
| if got := server.storage.GetDocument(baseMixinURI); got == nil || *got != baseMixinDoc { | ||
| t.Fatalf("storage for %s = %#v, want %q", baseMixinURI, got, baseMixinDoc) | ||
| } | ||
|
|
||
| if got := server.storage.GetDocument(localMixinURI); got == nil || *got != localMixinDoc { | ||
| t.Fatalf("storage for %s = %#v, want %q", localMixinURI, got, localMixinDoc) | ||
| } | ||
|
|
||
| buildInfo, ok := server.index.findCommand("build") | ||
| if !ok { | ||
| t.Fatal("expected build command from mixin to be indexed") | ||
| } | ||
|
|
||
| if buildInfo.fileURI != baseMixinURI { | ||
| t.Fatalf("build indexed at %s, want %s", buildInfo.fileURI, baseMixinURI) | ||
| } | ||
|
|
||
| testInfo, ok := server.index.findCommand("test") | ||
| if !ok { | ||
| t.Fatal("expected test command from mixin to be indexed") | ||
| } | ||
|
|
||
| if testInfo.fileURI != localMixinURI { | ||
| t.Fatalf("test indexed at %s, want %s", testInfo.fileURI, localMixinURI) | ||
| } | ||
| } | ||
|
|
||
| func TestLoadMixinsSkipsMissingFiles(t *testing.T) { | ||
| dir := t.TempDir() | ||
|
|
||
| mainPath := filepath.Join(dir, "lets.yaml") | ||
| mainURI := pathToURI(mainPath) | ||
|
|
||
| mainDoc := `mixins: | ||
| - missing.yaml | ||
| commands: | ||
| release: | ||
| cmd: echo release` | ||
|
|
||
| server := &lspServer{ | ||
| storage: newStorage(), | ||
| parser: newParser(logger), | ||
| index: newIndex(logger), | ||
| log: logger, | ||
| } | ||
|
|
||
| server.storage.AddDocument(mainURI, mainDoc) | ||
| server.loadMixins(mainURI) | ||
|
|
||
| if got := server.storage.GetDocument(pathToURI(filepath.Join(dir, "missing.yaml"))); got != nil { | ||
| t.Fatalf("expected missing mixin to not be stored, got %#v", got) | ||
| } | ||
|
|
||
| if _, ok := server.index.findCommand("missing"); ok { | ||
| t.Fatal("expected no indexed command for missing mixin") | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| package lsp | ||
|
|
||
| import ( | ||
| "maps" | ||
| "sync" | ||
|
|
||
| "github.com/lets-cli/lets/internal/set" | ||
| "github.com/tliron/commonlog" | ||
| lsp "github.com/tliron/glsp/protocol_3_16" | ||
| ) | ||
|
|
||
| // TODO: maybe use Command struct ? | ||
| type commandInfo struct { | ||
| fileURI string | ||
| // position stored at the time of indexing and may be stale | ||
| position lsp.Position | ||
| } | ||
|
|
||
| type index struct { | ||
| log commonlog.Logger | ||
| parser *parser | ||
| mu sync.RWMutex | ||
| commands map[string]commandInfo | ||
| commandsByURI map[string]set.Set[string] | ||
| } | ||
|
|
||
| func newIndex(log commonlog.Logger) *index { | ||
| return &index{ | ||
| log: log, | ||
| parser: newParser(log), | ||
| commands: make(map[string]commandInfo), | ||
| commandsByURI: make(map[string]set.Set[string]), | ||
| } | ||
| } | ||
|
|
||
| // IndexDocument extracts commands from a document and updates the index to reflect that document's current state. | ||
| func (i *index) IndexDocument(uri string, doc string) { | ||
| commands := i.parser.getCommands(&doc) | ||
|
|
||
| indexedCommands := make(map[string]commandInfo, len(commands)) | ||
| indexedNames := set.NewSet[string]() | ||
|
|
||
| for _, command := range commands { | ||
| indexedCommands[command.name] = commandInfo{ | ||
| fileURI: uri, | ||
| position: command.position, | ||
| } | ||
| indexedNames.Add(command.name) | ||
| } | ||
|
|
||
| i.mu.Lock() | ||
| defer i.mu.Unlock() | ||
|
|
||
| i.log.Debugf("Indexed %d commands in file %s", len(indexedNames), uri) | ||
|
|
||
| for name := range i.commandsByURI[uri] { | ||
| delete(i.commands, name) | ||
| } | ||
|
|
||
| maps.Copy(i.commands, indexedCommands) | ||
|
|
||
| if len(indexedNames) == 0 { | ||
| delete(i.commandsByURI, uri) | ||
| return | ||
| } | ||
|
|
||
| i.commandsByURI[uri] = indexedNames | ||
| } | ||
|
|
||
| func (i *index) findCommand(name string) (commandInfo, bool) { | ||
| i.mu.RLock() | ||
| defer i.mu.RUnlock() | ||
|
|
||
| command, ok := i.commands[name] | ||
|
|
||
| return command, ok | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (bug_risk): Indexing logic breaks when the same command name exists in multiple files.
This relies on command names being globally unique. With two files defining the same command,
maps.Copyoverwritescommands[name]whilecommandsByURIfor the original file still lists that command. On reindexing the first file, the cleanup loop deletescommands[name], removing the entry that actually belongs to the second file.If multiple files can legitimately define the same command, the index should be keyed by
(uri, name)or similar. If not, we should explicitly enforce/document global uniqueness and guard against duplicates rather than silently corrupting the index state.