Skip to content

Commit b2c4fb9

Browse files
committed
improved autocomplete
1 parent 60b5e28 commit b2c4fb9

File tree

3 files changed

+86
-6
lines changed

3 files changed

+86
-6
lines changed

lib/lsp/include/rlc/lsp/LSP.hpp

+4
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ namespace mlir::rlc::lsp
6363
const mlir::lsp::Position &completePos,
6464
mlir::lsp::CompletionList &list) const;
6565

66+
mlir::LogicalResult getCompleteEnum(
67+
const mlir::lsp::Position &completePos,
68+
mlir::lsp::CompletionList &list) const;
69+
6670
mlir::LogicalResult getCompleteFunction(
6771
const mlir::lsp::Position &completePos,
6872
mlir::lsp::CompletionList &list) const;

lib/lsp/src/LSP.cpp

+68-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ static mlir::lsp::Position locToPos(mlir::Location location)
3838
static llvm::Expected<mlir::lsp::Location> locToLoc(mlir::Location location)
3939
{
4040
auto castedBegin = location.cast<mlir::FileLineColLoc>();
41-
llvm::errs() << castedBegin.getFilename().str();
4241
auto uri = mlir::lsp::URIForFile::fromFile(castedBegin.getFilename().str());
4342
if (not uri)
4443
{
@@ -52,6 +51,10 @@ static llvm::Expected<mlir::lsp::Location> locToLoc(mlir::Location location)
5251
static mlir::lsp::Range locsToRange(mlir::Location begin, mlir::Location end)
5352
{
5453
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;
5558
return toReturn;
5659
}
5760

@@ -167,6 +170,15 @@ class mlir::rlc::lsp::LSPModuleInfoImpl
167170
declarations.emplace_back(
168171
locsToRange(loc, firstInstructionLoc), decl.getOperation());
169172
});
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+
});
170182
module.walk([this](mlir::rlc::CallOp decl) {
171183
if (decl->getNextNode() == nullptr)
172184
return;
@@ -214,6 +226,17 @@ class mlir::rlc::lsp::LSPModuleInfoImpl
214226
return nullptr;
215227
}
216228

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+
217240
mlir::Operation *getNearestMemberAccess(const mlir::lsp::Position &pos)
218241
{
219242
for (const auto &pair :
@@ -509,6 +532,35 @@ class mlir::rlc::lsp::LSPModuleInfoImpl
509532
return mlir::success();
510533
}
511534

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+
512564
mlir::LogicalResult getCompleteAccessMember(
513565
const mlir::lsp::Position &completePos, mlir::lsp::CompletionList &list)
514566
{
@@ -707,6 +759,9 @@ class mlir::rlc::lsp::LSPModuleInfoImpl
707759
llvm::SmallVector<std::pair<mlir::lsp::Range, mlir::Operation *>>
708760
functionAndActionFunctions;
709761

762+
llvm::SmallVector<std::pair<mlir::lsp::Range, mlir::rlc::UncheckedEnumUse>>
763+
enumUses;
764+
710765
llvm::SmallVector<mlir::rlc::lsp::Diagnostic> diagnostics;
711766
mlir::DialectRegistry Registry;
712767
mlir::MLIRContext context;
@@ -744,6 +799,13 @@ mlir::LogicalResult LSPModuleInfo::getCompleteAccessMember(
744799
return impl->getCompleteAccessMember(completePos, list);
745800
}
746801

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+
747809
mlir::LogicalResult LSPModuleInfo::getCompleteType(
748810
const mlir::lsp::Position &completePos,
749811
mlir::lsp::CompletionList &list) const
@@ -916,6 +978,11 @@ mlir::lsp::CompletionList RLCServer::getCodeCompletion(
916978
return list;
917979
}
918980

981+
if (maybeInfo->getCompleteEnum(completePos, list).succeeded())
982+
{
983+
return list;
984+
}
985+
919986
if (maybeInfo->getCompleteAccessMember(completePos, list).succeeded())
920987
{
921988
return list;

lib/parser/src/Parser.cpp

+14-5
Original file line numberDiff line numberDiff line change
@@ -276,13 +276,20 @@ Expected<mlir::Value> Parser::primaryExpression()
276276
return builder.create<mlir::rlc::UnresolvedReference>(location, lIdent);
277277

278278
auto enumName = lIdent;
279-
EXPECT(Token::Identifier);
280-
auto enumField = lIdent;
281-
return builder.create<mlir::rlc::UncheckedEnumUse>(
279+
std::string enumField = "";
280+
if (accept(Token::Identifier))
281+
{
282+
enumField = lIdent;
283+
}
284+
285+
auto toReturn = builder.create<mlir::rlc::UncheckedEnumUse>(
282286
location,
283287
mlir::rlc::UnknownType::get(builder.getContext()),
284288
enumName,
285289
enumField);
290+
if (enumField.empty())
291+
EXPECT(Token::Identifier);
292+
return toReturn;
286293
}
287294

288295
if (accept<Token::Double>())
@@ -1165,7 +1172,8 @@ llvm::Expected<mlir::rlc::IfStatement> Parser::ifStatement()
11651172
if (condExp == nullptr)
11661173
condExp =
11671174
builder.create<mlir::rlc::Constant>(getCurrentSourcePos(), false);
1168-
builder.create<mlir::rlc::Yield>(location, mlir::ValueRange(condExp));
1175+
auto yieldLoc = getCurrentSourcePos();
1176+
builder.create<mlir::rlc::Yield>(yieldLoc, mlir::ValueRange(condExp));
11691177

11701178
builder.setInsertionPointToEnd(trueB);
11711179
emitYieldIfNeeded(getCurrentSourcePos());
@@ -1342,7 +1350,8 @@ Expected<mlir::rlc::WhileStatement> Parser::whileStatement()
13421350
builder.setInsertionPointToEnd(condB);
13431351
if (exp == nullptr)
13441352
exp = builder.create<mlir::rlc::Constant>(getCurrentSourcePos(), false);
1345-
builder.create<mlir::rlc::Yield>(location, mlir::ValueRange({ exp }));
1353+
auto yieldLocation = getCurrentSourcePos();
1354+
builder.create<mlir::rlc::Yield>(yieldLocation, mlir::ValueRange({ exp }));
13461355
builder.setInsertionPointToEnd(bodyB);
13471356
emitYieldIfNeeded(getCurrentSourcePos());
13481357
builder.restoreInsertionPoint(pos);

0 commit comments

Comments
 (0)