@@ -488,50 +488,38 @@ func (sc *LSPServiceClientBase) populateDocumentSymbolCache(ctx context.Context,
488488 return
489489 }
490490
491- didOpen := func (uri string , langID string , text []byte ) error {
492- params := protocol.DidOpenTextDocumentParams {
493- TextDocument : protocol.TextDocumentItem {
494- URI : uri ,
495- LanguageID : langID ,
496- Version : 0 ,
497- Text : string (text ),
498- },
499- }
500- // typescript server seems to throw "No project" error without notification
501- // perhaps there's a better way to do this
502- return sc .Conn .Notify (ctx , "textDocument/didOpen" , params )
503- }
504-
505- didClose := func (uri string ) error {
506- params := protocol.DidCloseTextDocumentParams {
507- TextDocument : protocol.TextDocumentIdentifier {
508- URI : uri ,
509- },
510- }
511- return sc .Conn .Notify (ctx , "textDocument/didClose" , params )
512- }
513-
514491 queryDocumentSymbol := func (ctx context.Context , uri uri.URI , content []byte ) ([]protocol.DocumentSymbol , error ) {
515492 if symbols , exists := sc .symbolCache .GetDocumentSymbols (uri ); exists {
516493 return symbols , nil
517494 }
518- var symbols []protocol.DocumentSymbol
495+
496+ var symbols []struct {
497+ protocol.DocumentSymbol
498+ Location * protocol.Location `json:"location,omitempty"`
499+ }
519500 params := protocol.DocumentSymbolParams {
520501 TextDocument : protocol.TextDocumentIdentifier {
521502 URI : string (uri ),
522503 },
523504 }
524- if err := didOpen (string (uri ), sc .symbolSearchHelper .GetLanguageID (string (uri )), content ); err != nil {
505+ if err := sc . didOpen (ctx , string (uri ), sc .symbolSearchHelper .GetLanguageID (string (uri )), content ); err != nil {
525506 sc .Log .Error (err , "didOpen request failed" , "uri" , uri )
526507 }
527508 if err := sc .Conn .Call (ctx , "textDocument/documentSymbol" , params ).Await (ctx , & symbols ); err != nil {
528509 return nil , err
529510 }
530- if err := didClose (string (uri )); err != nil {
511+ if err := sc . didClose (ctx , string (uri )); err != nil {
531512 sc .Log .Error (err , "didClose request failed" , "uri" , uri )
532513 }
533- sc .symbolCache .SetDocumentSymbols (uri , symbols )
534- return symbols , nil
514+ documentSymbols := []protocol.DocumentSymbol {}
515+ for _ , symbol := range symbols {
516+ if symbol .Location != nil {
517+ symbol .DocumentSymbol .Range = symbol .Location .Range
518+ }
519+ documentSymbols = append (documentSymbols , symbol .DocumentSymbol )
520+ }
521+ sc .symbolCache .SetDocumentSymbols (uri , documentSymbols )
522+ return documentSymbols , nil
535523 }
536524
537525 workspaceSymbolKey := func (symbol protocol.WorkspaceSymbol ) string {
@@ -558,7 +546,7 @@ func (sc *LSPServiceClientBase) populateDocumentSymbolCache(ctx context.Context,
558546 // definition is found, the symbol will be used as a reference symbol.
559547 positions := sc .getMatchingPositions (string (content ), fileURI .Filename ())
560548 for _ , position := range positions {
561- definitions := sc .getDefinitionForPosition (ctx , position )
549+ definitions := sc .getDefinitionForPosition (ctx , fileURI , position )
562550 wsSymbolsForDefinitions := map [string ]protocol.WorkspaceSymbol {}
563551 for _ , definition := range definitions {
564552 uri , err := toURI (definition .URI )
@@ -657,6 +645,29 @@ func (sc *LSPServiceClientBase) drainPendingSymbolCacheUpdates() {
657645 }
658646}
659647
648+ func (sc * LSPServiceClientBase ) didOpen (ctx context.Context , uri string , langID string , text []byte ) error {
649+ params := protocol.DidOpenTextDocumentParams {
650+ TextDocument : protocol.TextDocumentItem {
651+ URI : uri ,
652+ LanguageID : langID ,
653+ Version : 0 ,
654+ Text : string (text ),
655+ },
656+ }
657+ // typescript server seems to throw "No project" error without notification
658+ // perhaps there's a better way to do this
659+ return sc .Conn .Notify (ctx , "textDocument/didOpen" , params )
660+ }
661+
662+ func (sc * LSPServiceClientBase ) didClose (ctx context.Context , uri string ) error {
663+ params := protocol.DidCloseTextDocumentParams {
664+ TextDocument : protocol.TextDocumentIdentifier {
665+ URI : uri ,
666+ },
667+ }
668+ return sc .Conn .Notify (ctx , "textDocument/didClose" , params )
669+ }
670+
660671func toURI (path string ) (uri.URI , error ) {
661672 if strings .HasPrefix (path , "file://" ) {
662673 return uri .Parse (path )
@@ -702,12 +713,23 @@ func (sc *LSPServiceClientBase) getMatchingPositions(content string, path string
702713 return positions
703714}
704715
705- func (sc * LSPServiceClientBase ) getDefinitionForPosition (ctx context.Context , position protocol.TextDocumentPositionParams ) []protocol.Location {
716+ func (sc * LSPServiceClientBase ) getDefinitionForPosition (ctx context.Context , uri uri. URI , position protocol.TextDocumentPositionParams ) []protocol.Location {
706717 res := []protocol.Location {}
707718 if sc .ServerCapabilities .Supports ("textDocument/definition" ) {
719+ content , err := os .ReadFile (uri .Filename ())
720+ if err != nil {
721+ sc .Log .Error (err , "unable to read file" , "uri" , uri )
722+ return res
723+ }
724+ if err := sc .didOpen (ctx , string (position .TextDocument .URI ), sc .symbolSearchHelper .GetLanguageID (string (position .TextDocument .URI )), content ); err != nil {
725+ sc .Log .Error (err , "didOpen request failed" , "uri" , position .TextDocument .URI )
726+ }
708727 if err := sc .Conn .Call (ctx , "textDocument/definition" , position ).Await (ctx , & res ); err != nil {
709728 sc .Log .Error (err , "textDocument/definition request failed" , "position" , position )
710729 }
730+ if err := sc .didClose (ctx , string (position .TextDocument .URI )); err != nil {
731+ sc .Log .Error (err , "didClose request failed" , "uri" , position .TextDocument .URI )
732+ }
711733 }
712734 return res
713735}
0 commit comments