#include "FuzzyMatch.h"
#include "Headers.h"
#include "Logger.h"
+#include "Protocol.h"
#include "Quality.h"
#include "SourceCode.h"
#include "TUScheduler.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringRef.h"
+#include "llvm/Support/Compiler.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/Format.h"
llvm_unreachable("Unhandled CodeCompletionResult::ResultKind.");
}
-/// Get the optional chunk as a string. This function is possibly recursive.
-///
-/// The parameter info for each parameter is appended to the Parameters.
-std::string getOptionalParameters(const CodeCompletionString &CCS,
- std::vector<ParameterInformation> &Parameters,
- SignatureQualitySignals &Signal) {
- std::string Result;
- for (const auto &Chunk : CCS) {
- switch (Chunk.Kind) {
- case CodeCompletionString::CK_Optional:
- assert(Chunk.Optional &&
- "Expected the optional code completion string to be non-null.");
- Result += getOptionalParameters(*Chunk.Optional, Parameters, Signal);
- break;
- case CodeCompletionString::CK_VerticalSpace:
- break;
- case CodeCompletionString::CK_Placeholder:
- // A string that acts as a placeholder for, e.g., a function call
- // argument.
- // Intentional fallthrough here.
- case CodeCompletionString::CK_CurrentParameter: {
- // A piece of text that describes the parameter that corresponds to
- // the code-completion location within a function call, message send,
- // macro invocation, etc.
- Result += Chunk.Text;
- ParameterInformation Info;
- Info.label = Chunk.Text;
- Parameters.push_back(std::move(Info));
- Signal.ContainsActiveParameter = true;
- Signal.NumberOfOptionalParameters++;
- break;
- }
- default:
- Result += Chunk.Text;
- break;
- }
- }
- return Result;
-}
-
// Identifier code completion result.
struct RawIdentifier {
llvm::StringRef Name;
public:
SignatureHelpCollector(const clang::CodeCompleteOptions &CodeCompleteOpts,
const SymbolIndex *Index, SignatureHelp &SigHelp)
- : CodeCompleteConsumer(CodeCompleteOpts),
- SigHelp(SigHelp),
+ : CodeCompleteConsumer(CodeCompleteOpts), SigHelp(SigHelp),
Allocator(std::make_shared<clang::GlobalCodeCompletionAllocator>()),
CCTUInfo(Allocator), Index(Index) {}
CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo; }
private:
+ void processParameterChunk(llvm::StringRef ChunkText,
+ SignatureInformation &Signature,
+ SignatureQualitySignals Signal) const {
+ // (!) this is O(n), should still be fast compared to building ASTs.
+ unsigned ParamStartOffset = lspLength(Signature.label);
+ unsigned ParamEndOffset = ParamStartOffset + lspLength(ChunkText);
+ // A piece of text that describes the parameter that corresponds to
+ // the code-completion location within a function call, message send,
+ // macro invocation, etc.
+ Signature.label += ChunkText;
+ ParameterInformation Info;
+ Info.labelOffsets.emplace(ParamStartOffset, ParamEndOffset);
+ // FIXME: only set 'labelOffsets' when all clients migrate out of it.
+ Info.labelString = ChunkText;
+
+ Signature.parameters.push_back(std::move(Info));
+ // FIXME: this should only be set on CK_CurrentParameter.
+ Signal.ContainsActiveParameter = true;
+ }
+
+ void processOptionalChunk(const CodeCompletionString &CCS,
+ SignatureInformation &Signature,
+ SignatureQualitySignals &Signal) const {
+ for (const auto &Chunk : CCS) {
+ switch (Chunk.Kind) {
+ case CodeCompletionString::CK_Optional:
+ assert(Chunk.Optional &&
+ "Expected the optional code completion string to be non-null.");
+ processOptionalChunk(*Chunk.Optional, Signature, Signal);
+ break;
+ case CodeCompletionString::CK_VerticalSpace:
+ break;
+ case CodeCompletionString::CK_CurrentParameter:
+ case CodeCompletionString::CK_Placeholder:
+ processParameterChunk(Chunk.Text, Signature, Signal);
+ Signal.NumberOfOptionalParameters++;
+ break;
+ default:
+ Signature.label += Chunk.Text;
+ break;
+ }
+ }
+ }
+
// FIXME(ioeric): consider moving CodeCompletionString logic here to
// CompletionString.h.
ScoredSignature processOverloadCandidate(const OverloadCandidate &Candidate,
assert(!ReturnType && "Unexpected CK_ResultType");
ReturnType = Chunk.Text;
break;
+ case CodeCompletionString::CK_CurrentParameter:
case CodeCompletionString::CK_Placeholder:
- // A string that acts as a placeholder for, e.g., a function call
- // argument.
- // Intentional fallthrough here.
- case CodeCompletionString::CK_CurrentParameter: {
- // A piece of text that describes the parameter that corresponds to
- // the code-completion location within a function call, message send,
- // macro invocation, etc.
- Signature.label += Chunk.Text;
- ParameterInformation Info;
- Info.label = Chunk.Text;
- Signature.parameters.push_back(std::move(Info));
+ processParameterChunk(Chunk.Text, Signature, Signal);
Signal.NumberOfParameters++;
- Signal.ContainsActiveParameter = true;
break;
- }
case CodeCompletionString::CK_Optional: {
// The rest of the parameters are defaulted/optional.
assert(Chunk.Optional &&
"Expected the optional code completion string to be non-null.");
- Signature.label += getOptionalParameters(*Chunk.Optional,
- Signature.parameters, Signal);
+ processOptionalChunk(*Chunk.Optional, Signature, Signal);
break;
}
case CodeCompletionString::CK_VerticalSpace:
PP.getIdentifierTable().getExternalIdentifierLookup();
if (!PreambleIdentifiers || !PreambleMacros)
return;
- for (const auto& MacroName : Preamble.MainFileMacros)
+ for (const auto &MacroName : Preamble.MainFileMacros)
if (auto *II = PreambleIdentifiers->get(MacroName))
if (II->isOutOfDate())
PreambleMacros->updateOutOfDateIdentifier(*II);
int NSema = 0, NIndex = 0, NSemaAndIndex = 0, NIdent = 0;
bool Incomplete = false; // Would more be available with a higher limit?
CompletionPrefix HeuristicPrefix;
- llvm::Optional<FuzzyMatcher> Filter; // Initialized once Sema runs.
+ llvm::Optional<FuzzyMatcher> Filter; // Initialized once Sema runs.
Range ReplacedRange;
std::vector<std::string> QueryScopes; // Initialized once Sema runs.
// Initialized once QueryScopes is initialized, if there are scopes.
return Result;
}
-CompletionPrefix
-guessCompletionPrefix(llvm::StringRef Content, unsigned Offset) {
+CompletionPrefix guessCompletionPrefix(llvm::StringRef Content,
+ unsigned Offset) {
assert(Offset <= Content.size());
StringRef Rest = Content.take_front(Offset);
CompletionPrefix Result;
return signatures(Test.code(), Test.point(), std::move(IndexSymbols));
}
+struct ExpectedParameter {
+ std::string Text;
+ std::pair<unsigned, unsigned> Offsets;
+};
MATCHER_P(ParamsAre, P, "") {
if (P.size() != arg.parameters.size())
return false;
- for (unsigned I = 0; I < P.size(); ++I)
- if (P[I] != arg.parameters[I].label)
+ for (unsigned I = 0; I < P.size(); ++I) {
+ if (P[I].Text != arg.parameters[I].labelString ||
+ P[I].Offsets != arg.parameters[I].labelOffsets)
return false;
+ }
return true;
}
MATCHER_P(SigDoc, Doc, "") { return arg.documentation == Doc; }
-Matcher<SignatureInformation> Sig(std::string Label,
- std::vector<std::string> Params) {
- return AllOf(SigHelpLabeled(Label), ParamsAre(Params));
+/// \p AnnotatedLabel is a signature label with ranges marking parameters, e.g.
+/// foo([[int p1]], [[double p2]]) -> void
+Matcher<SignatureInformation> Sig(llvm::StringRef AnnotatedLabel) {
+ llvm::Annotations A(AnnotatedLabel);
+ std::string Label = A.code();
+ std::vector<ExpectedParameter> Parameters;
+ for (auto Range : A.ranges()) {
+ Parameters.emplace_back();
+
+ ExpectedParameter &P = Parameters.back();
+ P.Text = Label.substr(Range.Begin, Range.End - Range.Begin);
+ P.Offsets.first = lspLength(llvm::StringRef(Label).substr(0, Range.Begin));
+ P.Offsets.second = lspLength(llvm::StringRef(Label).substr(1, Range.End));
+ }
+ return AllOf(SigHelpLabeled(Label), ParamsAre(Parameters));
}
TEST(SignatureHelpTest, Overloads) {
int main() { foo(^); }
)cpp");
EXPECT_THAT(Results.signatures,
- UnorderedElementsAre(
- Sig("foo(float x, float y) -> void", {"float x", "float y"}),
- Sig("foo(float x, int y) -> void", {"float x", "int y"}),
- Sig("foo(int x, float y) -> void", {"int x", "float y"}),
- Sig("foo(int x, int y) -> void", {"int x", "int y"})));
+ UnorderedElementsAre(Sig("foo([[float x]], [[float y]]) -> void"),
+ Sig("foo([[float x]], [[int y]]) -> void"),
+ Sig("foo([[int x]], [[float y]]) -> void"),
+ Sig("foo([[int x]], [[int y]]) -> void")));
// We always prefer the first signature.
EXPECT_EQ(0, Results.activeSignature);
EXPECT_EQ(0, Results.activeParameter);
)cpp");
EXPECT_THAT(Results.signatures,
UnorderedElementsAre(
- Sig("bar(int x, int y = 0) -> void", {"int x", "int y = 0"}),
- Sig("bar(float x = 0, int y = 42) -> void",
- {"float x = 0", "int y = 42"})));
+ Sig("bar([[int x]], [[int y = 0]]) -> void"),
+ Sig("bar([[float x = 0]], [[int y = 42]]) -> void")));
EXPECT_EQ(0, Results.activeSignature);
EXPECT_EQ(0, Results.activeParameter);
}
int main() { baz(baz(1,2,3), ^); }
)cpp");
EXPECT_THAT(Results.signatures,
- ElementsAre(Sig("baz(int a, int b, int c) -> int",
- {"int a", "int b", "int c"})));
+ ElementsAre(Sig("baz([[int a]], [[int b]], [[int c]]) -> int")));
EXPECT_EQ(0, Results.activeSignature);
EXPECT_EQ(1, Results.activeParameter);
}
void foo(int x, int y = 0);
int main() { foo(^); }
)cpp");
- EXPECT_THAT(
- Results.signatures,
- ElementsAre(
- Sig("foo(int x) -> void", {"int x"}),
- Sig("foo(int x, int y = 0) -> void", {"int x", "int y = 0"}),
- Sig("foo(float x, int y) -> void", {"float x", "int y"}),
- Sig("foo(int x, float y) -> void", {"int x", "float y"}),
- Sig("foo(float x, float y) -> void", {"float x", "float y"})));
+ EXPECT_THAT(Results.signatures,
+ ElementsAre(Sig("foo([[int x]]) -> void"),
+ Sig("foo([[int x]], [[int y = 0]]) -> void"),
+ Sig("foo([[float x]], [[int y]]) -> void"),
+ Sig("foo([[int x]], [[float y]]) -> void"),
+ Sig("foo([[float x]], [[float y]]) -> void")));
// We always prefer the first signature.
EXPECT_EQ(0, Results.activeSignature);
EXPECT_EQ(0, Results.activeParameter);
)cpp";
EXPECT_THAT(signatures(Sig0).signatures,
- ElementsAre(Sig("foo(T, T, T) -> void", {"T", "T", "T"})));
+ ElementsAre(Sig("foo([[T]], [[T]], [[T]]) -> void")));
StringRef Sig1 = R"cpp(
template <class T>
})cpp";
EXPECT_THAT(signatures(Sig1).signatures,
- ElementsAre(Sig("foo(T, T, T) -> void", {"T", "T", "T"})));
+ ElementsAre(Sig("foo([[T]], [[T]], [[T]]) -> void")));
StringRef Sig2 = R"cpp(
template <class ...T>
)cpp";
EXPECT_THAT(signatures(Sig2).signatures,
- ElementsAre(Sig("foo(T...) -> void", {"T..."})));
+ ElementsAre(Sig("foo([[T...]]) -> void")));
// It is debatable whether we should substitute the outer template parameter
// ('T') in that case. Currently we don't substitute it in signature help, but
)cpp";
EXPECT_THAT(signatures(Sig3).signatures,
- ElementsAre(Sig("foo(T, U) -> void", {"T", "U"})));
+ ElementsAre(Sig("foo([[T]], [[U]]) -> void")));
}
TEST(SignatureHelpTest, IndexDocumentation) {
EXPECT_THAT(
signatures(Sig0, {Foo0}).signatures,
- ElementsAre(AllOf(Sig("foo() -> int", {}), SigDoc("Doc from the index")),
- AllOf(Sig("foo(double) -> int", {"double"}), SigDoc(""))));
+ ElementsAre(AllOf(Sig("foo() -> int"), SigDoc("Doc from the index")),
+ AllOf(Sig("foo([[double]]) -> int"), SigDoc(""))));
StringRef Sig1 = R"cpp(
int foo();
EXPECT_THAT(
signatures(Sig1, {Foo0, Foo1, Foo2}).signatures,
- ElementsAre(AllOf(Sig("foo() -> int", {}), SigDoc("Doc from the index")),
- AllOf(Sig("foo(int) -> int", {"int"}),
- SigDoc("Overriden doc from sema")),
- AllOf(Sig("foo(int, int) -> int", {"int", "int"}),
- SigDoc("Doc from sema"))));
+ ElementsAre(
+ AllOf(Sig("foo() -> int"), SigDoc("Doc from the index")),
+ AllOf(Sig("foo([[int]]) -> int"), SigDoc("Overriden doc from sema")),
+ AllOf(Sig("foo([[int]], [[int]]) -> int"), SigDoc("Doc from sema"))));
}
TEST(SignatureHelpTest, DynamicIndexDocumentation) {
EXPECT_THAT(
llvm::cantFail(runSignatureHelp(Server, File, FileContent.point()))
.signatures,
- ElementsAre(AllOf(Sig("foo() -> int", {}), SigDoc("Member doc"))));
+ ElementsAre(AllOf(Sig("foo() -> int"), SigDoc("Member doc"))));
}
TEST(CompletionTest, CompletionFunctionArgsDisabled) {
void foo(int x, int y);
int main() { foo(1+^); }
)cpp");
- EXPECT_THAT(
- Results.signatures,
- ElementsAre(Sig("foo(int x) -> void", {"int x"}),
- Sig("foo(int x, int y) -> void", {"int x", "int y"})));
+ EXPECT_THAT(Results.signatures,
+ ElementsAre(Sig("foo([[int x]]) -> void"),
+ Sig("foo([[int x]], [[int y]]) -> void")));
EXPECT_EQ(0, Results.activeParameter);
}
{
void foo(int x, int y);
int main() { foo(1^); }
)cpp");
- EXPECT_THAT(
- Results.signatures,
- ElementsAre(Sig("foo(int x) -> void", {"int x"}),
- Sig("foo(int x, int y) -> void", {"int x", "int y"})));
+ EXPECT_THAT(Results.signatures,
+ ElementsAre(Sig("foo([[int x]]) -> void"),
+ Sig("foo([[int x]], [[int y]]) -> void")));
EXPECT_EQ(0, Results.activeParameter);
}
{
void foo(int x, int y);
int main() { foo(1^0); }
)cpp");
- EXPECT_THAT(
- Results.signatures,
- ElementsAre(Sig("foo(int x) -> void", {"int x"}),
- Sig("foo(int x, int y) -> void", {"int x", "int y"})));
+ EXPECT_THAT(Results.signatures,
+ ElementsAre(Sig("foo([[int x]]) -> void"),
+ Sig("foo([[int x]], [[int y]]) -> void")));
EXPECT_EQ(0, Results.activeParameter);
}
{
int bar(int x, int y);
int main() { bar(foo(2, 3^)); }
)cpp");
- EXPECT_THAT(Results.signatures, ElementsAre(Sig("foo(int x, int y) -> void",
- {"int x", "int y"})));
+ EXPECT_THAT(Results.signatures,
+ ElementsAre(Sig("foo([[int x]], [[int y]]) -> void")));
EXPECT_EQ(1, Results.activeParameter);
}
}
};
)cpp");
EXPECT_THAT(Results.signatures,
- UnorderedElementsAre(Sig("A(int)", {"int"}),
- Sig("A(A &&)", {"A &&"}),
- Sig("A(const A &)", {"const A &"})));
+ UnorderedElementsAre(Sig("A([[int]])"), Sig("A([[A &&]])"),
+ Sig("A([[const A &]])")));
}
{
const auto Results = signatures(R"cpp(
};
)cpp");
EXPECT_THAT(Results.signatures,
- UnorderedElementsAre(Sig("A(int)", {"int"}),
- Sig("A(A &&)", {"A &&"}),
- Sig("A(const A &)", {"const A &"})));
+ UnorderedElementsAre(Sig("A([[int]])"), Sig("A([[A &&]])"),
+ Sig("A([[const A &]])")));
}
}