Clang: Use new libclang code completion fix-its feature

Each completion coming from libclang now has it's own
list of fix-its which are required to be applied before
completion itself.

This saves one extra reparse cycle and gives an ability
to provide both kinds of completion (initial and corrected
one) for cases like shared_ptr, unique_ptr or any other
class with overloaded arrow operator.

Each of these extra fix-its is applied together with
corresponding completion dircetly before completion itself.

Change-Id: Ide37e45bb15fa2f1375cd6b86ecd43ced3593046
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Ivan Donchevskii
2018-06-15 14:35:58 +02:00
parent ee0f386aad
commit da4be3fdb7
31 changed files with 299 additions and 352 deletions

View File

@@ -1,58 +0,0 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#include "clangassistproposal.h"
#include <texteditor/texteditor.h>
namespace ClangCodeModel {
namespace Internal {
ClangAssistProposal::ClangAssistProposal(int cursorPos, TextEditor::GenericProposalModelPtr model)
: GenericProposal(cursorPos, model)
{
}
bool ClangAssistProposal::isCorrective(TextEditor::TextEditorWidget *editorWidget) const
{
auto clangAssistProposalModel = model().staticCast<ClangAssistProposalModel>();
return clangAssistProposalModel->neededCorrection()
== ClangBackEnd::CompletionCorrection::DotToArrowCorrection
&& editorWidget->textAt(basePosition() - 1, 1) == ".";
}
void ClangAssistProposal::makeCorrection(TextEditor::TextEditorWidget *editorWidget)
{
const int oldPosition = editorWidget->position();
editorWidget->setCursorPosition(basePosition() - 1);
editorWidget->replace(1, QLatin1String("->"));
editorWidget->setCursorPosition(oldPosition + 1);
moveBasePosition(1);
}
} // namespace Internal
} // namespace ClangCodeModel

View File

@@ -1,45 +0,0 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#pragma once
#include "clangassistproposalmodel.h"
#include <texteditor/codeassist/genericproposal.h>
namespace ClangCodeModel {
namespace Internal {
class ClangAssistProposal : public TextEditor::GenericProposal
{
public:
ClangAssistProposal(int cursorPos, TextEditor::GenericProposalModelPtr model);
bool isCorrective(TextEditor::TextEditorWidget *editorWidget) const override;
void makeCorrection(TextEditor::TextEditorWidget *editorWidget) override;
};
} // namespace Internal
} // namespace ClangCodeModel

View File

@@ -26,6 +26,7 @@
#include "clangassistproposalitem.h"
#include "clangcompletionchunkstotextconverter.h"
#include "clangfixitoperation.h"
#include <cplusplus/Icons.h>
#include <cplusplus/MatchingText.h>
@@ -35,9 +36,12 @@
#include <texteditor/completionsettings.h>
#include <texteditor/texteditorsettings.h>
#include <QCoreApplication>
#include <QTextCursor>
#include <utils/algorithm.h>
#include <utils/textutils.h>
#include <utils/qtcassert.h>
using namespace CPlusPlus;
using namespace ClangBackEnd;
@@ -119,7 +123,15 @@ void ClangAssistProposalItem::apply(TextEditor::TextDocumentManipulatorInterface
{
const CodeCompletion ccr = codeCompletion();
QString textToBeInserted = text();
if (!ccr.requiredFixIts.empty()) {
ClangFixItOperation fixItOperation(Utf8String(), ccr.requiredFixIts);
fixItOperation.perform();
const int shift = fixItsShift(manipulator);
basePosition += shift;
}
QString textToBeInserted = m_text;
QString extraCharacters;
int extraLength = 0;
int cursorOffset = 0;
@@ -269,7 +281,43 @@ void ClangAssistProposalItem::setText(const QString &text)
QString ClangAssistProposalItem::text() const
{
return m_text;
return m_text + (requiresFixIts() ? fixItText() : QString());
}
// FIXME: Indicate required fix-it without adding extra text.
QString ClangAssistProposalItem::fixItText() const
{
const FixItContainer &fixIt = m_codeCompletion.requiredFixIts.first();
const SourceRangeContainer &range = fixIt.range;
return QCoreApplication::translate("ClangCodeModel::ClangAssistProposalItem",
" (requires to correct [%1:%2-%3:%4] to \"%5\")")
.arg(range.start.line)
.arg(range.start.column)
.arg(range.end.line)
.arg(range.end.column)
.arg(fixIt.text.toString());
}
int ClangAssistProposalItem::fixItsShift(
const TextEditor::TextDocumentManipulatorInterface &manipulator) const
{
if (m_codeCompletion.requiredFixIts.empty())
return 0;
int shift = 0;
QTextCursor cursor = manipulator.textCursorAt(0);
for (const FixItContainer &fixIt : m_codeCompletion.requiredFixIts) {
const int fixItStartPos = Utils::Text::positionInText(
cursor.document(),
static_cast<int>(fixIt.range.start.line),
static_cast<int>(fixIt.range.start.column));
const int fixItEndPos = Utils::Text::positionInText(
cursor.document(),
static_cast<int>(fixIt.range.end.line),
static_cast<int>(fixIt.range.end.column));
shift += fixIt.text.toString().length() - (fixItEndPos - fixItStartPos);
}
return shift;
}
QIcon ClangAssistProposalItem::icon() const
@@ -362,6 +410,11 @@ quint64 ClangAssistProposalItem::hash() const
return 0;
}
bool ClangAssistProposalItem::requiresFixIts() const
{
return !m_codeCompletion.requiredFixIts.empty();
}
bool ClangAssistProposalItem::hasOverloadsWithParameters() const
{
return m_hasOverloadsWithParameters;

View File

@@ -50,6 +50,7 @@ public:
bool isSnippet() const final;
bool isValid() const final;
quint64 hash() const final;
bool requiresFixIts() const final;
void keepCompletionOperator(unsigned compOp);
@@ -60,6 +61,9 @@ public:
const ClangBackEnd::CodeCompletion &codeCompletion() const;
private:
QString fixItText() const;
int fixItsShift(const TextEditor::TextDocumentManipulatorInterface &manipulator) const;
ClangBackEnd::CodeCompletion m_codeCompletion;
QList<ClangBackEnd::CodeCompletion> m_overloads;
bool m_hasOverloadsWithParameters = false;

View File

@@ -36,12 +36,6 @@ namespace Internal {
constexpr int SORT_LIMIT = 30000;
ClangAssistProposalModel::ClangAssistProposalModel(
ClangBackEnd::CompletionCorrection neededCorrection)
: m_neededCorrection(neededCorrection)
{
}
bool ClangAssistProposalModel::containsDuplicates() const
{
return false;
@@ -62,6 +56,8 @@ void ClangAssistProposalModel::sort(const QString &/*prefix*/)
return static_cast<int>(first->prefixMatch())
< static_cast<int>(second->prefixMatch());
}
if (first->requiresFixIts() != second->requiresFixIts())
return first->requiresFixIts() < second->requiresFixIts();
return (first->order() > 0
&& (first->order() < second->order()
|| (first->order() == second->order() && first->text() < second->text())));
@@ -70,11 +66,6 @@ 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

View File

@@ -37,17 +37,12 @@ namespace Internal {
class ClangAssistProposalModel : public TextEditor::GenericProposalModel
{
public:
ClangAssistProposalModel(ClangBackEnd::CompletionCorrection neededCorrection);
ClangAssistProposalModel() = default;
bool containsDuplicates() const override;
bool isSortable(const QString &prefix) const override;
void sort(const QString &prefix) override;
ClangBackEnd::CompletionCorrection neededCorrection() const;
private:
ClangBackEnd::CompletionCorrection m_neededCorrection;
};
} // namespace Internal

View File

@@ -187,10 +187,8 @@ void BackendReceiver::completions(const CompletionsMessage &message)
const quint64 ticket = message.ticketNumber;
QScopedPointer<ClangCompletionAssistProcessor> processor(m_assistProcessorsTable.take(ticket));
if (processor) {
processor->handleAvailableCompletions(message.codeCompletions,
message.neededCorrection);
}
if (processor)
processor->handleAvailableCompletions(message.codeCompletions);
}
void BackendReceiver::annotations(const AnnotationsMessage &message)

View File

@@ -8,7 +8,6 @@ requires(!isEmpty(LLVM_VERSION))
SOURCES += \
clangactivationsequencecontextprocessor.cpp \
clangactivationsequenceprocessor.cpp \
clangassistproposal.cpp \
clangassistproposalitem.cpp \
clangassistproposalmodel.cpp \
clangbackendcommunicator.cpp \
@@ -46,7 +45,6 @@ SOURCES += \
HEADERS += \
clangactivationsequencecontextprocessor.h \
clangactivationsequenceprocessor.h \
clangassistproposal.h \
clangassistproposalitem.h \
clangassistproposalmodel.h \
clangbackendcommunicator.h \

View File

@@ -36,8 +36,6 @@ QtcPlugin {
"clangactivationsequencecontextprocessor.h",
"clangactivationsequenceprocessor.cpp",
"clangactivationsequenceprocessor.h",
"clangassistproposal.cpp",
"clangassistproposal.h",
"clangassistproposalitem.cpp",
"clangassistproposalitem.h",
"clangassistproposalmodel.cpp",

View File

@@ -26,7 +26,6 @@
#include "clangassistproposalitem.h"
#include "clangactivationsequenceprocessor.h"
#include "clangassistproposal.h"
#include "clangassistproposalmodel.h"
#include "clangcompletionassistprocessor.h"
#include "clangcompletioncontextanalyzer.h"
@@ -41,6 +40,7 @@
#include <texteditor/codeassist/assistproposalitem.h>
#include <texteditor/codeassist/functionhintproposal.h>
#include <texteditor/codeassist/genericproposal.h>
#include <texteditor/codeassist/ifunctionhintproposalmodel.h>
#include <cplusplus/BackwardsScanner.h>
@@ -68,7 +68,6 @@ namespace {
QList<AssistProposalItemInterface *> toAssistProposalItems(const CodeCompletions &completions)
{
bool signalCompletion = false; // TODO
bool slotCompletion = false; // TODO
@@ -143,8 +142,7 @@ static CodeCompletions filterFunctionSignatures(const CodeCompletions &completio
}
void ClangCompletionAssistProcessor::handleAvailableCompletions(
const CodeCompletions &completions,
CompletionCorrection neededCorrection)
const CodeCompletions &completions)
{
QTC_CHECK(m_completions.isEmpty());
@@ -154,7 +152,7 @@ void ClangCompletionAssistProcessor::handleAvailableCompletions(
if (m_addSnippets && !m_completions.isEmpty())
addSnippets();
setAsyncProposalAvailable(createProposal(neededCorrection));
setAsyncProposalAvailable(createProposal());
} else {
const CodeCompletions functionSignatures = filterFunctionSignatures(completions);
if (!functionSignatures.isEmpty())
@@ -591,13 +589,12 @@ bool ClangCompletionAssistProcessor::sendCompletionRequest(int position,
return false;
}
TextEditor::IAssistProposal *ClangCompletionAssistProcessor::createProposal(
CompletionCorrection neededCorrection)
TextEditor::IAssistProposal *ClangCompletionAssistProcessor::createProposal()
{
m_requestSent = false;
TextEditor::GenericProposalModelPtr model(new ClangAssistProposalModel(neededCorrection));
TextEditor::GenericProposalModelPtr model(new ClangAssistProposalModel());
model->loadContent(m_completions);
return new ClangAssistProposal(m_positionForProposal, model);
return new GenericProposal(m_positionForProposal, model);
}
IAssistProposal *ClangCompletionAssistProcessor::createFunctionHintProposal(

View File

@@ -50,8 +50,7 @@ public:
TextEditor::IAssistProposal *perform(const TextEditor::AssistInterface *interface) override;
void handleAvailableCompletions(const CodeCompletions &completions,
CompletionCorrection neededCorrection);
void handleAvailableCompletions(const CodeCompletions &completions);
bool running() final { return m_requestSent; }
const TextEditor::TextEditorWidget *textEditorWidget() const;
@@ -62,8 +61,7 @@ private:
int findStartOfName(int pos = -1) const;
bool accepts() const;
TextEditor::IAssistProposal *createProposal(
CompletionCorrection neededCorrection = CompletionCorrection::NoCorrection);
TextEditor::IAssistProposal *createProposal();
TextEditor::IAssistProposal *createFunctionHintProposal(
const CodeCompletions &completions);