feat(lsp): add go-to-definition for labels

This commit is contained in:
2026-08-05 11:08:00 +02:00
parent 8ddac0135e
commit 2d45dbf7ff
3 changed files with 45 additions and 0 deletions
+35
View File
@@ -90,6 +90,41 @@ func (s *Server) hover(p hoverParams) *Hover {
} }
} }
// definition returns the location of the label definition for a label reference.
func (s *Server) definition(p definitionParams) []Location {
text := s.docs[p.TextDocument.URI]
word, _ := wordAt(text, p.Position)
if word == "" {
return nil
}
// Parse the document to find label definitions.
f, errs := parser.Parse(uriPath(p.TextDocument.URI), text)
if f == nil || len(errs) > 0 {
return nil
}
// Find the label definition.
for _, d := range f.Decls {
if t, ok := d.(*ast.Text); ok {
for _, stmt := range t.Body {
if lbl, ok := stmt.(*ast.Label); ok {
if lbl.Name.Text == word {
return []Location{{
URI: p.TextDocument.URI,
Range: Range{
Start: Position{Line: lbl.Name.Pos.Line - 1, Character: lbl.Name.Pos.Column - 1},
End: Position{Line: lbl.Name.Pos.Line - 1, Character: lbl.Name.Pos.Column - 1 + len(word)},
},
}}
}
}
}
}
}
return nil
}
// documentSymbols returns functions and their labels, plus global symbols. // documentSymbols returns functions and their labels, plus global symbols.
func (s *Server) documentSymbols(p documentSymbolParams) []DocumentSymbol { func (s *Server) documentSymbols(p documentSymbolParams) []DocumentSymbol {
text := s.docs[p.TextDocument.URI] text := s.docs[p.TextDocument.URI]
+5
View File
@@ -152,6 +152,11 @@ type hoverParams struct {
Position Position `json:"position"` Position Position `json:"position"`
} }
type definitionParams struct {
TextDocument textDocumentIdentifier `json:"textDocument"`
Position Position `json:"position"`
}
// Hover is the hover response. // Hover is the hover response.
type Hover struct { type Hover struct {
Contents markupContent `json:"contents"` Contents markupContent `json:"contents"`
+5
View File
@@ -170,6 +170,11 @@ func (s *Server) dispatch(msg *rpcMessage) (exit bool) {
json.Unmarshal(msg.Params, &p) json.Unmarshal(msg.Params, &p)
s.respond(msg.ID, s.hover(p)) s.respond(msg.ID, s.hover(p))
case "textDocument/definition":
var p definitionParams
json.Unmarshal(msg.Params, &p)
s.respond(msg.ID, s.definition(p))
case "textDocument/documentSymbol": case "textDocument/documentSymbol":
var p documentSymbolParams var p documentSymbolParams
json.Unmarshal(msg.Params, &p) json.Unmarshal(msg.Params, &p)