55 "slices"
66
77 "github.com/lets-cli/lets/internal/util"
8+ "github.com/tliron/commonlog"
89 "github.com/tliron/glsp"
910 lsp "github.com/tliron/glsp/protocol_3_16"
1011)
@@ -43,6 +44,7 @@ func (s *lspServer) setTrace(context *glsp.Context, params *lsp.SetTraceParams)
4344
4445func (s * lspServer ) textDocumentDidOpen (context * glsp.Context , params * lsp.DidOpenTextDocumentParams ) error {
4546 s .storage .AddDocument (params .TextDocument .URI , params .TextDocument .Text )
47+ go s .index .IndexDocument (params .TextDocument .URI , params .TextDocument .Text )
4648 return nil
4749}
4850
@@ -51,6 +53,7 @@ func (s *lspServer) textDocumentDidChange(context *glsp.Context, params *lsp.Did
5153 switch c := change .(type ) {
5254 case lsp.TextDocumentContentChangeEventWhole :
5355 s .storage .AddDocument (params .TextDocument .URI , c .Text )
56+ go s .index .IndexDocument (params .TextDocument .URI , c .Text )
5457 case lsp.TextDocumentContentChangeEvent :
5558 return errors .New ("incremental changes not supported" )
5659 }
@@ -60,7 +63,9 @@ func (s *lspServer) textDocumentDidChange(context *glsp.Context, params *lsp.Did
6063}
6164
6265type definitionHandler struct {
66+ log commonlog.Logger
6367 parser * parser
68+ index * index
6469}
6570
6671func (h * definitionHandler ) findMixinsDefinition (doc * string , params * lsp.DefinitionParams ) (any , error ) {
@@ -89,46 +94,47 @@ func (h *definitionHandler) findMixinsDefinition(doc *string, params *lsp.Defini
8994 }, nil
9095}
9196
97+ func locationForCommand (uri string , position lsp.Position ) lsp.Location {
98+ return lsp.Location {
99+ URI : uri ,
100+ Range : lsp.Range {
101+ Start : lsp.Position {
102+ Line : position .Line ,
103+ Character : 2 , // TODO: do we have to assume indentation?
104+ },
105+ End : lsp.Position {
106+ Line : position .Line ,
107+ Character : 2 , // TODO: do we need + len ?
108+ },
109+ },
110+ }
111+ }
112+
92113func (h * definitionHandler ) findCommandDefinition (doc * string , params * lsp.DefinitionParams ) (any , error ) {
93114 path := normalizePath (params .TextDocument .URI )
94115
95116 commandName := h .parser .extractCommandReference (doc , params .Position )
96117 if commandName == "" {
97- h .parser . log .Debugf ("no command reference resolved at %s:%d:%d" , path , params .Position .Line , params .Position .Character )
118+ h .log .Debugf ("no command reference resolved at %s:%d:%d" , path , params .Position .Line , params .Position .Character )
98119 return nil , nil
99120 }
100121
101- command := h .parser .findCommand (doc , commandName )
102- if command == nil {
103- h .parser . log .Debugf ("command reference %q did not match any local command" , commandName )
122+ commandInfo , found := h .index .findCommand (commandName )
123+ if ! found {
124+ h .log .Debugf ("command reference %q did not match any local command" , commandName )
104125 return nil , nil
105126 }
106127
107- h .parser . log .Debugf (
128+ h .log .Debugf (
108129 "resolved command definition %q -> %s:%d:%d" ,
109130 commandName ,
110131 path ,
111- command .position .Line ,
112- command .position .Character ,
132+ commandInfo .position .Line ,
133+ commandInfo .position .Character ,
113134 )
114135
115- // TODO: theoretically we can have multiple commands with the same name if we have mixins
116- return []lsp.Location {
117- {
118- // TODO: support commands in other files
119- URI : params .TextDocument .URI ,
120- Range : lsp.Range {
121- Start : lsp.Position {
122- Line : command .position .Line ,
123- Character : 2 , // TODO: do we have to assume indentation?
124- },
125- End : lsp.Position {
126- Line : command .position .Line ,
127- Character : 2 , // TODO: do we need + len ?
128- },
129- },
130- },
131- }, nil
136+ loc := locationForCommand (commandInfo .fileURI , commandInfo .position )
137+ return []lsp.Location {loc }, nil
132138}
133139
134140type completionHandler struct {
@@ -165,7 +171,9 @@ func (h *completionHandler) buildDependsCompletions(doc *string, params *lsp.Com
165171// Returns: Location | []Location | []LocationLink | nil.
166172func (s * lspServer ) textDocumentDefinition (context * glsp.Context , params * lsp.DefinitionParams ) (any , error ) {
167173 definitionHandler := definitionHandler {
174+ log : s .log ,
168175 parser : newParser (s .log ),
176+ index : s .index ,
169177 }
170178 doc := s .storage .GetDocument (params .TextDocument .URI )
171179
0 commit comments