@@ -321,7 +321,7 @@ func (f *file) IndexSymbols(ctx context.Context) {
321321 for _ , sym := range unresolved {
322322 switch kind := sym .kind .(type ) {
323323 case * reference :
324- def := f .resolveASTDefinition (kind .def , kind .fullName )
324+ def := f .resolveASTDefinition (ctx , kind .def , kind .fullName )
325325 sym .def = def
326326 if def == nil {
327327 // In the case where the symbol is not resolved, we continue
@@ -339,7 +339,7 @@ func (f *file) IndexSymbols(ctx context.Context) {
339339 }
340340 referenceable .references = append (referenceable .references , sym )
341341 case * option :
342- def := f .resolveASTDefinition (kind .def , kind .defFullName )
342+ def := f .resolveASTDefinition (ctx , kind .def , kind .defFullName )
343343 sym .def = def
344344 if def != nil {
345345 referenceable , ok := def .kind .(* referenceable )
@@ -354,7 +354,7 @@ func (f *file) IndexSymbols(ctx context.Context) {
354354 referenceable .references = append (referenceable .references , sym )
355355 }
356356 }
357- typeDef := f .resolveASTDefinition (kind .typeDef , kind .typeDefFullName )
357+ typeDef := f .resolveASTDefinition (ctx , kind .typeDef , kind .typeDefFullName )
358358 sym .typeDef = typeDef
359359 default :
360360 // This shouldn't happen, logging a warning
@@ -756,36 +756,38 @@ func (f *file) messageToSymbolsHelper(msg ir.MessageValue, index int, parents []
756756
757757// resolveASTDefinition is a helper for resolving the [ast.DeclDef] to the *[symbol], if
758758// there is a matching indexed *[symbol].
759- func (f * file ) resolveASTDefinition (def ast.DeclDef , defName ir.FullName ) * symbol {
760- // No workspace, we cannot resolve the AST definition
761- if f . workspace == nil {
762- return nil
759+ func (f * file ) resolveASTDefinition (ctx context. Context , def ast.DeclDef , defName ir.FullName ) * symbol {
760+ // First check if the definition is in the current file.
761+ if def . Span (). Path () == f . objectInfo . LocalPath () {
762+ return f . referenceableSymbols [ defName ]
763763 }
764- // We resolve the import path of the span of the AST definition and search for it in our
765- // workspace.
766- fileInfo , ok := f .workspace .fileNameToFileInfo [def .Span ().Path ()]
767- if ! ok {
768- // Unable to resolve an importable path for the file in our workspace, log a debug
769- // statement and return no definition.
770- f .lsp .logger .Debug (
771- "unable to resolve an importable file path for local path" ,
772- slog .String ("file" , f .uri .Filename ()),
773- slog .String ("local path" , def .Span ().Path ()),
774- )
764+ // No workspace, we cannot resolve the AST definition from outside of the file.
765+ if f .workspace == nil {
775766 return nil
776767 }
777- file , ok := f .workspace .PathToFile ()[fileInfo .Path ()]
778- if ! ok {
779- // Check current file
780- if def .Span ().Path () != f .objectInfo .Path () {
781- // If there is no file for the [ast.DeclDef] span, which can if it is a predeclared
782- // type, for example, or if the file we are checking has not indexed imports, then
783- // we return nil.
784- return nil
768+ for objectInfo := range f .workspace .fileInfos (ctx ) {
769+ if objectInfo .LocalPath () == def .Span ().Path () {
770+ file , ok := f .workspace .PathToFile ()[objectInfo .Path ()]
771+ if ! ok {
772+ // This shouldn't happen, if it does, we log an error and optimistically continue
773+ // to walk file infos.
774+ f .lsp .logger .Error (
775+ "found local path that does not resolve to a file in the workspace" ,
776+ slog .String ("file" , f .uri .Filename ()),
777+ slog .String ("local path" , def .Span ().Path ()),
778+ )
779+ }
780+ return file .referenceableSymbols [defName ]
785781 }
786- file = f
787782 }
788- return file .referenceableSymbols [defName ]
783+ // Unable to resolve an importable path for the file in our workspace, log a debug
784+ // statement and return no definition.
785+ f .lsp .logger .Debug (
786+ "unable to resolve an importable file path for local path" ,
787+ slog .String ("file" , f .uri .Filename ()),
788+ slog .String ("local path" , def .Span ().Path ()),
789+ )
790+ return nil
789791}
790792
791793// SymbolAt finds a symbol in this file at the given cursor position, if one exists.
0 commit comments