Fix LSP go-to-definition for command aliases#322
Open
Conversation
Contributor
There was a problem hiding this comment.
Your free trial has ended. If you'd like to continue receiving code reviews, you can add a payment method here.
Reviewer's GuideTeach the LSP to recognize YAML merge alias positions (e.g., Sequence diagram for LSP go-to-definition on YAML command aliassequenceDiagram
actor Editor
participant LSPClient
participant lspServer
participant definitionHandler
participant parser
participant TreeSitter
Editor->>LSPClient: go to definition at position (alias *foo)
LSPClient->>lspServer: textDocument/definition(params)
lspServer->>parser: getPositionType(doc, position)
parser->>TreeSitter: parseYAMLDocument(doc)
TreeSitter-->>parser: tree, docBytes
parser->>parser: inMixinsPosition?
parser-->>lspServer: PositionTypeCommandAlias
lspServer->>definitionHandler: findCommandDefinition(doc, params)
definitionHandler->>parser: extractCommandReference(doc, position)
activate parser
parser->>parser: extractDependsCommandReference(doc, position)
parser->>TreeSitter: parseYAMLDocument(doc)
TreeSitter-->>parser: tree, docBytes
parser->>parser: query depends references
parser-->>parser: "" (no match)
parser->>parser: extractAliasCommandReference(doc, position)
parser->>TreeSitter: parseYAMLDocument(doc)
TreeSitter-->>parser: tree, docBytes
parser->>parser: query merge alias <<: *foo
parser-->>definitionHandler: commandName "foo"
deactivate parser
definitionHandler->>parser: findCommand(doc, commandName)
parser-->>definitionHandler: Command{name: foo}
definitionHandler-->>lspServer: location of command foo
lspServer-->>LSPClient: definition Location
LSPClient-->>Editor: jump cursor to command foo definition
Class diagram for updated LSP parser and definition handlingclassDiagram
class parser {
+getPositionType(document *string, position lsp_Position) PositionType
+inDependsPosition(document *string, position lsp_Position) bool
+inCommandAliasPosition(document *string, position lsp_Position) bool
+extractCommandReference(document *string, position lsp_Position) string
+extractDependsCommandReference(document *string, position lsp_Position) string
+extractAliasCommandReference(document *string, position lsp_Position) string
+findCommand(document *string, name string) *Command
}
class definitionHandler {
-parser *parser
+findMixinsDefinition(doc *string, params *lsp_DefinitionParams) any
+findCommandDefinition(doc *string, params *lsp_DefinitionParams) (any, error)
}
class lspServer {
-parser *parser
-definitionHandler *definitionHandler
+textDocumentDefinition(context *glsp_Context, params *lsp_DefinitionParams) (any, error)
}
class Command {
-name string
}
class PositionType {
<<enumeration>>
PositionTypeMixins
PositionTypeDepends
PositionTypeCommandAlias
PositionTypeNone
}
parser --> Command : uses
definitionHandler --> parser : composes
lspServer --> parser : has
lspServer --> definitionHandler : has
parser --> PositionType : returns
definitionHandler --> Command : returns location of
lspServer --> Command : via definitionHandler
File-Level Changes
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- Both
extractDependsCommandReferenceandextractAliasCommandReference(andinCommandAliasPosition) repeat the same parse/query/match boilerplate; consider extracting a small helper to reduce duplication and make future YAML query changes easier. - The go-to-definition path now reparses the YAML multiple times per request (e.g.,
getPositionTypeand thenextractCommandReference); if this becomes hot, consider threading a parsed tree or a lightweight cache through the parser/handler to avoid redundant work.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Both `extractDependsCommandReference` and `extractAliasCommandReference` (and `inCommandAliasPosition`) repeat the same parse/query/match boilerplate; consider extracting a small helper to reduce duplication and make future YAML query changes easier.
- The go-to-definition path now reparses the YAML multiple times per request (e.g., `getPositionType` and then `extractCommandReference`); if this becomes hot, consider threading a parsed tree or a lightweight cache through the parser/handler to avoid redundant work.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
<<: *foo) and extract the referenced command nameSummary by Sourcery
Improve LSP go-to-definition support for command references in YAML configs.
Bug Fixes:
<<: *testto jump to the corresponding command definition inlets self lsp.Enhancements:
dependsentries and command aliases in the LSP parser.Documentation:
Tests: