forked from qt-creator/qt-creator
CodeAssist: use shared pointer to pass around proposal models
Task-number: QTCREATORBUG-17752 Change-Id: Ia41f169b86ead209830e6f15764062389ced2b67 Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
@@ -30,14 +30,14 @@
|
||||
namespace ClangCodeModel {
|
||||
namespace Internal {
|
||||
|
||||
ClangAssistProposal::ClangAssistProposal(int cursorPos, TextEditor::GenericProposalModel *model)
|
||||
ClangAssistProposal::ClangAssistProposal(int cursorPos, TextEditor::GenericProposalModelPtr model)
|
||||
: GenericProposal(cursorPos, model)
|
||||
{
|
||||
}
|
||||
|
||||
bool ClangAssistProposal::isCorrective(TextEditor::TextEditorWidget *editorWidget) const
|
||||
{
|
||||
auto clangAssistProposalModel = static_cast<ClangAssistProposalModel*>(model());
|
||||
auto clangAssistProposalModel = model().staticCast<ClangAssistProposalModel>();
|
||||
|
||||
return clangAssistProposalModel->neededCorrection()
|
||||
== ClangBackEnd::CompletionCorrection::DotToArrowCorrection
|
||||
|
||||
@@ -35,7 +35,7 @@ namespace Internal {
|
||||
class ClangAssistProposal : public TextEditor::GenericProposal
|
||||
{
|
||||
public:
|
||||
ClangAssistProposal(int cursorPos, TextEditor::GenericProposalModel *model);
|
||||
ClangAssistProposal(int cursorPos, TextEditor::GenericProposalModelPtr model);
|
||||
|
||||
bool isCorrective(TextEditor::TextEditorWidget *editorWidget) const override;
|
||||
void makeCorrection(TextEditor::TextEditorWidget *editorWidget) override;
|
||||
|
||||
@@ -593,7 +593,7 @@ bool ClangCompletionAssistProcessor::sendCompletionRequest(int position,
|
||||
TextEditor::IAssistProposal *ClangCompletionAssistProcessor::createProposal(
|
||||
CompletionCorrection neededCorrection) const
|
||||
{
|
||||
ClangAssistProposalModel *model = new ClangAssistProposalModel(neededCorrection);
|
||||
TextEditor::GenericProposalModelPtr model(new ClangAssistProposalModel(neededCorrection));
|
||||
model->loadContent(m_completions);
|
||||
return new ClangAssistProposal(m_positionForProposal, model);
|
||||
}
|
||||
@@ -601,9 +601,8 @@ TextEditor::IAssistProposal *ClangCompletionAssistProcessor::createProposal(
|
||||
IAssistProposal *ClangCompletionAssistProcessor::createFunctionHintProposal(
|
||||
const ClangBackEnd::CodeCompletions &completions) const
|
||||
{
|
||||
auto *model = new ClangFunctionHintModel(completions);
|
||||
auto *proposal = new FunctionHintProposal(m_positionForProposal, model);
|
||||
return proposal;
|
||||
TextEditor::FunctionHintProposalModelPtr model(new ClangFunctionHintModel(completions));
|
||||
return new FunctionHintProposal(m_positionForProposal, model);
|
||||
}
|
||||
|
||||
} // namespace Internal
|
||||
|
||||
@@ -89,7 +89,7 @@ public:
|
||||
}
|
||||
|
||||
public:
|
||||
TextEditor::IAssistProposalModel *proposalModel;
|
||||
TextEditor::ProposalModelPtr proposalModel;
|
||||
};
|
||||
|
||||
static const CppTools::ProjectPartHeaderPaths toHeaderPaths(const QStringList &paths)
|
||||
@@ -102,17 +102,17 @@ static const CppTools::ProjectPartHeaderPaths toHeaderPaths(const QStringList &p
|
||||
return result;
|
||||
}
|
||||
|
||||
ProposalModel completionResults(TextEditor::BaseTextEditor *textEditor,
|
||||
const QStringList &includePaths,
|
||||
int timeOutInMs)
|
||||
TextEditor::ProposalModelPtr completionResults(TextEditor::BaseTextEditor *textEditor,
|
||||
const QStringList &includePaths,
|
||||
int timeOutInMs)
|
||||
{
|
||||
using namespace TextEditor;
|
||||
|
||||
TextEditorWidget *textEditorWidget = qobject_cast<TextEditorWidget *>(textEditor->widget());
|
||||
QTC_ASSERT(textEditorWidget, return ProposalModel());
|
||||
QTC_ASSERT(textEditorWidget, return TextEditor::ProposalModelPtr());
|
||||
AssistInterface *assistInterface = textEditorWidget->createAssistInterface(
|
||||
TextEditor::Completion, TextEditor::ExplicitlyInvoked);
|
||||
QTC_ASSERT(assistInterface, return ProposalModel());
|
||||
QTC_ASSERT(assistInterface, return TextEditor::ProposalModelPtr());
|
||||
if (!includePaths.isEmpty()) {
|
||||
auto clangAssistInterface = static_cast<ClangCompletionAssistInterface *>(assistInterface);
|
||||
clangAssistInterface->setHeaderPaths(toHeaderPaths(includePaths));
|
||||
@@ -121,19 +121,21 @@ ProposalModel completionResults(TextEditor::BaseTextEditor *textEditor,
|
||||
CompletionAssistProvider *assistProvider
|
||||
= textEditor->textDocument()->completionAssistProvider();
|
||||
QTC_ASSERT(qobject_cast<ClangCompletionAssistProvider *>(assistProvider),
|
||||
return ProposalModel());
|
||||
QTC_ASSERT(assistProvider, return ProposalModel());
|
||||
QTC_ASSERT(assistProvider->runType() == IAssistProvider::Asynchronous, return ProposalModel());
|
||||
return TextEditor::ProposalModelPtr());
|
||||
QTC_ASSERT(assistProvider, return TextEditor::ProposalModelPtr());
|
||||
QTC_ASSERT(assistProvider->runType() == IAssistProvider::Asynchronous,
|
||||
return TextEditor::ProposalModelPtr());
|
||||
|
||||
IAssistProcessor *processor = assistProvider->createProcessor();
|
||||
QTC_ASSERT(processor, return ProposalModel());
|
||||
QTC_ASSERT(processor, return TextEditor::ProposalModelPtr());
|
||||
|
||||
WaitForAsyncCompletions waitForCompletions;
|
||||
const WaitForAsyncCompletions::WaitResult result = waitForCompletions.wait(processor,
|
||||
assistInterface,
|
||||
timeOutInMs);
|
||||
QTC_ASSERT(result == WaitForAsyncCompletions::GotResults, return ProposalModel());
|
||||
return QSharedPointer<TextEditor::IAssistProposalModel>(waitForCompletions.proposalModel);
|
||||
QTC_ASSERT(result == WaitForAsyncCompletions::GotResults,
|
||||
return TextEditor::ProposalModelPtr());
|
||||
return waitForCompletions.proposalModel;
|
||||
}
|
||||
|
||||
} // namespace Internal
|
||||
|
||||
@@ -25,22 +25,19 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <texteditor/codeassist/iassistproposalmodel.h>
|
||||
|
||||
#include <QString>
|
||||
#include <QSharedPointer>
|
||||
|
||||
namespace TextEditor {
|
||||
class BaseTextEditor;
|
||||
class IAssistProposalModel;
|
||||
}
|
||||
namespace TextEditor { class BaseTextEditor; }
|
||||
|
||||
namespace ClangCodeModel {
|
||||
namespace Internal {
|
||||
|
||||
using ProposalModel = QSharedPointer<TextEditor::IAssistProposalModel>;
|
||||
|
||||
ProposalModel completionResults(TextEditor::BaseTextEditor *textEditor,
|
||||
const QStringList &includePaths = QStringList(),
|
||||
int timeOutInMs = 10000);
|
||||
TextEditor::ProposalModelPtr completionResults(TextEditor::BaseTextEditor *textEditor,
|
||||
const QStringList &includePaths = QStringList(),
|
||||
int timeOutInMs = 10000);
|
||||
|
||||
} // namespace Internal
|
||||
} // namespace ClangCodeModel
|
||||
|
||||
@@ -337,10 +337,10 @@ public:
|
||||
proposal = completionResults(openEditor.editor(), includePaths, 15000);
|
||||
}
|
||||
|
||||
ProposalModel proposal;
|
||||
TextEditor::ProposalModelPtr proposal;
|
||||
};
|
||||
|
||||
int indexOfItemWithText(ProposalModel model, const QByteArray &text)
|
||||
int indexOfItemWithText(TextEditor::ProposalModelPtr model, const QByteArray &text)
|
||||
{
|
||||
if (!model)
|
||||
return -1;
|
||||
@@ -354,12 +354,12 @@ int indexOfItemWithText(ProposalModel model, const QByteArray &text)
|
||||
return -1;
|
||||
}
|
||||
|
||||
bool hasItem(ProposalModel model, const QByteArray &text)
|
||||
bool hasItem(TextEditor::ProposalModelPtr model, const QByteArray &text)
|
||||
{
|
||||
return indexOfItemWithText(model, text) != -1;
|
||||
}
|
||||
|
||||
bool hasItem(ProposalModel model, const QByteArray &text, const QByteArray &detail)
|
||||
bool hasItem(TextEditor::ProposalModelPtr model, const QByteArray &text, const QByteArray &detail)
|
||||
{
|
||||
const int index = indexOfItemWithText(model, text);
|
||||
if (index != -1 && index < model->size()) {
|
||||
@@ -372,7 +372,7 @@ bool hasItem(ProposalModel model, const QByteArray &text, const QByteArray &deta
|
||||
return false;
|
||||
}
|
||||
|
||||
bool hasSnippet(ProposalModel model, const QByteArray &text)
|
||||
bool hasSnippet(TextEditor::ProposalModelPtr model, const QByteArray &text)
|
||||
{
|
||||
if (!model)
|
||||
return false;
|
||||
@@ -602,7 +602,7 @@ void ClangCodeCompletionTest::testCompleteProjectDependingCode()
|
||||
OpenEditorAtCursorPosition openEditor(testDocument);
|
||||
QVERIFY(openEditor.succeeded());
|
||||
|
||||
ProposalModel proposal = completionResults(openEditor.editor());
|
||||
TextEditor::ProposalModelPtr proposal = completionResults(openEditor.editor());
|
||||
QVERIFY(hasItem(proposal, "projectConfiguration1"));
|
||||
}
|
||||
|
||||
@@ -615,7 +615,7 @@ void ClangCodeCompletionTest::testCompleteProjectDependingCodeAfterChangingProje
|
||||
QVERIFY(openEditor.succeeded());
|
||||
|
||||
// Check completion without project
|
||||
ProposalModel proposal = completionResults(openEditor.editor());
|
||||
TextEditor::ProposalModelPtr proposal = completionResults(openEditor.editor());
|
||||
QVERIFY(hasItem(proposal, "noProjectConfigurationDetected"));
|
||||
|
||||
{
|
||||
@@ -667,7 +667,7 @@ void ClangCodeCompletionTest::testCompleteProjectDependingCodeInGeneratedUiFile(
|
||||
QVERIFY(openSource.succeeded());
|
||||
|
||||
// ...and check comletions
|
||||
ProposalModel proposal = completionResults(openSource.editor());
|
||||
TextEditor::ProposalModelPtr proposal = completionResults(openSource.editor());
|
||||
QVERIFY(hasItem(proposal, "menuBar"));
|
||||
QVERIFY(hasItem(proposal, "statusBar"));
|
||||
QVERIFY(hasItem(proposal, "centralWidget"));
|
||||
@@ -687,7 +687,7 @@ void ClangCodeCompletionTest::testCompleteAfterModifyingIncludedHeaderInOtherEdi
|
||||
// Test that declarations from header file are visible in source file
|
||||
OpenEditorAtCursorPosition openSource(sourceDocument);
|
||||
QVERIFY(openSource.succeeded());
|
||||
ProposalModel proposal = completionResults(openSource.editor());
|
||||
TextEditor::ProposalModelPtr proposal = completionResults(openSource.editor());
|
||||
QVERIFY(hasItem(proposal, "globalFromHeader"));
|
||||
|
||||
// Open header and insert a new declaration
|
||||
@@ -720,7 +720,7 @@ void ClangCodeCompletionTest::testCompleteAfterModifyingIncludedHeaderByRefactor
|
||||
// Open source and test that declaration from header file is visible in source file
|
||||
OpenEditorAtCursorPosition openSource(sourceDocument);
|
||||
QVERIFY(openSource.succeeded());
|
||||
ProposalModel proposal = completionResults(openSource.editor());
|
||||
TextEditor::ProposalModelPtr proposal = completionResults(openSource.editor());
|
||||
QVERIFY(hasItem(proposal, "globalFromHeader"));
|
||||
|
||||
// Modify header document without switching to its editor.
|
||||
@@ -755,7 +755,7 @@ void ClangCodeCompletionTest::testCompleteAfterChangingIncludedAndOpenHeaderExte
|
||||
// Open source and test completions
|
||||
OpenEditorAtCursorPosition openSource(sourceDocument);
|
||||
QVERIFY(openSource.succeeded());
|
||||
ProposalModel proposal = completionResults(openSource.editor());
|
||||
TextEditor::ProposalModelPtr proposal = completionResults(openSource.editor());
|
||||
QVERIFY(hasItem(proposal, "globalFromHeader"));
|
||||
|
||||
// Simulate external modification and wait for reload
|
||||
@@ -783,7 +783,7 @@ void ClangCodeCompletionTest::testCompleteAfterChangingIncludedAndNotOpenHeaderE
|
||||
// Open source and test completions
|
||||
OpenEditorAtCursorPosition openSource(sourceDocument);
|
||||
QVERIFY(openSource.succeeded());
|
||||
ProposalModel proposal = completionResults(openSource.editor());
|
||||
TextEditor::ProposalModelPtr proposal = completionResults(openSource.editor());
|
||||
QVERIFY(hasItem(proposal, "globalFromHeader"));
|
||||
|
||||
// Simulate external modification, e.g version control checkout
|
||||
|
||||
Reference in New Issue
Block a user