Commit aa6aaee5 authored by Marco Bubke's avatar Marco Bubke

Clang: Add brief comment support

Change-Id: I5af23b6694d3d0bf45ed0a30b4d91b350f0515df
Reviewed-by: default avatarNikolai Kosjar <nikolai.kosjar@theqtcompany.com>
parent b278dc87
...@@ -110,6 +110,16 @@ quint32 CodeCompletion::priority() const ...@@ -110,6 +110,16 @@ quint32 CodeCompletion::priority() const
return priority_; return priority_;
} }
void CodeCompletion::setBriefComment(const Utf8String &briefComment)
{
briefComment_ = briefComment;
}
const Utf8String &CodeCompletion::briefComment() const
{
return briefComment_;
}
quint32 &CodeCompletion::completionKindAsInt() quint32 &CodeCompletion::completionKindAsInt()
{ {
return reinterpret_cast<quint32&>(completionKind_); return reinterpret_cast<quint32&>(completionKind_);
...@@ -123,6 +133,7 @@ quint32 &CodeCompletion::availabilityAsInt() ...@@ -123,6 +133,7 @@ quint32 &CodeCompletion::availabilityAsInt()
QDataStream &operator<<(QDataStream &out, const CodeCompletion &message) QDataStream &operator<<(QDataStream &out, const CodeCompletion &message)
{ {
out << message.text_; out << message.text_;
out << message.briefComment_;
out << message.chunks_; out << message.chunks_;
out << message.priority_; out << message.priority_;
out << message.completionKind_; out << message.completionKind_;
...@@ -135,6 +146,7 @@ QDataStream &operator<<(QDataStream &out, const CodeCompletion &message) ...@@ -135,6 +146,7 @@ QDataStream &operator<<(QDataStream &out, const CodeCompletion &message)
QDataStream &operator>>(QDataStream &in, CodeCompletion &message) QDataStream &operator>>(QDataStream &in, CodeCompletion &message)
{ {
in >> message.text_; in >> message.text_;
in >> message.briefComment_;
in >> message.chunks_; in >> message.chunks_;
in >> message.priority_; in >> message.priority_;
in >> message.completionKindAsInt(); in >> message.completionKindAsInt();
......
...@@ -107,12 +107,16 @@ public: ...@@ -107,12 +107,16 @@ public:
void setPriority(quint32 priority); void setPriority(quint32 priority);
quint32 priority() const; quint32 priority() const;
void setBriefComment(const Utf8String &briefComment);
const Utf8String &briefComment() const;
private: private:
quint32 &completionKindAsInt(); quint32 &completionKindAsInt();
quint32 &availabilityAsInt(); quint32 &availabilityAsInt();
private: private:
Utf8String text_; Utf8String text_;
Utf8String briefComment_;
CodeCompletionChunks chunks_; CodeCompletionChunks chunks_;
quint32 priority_ = 0; quint32 priority_ = 0;
Kind completionKind_ = Other; Kind completionKind_ = Other;
......
...@@ -100,7 +100,12 @@ QList<AssistProposalItem *> toAssistProposalItems(const CodeCompletions &complet ...@@ -100,7 +100,12 @@ QList<AssistProposalItem *> toAssistProposalItems(const CodeCompletions &complet
items.insert(name, item); items.insert(name, item);
item->setText(name); item->setText(name);
item->setOrder(ccr.priority()); item->setOrder(ccr.priority());
item->setDetail(CompletionChunksToTextConverter::convertToToolTip(ccr.chunks())); QString detail = CompletionChunksToTextConverter::convertToToolTip(ccr.chunks());
if (!ccr.briefComment().isEmpty())
detail += QStringLiteral("\n\n") + ccr.briefComment().toString();
item->setDetail(detail);
item->setCodeCompletion(ccr); item->setCodeCompletion(ccr);
} }
......
...@@ -63,6 +63,7 @@ bool CodeCompletionsExtractor::next() ...@@ -63,6 +63,7 @@ bool CodeCompletionsExtractor::next()
extractPriority(); extractPriority();
extractAvailability(); extractAvailability();
extractHasParameters(); extractHasParameters();
extractBriefComment();
extractCompletionChunks(); extractCompletionChunks();
adaptPriority(); adaptPriority();
...@@ -249,6 +250,13 @@ void CodeCompletionsExtractor::extractHasParameters() ...@@ -249,6 +250,13 @@ void CodeCompletionsExtractor::extractHasParameters()
} }
} }
void CodeCompletionsExtractor::extractBriefComment()
{
ClangString briefComment = clang_getCompletionBriefComment(currentCxCodeCompleteResult.CompletionString);
currentCodeCompletion_.setBriefComment(briefComment);
}
void CodeCompletionsExtractor::extractCompletionChunks() void CodeCompletionsExtractor::extractCompletionChunks()
{ {
currentCodeCompletion_.setChunks(CodeCompletionChunkConverter::extract(currentCxCodeCompleteResult.CompletionString)); currentCodeCompletion_.setChunks(CodeCompletionChunkConverter::extract(currentCxCodeCompleteResult.CompletionString));
......
...@@ -65,6 +65,7 @@ private: ...@@ -65,6 +65,7 @@ private:
void extractPriority(); void extractPriority();
void extractAvailability(); void extractAvailability();
void extractHasParameters(); void extractHasParameters();
void extractBriefComment();
void extractCompletionChunks(); void extractCompletionChunks();
void adaptPriority(); void adaptPriority();
......
...@@ -230,7 +230,8 @@ void TranslationUnit::reparseTranslationUnit() const ...@@ -230,7 +230,8 @@ void TranslationUnit::reparseTranslationUnit() const
int TranslationUnit::defaultOptions() int TranslationUnit::defaultOptions()
{ {
return CXTranslationUnit_CacheCompletionResults return CXTranslationUnit_CacheCompletionResults
| CXTranslationUnit_PrecompiledPreamble; | CXTranslationUnit_PrecompiledPreamble
| CXTranslationUnit_IncludeBriefCommentsInCodeCompletion;
} }
uint TranslationUnit::unsavedFilesCount() const uint TranslationUnit::unsavedFilesCount() const
......
...@@ -101,6 +101,26 @@ MATCHER_P2(HasCompletionChunks, name, chunks, ...@@ -101,6 +101,26 @@ MATCHER_P2(HasCompletionChunks, name, chunks,
return false; return false;
} }
MATCHER_P2(HasBriefComment, name, briefComment,
std::string(negation ? "hasn't" : "has") + " completion of name " + PrintToString(name) +
" with the brief comment " + PrintToString(briefComment))
{
::CodeCompletionsExtractor &extractor = const_cast<::CodeCompletionsExtractor&>(arg);
while (extractor.next()) {
if (extractor.currentCodeCompletion().text() == name) {
if (extractor.currentCodeCompletion().briefComment() == briefComment) {
return true;
} else if (!extractor.peek(name)) {
*result_listener << "briefComment is " << PrintToString(arg.currentCodeCompletion().briefComment()) << " and not " << PrintToString(briefComment);
return false;
}
}
}
return false;
}
const Utf8String unsavedFileContent(const char *unsavedFilePath) const Utf8String unsavedFileContent(const char *unsavedFilePath)
{ {
QFile unsavedFileContentFile(QString::fromUtf8(unsavedFilePath)); QFile unsavedFileContentFile(QString::fromUtf8(unsavedFilePath));
...@@ -133,39 +153,18 @@ ClangCodeCompleteResults getResults(const TranslationUnit &translationUnit, uint ...@@ -133,39 +153,18 @@ ClangCodeCompleteResults getResults(const TranslationUnit &translationUnit, uint
class CodeCompletionsExtractor : public ::testing::Test class CodeCompletionsExtractor : public ::testing::Test
{ {
public:
static void TearDownTestCase();
protected: protected:
static ClangBackEnd::ProjectPart project; ClangBackEnd::ProjectPart project{Utf8StringLiteral("/path/to/projectfile")};
static ClangBackEnd::UnsavedFiles unsavedFiles; ClangBackEnd::UnsavedFiles unsavedFiles;
static TranslationUnit functionTranslationUnit; TranslationUnit functionTranslationUnit{Utf8StringLiteral(TESTDATA_DIR"/complete_extractor_function.cpp"), unsavedFiles, project};
static TranslationUnit variableTranslationUnit; TranslationUnit variableTranslationUnit{Utf8StringLiteral(TESTDATA_DIR"/complete_extractor_variable.cpp"), unsavedFiles, project};
static TranslationUnit classTranslationUnit ; TranslationUnit classTranslationUnit{Utf8StringLiteral(TESTDATA_DIR"/complete_extractor_class.cpp"), unsavedFiles, project};
static TranslationUnit namespaceTranslationUnit; TranslationUnit namespaceTranslationUnit{Utf8StringLiteral(TESTDATA_DIR"/complete_extractor_namespace.cpp"), unsavedFiles, project};
static TranslationUnit enumerationTranslationUnit; TranslationUnit enumerationTranslationUnit{Utf8StringLiteral(TESTDATA_DIR"/complete_extractor_enumeration.cpp"), unsavedFiles, project};
static TranslationUnit constructorTranslationUnit; TranslationUnit constructorTranslationUnit{Utf8StringLiteral(TESTDATA_DIR"/complete_extractor_constructor.cpp"), unsavedFiles, project};
TranslationUnit briefCommentTranslationUnit{Utf8StringLiteral(TESTDATA_DIR"/complete_extractor_brief_comment.cpp"), unsavedFiles, project};
}; };
ClangBackEnd::ProjectPart CodeCompletionsExtractor::project(Utf8StringLiteral("/path/to/projectfile"));
ClangBackEnd::UnsavedFiles CodeCompletionsExtractor::unsavedFiles;
TranslationUnit CodeCompletionsExtractor::functionTranslationUnit(Utf8StringLiteral(TESTDATA_DIR"/complete_extractor_function.cpp"), unsavedFiles, project);
TranslationUnit CodeCompletionsExtractor::variableTranslationUnit(Utf8StringLiteral(TESTDATA_DIR"/complete_extractor_variable.cpp"), unsavedFiles, project);
TranslationUnit CodeCompletionsExtractor::classTranslationUnit(Utf8StringLiteral(TESTDATA_DIR"/complete_extractor_class.cpp"), unsavedFiles, project);
TranslationUnit CodeCompletionsExtractor::namespaceTranslationUnit(Utf8StringLiteral(TESTDATA_DIR"/complete_extractor_namespace.cpp"), unsavedFiles, project);
TranslationUnit CodeCompletionsExtractor::enumerationTranslationUnit(Utf8StringLiteral(TESTDATA_DIR"/complete_extractor_enumeration.cpp"), unsavedFiles, project);
TranslationUnit CodeCompletionsExtractor::constructorTranslationUnit(Utf8StringLiteral(TESTDATA_DIR"/complete_extractor_constructor.cpp"), unsavedFiles, project);
void CodeCompletionsExtractor::TearDownTestCase()
{
functionTranslationUnit.reset();
variableTranslationUnit.reset();
classTranslationUnit.reset();
namespaceTranslationUnit.reset();
enumerationTranslationUnit.reset();
constructorTranslationUnit.reset();
}
TEST_F(CodeCompletionsExtractor, Function) TEST_F(CodeCompletionsExtractor, Function)
{ {
ClangCodeCompleteResults completeResults(getResults(functionTranslationUnit, 20)); ClangCodeCompleteResults completeResults(getResults(functionTranslationUnit, 20));
...@@ -657,5 +656,14 @@ TEST_F(CodeCompletionsExtractor, CompletionChunksClass) ...@@ -657,5 +656,14 @@ TEST_F(CodeCompletionsExtractor, CompletionChunksClass)
CodeCompletionChunks({{CodeCompletionChunk::TypedText, Utf8StringLiteral("Class")}}))); CodeCompletionChunks({{CodeCompletionChunk::TypedText, Utf8StringLiteral("Class")}})));
} }
TEST_F(CodeCompletionsExtractor, BriefComment)
{
ClangCodeCompleteResults completeResults(getResults(briefCommentTranslationUnit, 10));
::CodeCompletionsExtractor extractor(completeResults.data());
ASSERT_THAT(extractor, HasBriefComment(Utf8StringLiteral("BriefComment"), Utf8StringLiteral("A brief comment")));
}
} }
/**
* A brief comment
*/
void BriefComment()
{
}
void f() {
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment