2015-07-06 12:05:02 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2015-07-06 12:05:02 +02:00
|
|
|
**
|
|
|
|
|
** 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
|
2016-01-15 14:57:40 +01:00
|
|
|
** 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.
|
2015-07-06 12:05:02 +02:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** 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.
|
2015-07-06 12:05:02 +02:00
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "clangassistproposalitem.h"
|
|
|
|
|
|
2015-11-30 09:43:50 +01:00
|
|
|
#include "clangcompletionchunkstotextconverter.h"
|
2018-06-15 14:35:58 +02:00
|
|
|
#include "clangfixitoperation.h"
|
2018-09-25 16:19:41 +02:00
|
|
|
#include "clangutils.h"
|
2015-07-14 16:08:14 +02:00
|
|
|
|
2016-01-28 15:35:18 +01:00
|
|
|
#include <cplusplus/Icons.h>
|
2015-07-06 12:05:02 +02:00
|
|
|
#include <cplusplus/MatchingText.h>
|
2017-08-10 16:28:19 +02:00
|
|
|
#include <cplusplus/SimpleLexer.h>
|
2015-07-06 12:05:02 +02:00
|
|
|
#include <cplusplus/Token.h>
|
|
|
|
|
|
|
|
|
|
#include <texteditor/completionsettings.h>
|
2018-09-25 16:19:41 +02:00
|
|
|
#include <texteditor/texteditor.h>
|
2015-07-06 12:05:02 +02:00
|
|
|
#include <texteditor/texteditorsettings.h>
|
|
|
|
|
|
2018-06-15 14:35:58 +02:00
|
|
|
#include <QCoreApplication>
|
2018-09-25 16:19:41 +02:00
|
|
|
#include <QTextBlock>
|
2017-08-11 10:35:57 +02:00
|
|
|
#include <QTextCursor>
|
2018-09-25 16:19:41 +02:00
|
|
|
#include <QTextDocument>
|
2017-08-11 10:35:57 +02:00
|
|
|
|
2017-08-10 16:28:19 +02:00
|
|
|
#include <utils/algorithm.h>
|
2018-06-15 14:35:58 +02:00
|
|
|
#include <utils/textutils.h>
|
|
|
|
|
#include <utils/qtcassert.h>
|
2017-08-10 16:28:19 +02:00
|
|
|
|
2015-07-06 12:05:02 +02:00
|
|
|
using namespace CPlusPlus;
|
|
|
|
|
using namespace ClangBackEnd;
|
2018-09-25 16:19:41 +02:00
|
|
|
using namespace TextEditor;
|
2015-07-06 12:05:02 +02:00
|
|
|
|
|
|
|
|
namespace ClangCodeModel {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
2016-01-28 16:39:05 +01:00
|
|
|
bool ClangAssistProposalItem::prematurelyApplies(const QChar &typedCharacter) const
|
2015-07-06 12:05:02 +02:00
|
|
|
{
|
2015-07-06 15:01:46 +02:00
|
|
|
bool applies = false;
|
2015-07-06 12:05:02 +02:00
|
|
|
|
|
|
|
|
if (m_completionOperator == T_SIGNAL || m_completionOperator == T_SLOT)
|
2016-01-28 16:39:05 +01:00
|
|
|
applies = QString::fromLatin1("(,").contains(typedCharacter);
|
2015-07-06 12:05:02 +02:00
|
|
|
else if (m_completionOperator == T_STRING_LITERAL || m_completionOperator == T_ANGLE_STRING_LITERAL)
|
2016-01-28 16:39:05 +01:00
|
|
|
applies = (typedCharacter == QLatin1Char('/')) && text().endsWith(QLatin1Char('/'));
|
2018-07-31 11:44:07 +02:00
|
|
|
else if (firstCodeCompletion().completionKind == CodeCompletion::ObjCMessageCompletionKind)
|
2016-01-28 16:39:05 +01:00
|
|
|
applies = QString::fromLatin1(";.,").contains(typedCharacter);
|
2015-07-06 12:05:02 +02:00
|
|
|
else
|
2016-01-28 16:39:05 +01:00
|
|
|
applies = QString::fromLatin1(";.,:(").contains(typedCharacter);
|
2015-07-06 12:05:02 +02:00
|
|
|
|
2015-07-06 15:01:46 +02:00
|
|
|
if (applies)
|
2016-01-28 16:39:05 +01:00
|
|
|
m_typedCharacter = typedCharacter;
|
2015-07-06 12:05:02 +02:00
|
|
|
|
2015-07-06 15:01:46 +02:00
|
|
|
return applies;
|
2015-07-06 12:05:02 +02:00
|
|
|
}
|
|
|
|
|
|
2016-01-28 15:35:18 +01:00
|
|
|
bool ClangAssistProposalItem::implicitlyApplies() const
|
|
|
|
|
{
|
2017-05-17 15:53:47 +02:00
|
|
|
return true;
|
2016-01-28 15:35:18 +01:00
|
|
|
}
|
|
|
|
|
|
2018-09-25 16:19:41 +02:00
|
|
|
static QString textUntilPreviousStatement(TextDocumentManipulatorInterface &manipulator,
|
2017-08-10 16:28:19 +02:00
|
|
|
int startPosition)
|
|
|
|
|
{
|
|
|
|
|
static const QString stopCharacters(";{}#");
|
|
|
|
|
|
|
|
|
|
int endPosition = 0;
|
|
|
|
|
for (int i = startPosition; i >= 0 ; --i) {
|
|
|
|
|
if (stopCharacters.contains(manipulator.characterAt(i))) {
|
|
|
|
|
endPosition = i + 1;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return manipulator.textAt(endPosition, startPosition - endPosition);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 7.3.3: using typename(opt) nested-name-specifier unqualified-id ;
|
2018-09-25 16:19:41 +02:00
|
|
|
static bool isAtUsingDeclaration(TextDocumentManipulatorInterface &manipulator,
|
2017-08-10 16:28:19 +02:00
|
|
|
int basePosition)
|
|
|
|
|
{
|
|
|
|
|
SimpleLexer lexer;
|
|
|
|
|
lexer.setLanguageFeatures(LanguageFeatures::defaultFeatures());
|
|
|
|
|
const QString textToLex = textUntilPreviousStatement(manipulator, basePosition);
|
|
|
|
|
const Tokens tokens = lexer(textToLex);
|
|
|
|
|
if (tokens.empty())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// The nested-name-specifier always ends with "::", so check for this first.
|
|
|
|
|
const Token lastToken = tokens[tokens.size() - 1];
|
|
|
|
|
if (lastToken.kind() != T_COLON_COLON)
|
|
|
|
|
return false;
|
|
|
|
|
|
2018-09-25 16:19:41 +02:00
|
|
|
return ::Utils::contains(tokens, [](const Token &token) {
|
2017-08-10 16:28:19 +02:00
|
|
|
return token.kind() == T_USING;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-31 11:44:07 +02:00
|
|
|
static QString methodDefinitionParameters(const CodeCompletionChunks &chunks)
|
|
|
|
|
{
|
|
|
|
|
QString result;
|
|
|
|
|
|
|
|
|
|
auto typedTextChunkIt = std::find_if(chunks.begin(), chunks.end(),
|
|
|
|
|
[](const CodeCompletionChunk &chunk) {
|
|
|
|
|
return chunk.kind == CodeCompletionChunk::TypedText;
|
|
|
|
|
});
|
|
|
|
|
if (typedTextChunkIt == chunks.end())
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
|
|
std::for_each(++typedTextChunkIt, chunks.end(), [&result](const CodeCompletionChunk &chunk) {
|
|
|
|
|
if (chunk.kind == CodeCompletionChunk::Placeholder && chunk.text.contains('=')) {
|
|
|
|
|
Utf8String text = chunk.text.mid(0, chunk.text.indexOf('='));
|
|
|
|
|
if (text.endsWith(' '))
|
|
|
|
|
text.chop(1);
|
|
|
|
|
result += text;
|
|
|
|
|
} else {
|
|
|
|
|
result += chunk.text;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-09 12:46:00 +01:00
|
|
|
static bool skipParenForFunctionLikeSnippet(const std::vector<int> &placeholderPositions,
|
|
|
|
|
const QString &text,
|
|
|
|
|
int position)
|
|
|
|
|
{
|
|
|
|
|
return placeholderPositions.size() == 1
|
|
|
|
|
&& position > 0
|
|
|
|
|
&& text[position - 1] == '('
|
|
|
|
|
&& text[position] == ')'
|
|
|
|
|
&& position + 1 == text.size();
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-25 16:19:41 +02:00
|
|
|
void ClangAssistProposalItem::apply(TextDocumentManipulatorInterface &manipulator,
|
2016-01-28 15:35:18 +01:00
|
|
|
int basePosition) const
|
2015-07-06 12:05:02 +02:00
|
|
|
{
|
2018-07-31 11:44:07 +02:00
|
|
|
const CodeCompletion ccr = firstCodeCompletion();
|
2015-07-06 12:05:02 +02:00
|
|
|
|
2018-06-15 14:35:58 +02:00
|
|
|
if (!ccr.requiredFixIts.empty()) {
|
2018-09-25 16:19:41 +02:00
|
|
|
// Important: Calculate shift before changing the document.
|
|
|
|
|
basePosition += fixItsShift(manipulator);
|
|
|
|
|
|
2018-06-15 14:35:58 +02:00
|
|
|
ClangFixItOperation fixItOperation(Utf8String(), ccr.requiredFixIts);
|
|
|
|
|
fixItOperation.perform();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString textToBeInserted = m_text;
|
2016-01-28 16:39:05 +01:00
|
|
|
QString extraCharacters;
|
2015-07-06 12:05:02 +02:00
|
|
|
int extraLength = 0;
|
|
|
|
|
int cursorOffset = 0;
|
2016-06-10 15:13:38 +02:00
|
|
|
bool setAutoCompleteSkipPos = false;
|
2017-06-28 09:51:53 +02:00
|
|
|
int currentPosition = manipulator.currentPosition();
|
2015-07-06 12:05:02 +02:00
|
|
|
|
|
|
|
|
if (m_completionOperator == T_SIGNAL || m_completionOperator == T_SLOT) {
|
2016-01-28 16:39:05 +01:00
|
|
|
extraCharacters += QLatin1Char(')');
|
|
|
|
|
if (m_typedCharacter == QLatin1Char('(')) // Eat the opening parenthesis
|
|
|
|
|
m_typedCharacter = QChar();
|
2018-04-04 18:25:23 +02:00
|
|
|
} else if (ccr.completionKind == CodeCompletion::KeywordCompletionKind) {
|
2015-07-14 16:08:14 +02:00
|
|
|
CompletionChunksToTextConverter converter;
|
2016-01-18 09:51:00 +01:00
|
|
|
converter.setupForKeywords();
|
2018-04-04 18:25:23 +02:00
|
|
|
converter.parseChunks(ccr.chunks);
|
2015-07-14 16:08:14 +02:00
|
|
|
|
2015-08-27 17:12:30 +02:00
|
|
|
textToBeInserted = converter.text();
|
2018-11-09 12:46:00 +01:00
|
|
|
|
|
|
|
|
if (converter.hasPlaceholderPositions()) {
|
|
|
|
|
const std::vector<int> &placeholderPositions = converter.placeholderPositions();
|
|
|
|
|
const int position = placeholderPositions[0];
|
|
|
|
|
cursorOffset = position - converter.text().size();
|
|
|
|
|
// If the snippet looks like a function call, e.g. "sizeof(<PLACEHOLDER>)",
|
|
|
|
|
// ensure that we can "overtype" ')' after inserting it.
|
|
|
|
|
setAutoCompleteSkipPos = skipParenForFunctionLikeSnippet(placeholderPositions,
|
|
|
|
|
textToBeInserted,
|
|
|
|
|
position);
|
|
|
|
|
}
|
2018-04-04 18:25:23 +02:00
|
|
|
} else if (ccr.completionKind == CodeCompletion::NamespaceCompletionKind) {
|
2016-01-18 12:14:10 +01:00
|
|
|
CompletionChunksToTextConverter converter;
|
2018-04-04 18:25:23 +02:00
|
|
|
converter.parseChunks(ccr.chunks); // Appends "::" after name space name
|
2016-01-18 12:14:10 +01:00
|
|
|
textToBeInserted = converter.text();
|
2018-10-25 14:05:31 +02:00
|
|
|
|
|
|
|
|
// Clang does not provide the "::" chunk consistently for namespaces, e.g.
|
|
|
|
|
//
|
|
|
|
|
// namespace a { namespace b { namespace c {} } }
|
|
|
|
|
// <CURSOR> // complete "a" ==> "a::"
|
|
|
|
|
// a::<CURSOR> // complete "b" ==> "b", not "b::"
|
|
|
|
|
//
|
|
|
|
|
// Remove "::" to avoid any confusion for now.
|
|
|
|
|
if (textToBeInserted.endsWith("::"))
|
|
|
|
|
textToBeInserted.chop(2);
|
2018-04-04 18:25:23 +02:00
|
|
|
} else if (!ccr.text.isEmpty()) {
|
2018-09-25 16:19:41 +02:00
|
|
|
const CompletionSettings &completionSettings =
|
|
|
|
|
TextEditorSettings::instance()->completionSettings();
|
2015-07-06 12:05:02 +02:00
|
|
|
const bool autoInsertBrackets = completionSettings.m_autoInsertBrackets;
|
|
|
|
|
|
|
|
|
|
if (autoInsertBrackets &&
|
2018-04-04 18:25:23 +02:00
|
|
|
(ccr.completionKind == CodeCompletion::FunctionCompletionKind
|
2018-07-31 09:48:02 +02:00
|
|
|
|| ccr.completionKind == CodeCompletion::FunctionDefinitionCompletionKind
|
2018-04-04 18:25:23 +02:00
|
|
|
|| ccr.completionKind == CodeCompletion::DestructorCompletionKind
|
2018-08-02 15:15:59 +02:00
|
|
|
|| ccr.completionKind == CodeCompletion::ConstructorCompletionKind
|
2018-04-04 18:25:23 +02:00
|
|
|
|| ccr.completionKind == CodeCompletion::SignalCompletionKind
|
|
|
|
|
|| ccr.completionKind == CodeCompletion::SlotCompletionKind)) {
|
2015-07-06 12:05:02 +02:00
|
|
|
// When the user typed the opening parenthesis, he'll likely also type the closing one,
|
|
|
|
|
// in which case it would be annoying if we put the cursor after the already automatically
|
|
|
|
|
// inserted closing parenthesis.
|
2016-01-28 16:39:05 +01:00
|
|
|
const bool skipClosingParenthesis = m_typedCharacter != QLatin1Char('(');
|
2017-06-01 16:36:56 +02:00
|
|
|
QTextCursor cursor = manipulator.textCursorAt(basePosition);
|
2018-07-31 09:48:02 +02:00
|
|
|
|
2017-08-01 10:55:27 +02:00
|
|
|
bool abandonParen = false;
|
2018-10-17 09:42:48 +02:00
|
|
|
if (Utils::Text::matchPreviousWord(manipulator, cursor, "&")) {
|
|
|
|
|
Utils::Text::moveToPreviousWord(manipulator, cursor);
|
|
|
|
|
Utils::Text::moveToPreviousChar(manipulator, cursor);
|
2017-08-01 10:55:27 +02:00
|
|
|
const QChar prevChar = manipulator.characterAt(cursor.position());
|
2018-07-31 11:44:07 +02:00
|
|
|
cursor.setPosition(basePosition);
|
2017-08-01 10:55:27 +02:00
|
|
|
abandonParen = QString("(;,{}").contains(prevChar);
|
|
|
|
|
}
|
2017-08-10 16:28:19 +02:00
|
|
|
if (!abandonParen)
|
|
|
|
|
abandonParen = isAtUsingDeclaration(manipulator, basePosition);
|
2018-07-31 11:44:07 +02:00
|
|
|
|
2018-07-31 09:48:02 +02:00
|
|
|
if (!abandonParen && ccr.completionKind == CodeCompletion::FunctionDefinitionCompletionKind) {
|
|
|
|
|
const CodeCompletionChunk resultType = ccr.chunks.first();
|
2018-10-05 14:34:50 +02:00
|
|
|
if (resultType.kind == CodeCompletionChunk::ResultType) {
|
2018-10-17 09:42:48 +02:00
|
|
|
if (Utils::Text::matchPreviousWord(manipulator, cursor, resultType.text.toString())) {
|
2018-10-05 14:34:50 +02:00
|
|
|
extraCharacters += methodDefinitionParameters(ccr.chunks);
|
|
|
|
|
// To skip the next block.
|
|
|
|
|
abandonParen = true;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Do nothing becasue it's not a function definition.
|
|
|
|
|
|
|
|
|
|
// It's a clang bug that the function might miss a ResultType chunk
|
|
|
|
|
// when the base class method is called from the overriding method
|
|
|
|
|
// of the derived class. For example:
|
|
|
|
|
// void Derived::foo() override { Base::<complete here> }
|
2018-07-31 09:48:02 +02:00
|
|
|
}
|
|
|
|
|
}
|
2017-08-01 10:55:27 +02:00
|
|
|
if (!abandonParen) {
|
2017-06-01 16:36:56 +02:00
|
|
|
if (completionSettings.m_spaceAfterFunctionName)
|
|
|
|
|
extraCharacters += QLatin1Char(' ');
|
|
|
|
|
extraCharacters += QLatin1Char('(');
|
|
|
|
|
if (m_typedCharacter == QLatin1Char('('))
|
|
|
|
|
m_typedCharacter = QChar();
|
2015-07-06 12:05:02 +02:00
|
|
|
|
2017-06-01 16:36:56 +02:00
|
|
|
// If the function doesn't return anything, automatically place the semicolon,
|
|
|
|
|
// unless we're doing a scope completion (then it might be function definition).
|
2017-06-28 09:51:53 +02:00
|
|
|
const QChar characterAtCursor = manipulator.characterAt(currentPosition);
|
2017-06-01 16:36:56 +02:00
|
|
|
bool endWithSemicolon = m_typedCharacter == QLatin1Char(';')/*
|
|
|
|
|
|| (function->returnType()->isVoidType() && m_completionOperator != T_COLON_COLON)*/; //###
|
|
|
|
|
const QChar semicolon = m_typedCharacter.isNull() ? QLatin1Char(';') : m_typedCharacter;
|
2015-07-06 12:05:02 +02:00
|
|
|
|
2017-06-01 16:36:56 +02:00
|
|
|
if (endWithSemicolon && characterAtCursor == semicolon) {
|
|
|
|
|
endWithSemicolon = false;
|
2016-01-28 16:39:05 +01:00
|
|
|
m_typedCharacter = QChar();
|
2015-07-06 12:05:02 +02:00
|
|
|
}
|
2017-06-01 16:36:56 +02:00
|
|
|
|
|
|
|
|
// If the function takes no arguments, automatically place the closing parenthesis
|
2018-04-04 18:25:23 +02:00
|
|
|
if (!hasOverloadsWithParameters() && !ccr.hasParameters && skipClosingParenthesis) {
|
2016-01-28 16:39:05 +01:00
|
|
|
extraCharacters += QLatin1Char(')');
|
2015-07-06 12:05:02 +02:00
|
|
|
if (endWithSemicolon) {
|
2016-01-28 16:39:05 +01:00
|
|
|
extraCharacters += semicolon;
|
|
|
|
|
m_typedCharacter = QChar();
|
2015-07-06 12:05:02 +02:00
|
|
|
}
|
2017-08-11 10:35:57 +02:00
|
|
|
} else {
|
2017-06-01 16:36:56 +02:00
|
|
|
const QChar lookAhead = manipulator.characterAt(manipulator.currentPosition() + 1);
|
|
|
|
|
if (MatchingText::shouldInsertMatchingText(lookAhead)) {
|
|
|
|
|
extraCharacters += QLatin1Char(')');
|
|
|
|
|
--cursorOffset;
|
|
|
|
|
setAutoCompleteSkipPos = true;
|
|
|
|
|
if (endWithSemicolon) {
|
|
|
|
|
extraCharacters += semicolon;
|
|
|
|
|
--cursorOffset;
|
|
|
|
|
m_typedCharacter = QChar();
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-07-06 12:05:02 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Append an unhandled typed character, adjusting cursor offset when it had been adjusted before
|
2016-01-28 16:39:05 +01:00
|
|
|
if (!m_typedCharacter.isNull()) {
|
|
|
|
|
extraCharacters += m_typedCharacter;
|
2015-07-06 12:05:02 +02:00
|
|
|
if (cursorOffset != 0)
|
|
|
|
|
--cursorOffset;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Avoid inserting characters that are already there
|
2017-05-17 15:53:47 +02:00
|
|
|
QTextCursor cursor = manipulator.textCursorAt(basePosition);
|
|
|
|
|
cursor.movePosition(QTextCursor::EndOfWord);
|
2017-06-28 09:51:53 +02:00
|
|
|
const QString textAfterCursor = manipulator.textAt(currentPosition,
|
|
|
|
|
cursor.position() - currentPosition);
|
|
|
|
|
|
|
|
|
|
if (textToBeInserted != textAfterCursor
|
|
|
|
|
&& textToBeInserted.indexOf(textAfterCursor, currentPosition - basePosition) >= 0) {
|
|
|
|
|
currentPosition = cursor.position();
|
|
|
|
|
}
|
2017-05-17 15:53:47 +02:00
|
|
|
|
2016-01-28 16:39:05 +01:00
|
|
|
for (int i = 0; i < extraCharacters.length(); ++i) {
|
|
|
|
|
const QChar a = extraCharacters.at(i);
|
2017-05-17 15:53:47 +02:00
|
|
|
const QChar b = manipulator.characterAt(currentPosition + i);
|
2015-07-06 12:05:02 +02:00
|
|
|
if (a == b)
|
|
|
|
|
++extraLength;
|
|
|
|
|
else
|
|
|
|
|
break;
|
|
|
|
|
}
|
2015-08-27 17:12:30 +02:00
|
|
|
|
2016-01-28 16:39:05 +01:00
|
|
|
textToBeInserted += extraCharacters;
|
2015-07-06 12:05:02 +02:00
|
|
|
|
2017-05-17 15:53:47 +02:00
|
|
|
const int length = currentPosition - basePosition + extraLength;
|
2015-08-27 17:12:30 +02:00
|
|
|
|
2016-01-19 14:54:59 +01:00
|
|
|
const bool isReplaced = manipulator.replace(basePosition, length, textToBeInserted);
|
2017-05-17 15:53:47 +02:00
|
|
|
manipulator.setCursorPosition(basePosition + textToBeInserted.length());
|
2016-01-19 14:54:59 +01:00
|
|
|
if (isReplaced) {
|
2015-08-27 17:12:30 +02:00
|
|
|
if (cursorOffset)
|
2016-01-19 14:54:59 +01:00
|
|
|
manipulator.setCursorPosition(manipulator.currentPosition() + cursorOffset);
|
2016-06-10 15:13:38 +02:00
|
|
|
if (setAutoCompleteSkipPos)
|
|
|
|
|
manipulator.setAutoCompleteSkipPosition(manipulator.currentPosition());
|
2016-01-19 14:54:59 +01:00
|
|
|
|
2018-04-04 18:25:23 +02:00
|
|
|
if (ccr.completionKind == CodeCompletion::KeywordCompletionKind)
|
2016-01-19 14:54:59 +01:00
|
|
|
manipulator.autoIndent(basePosition, textToBeInserted.size());
|
2015-07-14 16:08:14 +02:00
|
|
|
}
|
2015-07-06 12:05:02 +02:00
|
|
|
}
|
|
|
|
|
|
2016-01-28 15:35:18 +01:00
|
|
|
void ClangAssistProposalItem::setText(const QString &text)
|
|
|
|
|
{
|
|
|
|
|
m_text = text;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString ClangAssistProposalItem::text() const
|
|
|
|
|
{
|
2018-09-25 16:19:41 +02:00
|
|
|
return m_text;
|
2018-06-15 14:35:58 +02:00
|
|
|
}
|
|
|
|
|
|
2018-07-31 11:44:07 +02:00
|
|
|
const QVector<ClangBackEnd::FixItContainer> &ClangAssistProposalItem::firstCompletionFixIts() const
|
|
|
|
|
{
|
|
|
|
|
return firstCodeCompletion().requiredFixIts;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-25 16:19:41 +02:00
|
|
|
std::pair<int, int> fixItPositionsRange(const FixItContainer &fixIt, const QTextCursor &cursor)
|
|
|
|
|
{
|
|
|
|
|
const QTextBlock startLine = cursor.document()->findBlockByNumber(fixIt.range.start.line - 1);
|
|
|
|
|
const QTextBlock endLine = cursor.document()->findBlockByNumber(fixIt.range.end.line - 1);
|
|
|
|
|
|
|
|
|
|
const int fixItStartPos = ::Utils::Text::positionInText(
|
|
|
|
|
cursor.document(),
|
|
|
|
|
static_cast<int>(fixIt.range.start.line),
|
|
|
|
|
Utils::cppEditorColumn(startLine, static_cast<int>(fixIt.range.start.column)));
|
|
|
|
|
const int fixItEndPos = ::Utils::Text::positionInText(
|
|
|
|
|
cursor.document(),
|
|
|
|
|
static_cast<int>(fixIt.range.end.line),
|
|
|
|
|
Utils::cppEditorColumn(endLine, static_cast<int>(fixIt.range.end.column)));
|
|
|
|
|
return std::make_pair(fixItStartPos, fixItEndPos);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static QString textReplacedByFixit(const FixItContainer &fixIt)
|
|
|
|
|
{
|
|
|
|
|
TextEditorWidget *textEditorWidget = TextEditorWidget::currentTextEditorWidget();
|
|
|
|
|
if (!textEditorWidget)
|
|
|
|
|
return QString();
|
|
|
|
|
const std::pair<int, int> fixItPosRange = fixItPositionsRange(fixIt,
|
|
|
|
|
textEditorWidget->textCursor());
|
|
|
|
|
return textEditorWidget->textAt(fixItPosRange.first,
|
|
|
|
|
fixItPosRange.second - fixItPosRange.first);
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-15 14:35:58 +02:00
|
|
|
QString ClangAssistProposalItem::fixItText() const
|
|
|
|
|
{
|
2018-07-31 11:44:07 +02:00
|
|
|
const FixItContainer &fixIt = firstCompletionFixIts().first();
|
2018-06-15 14:35:58 +02:00
|
|
|
return QCoreApplication::translate("ClangCodeModel::ClangAssistProposalItem",
|
2018-10-24 10:44:21 +02:00
|
|
|
"Requires changing \"%1\" to \"%2\"")
|
2018-10-22 17:28:18 +02:00
|
|
|
.arg(textReplacedByFixit(fixIt), fixIt.text.toString());
|
2018-06-15 14:35:58 +02:00
|
|
|
}
|
|
|
|
|
|
2018-09-25 16:19:41 +02:00
|
|
|
int ClangAssistProposalItem::fixItsShift(const TextDocumentManipulatorInterface &manipulator) const
|
2018-06-15 14:35:58 +02:00
|
|
|
{
|
2018-07-31 11:44:07 +02:00
|
|
|
const QVector<ClangBackEnd::FixItContainer> &requiredFixIts = firstCompletionFixIts();
|
|
|
|
|
if (requiredFixIts.empty())
|
2018-06-15 14:35:58 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
int shift = 0;
|
2018-09-25 16:19:41 +02:00
|
|
|
const QTextCursor cursor = manipulator.textCursorAt(0);
|
2018-07-31 11:44:07 +02:00
|
|
|
for (const FixItContainer &fixIt : requiredFixIts) {
|
2018-09-25 16:19:41 +02:00
|
|
|
const std::pair<int, int> fixItPosRange = fixItPositionsRange(fixIt, cursor);
|
|
|
|
|
shift += fixIt.text.toString().length() - (fixItPosRange.second - fixItPosRange.first);
|
2018-06-15 14:35:58 +02:00
|
|
|
}
|
|
|
|
|
return shift;
|
2016-01-28 15:35:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QIcon ClangAssistProposalItem::icon() const
|
|
|
|
|
{
|
|
|
|
|
using CPlusPlus::Icons;
|
|
|
|
|
static const char SNIPPET_ICON_PATH[] = ":/texteditor/images/snippet.png";
|
|
|
|
|
static const QIcon snippetIcon = QIcon(QLatin1String(SNIPPET_ICON_PATH));
|
|
|
|
|
|
2018-07-31 11:44:07 +02:00
|
|
|
const ClangBackEnd::CodeCompletion &completion = firstCodeCompletion();
|
|
|
|
|
switch (completion.completionKind) {
|
2016-01-28 15:35:18 +01:00
|
|
|
case CodeCompletion::ClassCompletionKind:
|
|
|
|
|
case CodeCompletion::TemplateClassCompletionKind:
|
|
|
|
|
case CodeCompletion::TypeAliasCompletionKind:
|
2018-07-25 10:00:38 +02:00
|
|
|
return ::Utils::CodeModelIcon::iconForType(::Utils::CodeModelIcon::Class);
|
2016-01-28 15:35:18 +01:00
|
|
|
case CodeCompletion::EnumerationCompletionKind:
|
2018-07-25 10:00:38 +02:00
|
|
|
return ::Utils::CodeModelIcon::iconForType(::Utils::CodeModelIcon::Enum);
|
2016-01-28 15:35:18 +01:00
|
|
|
case CodeCompletion::EnumeratorCompletionKind:
|
2018-07-25 10:00:38 +02:00
|
|
|
return ::Utils::CodeModelIcon::iconForType(::Utils::CodeModelIcon::Enumerator);
|
2016-01-28 15:35:18 +01:00
|
|
|
case CodeCompletion::ConstructorCompletionKind:
|
|
|
|
|
case CodeCompletion::DestructorCompletionKind:
|
|
|
|
|
case CodeCompletion::FunctionCompletionKind:
|
2018-07-31 09:48:02 +02:00
|
|
|
case CodeCompletion::FunctionDefinitionCompletionKind:
|
2016-01-28 15:35:18 +01:00
|
|
|
case CodeCompletion::TemplateFunctionCompletionKind:
|
|
|
|
|
case CodeCompletion::ObjCMessageCompletionKind:
|
2018-07-31 11:44:07 +02:00
|
|
|
switch (completion.availability) {
|
2016-01-28 15:35:18 +01:00
|
|
|
case CodeCompletion::Available:
|
|
|
|
|
case CodeCompletion::Deprecated:
|
2018-07-25 10:00:38 +02:00
|
|
|
return ::Utils::CodeModelIcon::iconForType(::Utils::CodeModelIcon::FuncPublic);
|
2016-01-28 15:35:18 +01:00
|
|
|
default:
|
2018-07-25 10:00:38 +02:00
|
|
|
return ::Utils::CodeModelIcon::iconForType(::Utils::CodeModelIcon::FuncPrivate);
|
2016-01-28 15:35:18 +01:00
|
|
|
}
|
|
|
|
|
case CodeCompletion::SignalCompletionKind:
|
2018-07-25 10:00:38 +02:00
|
|
|
return ::Utils::CodeModelIcon::iconForType(::Utils::CodeModelIcon::Signal);
|
2016-01-28 15:35:18 +01:00
|
|
|
case CodeCompletion::SlotCompletionKind:
|
2018-07-31 11:44:07 +02:00
|
|
|
switch (completion.availability) {
|
2016-01-28 15:35:18 +01:00
|
|
|
case CodeCompletion::Available:
|
|
|
|
|
case CodeCompletion::Deprecated:
|
2018-07-25 10:00:38 +02:00
|
|
|
return ::Utils::CodeModelIcon::iconForType(::Utils::CodeModelIcon::SlotPublic);
|
2016-01-28 15:35:18 +01:00
|
|
|
case CodeCompletion::NotAccessible:
|
|
|
|
|
case CodeCompletion::NotAvailable:
|
2018-07-25 10:00:38 +02:00
|
|
|
return ::Utils::CodeModelIcon::iconForType(::Utils::CodeModelIcon::SlotPrivate);
|
2016-01-28 15:35:18 +01:00
|
|
|
}
|
2018-04-04 22:51:01 +03:00
|
|
|
break;
|
2016-01-28 15:35:18 +01:00
|
|
|
case CodeCompletion::NamespaceCompletionKind:
|
2018-07-25 10:00:38 +02:00
|
|
|
return ::Utils::CodeModelIcon::iconForType(::Utils::CodeModelIcon::Namespace);
|
2016-01-28 15:35:18 +01:00
|
|
|
case CodeCompletion::PreProcessorCompletionKind:
|
2018-07-25 10:00:38 +02:00
|
|
|
return ::Utils::CodeModelIcon::iconForType(::Utils::CodeModelIcon::Macro);
|
2016-01-28 15:35:18 +01:00
|
|
|
case CodeCompletion::VariableCompletionKind:
|
2018-07-31 11:44:07 +02:00
|
|
|
switch (completion.availability) {
|
2016-01-28 15:35:18 +01:00
|
|
|
case CodeCompletion::Available:
|
|
|
|
|
case CodeCompletion::Deprecated:
|
2018-07-25 10:00:38 +02:00
|
|
|
return ::Utils::CodeModelIcon::iconForType(::Utils::CodeModelIcon::VarPublic);
|
2016-01-28 15:35:18 +01:00
|
|
|
default:
|
2018-07-25 10:00:38 +02:00
|
|
|
return ::Utils::CodeModelIcon::iconForType(::Utils::CodeModelIcon::VarPrivate);
|
2016-01-28 15:35:18 +01:00
|
|
|
}
|
|
|
|
|
case CodeCompletion::KeywordCompletionKind:
|
2018-07-25 10:00:38 +02:00
|
|
|
return ::Utils::CodeModelIcon::iconForType(::Utils::CodeModelIcon::Keyword);
|
2016-01-28 15:35:18 +01:00
|
|
|
case CodeCompletion::ClangSnippetKind:
|
|
|
|
|
return snippetIcon;
|
|
|
|
|
case CodeCompletion::Other:
|
2018-07-25 10:00:38 +02:00
|
|
|
return ::Utils::CodeModelIcon::iconForType(::Utils::CodeModelIcon::Unknown);
|
2017-04-28 17:46:40 +02:00
|
|
|
default:
|
|
|
|
|
break;
|
2016-01-28 15:35:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return QIcon();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString ClangAssistProposalItem::detail() const
|
|
|
|
|
{
|
2018-07-31 11:44:07 +02:00
|
|
|
QString detail;
|
|
|
|
|
for (const ClangBackEnd::CodeCompletion &codeCompletion : m_codeCompletions) {
|
|
|
|
|
if (!detail.isEmpty())
|
|
|
|
|
detail += "<br>";
|
|
|
|
|
detail += CompletionChunksToTextConverter::convertToToolTipWithHtml(
|
|
|
|
|
codeCompletion.chunks, codeCompletion.completionKind);
|
2018-09-25 16:19:41 +02:00
|
|
|
|
2018-07-31 11:44:07 +02:00
|
|
|
if (!codeCompletion.briefComment.isEmpty())
|
|
|
|
|
detail += "<br>" + codeCompletion.briefComment.toString();
|
|
|
|
|
}
|
2016-01-28 15:35:18 +01:00
|
|
|
|
2018-09-25 16:19:41 +02:00
|
|
|
if (requiresFixIts())
|
|
|
|
|
detail += "<br><br><b>" + fixItText() + "</b>";
|
|
|
|
|
|
2016-01-28 15:35:18 +01:00
|
|
|
return detail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ClangAssistProposalItem::isSnippet() const
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ClangAssistProposalItem::isValid() const
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
quint64 ClangAssistProposalItem::hash() const
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-15 14:35:58 +02:00
|
|
|
bool ClangAssistProposalItem::requiresFixIts() const
|
|
|
|
|
{
|
2018-07-31 11:44:07 +02:00
|
|
|
return !firstCompletionFixIts().empty();
|
2018-06-15 14:35:58 +02:00
|
|
|
}
|
|
|
|
|
|
2017-08-11 12:29:30 +02:00
|
|
|
bool ClangAssistProposalItem::hasOverloadsWithParameters() const
|
2015-07-06 12:05:02 +02:00
|
|
|
{
|
2017-08-11 12:29:30 +02:00
|
|
|
return m_hasOverloadsWithParameters;
|
2015-07-06 12:05:02 +02:00
|
|
|
}
|
|
|
|
|
|
2017-08-11 12:29:30 +02:00
|
|
|
void ClangAssistProposalItem::setHasOverloadsWithParameters(bool hasOverloadsWithParameters)
|
2015-07-06 12:05:02 +02:00
|
|
|
{
|
2017-08-11 12:29:30 +02:00
|
|
|
m_hasOverloadsWithParameters = hasOverloadsWithParameters;
|
2015-07-06 12:05:02 +02:00
|
|
|
}
|
|
|
|
|
|
2017-08-11 12:29:30 +02:00
|
|
|
void ClangAssistProposalItem::keepCompletionOperator(unsigned compOp)
|
2015-07-06 12:05:02 +02:00
|
|
|
{
|
2017-08-11 12:29:30 +02:00
|
|
|
m_completionOperator = compOp;
|
2015-07-06 12:05:02 +02:00
|
|
|
}
|
|
|
|
|
|
2018-07-31 11:44:07 +02:00
|
|
|
void ClangAssistProposalItem::appendCodeCompletion(const CodeCompletion &codeCompletion)
|
2015-07-06 12:05:02 +02:00
|
|
|
{
|
2018-07-31 11:44:07 +02:00
|
|
|
m_codeCompletions.push_back(codeCompletion);
|
2015-07-06 12:05:02 +02:00
|
|
|
}
|
|
|
|
|
|
2018-07-31 11:44:07 +02:00
|
|
|
const ClangBackEnd::CodeCompletion &ClangAssistProposalItem::firstCodeCompletion() const
|
2015-07-06 12:05:02 +02:00
|
|
|
{
|
2018-07-31 11:44:07 +02:00
|
|
|
return m_codeCompletions.at(0);
|
2015-07-06 12:05:02 +02:00
|
|
|
}
|
|
|
|
|
|
2018-08-02 15:15:59 +02:00
|
|
|
void ClangAssistProposalItem::removeFirstCodeCompletion()
|
|
|
|
|
{
|
|
|
|
|
QTC_ASSERT(!m_codeCompletions.empty(), return;);
|
|
|
|
|
m_codeCompletions.erase(m_codeCompletions.begin());
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-06 12:05:02 +02:00
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace ClangCodeModel
|