-
Notifications
You must be signed in to change notification settings - Fork 66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#132 Port remaining grammar tests to C++ #134
#132 Port remaining grammar tests to C++ #134
Conversation
Signed-off-by: vityaman <[email protected]>
Signed-off-by: vityaman <[email protected]>
Signed-off-by: vityaman <[email protected]>
Signed-off-by: vityaman <[email protected]>
Signed-off-by: vityaman <[email protected]>
Signed-off-by: vityaman <[email protected]>
Signed-off-by: vityaman <[email protected]>
ports/cpp/test/utility/Extension.hpp
Outdated
inline auto Candidates(CodeCompletionCore &completion, | ||
std::size_t caretTokenIndex) { | ||
return completion.collectCandidates(caretTokenIndex, | ||
/*context=*/nullptr, /*size_t=*/0, | ||
/*cancel=*/nullptr); | ||
} | ||
|
||
inline auto Candidates(CodeCompletionCore &completion, | ||
std::size_t caretTokenIndex, | ||
antlr4::ParserRuleContext *context) { | ||
return completion.collectCandidates(caretTokenIndex, | ||
/*context=*/context, /*size_t=*/0, | ||
/*cancel=*/nullptr); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Current collectCandidates
is overloaded with parameters. I have thoughts, that it would be better to have something like this (just thoughts):
class CodeCompletionCore final {
struct Params {
antlr4::Parser* parser;
std::chrono::milliseconds timeout = 0ms;
std::atomic<bool>* is_cancelled = nullptr;
}
explicit CodeCompletionCore(Params params);
CandidatesCollection collectCandidates(std::size_t caretTokenIndex);
CandidatesCollection collectCandidates(
std::size_t caretTokenIndex, antlr4::ParserRuleContext* context);
}
auto Usage(antlr4::Parser* parser) {
CodeCompletionCode completion({
.parser = parser,
.timeout = 100ms,
});
return completion;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure what your point here is. You don't like default parameters?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem with current signature
CandidatesCollection collectCandidates(
size_t caretTokenIndex,
antlr4::ParserRuleContext * context = nullptr,
size_t timeoutMS = 0,
std::atomic<bool> * cancel = nullptr
);
It is that, it seems, that timeoutMS
and cancel
will not change from call to call, and their presence generates many combinations of arguments sets.
collectCandidates(0); // OK, readable
collectCandidates(0, ctx); // OK, readable
collectCandidates(0, nullptr, /* timeoutMS = */ 100); // Forces me to put nullptr
collectCandidates(0, nullptr, /* timeoutMS = */ 0, &cancel); // Forces me to add even zero timeout
collectCandidates(0, nullptr, 0, &cancel); // What?
It makes reading caller code harder. Also, it forces putting this comments like /* timeoutMS = */
because there are no named arguments in C++
.
In addition, in examples I showed hard-coded values, but in real code there will be variable names, which will make this line of code longer.
But in constructor we can provide user with named arguments with such Params
struct.
I think that a default argument is okay if there are no more than one of such argument (talking about languages without named arguments).
Also, I agree that overloading the method is redundant here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added default parameters and removed that functions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An alternative would be a simple struct as parameter (object/interface in TypeScript), which allows to specify parameters in any order, have names for each and leave out any that are marked as being optional. I use this often, as it comes as close to named parameters (e.g. like in Swift) as you can get.
// These are in TypeScript version, but not in C++ port | ||
// CPP14Parser::RuleFunctionbody, | ||
// CPP14Parser::RuleCompoundstatement, | ||
// CPP14Parser::RuleStatementseq, | ||
// CPP14Parser::RuleStatement, | ||
// CPP14Parser::RuleDeclarationstatement, | ||
// CPP14Parser::RuleBlockdeclaration, | ||
// CPP14Parser::RuleSimpledeclaration, | ||
// CPP14Parser::RuleInitdeclaratorlist, | ||
// CPP14Parser::RuleInitdeclarator, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will create an issue to analyze these results.
ports/cpp/test/utility/Extension.hpp
Outdated
inline auto Candidates(CodeCompletionCore &completion, | ||
std::size_t caretTokenIndex) { | ||
return completion.collectCandidates(caretTokenIndex, | ||
/*context=*/nullptr, /*size_t=*/0, | ||
/*cancel=*/nullptr); | ||
} | ||
|
||
inline auto Candidates(CodeCompletionCore &completion, | ||
std::size_t caretTokenIndex, | ||
antlr4::ParserRuleContext *context) { | ||
return completion.collectCandidates(caretTokenIndex, | ||
/*context=*/context, /*size_t=*/0, | ||
/*cancel=*/nullptr); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure what your point here is. You don't like default parameters?
Signed-off-by: vityaman <[email protected]>
Thanks! |
Whitebox
andCPP14
grammar tests.CPP14Test.cpp
.C++
from23
to20
.