Skip to content

Commit a98543c

Browse files
authored
Add option to trigger the code completion from cli
1 parent a2c00e1 commit a98543c

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

src/frontend/cxx/frontend.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,18 @@ auto runOnFile(const CLI& cli, const std::string& fileName) -> bool {
218218
preprocessor->undefMacro(macro);
219219
}
220220

221+
if (auto completion = cli.getSingle("-code-completion-at")) {
222+
auto re = std::regex(R"((.*):(\d+):(\d+))");
223+
std::smatch match;
224+
if (std::regex_match(*completion, match, re)) {
225+
auto line = std::stoi(match[2]);
226+
auto column = std::stoi(match[3]);
227+
preprocessor->requestCodeCompletionAt(line, column);
228+
} else {
229+
std::cerr << "cxx: invalid argument to -code-completion-at\n";
230+
}
231+
}
232+
221233
auto outputs = cli.get("-o");
222234

223235
auto outfile = !outputs.empty() && outputs.back() != "-"

src/parser/cxx/cli.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ std::vector<CLIOptionDescr> options{
199199

200200
{"-j", "<n>", "Run <n> jobs in parallel.", CLIOptionDescrKind::kSeparated},
201201

202+
{"-code-completion-at", "<file>:<line>:<column>", CLIOptionDescrKind::kJoined},
203+
202204
{"-v", "Show commands to run and use verbose output", &CLI::opt_v},
203205

204206
};

src/parser/cxx/parser.cc

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@
3838
#include <algorithm>
3939
#include <cstring>
4040
#include <format>
41+
#include <iostream>
4142
#include <ranges>
43+
#include <unordered_set>
44+
45+
#include "cxx/source_location.h"
46+
#include "cxx/token_fwd.h"
4247

4348
namespace cxx {
4449

@@ -1503,6 +1508,14 @@ auto Parser::parse_unqualified_id(UnqualifiedIdAST*& yyast,
15031508
NestedNameSpecifierAST* nestedNameSpecifier,
15041509
bool isTemplateIntroduced,
15051510
bool inRequiresClause) -> bool {
1511+
if (!stopParsing_ && lookat(TokenKind::T_CODE_COMPLETION)) {
1512+
stopParsing_ = true;
1513+
1514+
warning(std::format("requested code completion of unqualified-id"));
1515+
1516+
consumeToken();
1517+
}
1518+
15061519
auto lookat_template_id = [&] {
15071520
LookaheadParser lookahead{this};
15081521

@@ -2434,6 +2447,14 @@ auto Parser::parse_member_expression(ExpressionAST*& yyast) -> bool {
24342447

24352448
ast->isTemplateIntroduced = match(TokenKind::T_TEMPLATE, ast->templateLoc);
24362449

2450+
if (!stopParsing_ && lookat(TokenKind::T_CODE_COMPLETION)) {
2451+
stopParsing_ = true;
2452+
2453+
warning(std::format("requested code completion of member access"));
2454+
2455+
consumeToken();
2456+
}
2457+
24372458
if (!parse_unqualified_id(ast->unqualifiedId, ast->nestedNameSpecifier,
24382459
ast->isTemplateIntroduced,
24392460
/*inRequiresClause*/ false))
@@ -5184,6 +5205,16 @@ auto Parser::parse_attribute_declaration(DeclarationAST*& yyast) -> bool {
51845205

51855206
auto Parser::parse_decl_specifier(SpecifierAST*& yyast, DeclSpecs& specs)
51865207
-> bool {
5208+
if (!stopParsing_ && lookat(TokenKind::T_CODE_COMPLETION)) {
5209+
stopParsing_ = true;
5210+
5211+
warning(
5212+
std::format("requested code completion of decl specifier in scope '{}'",
5213+
to_string(scope_->owner()->name())));
5214+
5215+
consumeToken();
5216+
}
5217+
51875218
switch (TokenKind(LA())) {
51885219
case TokenKind::T_TYPEDEF: {
51895220
auto ast = make_node<TypedefSpecifierAST>(pool_);
@@ -5666,6 +5697,13 @@ auto Parser::parse_named_type_specifier(SpecifierAST*& yyast, DeclSpecs& specs)
56665697
SourceLocation templateLoc;
56675698
const auto isTemplateIntroduced = match(TokenKind::T_TEMPLATE, templateLoc);
56685699

5700+
if (SourceLocation completionLoc;
5701+
match(TokenKind::T_CODE_COMPLETION, completionLoc)) {
5702+
stopParsing_ = true;
5703+
parse_warn(completionLoc, "code completion at type specifier");
5704+
return false;
5705+
}
5706+
56695707
if (!lookat(TokenKind::T_IDENTIFIER)) return false;
56705708

56715709
UnqualifiedIdAST* unqualifiedId = nullptr;

src/parser/cxx/parser.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ class Parser final {
108108
static auto prec(TokenKind tk) -> Prec;
109109

110110
[[nodiscard]] auto shouldStopParsing() const -> bool {
111+
if (stopParsing_) return true;
111112
if (config_.stopParsingPredicate) return config_.stopParsingPredicate();
112113
return false;
113114
}
@@ -871,7 +872,7 @@ class Parser final {
871872
std::uint32_t cursor_ = 0;
872873
int templateParameterDepth_ = -1;
873874
int templateParameterCount_ = 0;
874-
875+
bool stopParsing_ = false;
875876
std::vector<FunctionDefinitionAST*> pendingFunctionDefinitions_;
876877

877878
template <typename T>

0 commit comments

Comments
 (0)