1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "profilecompletion.h"
35 #include "profileeditor.h"
36 #include "profilekeywords.h"
37 #include <texteditor/itexteditor.h>
38 #include <texteditor/completionsettings.h>
39 #include <cplusplus/Icons.h>
40 #include <QtCore/QDebug>
42 using namespace Qt4ProjectManager::Internal;
44 ProFileCompletion::ProFileCompletion(QObject *parent) :
45 TextEditor::ICompletionCollector(parent),
48 m_variableIcon(CPlusPlus::Icons().iconForType(CPlusPlus::Icons::VarPublicIconType)),
49 m_functionIcon(CPlusPlus::Icons().iconForType(CPlusPlus::Icons::FuncPublicIconType))
53 ProFileCompletion::~ProFileCompletion()
58 QList<TextEditor::CompletionItem> ProFileCompletion::getCompletions()
60 QList<TextEditor::CompletionItem> completionItems;
61 completions(&completionItems);
63 return completionItems;
66 bool ProFileCompletion::shouldRestartCompletion()
71 TextEditor::ITextEditor *ProFileCompletion::editor() const
76 int ProFileCompletion::startPosition() const
78 return m_startPosition;
81 bool ProFileCompletion::supportsEditor(TextEditor::ITextEditor *editor) const
83 return qobject_cast<ProFileEditor *>(editor) != 0;
86 bool ProFileCompletion::supportsPolicy(TextEditor::CompletionPolicy policy) const
88 return policy == TextEditor::SemanticCompletion;
91 bool ProFileCompletion::triggersCompletion(TextEditor::ITextEditor *editor)
94 const int pos = editor->position();
96 if (completionSettings().m_completionTrigger == TextEditor::AutomaticCompletion) {
97 QChar characterUnderCursor = editor->characterAt(pos);
98 if (!characterUnderCursor.isLetterOrNumber()) {
99 m_startPosition = findStartOfName();
100 if (pos - m_startPosition >= 3 && !isInComment())
107 int ProFileCompletion::findStartOfName(int pos) const
110 pos = m_editor->position();
113 // Skip to the start of a name
115 chr = m_editor->characterAt(--pos);
116 } while (chr.isLetterOrNumber() || chr == QLatin1Char('_'));
121 bool ProFileCompletion::isInComment() const
123 const int beginOfLinePosition = m_editor->position(TextEditor::ITextEditor::StartOfLine);
124 const QString lineBeginning = m_editor->textAt(beginOfLinePosition,
125 m_startPosition - beginOfLinePosition);
126 if (lineBeginning.contains(QLatin1Char('#')))
131 int ProFileCompletion::startCompletion(TextEditor::ITextEditor *editor)
134 m_startPosition = findStartOfName();
136 return m_startPosition;
139 void ProFileCompletion::completions(QList<TextEditor::CompletionItem> *completions)
141 const int length = m_editor->position() - m_startPosition;
148 const QString key = m_editor->textAt(m_startPosition, length);
150 QList<TextEditor::CompletionItem> items;
151 QStringList keywords = ProFileKeywords::variables()
152 + ProFileKeywords::functions();
154 for (int i = 0; i < keywords.count(); i++) {
155 TextEditor::CompletionItem item(this);
156 item.text = keywords[i];
157 item.data = QVariant::fromValue(item.text);
158 item.icon = ProFileKeywords::isFunction(item.text)
159 ? m_functionIcon : m_variableIcon;
163 filter(items, completions, key);
166 bool ProFileCompletion::typedCharCompletes(const TextEditor::CompletionItem &item, QChar typedChar)
168 // only '(' in case of a function
169 if (typedChar == QLatin1Char('(') && ProFileKeywords::isFunction(item.text))
174 void ProFileCompletion::complete(const TextEditor::CompletionItem &item, QChar typedChar)
178 int replaceLength = m_editor->position() - m_startPosition;
179 if (replaceLength < 0)
182 QString toInsert = item.text;
183 int cursorOffset = 0;
184 if (ProFileKeywords::isFunction(toInsert)
185 && completionSettings().m_autoInsertBrackets) {
186 if (completionSettings().m_spaceAfterFunctionName) {
187 if (m_editor->textAt(m_editor->position(), 2) == QLatin1String(" (")) {
189 } else if (m_editor->characterAt(m_editor->position()) == QLatin1Char('(')
190 || m_editor->characterAt(m_editor->position()) == QLatin1Char(' ')) {
192 toInsert += QLatin1String(" (");
194 toInsert += QLatin1String(" ()");
198 if (m_editor->characterAt(m_editor->position()) == QLatin1Char('(')) {
201 toInsert += QLatin1String("()");
207 m_editor->setCursorPosition(m_startPosition);
208 m_editor->replace(replaceLength, toInsert);
210 m_editor->setCursorPosition(m_editor->position() + cursorOffset);
213 bool ProFileCompletion::partiallyComplete(const QList<TextEditor::CompletionItem> &completionItems)
215 if (completionItems.count() == 1) {
216 complete(completionItems.first(), QChar());
220 return TextEditor::ICompletionCollector::partiallyComplete(completionItems);
223 void ProFileCompletion::cleanup()