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 #ifndef COMPLETIONCOLLECTORINTERFACE_H
35 #define COMPLETIONCOLLECTORINTERFACE_H
37 #include "texteditor_global.h"
39 #include <QtCore/QObject>
40 #include <QtCore/QVariant>
41 #include <QtGui/QIcon>
43 namespace TextEditor {
46 class ICompletionCollectorPrivate;
49 class ICompletionCollector;
51 class CompletionSettings;
55 QuickFixCompletion, // Used for "Quick Fix" operation.
56 TextCompletion, // Plain word completion.
57 SemanticCompletion // Completion using code models.
63 CompletionItem(ICompletionCollector *collector = 0)
72 { return collector != 0; }
81 ICompletionCollector *collector;
85 /* Defines the interface to completion collectors. A completion collector tells
86 * the completion support code when a completion is triggered and provides the
87 * list of possible completions. It keeps an internal state so that it can be
88 * polled for the list of completions, which is reset with a call to reset.
90 class TEXTEDITOR_EXPORT ICompletionCollector : public QObject
94 ICompletionCollector(QObject *parent = 0);
95 virtual ~ICompletionCollector();
97 const CompletionSettings &completionSettings() const;
99 virtual QList<CompletionItem> getCompletions();
100 virtual bool shouldRestartCompletion();
102 /* Returns the current active ITextEditor */
103 virtual ITextEditor *editor() const = 0;
104 virtual int startPosition() const = 0;
107 * Returns true if this completion collector can be used with the given editor.
109 virtual bool supportsEditor(ITextEditor *editor) const = 0;
112 * Returns true if this completion collector supports the given completion policy.
114 virtual bool supportsPolicy(CompletionPolicy policy) const = 0;
116 /* This method should return whether the cursor is at a position which could
117 * trigger an autocomplete. It will be called each time a character is typed in
120 virtual bool triggersCompletion(ITextEditor *editor) = 0;
122 // returns starting position
123 virtual int startCompletion(ITextEditor *editor) = 0;
125 /* This method should add all the completions it wants to show into the list,
126 * based on the given cursor position.
128 virtual void completions(QList<CompletionItem> *completions) = 0;
131 * This method should return true when the given typed character should cause
132 * the selected completion item to be completed.
134 virtual bool typedCharCompletes(const CompletionItem &item, QChar typedChar) = 0;
137 * This method should complete the given completion item.
139 * \param typedChar Non-null when completion was triggered by typing a
140 * character. Possible values depend on typedCharCompletes()
142 virtual void complete(const CompletionItem &item, QChar typedChar) = 0;
144 /* This method gives the completion collector a chance to partially complete
145 * based on a set of items. The general use case is to complete the common
146 * prefix shared by all possible completion items.
148 * Returns whether the completion popup should be closed.
150 virtual bool partiallyComplete(const QList<TextEditor::CompletionItem> &completionItems);
152 virtual void sortCompletion(QList<TextEditor::CompletionItem> &completionItems);
154 /* Called when it's safe to clean up the completion items.
156 virtual void cleanup() = 0;
160 void filter(const QList<TextEditor::CompletionItem> &items,
161 QList<TextEditor::CompletionItem> *filteredItems,
165 void setCompletionSettings(const TextEditor::CompletionSettings &);
168 static bool compareChar(const QChar &item, const QChar &other);
169 static bool lessThan(const QString &item, const QString &other);
170 static bool completionItemLessThan(const CompletionItem &item, const CompletionItem &other);
173 Internal::ICompletionCollectorPrivate *m_d;
176 class TEXTEDITOR_EXPORT IQuickFixCollector : public ICompletionCollector
181 IQuickFixCollector(QObject *parent = 0) : ICompletionCollector(parent) {}
182 virtual ~IQuickFixCollector() {}
184 virtual bool typedCharCompletes(const CompletionItem &, QChar)
187 virtual void fix(const TextEditor::CompletionItem &item) = 0;
189 virtual void complete(const CompletionItem &item, QChar typedChar)
195 virtual bool triggersCompletion(TextEditor::ITextEditor *)
198 virtual bool partiallyComplete(const QList<TextEditor::CompletionItem> &)
203 } // namespace TextEditor
205 #endif // COMPLETIONCOLLECTORINTERFACE_H