@@ -102,6 +102,7 @@ class mlir::rlc::lsp::LSPModuleInfoImpl
102102 explicit LSPModuleInfoImpl (
103103 llvm::StringRef path, llvm::StringRef contents, LSPContext &lspContext)
104104 : context(mlir::MLIRContext::Threading::DISABLED),
105+ path(path.str()),
105106 diagnosticHandler(
106107 &context,
107108 [this ](mlir::Diagnostic &diagnostic) {
@@ -651,34 +652,81 @@ class mlir::rlc::lsp::LSPModuleInfoImpl
651652 return mlir::success ();
652653 }
653654
655+ template <template <class > class trait >
656+ mlir::Operation *getNearestOpWithTrait (const mlir::lsp::Position &defPos)
657+ {
658+ mlir::Operation *nearest = nullptr ;
659+ int column = 0 ;
660+ module .walk ([&](mlir::Operation *op) {
661+ if (not op->hasTrait <trait>())
662+ return ;
663+ mlir::Location loc = op->getLoc ();
664+ if (loc == nullptr )
665+ return ;
666+
667+ auto pos = locToPos (loc);
668+
669+ if (defPos.line != pos.line )
670+ return ;
671+
672+ auto castedBegin = mlir::cast<mlir::FileLineColLoc>(loc);
673+ if (path != castedBegin.getFilename ())
674+ return ;
675+
676+ if (abs (defPos.character - pos.character ) < abs (column - pos.character ))
677+ {
678+ column = pos.character ;
679+ nearest = op;
680+ }
681+ });
682+ return nearest;
683+ }
684+
685+ mlir::Operation *findDeclarationOf (mlir::Type type)
686+ {
687+ if (auto casted = mlir::dyn_cast<mlir::rlc::ClassType>(type))
688+ for (auto classDecl : module .getOps <mlir::rlc::ClassDeclaration>())
689+ {
690+ if (classDecl.getName () == casted.getName ())
691+ return classDecl;
692+ }
693+
694+ return nullptr ;
695+ }
696+
654697 void getLocationsOf (
655698 const mlir::lsp::Position &defPos,
656699 std::vector<mlir::lsp::Location> &locations)
657700 {
658- auto *nearestDecl = getOperation (defPos);
701+ auto *nearestDecl =
702+ getNearestOpWithTrait<mlir::rlc::DefinitionUser::Trait>(defPos);
659703 if (nearestDecl == nullptr )
660704 return ;
661705
662- auto casted = mlir::dyn_cast<mlir::rlc::CallOp >(nearestDecl);
706+ auto casted = mlir::dyn_cast<mlir::rlc::DefinitionUser >(nearestDecl);
663707 if (not casted)
664708 return ;
665709
666- auto templateInst =
667- mlir::dyn_cast<mlir::rlc::TemplateInstantiationOp>(nearestDecl);
668- if (templateInst)
710+ for (auto *op : casted.getUsedOperations ())
669711 {
670- auto maybeLoc = locToLoc (templateInst. getInputTemplate (). getLoc ());
712+ auto maybeLoc = locToLoc (op-> getLoc ());
671713 if (maybeLoc)
672714 locations.push_back (*maybeLoc);
673715 else
674716 llvm::consumeError (maybeLoc.takeError ());
675- return ;
676- };
677- auto maybeLoc = locToLoc (casted.getCallee ().getLoc ());
678- if (maybeLoc)
679- locations.push_back (*maybeLoc);
680- else
681- llvm::consumeError (maybeLoc.takeError ());
717+ }
718+
719+ for (auto type : casted.getUsedTypes ())
720+ {
721+ auto op = findDeclarationOf (type);
722+ if (not op)
723+ continue ;
724+ auto maybeLoc = locToLoc (op->getLoc ());
725+ if (maybeLoc)
726+ locations.push_back (*maybeLoc);
727+ else
728+ llvm::consumeError (maybeLoc.takeError ());
729+ }
682730 }
683731
684732 void findReferencesOf (
@@ -769,6 +817,7 @@ class mlir::rlc::lsp::LSPModuleInfoImpl
769817 llvm::SmallVector<mlir::rlc::lsp::Diagnostic> diagnostics;
770818 mlir::DialectRegistry Registry;
771819 mlir::MLIRContext context;
820+ std::string path;
772821 mlir::ScopedDiagnosticHandler diagnosticHandler;
773822 mlir::ModuleOp module ;
774823 std::string currentFileContent;
0 commit comments