2012-09-06 22:38:25 +08:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2012-09-06 22:38:25 +08: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.
|
2012-09-06 22:38:25 +08: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.
|
2012-09-06 22:38:25 +08:00
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "keywordscompletionassist.h"
|
|
|
|
|
|
2014-09-04 00:04:18 +02:00
|
|
|
#include <texteditor/codeassist/assistinterface.h>
|
2012-09-06 22:38:25 +08:00
|
|
|
#include <texteditor/codeassist/genericproposal.h>
|
|
|
|
|
#include <texteditor/codeassist/functionhintproposal.h>
|
2014-09-04 00:04:18 +02:00
|
|
|
#include <texteditor/codeassist/genericproposalmodel.h>
|
2013-08-29 16:36:42 +02:00
|
|
|
#include <texteditor/completionsettings.h>
|
|
|
|
|
#include <texteditor/texteditorsettings.h>
|
2014-09-26 09:14:03 +02:00
|
|
|
#include <texteditor/texteditor.h>
|
2012-09-06 22:38:25 +08:00
|
|
|
|
2014-09-03 08:56:24 +02:00
|
|
|
namespace TextEditor {
|
2012-09-06 22:38:25 +08:00
|
|
|
|
|
|
|
|
// --------------------------
|
|
|
|
|
// Keywords
|
|
|
|
|
// --------------------------
|
|
|
|
|
Keywords::Keywords()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Note: variables and functions must be sorted
|
|
|
|
|
Keywords::Keywords(const QStringList &variabels, const QStringList &functions, const QMap<QString, QStringList> &functionArgs)
|
|
|
|
|
: m_variables(variabels), m_functions(functions), m_functionArgs(functionArgs)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Keywords::isVariable(const QString &word) const
|
|
|
|
|
{
|
|
|
|
|
return qBinaryFind(m_variables, word) != m_variables.constEnd();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Keywords::isFunction(const QString &word) const
|
|
|
|
|
{
|
|
|
|
|
return qBinaryFind(m_functions, word) != m_functions.constEnd();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QStringList Keywords::variables() const
|
|
|
|
|
{
|
|
|
|
|
return m_variables;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QStringList Keywords::functions() const
|
|
|
|
|
{
|
|
|
|
|
return m_functions;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QStringList Keywords::argsForFunction(const QString &function) const
|
|
|
|
|
{
|
|
|
|
|
return m_functionArgs.value(function);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// --------------------------
|
|
|
|
|
// KeywordsAssistProposalItem
|
|
|
|
|
// --------------------------
|
2014-09-15 00:12:27 +02:00
|
|
|
KeywordsAssistProposalItem::KeywordsAssistProposalItem(bool isFunction)
|
|
|
|
|
: m_isFunction(isFunction)
|
2012-09-06 22:38:25 +08:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-02 10:58:20 +01:00
|
|
|
KeywordsAssistProposalItem::~KeywordsAssistProposalItem() Q_DECL_NOEXCEPT
|
2012-09-06 22:38:25 +08:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
bool KeywordsAssistProposalItem::prematurelyApplies(const QChar &c) const
|
|
|
|
|
{
|
|
|
|
|
// only '(' in case of a function
|
2014-09-15 00:12:27 +02:00
|
|
|
return c == QLatin1Char('(') && m_isFunction;
|
2012-09-06 22:38:25 +08:00
|
|
|
}
|
|
|
|
|
|
2016-01-19 14:54:59 +01:00
|
|
|
void KeywordsAssistProposalItem::applyContextualContent(TextDocumentManipulatorInterface &manipulator,
|
2012-09-06 22:38:25 +08:00
|
|
|
int basePosition) const
|
|
|
|
|
{
|
2013-09-19 17:59:27 +02:00
|
|
|
const CompletionSettings &settings = TextEditorSettings::completionSettings();
|
2012-09-06 22:38:25 +08:00
|
|
|
|
2016-01-19 14:54:59 +01:00
|
|
|
int replaceLength = manipulator.currentPosition() - basePosition;
|
2012-09-06 22:38:25 +08:00
|
|
|
QString toInsert = text();
|
|
|
|
|
int cursorOffset = 0;
|
2016-01-19 14:54:59 +01:00
|
|
|
const QChar characterAtCurrentPosition = manipulator.characterAt(manipulator.currentPosition());
|
2016-06-10 15:13:38 +02:00
|
|
|
bool setAutoCompleteSkipPosition = false;
|
2016-01-19 14:54:59 +01:00
|
|
|
|
2014-09-15 00:12:27 +02:00
|
|
|
if (m_isFunction && settings.m_autoInsertBrackets) {
|
2012-09-06 22:38:25 +08:00
|
|
|
if (settings.m_spaceAfterFunctionName) {
|
2016-01-19 14:54:59 +01:00
|
|
|
if (manipulator.textAt(manipulator.currentPosition(), 2) == QLatin1String(" (")) {
|
2012-09-06 22:38:25 +08:00
|
|
|
cursorOffset = 2;
|
2016-01-19 14:54:59 +01:00
|
|
|
} else if ( characterAtCurrentPosition == QLatin1Char('(')
|
|
|
|
|
|| characterAtCurrentPosition == QLatin1Char(' ')) {
|
2012-09-06 22:38:25 +08:00
|
|
|
replaceLength += 1;
|
|
|
|
|
toInsert += QLatin1String(" (");
|
|
|
|
|
} else {
|
|
|
|
|
toInsert += QLatin1String(" ()");
|
|
|
|
|
cursorOffset = -1;
|
2016-06-10 15:13:38 +02:00
|
|
|
setAutoCompleteSkipPosition = true;
|
2012-09-06 22:38:25 +08:00
|
|
|
}
|
|
|
|
|
} else {
|
2016-01-19 14:54:59 +01:00
|
|
|
if (characterAtCurrentPosition == QLatin1Char('(')) {
|
2012-09-06 22:38:25 +08:00
|
|
|
cursorOffset = 1;
|
|
|
|
|
} else {
|
|
|
|
|
toInsert += QLatin1String("()");
|
|
|
|
|
cursorOffset = -1;
|
2016-06-10 15:13:38 +02:00
|
|
|
setAutoCompleteSkipPosition = true;
|
2012-09-06 22:38:25 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-19 14:54:59 +01:00
|
|
|
manipulator.replace(basePosition, replaceLength, toInsert);
|
2012-09-06 22:38:25 +08: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 (setAutoCompleteSkipPosition)
|
|
|
|
|
manipulator.setAutoCompleteSkipPosition(manipulator.currentPosition());
|
2012-09-06 22:38:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -------------------------
|
|
|
|
|
// KeywordsFunctionHintModel
|
|
|
|
|
// -------------------------
|
|
|
|
|
KeywordsFunctionHintModel::KeywordsFunctionHintModel(const QStringList &functionSymbols)
|
|
|
|
|
: m_functionSymbols(functionSymbols)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
KeywordsFunctionHintModel::~KeywordsFunctionHintModel()
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
void KeywordsFunctionHintModel::reset()
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
int KeywordsFunctionHintModel::size() const
|
|
|
|
|
{
|
|
|
|
|
return m_functionSymbols.size();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString KeywordsFunctionHintModel::text(int index) const
|
|
|
|
|
{
|
|
|
|
|
return m_functionSymbols.at(index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int KeywordsFunctionHintModel::activeArgument(const QString &prefix) const
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(prefix);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ---------------------------------
|
|
|
|
|
// KeywordsCompletionAssistProcessor
|
|
|
|
|
// ---------------------------------
|
|
|
|
|
KeywordsCompletionAssistProcessor::KeywordsCompletionAssistProcessor(Keywords keywords)
|
|
|
|
|
: m_startPosition(-1)
|
|
|
|
|
, m_variableIcon(QLatin1String(":/codemodel/images/keyword.png"))
|
|
|
|
|
, m_functionIcon(QLatin1String(":/codemodel/images/func.png"))
|
|
|
|
|
, m_keywords(keywords)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
KeywordsCompletionAssistProcessor::~KeywordsCompletionAssistProcessor()
|
|
|
|
|
{}
|
|
|
|
|
|
2014-09-04 00:04:18 +02:00
|
|
|
IAssistProposal *KeywordsCompletionAssistProcessor::perform(const AssistInterface *interface)
|
2012-09-06 22:38:25 +08:00
|
|
|
{
|
|
|
|
|
m_interface.reset(interface);
|
|
|
|
|
|
|
|
|
|
if (isInComment())
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
if (interface->reason() == IdleEditor && !acceptsIdleEditor())
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
if (m_startPosition == -1)
|
|
|
|
|
m_startPosition = findStartOfName();
|
|
|
|
|
|
|
|
|
|
int nextCharPos = m_startPosition + m_word.length();
|
|
|
|
|
if (m_keywords.isFunction(m_word)
|
|
|
|
|
&& m_interface->characterAt(nextCharPos) == QLatin1Char('(')) {
|
|
|
|
|
QStringList functionSymbols = m_keywords.argsForFunction(m_word);
|
|
|
|
|
IFunctionHintProposalModel *model =
|
|
|
|
|
new KeywordsFunctionHintModel(functionSymbols);
|
|
|
|
|
IAssistProposal *proposal = new FunctionHintProposal(m_startPosition, model);
|
|
|
|
|
return proposal;
|
|
|
|
|
} else {
|
2016-02-01 14:51:01 +01:00
|
|
|
QList<AssistProposalItemInterface *> items;
|
2012-09-06 22:38:25 +08:00
|
|
|
addWordsToProposalList(&items, m_keywords.variables(), m_variableIcon);
|
|
|
|
|
addWordsToProposalList(&items, m_keywords.functions(), m_functionIcon);
|
2014-09-13 02:22:24 +02:00
|
|
|
return new GenericProposal(m_startPosition, items);
|
2012-09-06 22:38:25 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QChar KeywordsCompletionAssistProcessor::startOfCommentChar() const
|
|
|
|
|
{
|
|
|
|
|
return QLatin1Char('#');
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-24 21:57:00 +01:00
|
|
|
void KeywordsCompletionAssistProcessor::setKeywords(Keywords keywords)
|
|
|
|
|
{
|
|
|
|
|
m_keywords = keywords;
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-06 22:38:25 +08:00
|
|
|
bool KeywordsCompletionAssistProcessor::acceptsIdleEditor()
|
|
|
|
|
{
|
|
|
|
|
const int pos = m_interface->position();
|
|
|
|
|
QChar characterUnderCursor = m_interface->characterAt(pos);
|
|
|
|
|
if (!characterUnderCursor.isLetterOrNumber()) {
|
|
|
|
|
m_startPosition = findStartOfName();
|
|
|
|
|
if (pos - m_startPosition >= 3 && !isInComment())
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int KeywordsCompletionAssistProcessor::findStartOfName(int pos)
|
|
|
|
|
{
|
|
|
|
|
if (pos == -1)
|
|
|
|
|
pos = m_interface->position();
|
|
|
|
|
|
|
|
|
|
QChar chr = m_interface->characterAt(pos-1);
|
Remove braces for single lines of conditions
#!/usr/bin/env ruby
Dir.glob('**/*.cpp') { |file|
# skip ast (excluding paste, astpath, and canv'ast'imer)
next if file =~ /ast[^eip]|keywords\.|qualifiers|preprocessor|names.cpp/i
s = File.read(file)
next if s.include?('qlalr')
orig = s.dup
s.gsub!(/\n *if [^\n]*{\n[^\n]*\n\s+}(\s+else if [^\n]* {\n[^\n]*\n\s+})*(\s+else {\n[^\n]*\n\s+})?\n/m) { |m|
res = $&
if res =~ /^\s*(\/\/|[A-Z_]{3,})/ # C++ comment or macro (Q_UNUSED, SDEBUG), do not touch braces
res
else
res.gsub!('} else', 'else')
res.gsub!(/\n +} *\n/m, "\n")
res.gsub(/ *{$/, '')
end
}
s.gsub!(/ *$/, '')
File.open(file, 'wb').write(s) if s != orig
}
Change-Id: I3b30ee60df0986f66c02132c65fc38a3fbb6bbdc
Reviewed-by: hjk <qthjk@ovi.com>
2013-01-08 03:32:53 +02:00
|
|
|
if (chr == QLatin1Char('('))
|
2012-09-06 22:38:25 +08:00
|
|
|
--pos;
|
|
|
|
|
// Skip to the start of a name
|
|
|
|
|
do {
|
|
|
|
|
chr = m_interface->characterAt(--pos);
|
|
|
|
|
} while (chr.isLetterOrNumber() || chr == QLatin1Char('_'));
|
|
|
|
|
|
|
|
|
|
int start = ++pos;
|
|
|
|
|
m_word.clear();
|
|
|
|
|
do {
|
|
|
|
|
m_word += m_interface->characterAt(pos);
|
|
|
|
|
chr = m_interface->characterAt(++pos);
|
|
|
|
|
} while ((chr.isLetterOrNumber() || chr == QLatin1Char('_'))
|
|
|
|
|
&& chr != QLatin1Char('('));
|
|
|
|
|
|
|
|
|
|
return start;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool KeywordsCompletionAssistProcessor::isInComment() const
|
|
|
|
|
{
|
|
|
|
|
QTextCursor tc(m_interface->textDocument());
|
|
|
|
|
tc.setPosition(m_interface->position());
|
|
|
|
|
tc.movePosition(QTextCursor::StartOfLine, QTextCursor::KeepAnchor);
|
|
|
|
|
const QString &lineBeginning = tc.selectedText();
|
|
|
|
|
if (lineBeginning.contains(startOfCommentChar()))
|
|
|
|
|
return true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-01 14:51:01 +01:00
|
|
|
void KeywordsCompletionAssistProcessor::addWordsToProposalList(QList<AssistProposalItemInterface *> *items, const QStringList &words, const QIcon &icon)
|
2012-09-06 22:38:25 +08:00
|
|
|
{
|
|
|
|
|
if (!items)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < words.count(); ++i) {
|
2014-09-15 00:12:27 +02:00
|
|
|
AssistProposalItem *item = new KeywordsAssistProposalItem(m_keywords.isFunction(words.at(i)));
|
2012-09-06 22:38:25 +08:00
|
|
|
item->setText(words.at(i));
|
|
|
|
|
item->setIcon(icon);
|
|
|
|
|
items->append(item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-03 08:56:24 +02:00
|
|
|
} // namespace TextEditor
|