forked from qt-creator/qt-creator
Clang: Correct member access operator if possible
1 struct Foo { int member; };
2 void f(Foo *foo)
3 {
4 foo.<REQUEST COMPLETION> // correct '.' to '->' and provide results
5 }
The preferred approach would be to check if "foo" in line 4 is of
pointer type, but there is no suitable cursor (only CompoundStmt) at
that position since the code is usually not yet parsed and thus invalid.
Thus, just run the completion as is. If there are not any results for a
dot completion, re-run the completion with "." exchanged by "->". This
approach is inherently slower than the preferred approach implemented in
the built-in code model.
The following rare cases are not handled:
1) Requesting completion after white space:
Works: foo.<COMPLETE HERE>
Fails: foo. <COMPLETE HERE>
2) Opening a file and requesting completion (ctrl+space) without prior
editing. No editing before triggering completion means that no
unsaved file is generated on the backend side, which is a
requirement for the correction.
Task-number: QTCREATORBUG-11581
Change-Id: I6bc8e8594778774ab342755fdb01a8a3e5c52ba0
Reviewed-by: Marco Bubke <marco.bubke@theqtcompany.com>
This commit is contained in:
@@ -42,7 +42,10 @@ ClangAssistProposal::ClangAssistProposal(int cursorPos, TextEditor::GenericPropo
|
||||
|
||||
bool ClangAssistProposal::isCorrective() const
|
||||
{
|
||||
return false;
|
||||
auto clangAssistProposalModel = static_cast<ClangAssistProposalModel*>(model());
|
||||
|
||||
return clangAssistProposalModel->neededCorrection()
|
||||
== ClangBackEnd::CompletionCorrection::DotToArrowCorrection;
|
||||
}
|
||||
|
||||
void ClangAssistProposal::makeCorrection(TextEditor::TextEditorWidget *editorWidget)
|
||||
|
||||
@@ -39,6 +39,12 @@
|
||||
namespace ClangCodeModel {
|
||||
namespace Internal {
|
||||
|
||||
ClangAssistProposalModel::ClangAssistProposalModel(
|
||||
ClangBackEnd::CompletionCorrection neededCorrection)
|
||||
: m_neededCorrection(neededCorrection)
|
||||
{
|
||||
}
|
||||
|
||||
bool ClangAssistProposalModel::isSortable(const QString &/*prefix*/) const
|
||||
{
|
||||
return true;
|
||||
@@ -57,6 +63,11 @@ void ClangAssistProposalModel::sort(const QString &/*prefix*/)
|
||||
std::sort(m_currentItems.begin(), m_currentItems.end(), currentItemsCompare);
|
||||
}
|
||||
|
||||
ClangBackEnd::CompletionCorrection ClangAssistProposalModel::neededCorrection() const
|
||||
{
|
||||
return m_neededCorrection;
|
||||
}
|
||||
|
||||
} // namespace Internal
|
||||
} // namespace ClangCodeModel
|
||||
|
||||
|
||||
@@ -35,14 +35,23 @@
|
||||
|
||||
#include <texteditor/codeassist/genericproposalmodel.h>
|
||||
|
||||
#include <clangbackendipc/clangbackendipc_global.h>
|
||||
|
||||
namespace ClangCodeModel {
|
||||
namespace Internal {
|
||||
|
||||
class ClangAssistProposalModel : public TextEditor::GenericProposalModel
|
||||
{
|
||||
public:
|
||||
ClangAssistProposalModel(ClangBackEnd::CompletionCorrection neededCorrection);
|
||||
|
||||
bool isSortable(const QString &prefix) const override;
|
||||
void sort(const QString &prefix) override;
|
||||
|
||||
ClangBackEnd::CompletionCorrection neededCorrection() const;
|
||||
|
||||
private:
|
||||
ClangBackEnd::CompletionCorrection m_neededCorrection;
|
||||
};
|
||||
|
||||
} // namespace Internal
|
||||
|
||||
@@ -163,7 +163,9 @@ void IpcReceiver::codeCompleted(const CodeCompletedMessage &message)
|
||||
const quint64 ticket = message.ticketNumber();
|
||||
QScopedPointer<ClangCompletionAssistProcessor> processor(m_assistProcessorsTable.take(ticket));
|
||||
if (processor) {
|
||||
const bool finished = processor->handleAvailableAsyncCompletions(message.codeCompletions());
|
||||
const bool finished = processor->handleAvailableAsyncCompletions(
|
||||
message.codeCompletions(),
|
||||
message.neededCorrection());
|
||||
if (!finished)
|
||||
processor.take();
|
||||
}
|
||||
|
||||
@@ -240,13 +240,14 @@ IAssistProposal *ClangCompletionAssistProcessor::perform(const AssistInterface *
|
||||
}
|
||||
|
||||
bool ClangCompletionAssistProcessor::handleAvailableAsyncCompletions(
|
||||
const CodeCompletions &completions)
|
||||
const CodeCompletions &completions,
|
||||
CompletionCorrection neededCorrection)
|
||||
{
|
||||
bool handled = true;
|
||||
|
||||
switch (m_sentRequestType) {
|
||||
case CompletionRequestType::NormalCompletion:
|
||||
handleAvailableCompletions(completions);
|
||||
handleAvailableCompletions(completions, neededCorrection);
|
||||
break;
|
||||
case CompletionRequestType::FunctionHintCompletion:
|
||||
handled = handleAvailableFunctionHintCompletions(completions);
|
||||
@@ -775,14 +776,17 @@ bool ClangCompletionAssistProcessor::sendCompletionRequest(int position,
|
||||
return false;
|
||||
}
|
||||
|
||||
TextEditor::IAssistProposal *ClangCompletionAssistProcessor::createProposal() const
|
||||
TextEditor::IAssistProposal *ClangCompletionAssistProcessor::createProposal(
|
||||
CompletionCorrection neededCorrection) const
|
||||
{
|
||||
ClangAssistProposalModel *model = new ClangAssistProposalModel;
|
||||
ClangAssistProposalModel *model = new ClangAssistProposalModel(neededCorrection);
|
||||
model->loadContent(m_completions);
|
||||
return new ClangAssistProposal(m_positionForProposal, model);
|
||||
}
|
||||
|
||||
void ClangCompletionAssistProcessor::handleAvailableCompletions(const CodeCompletions &completions)
|
||||
void ClangCompletionAssistProcessor::handleAvailableCompletions(
|
||||
const CodeCompletions &completions,
|
||||
CompletionCorrection neededCorrection)
|
||||
{
|
||||
QTC_CHECK(m_completions.isEmpty());
|
||||
|
||||
@@ -790,7 +794,7 @@ void ClangCompletionAssistProcessor::handleAvailableCompletions(const CodeComple
|
||||
if (m_addSnippets)
|
||||
addSnippets();
|
||||
|
||||
setAsyncProposalAvailable(createProposal());
|
||||
setAsyncProposalAvailable(createProposal(neededCorrection));
|
||||
}
|
||||
|
||||
bool ClangCompletionAssistProcessor::handleAvailableFunctionHintCompletions(
|
||||
|
||||
@@ -43,6 +43,7 @@ namespace ClangCodeModel {
|
||||
namespace Internal {
|
||||
|
||||
using ClangBackEnd::CodeCompletions;
|
||||
using ClangBackEnd::CompletionCorrection;
|
||||
|
||||
class ClangCompletionAssistProcessor : public CppTools::CppCompletionAssistProcessor
|
||||
{
|
||||
@@ -54,7 +55,8 @@ public:
|
||||
|
||||
TextEditor::IAssistProposal *perform(const TextEditor::AssistInterface *interface) override;
|
||||
|
||||
bool handleAvailableAsyncCompletions(const CodeCompletions &completions);
|
||||
bool handleAvailableAsyncCompletions(const CodeCompletions &completions,
|
||||
CompletionCorrection neededCorrection);
|
||||
|
||||
const TextEditor::TextEditorWidget *textEditorWidget() const;
|
||||
|
||||
@@ -64,7 +66,8 @@ private:
|
||||
int findStartOfName(int pos = -1) const;
|
||||
bool accepts() const;
|
||||
|
||||
TextEditor::IAssistProposal *createProposal() const;
|
||||
TextEditor::IAssistProposal *createProposal(
|
||||
CompletionCorrection neededCorrection = CompletionCorrection::NoCorrection) const;
|
||||
|
||||
bool completeInclude(const QTextCursor &cursor);
|
||||
bool completeInclude(int position);
|
||||
@@ -85,7 +88,8 @@ private:
|
||||
void sendFileContent(const QByteArray &customFileContent);
|
||||
bool sendCompletionRequest(int position, const QByteArray &customFileContent);
|
||||
|
||||
void handleAvailableCompletions(const CodeCompletions &completions);
|
||||
void handleAvailableCompletions(const CodeCompletions &completions,
|
||||
CompletionCorrection neededCorrection);
|
||||
bool handleAvailableFunctionHintCompletions(const CodeCompletions &completions);
|
||||
|
||||
private:
|
||||
|
||||
@@ -689,6 +689,7 @@ class ProjectLessCompletionTest
|
||||
{
|
||||
public:
|
||||
ProjectLessCompletionTest(const QByteArray &testFileName,
|
||||
const QString &textToInsert = QString(),
|
||||
const QStringList &includePaths = QStringList())
|
||||
{
|
||||
CppTools::Tests::TestCase garbageCollectionGlobalSnapshot;
|
||||
@@ -697,8 +698,11 @@ public:
|
||||
const TestDocument testDocument(testFileName, globalTemporaryDir());
|
||||
QVERIFY(testDocument.isCreatedAndHasValidCursorPosition());
|
||||
OpenEditorAtCursorPosition openEditor(testDocument);
|
||||
|
||||
QVERIFY(openEditor.succeeded());
|
||||
|
||||
if (!textToInsert.isEmpty())
|
||||
openEditor.editor()->insert(textToInsert);
|
||||
|
||||
proposal = completionResults(openEditor.editor(), includePaths);
|
||||
}
|
||||
|
||||
@@ -880,7 +884,9 @@ void ClangCodeCompletionTest::testCompletePreprocessorKeywords()
|
||||
void ClangCodeCompletionTest::testCompleteIncludeDirective()
|
||||
{
|
||||
CppTools::Tests::TemporaryCopiedDir testDir(qrcPath("exampleIncludeDir"));
|
||||
ProjectLessCompletionTest t("includeDirectiveCompletion.cpp", QStringList(testDir.path()));
|
||||
ProjectLessCompletionTest t("includeDirectiveCompletion.cpp",
|
||||
QString(),
|
||||
QStringList(testDir.path()));
|
||||
|
||||
QVERIFY(hasItem(t.proposal, "file.h"));
|
||||
QVERIFY(hasItem(t.proposal, "otherFile.h"));
|
||||
@@ -931,6 +937,18 @@ void ClangCodeCompletionTest::testCompleteConstructorAndFallbackToGlobalCompleti
|
||||
QVERIFY(!hasSnippet(t.proposal, "class"));
|
||||
}
|
||||
|
||||
void ClangCodeCompletionTest::testCompleteWithDotToArrowCorrection()
|
||||
{
|
||||
// Inserting the dot for this test is important since it will send the editor
|
||||
// content to the backend and thus generate an unsaved file on the backend
|
||||
// side. The unsaved file enables us to do the dot to arrow correction.
|
||||
|
||||
ProjectLessCompletionTest t("dotToArrowCorrection.cpp",
|
||||
QStringLiteral("."));
|
||||
|
||||
QVERIFY(hasItem(t.proposal, "member"));
|
||||
}
|
||||
|
||||
void ClangCodeCompletionTest::testCompleteProjectDependingCode()
|
||||
{
|
||||
const TestDocument testDocument("completionWithProject.cpp");
|
||||
|
||||
@@ -53,6 +53,8 @@ private slots:
|
||||
void testCompleteFunctions();
|
||||
void testCompleteConstructorAndFallbackToGlobalCompletion();
|
||||
|
||||
void testCompleteWithDotToArrowCorrection();
|
||||
|
||||
void testCompleteProjectDependingCode();
|
||||
void testCompleteProjectDependingCodeAfterChangingProject();
|
||||
void testCompleteProjectDependingCodeInGeneratedUiFile();
|
||||
|
||||
@@ -21,5 +21,6 @@
|
||||
<file>objc_messages_2.mm</file>
|
||||
<file>objc_messages_3.mm</file>
|
||||
<file>preprocessorKeywordsCompletion.cpp</file>
|
||||
<file>dotToArrowCorrection.cpp</file>
|
||||
</qresource>
|
||||
</RCC>
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
struct Bar { int member; };
|
||||
void f(Bar *bar)
|
||||
{
|
||||
bar /* COMPLETE HERE */
|
||||
}
|
||||
Reference in New Issue
Block a user