Skip to content

Commit

Permalink
mike-lischke#132 Add default parameters to collectCandidates
Browse files Browse the repository at this point in the history
Signed-off-by: vityaman <[email protected]>
  • Loading branch information
vityaman authored and mike-lischke committed Jul 28, 2024
1 parent 5427f05 commit 71554ed
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 76 deletions.
2 changes: 1 addition & 1 deletion ports/cpp/source/antlr4-c3/CodeCompletionCore.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ class CodeCompletionCore
* @returns The collection of completion candidates. If cancelled or timed out, the returned collection will have its 'cancelled'
* value set to true and the collected candidates may be incomplete.
*/
CandidatesCollection collectCandidates(size_t caretTokenIndex, antlr4::ParserRuleContext * context, size_t timeoutMS, std::atomic<bool> * cancel);
CandidatesCollection collectCandidates(size_t caretTokenIndex, antlr4::ParserRuleContext * context = nullptr, size_t timeoutMS = 0, std::atomic<bool> * cancel = nullptr);



Expand Down
17 changes: 8 additions & 9 deletions ports/cpp/test/cpp14/Cpp14Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include <utility/Testing.hpp>

#include <filesystem>
#include <ranges>

namespace c3::test {

Expand Down Expand Up @@ -51,7 +50,7 @@ TEST(CPP14Parser, SimpleExample) {

{
// 1) At the input start.
auto candidates = Candidates(completion, 0);
auto candidates = completion.collectCandidates(0);

EXPECT_THAT(
Keys(candidates.tokens),
Expand Down Expand Up @@ -99,7 +98,7 @@ TEST(CPP14Parser, SimpleExample) {
// Note when counting token indexes: the C++14 grammar skips all
// whitespaces, hence there are no tokens for them.
completion.translateRulesTopDown = translateRulesTopDown;
auto candidates = Candidates(completion, 10);
auto candidates = completion.collectCandidates(10);

const std::vector idexpressionStack = {
CPP14Parser::RuleTranslationunit,
Expand Down Expand Up @@ -180,7 +179,7 @@ TEST(CPP14Parser, SimpleExample) {
// Note when counting token indexes: the C++14 grammar skips all
// whitespaces, hence there are no tokens for them.
completion.translateRulesTopDown = true;
auto candidates = Candidates(completion, 10);
auto candidates = completion.collectCandidates(10);

EXPECT_EQ(candidates.tokens.size(), 82);

Expand Down Expand Up @@ -241,15 +240,15 @@ TEST(CPP14Parser, SimpleCppExampleWithErrorsInInput) {

{
// At the opening parenthesis.
auto candidates = Candidates(completion, 11);
auto candidates = completion.collectCandidates(11);

EXPECT_THAT(Keys(candidates.tokens),
UnorderedElementsAre(CPP14Lexer::LeftParen));
}
{
// At the closing parenthesis -> again everything in an expression allowed
// (no control flow this time, though).
auto candidates = Candidates(completion, 12);
auto candidates = completion.collectCandidates(12);

EXPECT_EQ(candidates.tokens.size(), 65);

Expand All @@ -271,7 +270,7 @@ TEST(CPP14Parser, SimpleCppExampleWithErrorsInInput) {
}
{
// After the error position -> no suggestions.
auto candidates = Candidates(completion, 13);
auto candidates = completion.collectCandidates(13);

EXPECT_EQ(candidates.tokens.size(), 0);
EXPECT_EQ(candidates.rules.size(), 0);
Expand Down Expand Up @@ -348,7 +347,7 @@ TEST(CPP14Parser, RealCppFile) {
});

{
auto candidates = Candidates(completion, 3469);
auto candidates = completion.collectCandidates(3469);

EXPECT_THAT(Keys(candidates.rules),
UnorderedElementsAre(CPP14Parser::RuleClassname,
Expand All @@ -362,7 +361,7 @@ TEST(CPP14Parser, RealCppFile) {
// We should receive more specific rules when translating top down.

completion.translateRulesTopDown = true;
auto candidates = Candidates(completion, 3469);
auto candidates = completion.collectCandidates(3469);

EXPECT_THAT(candidates.rules[CPP14Parser::RuleClassname].ruleList,
ElementsAreArray(classnameStack));
Expand Down
55 changes: 16 additions & 39 deletions ports/cpp/test/expr/ExprTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,10 @@ TEST(SimpleExpressionParser, MostSimpleSetup) {
EXPECT_EQ(pipeline.listener.GetErrorCount(), 0);

c3::CodeCompletionCore completion(&pipeline.parser);
const auto collectCandidatesAt = [&](std::size_t tokenIndex) {
return completion.collectCandidates(tokenIndex,
/*context=*/nullptr, /*size_t=*/0,
/*cancel=*/nullptr);
};

{
// 1) At the input start.
auto candidates = collectCandidatesAt(0);
auto candidates = completion.collectCandidates(0);
EXPECT_THAT(
Keys(candidates.tokens),
UnorderedElementsAre(ExprLexer::VAR, ExprLexer::LET, ExprLexer::ID));
Expand All @@ -40,31 +35,31 @@ TEST(SimpleExpressionParser, MostSimpleSetup) {
// 2) On the first whitespace. In real implementations you would do some
// additional checks where in the whitespace the caret is, as the outcome is
// different depending on that position.
auto candidates = collectCandidatesAt(1);
auto candidates = completion.collectCandidates(1);
EXPECT_THAT(Keys(candidates.tokens), UnorderedElementsAre(ExprLexer::ID));
}
{
// 3) On the variable name ('c').
auto candidates = collectCandidatesAt(2);
auto candidates = completion.collectCandidates(2);
EXPECT_THAT(Keys(candidates.tokens), UnorderedElementsAre(ExprLexer::ID));
}
{
// 4) On the equal sign (ignoring whitespace positions from now on).
auto candidates = collectCandidatesAt(4);
auto candidates = completion.collectCandidates(4);
EXPECT_THAT(Keys(candidates.tokens),
UnorderedElementsAre(ExprLexer::EQUAL));
}
{
// 5) On the variable reference 'a'. But since we have not configure the c3
// engine to return us var refs (or function refs for that matter) we only
// get an ID here.
auto candidates = collectCandidatesAt(6);
auto candidates = completion.collectCandidates(6);
EXPECT_THAT(Keys(candidates.tokens), UnorderedElementsAre(ExprLexer::ID));
}
{
// 6) On the '+' operator. Usually you would not show operators as
// candidates, but we have not set up the c3 engine yet to not return them.
auto candidates = collectCandidatesAt(8);
auto candidates = completion.collectCandidates(8);
EXPECT_THAT(Keys(candidates.tokens),
UnorderedElementsAre(ExprLexer::PLUS, ExprLexer::MINUS,
ExprLexer::MULTIPLY, ExprLexer::DIVIDE,
Expand All @@ -87,15 +82,9 @@ TEST(SimpleExpressionParser, TypicalSetup) {
ExprParser::RuleVariableRef,
};

const auto collectCandidatesAt = [&](std::size_t tokenIndex) {
return completion.collectCandidates(tokenIndex,
/*context=*/nullptr, /*size_t=*/0,
/*cancel=*/nullptr);
};

{
// 1) At the input start.
auto candidates = collectCandidatesAt(0);
auto candidates = completion.collectCandidates(0);
EXPECT_THAT(Keys(candidates.tokens),
UnorderedElementsAre(ExprLexer::VAR, ExprLexer::LET));

Expand All @@ -105,17 +94,17 @@ TEST(SimpleExpressionParser, TypicalSetup) {
}
{
// 2) On the variable name ('c').
auto candidates = collectCandidatesAt(2);
auto candidates = completion.collectCandidates(2);
EXPECT_EQ(candidates.tokens.size(), 0);
}
{
// 4) On the equal sign.
auto candidates = collectCandidatesAt(4);
auto candidates = completion.collectCandidates(4);
EXPECT_EQ(candidates.tokens.size(), 0);
}
{
// 5) On the variable reference 'a'.
auto candidates = collectCandidatesAt(6);
auto candidates = completion.collectCandidates(6);
EXPECT_EQ(candidates.tokens.size(), 0);
// Here we get 2 rule indexes, derived from 2 different IDs possible at this
// caret position. These are what we told the engine above to be preferred
Expand All @@ -129,7 +118,7 @@ TEST(SimpleExpressionParser, TypicalSetup) {
{
// 6) On the whitespace just after the variable reference 'a' (but it could
// still be a function reference!).
auto candidates = collectCandidatesAt(7);
auto candidates = completion.collectCandidates(7);
EXPECT_EQ(candidates.tokens.size(), 0);
EXPECT_THAT(Keys(candidates.rules),
UnorderedElementsAre(ExprParser::RuleFunctionRef));
Expand All @@ -145,15 +134,9 @@ TEST(SimpleExpressionParser, RecursivePreferredRule) {
c3::CodeCompletionCore completion(&pipeline.parser);
completion.preferredRules = {ExprParser::RuleSimpleExpression};

const auto collectCandidatesAt = [&](std::size_t tokenIndex) {
return completion.collectCandidates(tokenIndex,
/*context=*/nullptr, /*size_t=*/0,
/*cancel=*/nullptr);
};

{
// 1) On the variable reference 'a'.
auto candidates = collectCandidatesAt(6);
auto candidates = completion.collectCandidates(6);
EXPECT_THAT(Keys(candidates.rules),
UnorderedElementsAre(ExprParser::RuleSimpleExpression));
// The start token of the simpleExpression rule begins at token 'a'.
Expand All @@ -163,7 +146,7 @@ TEST(SimpleExpressionParser, RecursivePreferredRule) {
{
// 2) On the variable reference 'b'.
completion.translateRulesTopDown = false;
auto candidates = collectCandidatesAt(10);
auto candidates = completion.collectCandidates(10);
EXPECT_THAT(Keys(candidates.rules),
UnorderedElementsAre(ExprParser::RuleSimpleExpression));
// When translateRulesTopDown is false, startTokenIndex should match the
Expand All @@ -175,7 +158,7 @@ TEST(SimpleExpressionParser, RecursivePreferredRule) {
{
// 3) On the variable reference 'b' topDown preferred rules.
completion.translateRulesTopDown = true;
auto candidates = collectCandidatesAt(10);
auto candidates = completion.collectCandidates(10);
EXPECT_THAT(Keys(candidates.rules),
UnorderedElementsAre(ExprParser::RuleSimpleExpression));
// When translateRulesTopDown is true, startTokenIndex should match the
Expand All @@ -198,15 +181,9 @@ TEST(SimpleExpressionParser, CandidateRulesWithDifferentStartTokens) {
};
completion.translateRulesTopDown = true;

const auto collectCandidatesAt = [&](std::size_t tokenIndex) {
return completion.collectCandidates(tokenIndex,
/*context=*/nullptr, /*size_t=*/0,
/*cancel=*/nullptr);
};

{
// 1) On the token 'var'.
auto candidates = collectCandidatesAt(0);
auto candidates = completion.collectCandidates(0);
EXPECT_THAT(Keys(candidates.rules),
UnorderedElementsAre(ExprParser::RuleAssignment,
ExprParser::RuleVariableRef));
Expand All @@ -217,7 +194,7 @@ TEST(SimpleExpressionParser, CandidateRulesWithDifferentStartTokens) {
}
{
// 2) On the variable reference 'a'.
auto candidates = collectCandidatesAt(6);
auto candidates = completion.collectCandidates(6);
EXPECT_THAT(Keys(candidates.rules),
UnorderedElementsAre(ExprParser::RuleAssignment,
ExprParser::RuleVariableRef));
Expand Down
22 changes: 0 additions & 22 deletions ports/cpp/test/utility/Extension.hpp

This file was deleted.

1 change: 0 additions & 1 deletion ports/cpp/test/utility/Testing.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <gtest/gtest.h>

#include "AntlrPipeline.hpp"
#include "Extension.hpp"
#include "Collections.hpp"

namespace c3::test {
Expand Down
8 changes: 4 additions & 4 deletions ports/cpp/test/whitebox/WhiteboxTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ TEST(WhiteboxGrammarTests, CaretAtTransitionToRuleWithNonExhaustiveFollowSet) {
EXPECT_EQ(pipeline.listener.GetErrorCount(), 1);

c3::CodeCompletionCore completion(&pipeline.parser);
auto candidates = Candidates(completion, 1, ctx);
auto candidates = completion.collectCandidates(1, ctx);

EXPECT_THAT(Keys(candidates.tokens),
UnorderedElementsAre(WhiteboxLexer::IPSUM, WhiteboxLexer::DOLOR,
Expand All @@ -34,7 +34,7 @@ TEST(WhiteboxGrammarTests, CaretAtTransitionToRuleWithEmptyFollowSet) {
EXPECT_EQ(pipeline.listener.GetErrorCount(), 1);

c3::CodeCompletionCore completion(&pipeline.parser);
auto candidates = Candidates(completion, 1, ctx);
auto candidates = completion.collectCandidates(1, ctx);

EXPECT_THAT(Keys(candidates.tokens),
UnorderedElementsAre(WhiteboxLexer::IPSUM, WhiteboxLexer::DOLOR,
Expand Down Expand Up @@ -62,7 +62,7 @@ TEST(WhiteboxGrammarTests, CaretAtOneOfMultiplePossibleStates) {
}();

c3::CodeCompletionCore completion(&pipeline.parser);
auto candidates = Candidates(completion, 2, ctx);
auto candidates = completion.collectCandidates(2, ctx);

EXPECT_THAT(Keys(candidates.tokens),
UnorderedElementsAre(WhiteboxLexer::DOLOR));
Expand All @@ -78,7 +78,7 @@ TEST(WhiteboxGrammarTests,
auto *ctx = pipeline.parser.test8();

c3::CodeCompletionCore completion(&pipeline.parser);
auto candidates = Candidates(completion, 2, ctx);
auto candidates = completion.collectCandidates(2, ctx);

EXPECT_THAT(Keys(candidates.tokens),
UnorderedElementsAre(WhiteboxLexer::DOLOR));
Expand Down

0 comments on commit 71554ed

Please sign in to comment.