OSDN Git Service

8c76f3f3198a07dcfec3b52d68ab8a88ae6f6a9e
[qt-creator-jp/qt-creator-jp.git] / src / plugins / texteditor / quickfix.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
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
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
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.
24 **
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.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "quickfix.h"
35 #include "basetexteditor.h"
36
37 #include <coreplugin/ifile.h>
38 #include <extensionsystem/pluginmanager.h>
39 #include <QtGui/QApplication>
40 #include <QtGui/QTextBlock>
41
42 #include <QtCore/QDebug>
43
44 using namespace TextEditor;
45
46 QuickFixState::QuickFixState(TextEditor::BaseTextEditorWidget *editor)
47     : _editor(editor)
48 {
49 }
50
51 QuickFixState::~QuickFixState()
52 {
53 }
54
55 TextEditor::BaseTextEditorWidget *QuickFixState::editor() const
56 {
57     return _editor;
58 }
59
60
61 QuickFixOperation::QuickFixOperation(int priority)
62 {
63     setPriority(priority);
64 }
65
66 QuickFixOperation::~QuickFixOperation()
67 {
68 }
69
70 int QuickFixOperation::priority() const
71 {
72     return _priority;
73 }
74
75 void QuickFixOperation::setPriority(int priority)
76 {
77     _priority = priority;
78 }
79
80 QString QuickFixOperation::description() const
81 {
82     return _description;
83 }
84
85 void QuickFixOperation::setDescription(const QString &description)
86 {
87     _description = description;
88 }
89
90
91
92 QuickFixFactory::QuickFixFactory(QObject *parent)
93     : QObject(parent)
94 {
95 }
96
97 QuickFixFactory::~QuickFixFactory()
98 {
99 }
100
101 QuickFixCollector::QuickFixCollector()
102     : m_editor(0)
103 {
104 }
105
106 QuickFixCollector::~QuickFixCollector()
107 {
108 }
109
110 TextEditor::ITextEditor *QuickFixCollector::editor() const
111 {
112     return m_editor;
113 }
114
115 int QuickFixCollector::startPosition() const
116 {
117     return m_editor->position();
118 }
119
120 bool QuickFixCollector::triggersCompletion(TextEditor::ITextEditor *)
121 {
122     return false;
123 }
124
125 int QuickFixCollector::startCompletion(TextEditor::ITextEditor *editable)
126 {
127     Q_ASSERT(editable != 0);
128
129     m_editor = editable;
130     TextEditor::BaseTextEditorWidget *editor = qobject_cast<TextEditor::BaseTextEditorWidget *>(editable->widget());
131     Q_ASSERT(editor != 0);
132
133     if (TextEditor::QuickFixState *state = initializeCompletion(editor)) {
134         QMap<int, QList<QuickFixOperation::Ptr> > matchedOps;
135
136         foreach (QuickFixFactory *factory, quickFixFactories()) {
137             QList<QuickFixOperation::Ptr> ops = factory->matchingOperations(state);
138
139             foreach (QuickFixOperation::Ptr op, ops) {
140                 const int priority = op->priority();
141                 if (priority != -1)
142                     matchedOps[priority].append(op);
143             }
144         }
145
146         QMapIterator<int, QList<TextEditor::QuickFixOperation::Ptr> > it(matchedOps);
147         it.toBack();
148         if (it.hasPrevious()) {
149             it.previous();
150             m_quickFixes = it.value();
151         }
152
153         delete state;
154
155         if (! m_quickFixes.isEmpty())
156             return editable->position();
157     }
158
159     return -1;
160 }
161
162 void QuickFixCollector::completions(QList<TextEditor::CompletionItem> *quickFixItems)
163 {
164     for (int i = 0; i < m_quickFixes.size(); ++i) {
165         TextEditor::QuickFixOperation::Ptr op = m_quickFixes.at(i);
166
167         TextEditor::CompletionItem item(this);
168         item.text = op->description();
169         item.data = QVariant::fromValue(i);
170         quickFixItems->append(item);
171     }
172 }
173
174 void QuickFixCollector::fix(const TextEditor::CompletionItem &item)
175 {
176     const int index = item.data.toInt();
177
178     if (index < m_quickFixes.size()) {
179         TextEditor::QuickFixOperation::Ptr quickFix = m_quickFixes.at(index);
180         quickFix->perform();
181     }
182 }
183
184 void QuickFixCollector::cleanup()
185 {
186     m_quickFixes.clear();
187 }