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"
|
2015-07-14 16:08:14 +02:00
|
|
|
|
2015-07-06 12:05:02 +02:00
|
|
|
#include <cplusplus/MatchingText.h>
|
|
|
|
|
#include <cplusplus/Token.h>
|
|
|
|
|
|
|
|
|
|
#include <texteditor/completionsettings.h>
|
2015-07-14 16:08:14 +02:00
|
|
|
#include <texteditor/textdocument.h>
|
2015-07-06 12:05:02 +02:00
|
|
|
#include <texteditor/texteditor.h>
|
|
|
|
|
#include <texteditor/texteditorsettings.h>
|
|
|
|
|
|
|
|
|
|
using namespace CPlusPlus;
|
|
|
|
|
using namespace ClangBackEnd;
|
|
|
|
|
|
|
|
|
|
namespace ClangCodeModel {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
|
|
|
|
bool ClangAssistProposalItem::prematurelyApplies(const QChar &typedChar) const
|
|
|
|
|
{
|
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)
|
2015-07-06 15:01:46 +02:00
|
|
|
applies = QString::fromLatin1("(,").contains(typedChar);
|
2015-07-06 12:05:02 +02:00
|
|
|
else if (m_completionOperator == T_STRING_LITERAL || m_completionOperator == T_ANGLE_STRING_LITERAL)
|
2015-07-06 15:01:46 +02:00
|
|
|
applies = (typedChar == QLatin1Char('/')) && text().endsWith(QLatin1Char('/'));
|
2015-07-06 14:44:25 +02:00
|
|
|
else if (codeCompletion().completionKind() == CodeCompletion::ObjCMessageCompletionKind)
|
2015-07-06 15:01:46 +02:00
|
|
|
applies = QString::fromLatin1(";.,").contains(typedChar);
|
2015-07-06 12:05:02 +02:00
|
|
|
else
|
2015-07-06 15:01:46 +02:00
|
|
|
applies = QString::fromLatin1(";.,:(").contains(typedChar);
|
2015-07-06 12:05:02 +02:00
|
|
|
|
2015-07-06 15:01:46 +02:00
|
|
|
if (applies)
|
2015-07-06 12:05:02 +02:00
|
|
|
m_typedChar = typedChar;
|
|
|
|
|
|
2015-07-06 15:01:46 +02:00
|
|
|
return applies;
|
2015-07-06 12:05:02 +02:00
|
|
|
}
|
|
|
|
|
|
2015-07-14 16:08:14 +02:00
|
|
|
static bool hasOnlyBlanksBeforeCursorInLine(QTextCursor textCursor)
|
|
|
|
|
{
|
|
|
|
|
textCursor.movePosition(QTextCursor::StartOfLine, QTextCursor::KeepAnchor);
|
|
|
|
|
|
|
|
|
|
const auto textBeforeCursor = textCursor.selectedText();
|
|
|
|
|
|
|
|
|
|
const auto nonSpace = std::find_if(textBeforeCursor.cbegin(),
|
|
|
|
|
textBeforeCursor.cend(),
|
|
|
|
|
[] (const QChar &signBeforeCursor) {
|
|
|
|
|
return !signBeforeCursor.isSpace();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return nonSpace == textBeforeCursor.cend();
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-06 12:05:02 +02:00
|
|
|
void ClangAssistProposalItem::applyContextualContent(TextEditor::TextEditorWidget *editorWidget,
|
|
|
|
|
int basePosition) const
|
|
|
|
|
{
|
2015-07-06 14:44:25 +02:00
|
|
|
const CodeCompletion ccr = codeCompletion();
|
2015-07-06 12:05:02 +02:00
|
|
|
|
2015-08-27 17:12:30 +02:00
|
|
|
QString textToBeInserted = text();
|
2015-07-06 12:05:02 +02:00
|
|
|
QString extraChars;
|
|
|
|
|
int extraLength = 0;
|
|
|
|
|
int cursorOffset = 0;
|
|
|
|
|
|
|
|
|
|
bool autoParenthesesEnabled = true;
|
|
|
|
|
if (m_completionOperator == T_SIGNAL || m_completionOperator == T_SLOT) {
|
|
|
|
|
extraChars += QLatin1Char(')');
|
|
|
|
|
if (m_typedChar == QLatin1Char('(')) // Eat the opening parenthesis
|
|
|
|
|
m_typedChar = QChar();
|
|
|
|
|
} else if (m_completionOperator == T_STRING_LITERAL || m_completionOperator == T_ANGLE_STRING_LITERAL) {
|
2015-08-27 17:12:30 +02:00
|
|
|
if (!textToBeInserted.endsWith(QLatin1Char('/'))) {
|
2015-07-06 12:05:02 +02:00
|
|
|
extraChars += QLatin1Char((m_completionOperator == T_ANGLE_STRING_LITERAL) ? '>' : '"');
|
|
|
|
|
} else {
|
|
|
|
|
if (m_typedChar == QLatin1Char('/')) // Eat the slash
|
|
|
|
|
m_typedChar = QChar();
|
|
|
|
|
}
|
2015-07-14 16:08:14 +02:00
|
|
|
} else if (ccr.completionKind() == CodeCompletion::KeywordCompletionKind) {
|
|
|
|
|
CompletionChunksToTextConverter converter;
|
2016-01-18 09:51:00 +01:00
|
|
|
converter.setupForKeywords();
|
2015-07-14 16:08:14 +02:00
|
|
|
|
|
|
|
|
converter.parseChunks(ccr.chunks());
|
|
|
|
|
|
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();
|
2016-01-18 12:14:10 +01:00
|
|
|
} else if (ccr.completionKind() == CodeCompletion::NamespaceCompletionKind) {
|
|
|
|
|
CompletionChunksToTextConverter converter;
|
|
|
|
|
|
|
|
|
|
converter.parseChunks(ccr.chunks()); // Appends "::" after name space name
|
|
|
|
|
|
|
|
|
|
textToBeInserted = converter.text();
|
2015-07-06 12:05:02 +02:00
|
|
|
} else if (!ccr.text().isEmpty()) {
|
|
|
|
|
const TextEditor::CompletionSettings &completionSettings =
|
|
|
|
|
TextEditor::TextEditorSettings::instance()->completionSettings();
|
|
|
|
|
const bool autoInsertBrackets = completionSettings.m_autoInsertBrackets;
|
|
|
|
|
|
|
|
|
|
if (autoInsertBrackets &&
|
|
|
|
|
(ccr.completionKind() == CodeCompletion::FunctionCompletionKind
|
|
|
|
|
|| ccr.completionKind() == CodeCompletion::DestructorCompletionKind
|
|
|
|
|
|| ccr.completionKind() == CodeCompletion::SignalCompletionKind
|
|
|
|
|
|| ccr.completionKind() == CodeCompletion::SlotCompletionKind)) {
|
|
|
|
|
// 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.
|
|
|
|
|
const bool skipClosingParenthesis = m_typedChar != QLatin1Char('(');
|
|
|
|
|
|
|
|
|
|
if (completionSettings.m_spaceAfterFunctionName)
|
|
|
|
|
extraChars += QLatin1Char(' ');
|
|
|
|
|
extraChars += QLatin1Char('(');
|
|
|
|
|
if (m_typedChar == QLatin1Char('('))
|
|
|
|
|
m_typedChar = QChar();
|
|
|
|
|
|
|
|
|
|
// If the function doesn't return anything, automatically place the semicolon,
|
|
|
|
|
// unless we're doing a scope completion (then it might be function definition).
|
|
|
|
|
const QChar characterAtCursor = editorWidget->characterAt(editorWidget->position());
|
|
|
|
|
bool endWithSemicolon = m_typedChar == QLatin1Char(';')/*
|
|
|
|
|
|| (function->returnType()->isVoidType() && m_completionOperator != T_COLON_COLON)*/; //###
|
|
|
|
|
const QChar semicolon = m_typedChar.isNull() ? QLatin1Char(';') : m_typedChar;
|
|
|
|
|
|
|
|
|
|
if (endWithSemicolon && characterAtCursor == semicolon) {
|
|
|
|
|
endWithSemicolon = false;
|
|
|
|
|
m_typedChar = QChar();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If the function takes no arguments, automatically place the closing parenthesis
|
|
|
|
|
if (!isOverloaded() && !ccr.hasParameters() && skipClosingParenthesis) {
|
|
|
|
|
extraChars += QLatin1Char(')');
|
|
|
|
|
if (endWithSemicolon) {
|
|
|
|
|
extraChars += semicolon;
|
|
|
|
|
m_typedChar = QChar();
|
|
|
|
|
}
|
|
|
|
|
} else if (autoParenthesesEnabled) {
|
|
|
|
|
const QChar lookAhead = editorWidget->characterAt(editorWidget->position() + 1);
|
|
|
|
|
if (MatchingText::shouldInsertMatchingText(lookAhead)) {
|
|
|
|
|
extraChars += QLatin1Char(')');
|
|
|
|
|
--cursorOffset;
|
|
|
|
|
if (endWithSemicolon) {
|
|
|
|
|
extraChars += semicolon;
|
|
|
|
|
--cursorOffset;
|
|
|
|
|
m_typedChar = QChar();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
|
if (autoInsertBrackets && data().canConvert<CompleteFunctionDeclaration>()) {
|
|
|
|
|
if (m_typedChar == QLatin1Char('('))
|
|
|
|
|
m_typedChar = QChar();
|
|
|
|
|
|
|
|
|
|
// everything from the closing parenthesis on are extra chars, to
|
|
|
|
|
// make sure an auto-inserted ")" gets replaced by ") const" if necessary
|
|
|
|
|
int closingParen = toInsert.lastIndexOf(QLatin1Char(')'));
|
|
|
|
|
extraChars = toInsert.mid(closingParen);
|
|
|
|
|
toInsert.truncate(closingParen);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Append an unhandled typed character, adjusting cursor offset when it had been adjusted before
|
|
|
|
|
if (!m_typedChar.isNull()) {
|
|
|
|
|
extraChars += m_typedChar;
|
|
|
|
|
if (cursorOffset != 0)
|
|
|
|
|
--cursorOffset;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Avoid inserting characters that are already there
|
|
|
|
|
const int endsPosition = editorWidget->position(TextEditor::EndOfLinePosition);
|
|
|
|
|
const QString existingText = editorWidget->textAt(editorWidget->position(), endsPosition - editorWidget->position());
|
|
|
|
|
int existLength = 0;
|
2015-07-14 16:08:14 +02:00
|
|
|
if (!existingText.isEmpty() && ccr.completionKind() != CodeCompletion::KeywordCompletionKind) {
|
2015-07-06 12:05:02 +02:00
|
|
|
// Calculate the exist length in front of the extra chars
|
2015-08-27 17:12:30 +02:00
|
|
|
existLength = textToBeInserted.length() - (editorWidget->position() - basePosition);
|
|
|
|
|
while (!existingText.startsWith(textToBeInserted.right(existLength))) {
|
2015-07-06 12:05:02 +02:00
|
|
|
if (--existLength == 0)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (int i = 0; i < extraChars.length(); ++i) {
|
|
|
|
|
const QChar a = extraChars.at(i);
|
|
|
|
|
const QChar b = editorWidget->characterAt(editorWidget->position() + i + existLength);
|
|
|
|
|
if (a == b)
|
|
|
|
|
++extraLength;
|
|
|
|
|
else
|
|
|
|
|
break;
|
|
|
|
|
}
|
2015-08-27 17:12:30 +02:00
|
|
|
|
|
|
|
|
textToBeInserted += extraChars;
|
2015-07-06 12:05:02 +02:00
|
|
|
|
|
|
|
|
// Insert the remainder of the name
|
|
|
|
|
const int length = editorWidget->position() - basePosition + existLength + extraLength;
|
2015-08-27 17:12:30 +02:00
|
|
|
const auto textToBeReplaced = editorWidget->textAt(basePosition, length);
|
|
|
|
|
|
|
|
|
|
if (textToBeReplaced != textToBeInserted) {
|
|
|
|
|
editorWidget->setCursorPosition(basePosition);
|
|
|
|
|
editorWidget->replace(length, textToBeInserted);
|
|
|
|
|
if (cursorOffset)
|
|
|
|
|
editorWidget->setCursorPosition(editorWidget->position() + cursorOffset);
|
|
|
|
|
|
|
|
|
|
// indent the statement
|
|
|
|
|
if (ccr.completionKind() == CodeCompletion::KeywordCompletionKind) {
|
|
|
|
|
auto selectionCursor = editorWidget->textCursor();
|
|
|
|
|
selectionCursor.setPosition(basePosition);
|
|
|
|
|
selectionCursor.setPosition(basePosition + textToBeInserted.size(), QTextCursor::KeepAnchor);
|
|
|
|
|
|
|
|
|
|
auto basePositionCursor = editorWidget->textCursor();
|
|
|
|
|
basePositionCursor.setPosition(basePosition);
|
|
|
|
|
if (hasOnlyBlanksBeforeCursorInLine(basePositionCursor))
|
|
|
|
|
editorWidget->textDocument()->autoIndent(selectionCursor);
|
|
|
|
|
}
|
2015-07-14 16:08:14 +02:00
|
|
|
}
|
2015-07-06 12:05:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ClangAssistProposalItem::keepCompletionOperator(unsigned compOp)
|
|
|
|
|
{
|
|
|
|
|
m_completionOperator = compOp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ClangAssistProposalItem::isOverloaded() const
|
|
|
|
|
{
|
|
|
|
|
return !m_overloads.isEmpty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ClangAssistProposalItem::addOverload(const CodeCompletion &ccr)
|
|
|
|
|
{
|
|
|
|
|
m_overloads.append(ccr);
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
|