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"
|
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>
|
|
|
|
|
#include <texteditor/texteditorsettings.h>
|
|
|
|
|
|
2018-06-15 14:35:58 +02:00
|
|
|
#include <QCoreApplication>
|
2017-08-11 10:35:57 +02:00
|
|
|
#include <QTextCursor>
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
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-04-04 18:25:23 +02:00
|
|
|
else if (codeCompletion().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
|
|
|
}
|
|
|
|
|
|
2017-08-01 10:55:27 +02:00
|
|
|
static void moveToPrevChar(TextEditor::TextDocumentManipulatorInterface &manipulator,
|
|
|
|
|
QTextCursor &cursor)
|
|
|
|
|
{
|
|
|
|
|
cursor.movePosition(QTextCursor::PreviousCharacter);
|
|
|
|
|
while (manipulator.characterAt(cursor.position()).isSpace())
|
|
|
|
|
cursor.movePosition(QTextCursor::PreviousCharacter);
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-10 16:28:19 +02:00
|
|
|
static QString textUntilPreviousStatement(TextEditor::TextDocumentManipulatorInterface &manipulator,
|
|
|
|
|
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 ;
|
|
|
|
|
static bool isAtUsingDeclaration(TextEditor::TextDocumentManipulatorInterface &manipulator,
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
return Utils::contains(tokens, [](const Token &token) {
|
|
|
|
|
return token.kind() == T_USING;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-19 14:54:59 +01:00
|
|
|
void ClangAssistProposalItem::apply(TextEditor::TextDocumentManipulatorInterface &manipulator,
|
2016-01-28 15:35:18 +01:00
|
|
|
int basePosition) const
|
2015-07-06 12:05:02 +02:00
|
|
|
{
|
2015-07-06 14:44:25 +02:00
|
|
|
const CodeCompletion ccr = codeCompletion();
|
2015-07-06 12:05:02 +02:00
|
|
|
|
2018-06-15 14:35:58 +02:00
|
|
|
if (!ccr.requiredFixIts.empty()) {
|
|
|
|
|
ClangFixItOperation fixItOperation(Utf8String(), ccr.requiredFixIts);
|
|
|
|
|
fixItOperation.perform();
|
|
|
|
|
|
|
|
|
|
const int shift = fixItsShift(manipulator);
|
|
|
|
|
basePosition += shift;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
2015-07-14 16:08:14 +02:00
|
|
|
|
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();
|
2015-07-14 16:08:14 +02:00
|
|
|
if (converter.hasPlaceholderPositions())
|
|
|
|
|
cursorOffset = converter.placeholderPositions().at(0) - converter.text().size();
|
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-04-04 18:25:23 +02:00
|
|
|
} else if (!ccr.text.isEmpty()) {
|
2015-07-06 12:05:02 +02:00
|
|
|
const TextEditor::CompletionSettings &completionSettings =
|
|
|
|
|
TextEditor::TextEditorSettings::instance()->completionSettings();
|
|
|
|
|
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
|
|
|
|
|
|| 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);
|
|
|
|
|
cursor.movePosition(QTextCursor::PreviousWord);
|
|
|
|
|
while (manipulator.characterAt(cursor.position()) == ':')
|
|
|
|
|
cursor.movePosition(QTextCursor::PreviousWord, QTextCursor::MoveAnchor, 2);
|
2017-08-01 10:55:27 +02:00
|
|
|
|
2018-07-31 09:48:02 +02:00
|
|
|
const int previousWordStart = cursor.position();
|
2017-08-01 10:55:27 +02:00
|
|
|
// Move to the last character in the previous word
|
|
|
|
|
cursor.movePosition(QTextCursor::NextWord);
|
|
|
|
|
moveToPrevChar(manipulator, cursor);
|
2018-07-31 09:48:02 +02:00
|
|
|
const QString previousWord = manipulator.textAt(previousWordStart,
|
|
|
|
|
cursor.position() - previousWordStart + 1);
|
|
|
|
|
|
2017-08-01 10:55:27 +02:00
|
|
|
bool abandonParen = false;
|
2018-07-31 09:48:02 +02:00
|
|
|
if (previousWord == "&") {
|
2017-08-01 10:55:27 +02:00
|
|
|
moveToPrevChar(manipulator, cursor);
|
|
|
|
|
const QChar prevChar = manipulator.characterAt(cursor.position());
|
|
|
|
|
abandonParen = QString("(;,{}").contains(prevChar);
|
|
|
|
|
}
|
2017-08-10 16:28:19 +02:00
|
|
|
if (!abandonParen)
|
|
|
|
|
abandonParen = isAtUsingDeclaration(manipulator, basePosition);
|
2018-07-31 09:48:02 +02:00
|
|
|
if (!abandonParen && ccr.completionKind == CodeCompletion::FunctionDefinitionCompletionKind) {
|
|
|
|
|
const CodeCompletionChunk resultType = ccr.chunks.first();
|
|
|
|
|
QTC_ASSERT(resultType.kind == CodeCompletionChunk::ResultType, return;);
|
|
|
|
|
if (previousWord == resultType.text.toString()) {
|
|
|
|
|
bool skipChunks = true;
|
|
|
|
|
for (const CodeCompletionChunk &chunk : ccr.chunks) {
|
|
|
|
|
if (chunk.kind == CodeCompletionChunk::TypedText) {
|
|
|
|
|
skipChunks = false;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (skipChunks)
|
|
|
|
|
continue;
|
|
|
|
|
extraCharacters += chunk.text;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// To skip the next block.
|
|
|
|
|
abandonParen = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
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-06-15 14:35:58 +02:00
|
|
|
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;
|
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-04-04 18:25:23 +02:00
|
|
|
switch (m_codeCompletion.completionKind) {
|
2016-01-28 15:35:18 +01:00
|
|
|
case CodeCompletion::ClassCompletionKind:
|
|
|
|
|
case CodeCompletion::TemplateClassCompletionKind:
|
|
|
|
|
case CodeCompletion::TypeAliasCompletionKind:
|
2016-04-06 10:08:01 +02:00
|
|
|
return Icons::iconForType(Icons::ClassIconType);
|
2016-01-28 15:35:18 +01:00
|
|
|
case CodeCompletion::EnumerationCompletionKind:
|
2016-04-06 10:08:01 +02:00
|
|
|
return Icons::iconForType(Icons::EnumIconType);
|
2016-01-28 15:35:18 +01:00
|
|
|
case CodeCompletion::EnumeratorCompletionKind:
|
2016-04-06 10:08:01 +02:00
|
|
|
return Icons::iconForType(Icons::EnumeratorIconType);
|
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-04-04 18:25:23 +02:00
|
|
|
switch (m_codeCompletion.availability) {
|
2016-01-28 15:35:18 +01:00
|
|
|
case CodeCompletion::Available:
|
|
|
|
|
case CodeCompletion::Deprecated:
|
2016-04-06 10:08:01 +02:00
|
|
|
return Icons::iconForType(Icons::FuncPublicIconType);
|
2016-01-28 15:35:18 +01:00
|
|
|
default:
|
2016-04-06 10:08:01 +02:00
|
|
|
return Icons::iconForType(Icons::FuncPrivateIconType);
|
2016-01-28 15:35:18 +01:00
|
|
|
}
|
|
|
|
|
case CodeCompletion::SignalCompletionKind:
|
2016-04-06 10:08:01 +02:00
|
|
|
return Icons::iconForType(Icons::SignalIconType);
|
2016-01-28 15:35:18 +01:00
|
|
|
case CodeCompletion::SlotCompletionKind:
|
2018-04-04 18:25:23 +02:00
|
|
|
switch (m_codeCompletion.availability) {
|
2016-01-28 15:35:18 +01:00
|
|
|
case CodeCompletion::Available:
|
|
|
|
|
case CodeCompletion::Deprecated:
|
2016-04-06 10:08:01 +02:00
|
|
|
return Icons::iconForType(Icons::SlotPublicIconType);
|
2016-01-28 15:35:18 +01:00
|
|
|
case CodeCompletion::NotAccessible:
|
|
|
|
|
case CodeCompletion::NotAvailable:
|
2016-04-06 10:08:01 +02:00
|
|
|
return Icons::iconForType(Icons::SlotPrivateIconType);
|
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:
|
2016-04-06 10:08:01 +02:00
|
|
|
return Icons::iconForType(Icons::NamespaceIconType);
|
2016-01-28 15:35:18 +01:00
|
|
|
case CodeCompletion::PreProcessorCompletionKind:
|
2016-04-06 10:08:01 +02:00
|
|
|
return Icons::iconForType(Icons::MacroIconType);
|
2016-01-28 15:35:18 +01:00
|
|
|
case CodeCompletion::VariableCompletionKind:
|
2018-04-04 18:25:23 +02:00
|
|
|
switch (m_codeCompletion.availability) {
|
2016-01-28 15:35:18 +01:00
|
|
|
case CodeCompletion::Available:
|
|
|
|
|
case CodeCompletion::Deprecated:
|
2016-04-06 10:08:01 +02:00
|
|
|
return Icons::iconForType(Icons::VarPublicIconType);
|
2016-01-28 15:35:18 +01:00
|
|
|
default:
|
2016-04-06 10:08:01 +02:00
|
|
|
return Icons::iconForType(Icons::VarPrivateIconType);
|
2016-01-28 15:35:18 +01:00
|
|
|
}
|
|
|
|
|
case CodeCompletion::KeywordCompletionKind:
|
2016-04-06 10:08:01 +02:00
|
|
|
return Icons::iconForType(Icons::KeywordIconType);
|
2016-01-28 15:35:18 +01:00
|
|
|
case CodeCompletion::ClangSnippetKind:
|
|
|
|
|
return snippetIcon;
|
|
|
|
|
case CodeCompletion::Other:
|
2016-04-06 10:08:01 +02:00
|
|
|
return Icons::iconForType(Icons::UnknownIconType);
|
2017-04-28 17:46:40 +02:00
|
|
|
default:
|
|
|
|
|
break;
|
2016-01-28 15:35:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return QIcon();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString ClangAssistProposalItem::detail() const
|
|
|
|
|
{
|
2017-06-12 11:32:16 +02:00
|
|
|
QString detail = CompletionChunksToTextConverter::convertToToolTipWithHtml(
|
2018-04-04 18:25:23 +02:00
|
|
|
m_codeCompletion.chunks, m_codeCompletion.completionKind);
|
2016-01-28 15:35:18 +01:00
|
|
|
|
2018-04-04 18:25:23 +02:00
|
|
|
if (!m_codeCompletion.briefComment.isEmpty())
|
|
|
|
|
detail += QStringLiteral("\n\n") + m_codeCompletion.briefComment.toString();
|
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
|
|
|
|
|
{
|
|
|
|
|
return !m_codeCompletion.requiredFixIts.empty();
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2015-07-15 17:01:50 +02:00
|
|
|
void ClangAssistProposalItem::setCodeCompletion(const CodeCompletion &codeCompletion)
|
2015-07-06 12:05:02 +02:00
|
|
|
{
|
2015-07-15 17:01:50 +02:00
|
|
|
m_codeCompletion = codeCompletion;
|
2015-07-06 12:05:02 +02:00
|
|
|
}
|
|
|
|
|
|
2015-07-15 17:01:50 +02:00
|
|
|
const ClangBackEnd::CodeCompletion &ClangAssistProposalItem::codeCompletion() const
|
2015-07-06 12:05:02 +02:00
|
|
|
{
|
2015-07-15 17:01:50 +02:00
|
|
|
return m_codeCompletion;
|
2015-07-06 12:05:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace ClangCodeModel
|