@@ -38,7 +38,6 @@ static mlir::lsp::Position locToPos(mlir::Location location)
38
38
static llvm::Expected<mlir::lsp::Location> locToLoc (mlir::Location location)
39
39
{
40
40
auto castedBegin = location.cast <mlir::FileLineColLoc>();
41
- llvm::errs () << castedBegin.getFilename ().str ();
42
41
auto uri = mlir::lsp::URIForFile::fromFile (castedBegin.getFilename ().str ());
43
42
if (not uri)
44
43
{
@@ -52,6 +51,10 @@ static llvm::Expected<mlir::lsp::Location> locToLoc(mlir::Location location)
52
51
static mlir::lsp::Range locsToRange (mlir::Location begin, mlir::Location end)
53
52
{
54
53
auto toReturn = mlir::lsp::Range (locToPos (begin), locToPos (end));
54
+ // if the range goes backward, say that it is a one character wide range
55
+ // starting from the known start
56
+ if (toReturn.end .character < toReturn.start .character )
57
+ toReturn.end .character = toReturn.start .character + 1 ;
55
58
return toReturn;
56
59
}
57
60
@@ -167,6 +170,15 @@ class mlir::rlc::lsp::LSPModuleInfoImpl
167
170
declarations.emplace_back (
168
171
locsToRange (loc, firstInstructionLoc), decl.getOperation ());
169
172
});
173
+ module.walk ([this ](mlir::rlc::UncheckedEnumUse enumUse) {
174
+ if (enumUse->getNextNode () == nullptr )
175
+ return ;
176
+ auto firstInstructionLoc =
177
+ enumUse->getNextNode ()->getLoc ().cast <mlir::FileLineColLoc>();
178
+ auto loc = enumUse.getLoc ().cast <mlir::FileLineColLoc>();
179
+ enumUses.emplace_back (locsToRange (loc, firstInstructionLoc), enumUse);
180
+ enumUses.back ().first .end .character ++;
181
+ });
170
182
module.walk ([this ](mlir::rlc::CallOp decl) {
171
183
if (decl->getNextNode () == nullptr )
172
184
return ;
@@ -214,6 +226,17 @@ class mlir::rlc::lsp::LSPModuleInfoImpl
214
226
return nullptr ;
215
227
}
216
228
229
+ mlir::rlc::UncheckedEnumUse getNearestEnumUse (const mlir::lsp::Position &pos)
230
+ {
231
+ for (const auto &pair :
232
+ llvm::make_range (enumUses.rbegin (), enumUses.rend ()))
233
+ {
234
+ if (pair.first .contains (pos) and opIsInSameFile (pair.second ))
235
+ return pair.second ;
236
+ }
237
+ return nullptr ;
238
+ }
239
+
217
240
mlir::Operation *getNearestMemberAccess (const mlir::lsp::Position &pos)
218
241
{
219
242
for (const auto &pair :
@@ -509,6 +532,35 @@ class mlir::rlc::lsp::LSPModuleInfoImpl
509
532
return mlir::success ();
510
533
}
511
534
535
+ mlir::LogicalResult getCompleteEnum (
536
+ const mlir::lsp::Position &completePos, mlir::lsp::CompletionList &list)
537
+ {
538
+ auto enumUse = getNearestEnumUse (completePos);
539
+ if (enumUse == nullptr )
540
+ {
541
+ return mlir::failure ();
542
+ }
543
+
544
+ for (auto enumDecl : module.getOps <mlir::rlc::EnumDeclarationOp>())
545
+ {
546
+ if (enumDecl.getName () != enumUse.getEnumName ())
547
+ continue ;
548
+
549
+ using KeyType = std::pair<std::string, const void *>;
550
+ std::set<KeyType> alreadyEmitted;
551
+ for (auto field :
552
+ enumDecl.getBody ().getOps <mlir::rlc::EnumFieldDeclarationOp>())
553
+ {
554
+ mlir::lsp::CompletionItem item;
555
+ item.label = field.getName ();
556
+ item.kind = mlir::lsp::CompletionItemKind::Function;
557
+ item.insertTextFormat = mlir::lsp::InsertTextFormat::PlainText;
558
+ list.items .push_back (item);
559
+ }
560
+ }
561
+ return mlir::success ();
562
+ }
563
+
512
564
mlir::LogicalResult getCompleteAccessMember (
513
565
const mlir::lsp::Position &completePos, mlir::lsp::CompletionList &list)
514
566
{
@@ -707,6 +759,9 @@ class mlir::rlc::lsp::LSPModuleInfoImpl
707
759
llvm::SmallVector<std::pair<mlir::lsp::Range, mlir::Operation *>>
708
760
functionAndActionFunctions;
709
761
762
+ llvm::SmallVector<std::pair<mlir::lsp::Range, mlir::rlc::UncheckedEnumUse>>
763
+ enumUses;
764
+
710
765
llvm::SmallVector<mlir::rlc::lsp::Diagnostic> diagnostics;
711
766
mlir::DialectRegistry Registry;
712
767
mlir::MLIRContext context;
@@ -744,6 +799,13 @@ mlir::LogicalResult LSPModuleInfo::getCompleteAccessMember(
744
799
return impl->getCompleteAccessMember (completePos, list);
745
800
}
746
801
802
+ mlir::LogicalResult LSPModuleInfo::getCompleteEnum (
803
+ const mlir::lsp::Position &completePos,
804
+ mlir::lsp::CompletionList &list) const
805
+ {
806
+ return impl->getCompleteEnum (completePos, list);
807
+ }
808
+
747
809
mlir::LogicalResult LSPModuleInfo::getCompleteType (
748
810
const mlir::lsp::Position &completePos,
749
811
mlir::lsp::CompletionList &list) const
@@ -916,6 +978,11 @@ mlir::lsp::CompletionList RLCServer::getCodeCompletion(
916
978
return list;
917
979
}
918
980
981
+ if (maybeInfo->getCompleteEnum (completePos, list).succeeded ())
982
+ {
983
+ return list;
984
+ }
985
+
919
986
if (maybeInfo->getCompleteAccessMember (completePos, list).succeeded ())
920
987
{
921
988
return list;
0 commit comments