OSDN Git Service

e5cddc71c1398f144a4cf8aa90f7804e3ac62f81
[qt-creator-jp/qt-creator-jp.git] / src / plugins / texteditor / autocompleter.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 "autocompleter.h"
35 #include "basetextdocumentlayout.h"
36 #include "texteditorsettings.h"
37 #include "tabsettings.h"
38
39 #include <QtCore/QDebug>
40 #include <QtGui/QTextCursor>
41
42 using namespace TextEditor;
43
44 AutoCompleter::AutoCompleter() :
45     m_allowSkippingOfBlockEnd(false),
46     m_surroundWithEnabled(true),
47     m_autoParenthesesEnabled(true)
48 {}
49
50 AutoCompleter::~AutoCompleter()
51 {}
52
53 void AutoCompleter::setAutoParenthesesEnabled(bool b)
54 {
55     m_autoParenthesesEnabled = b;
56 }
57
58 bool AutoCompleter::isAutoParenthesesEnabled() const
59 {
60     return m_autoParenthesesEnabled;
61 }
62
63 void AutoCompleter::setSurroundWithEnabled(bool b)
64 {
65     m_surroundWithEnabled = b;
66 }
67
68 bool AutoCompleter::isSurroundWithEnabled() const
69 {
70     return m_surroundWithEnabled;
71 }
72
73 void AutoCompleter::countBracket(QChar open, QChar close, QChar c, int *errors, int *stillopen)
74 {
75     if (c == open)
76         ++*stillopen;
77     else if (c == close)
78         --*stillopen;
79
80     if (*stillopen < 0) {
81         *errors += -1 * (*stillopen);
82         *stillopen = 0;
83     }
84 }
85
86 void AutoCompleter::countBrackets(QTextCursor cursor,
87                                   int from,
88                                   int end,
89                                   QChar open,
90                                   QChar close,
91                                   int *errors,
92                                   int *stillopen)
93 {
94     cursor.setPosition(from);
95     QTextBlock block = cursor.block();
96     while (block.isValid() && block.position() < end) {
97         TextEditor::Parentheses parenList = TextEditor::BaseTextDocumentLayout::parentheses(block);
98         if (!parenList.isEmpty() && !TextEditor::BaseTextDocumentLayout::ifdefedOut(block)) {
99             for (int i = 0; i < parenList.count(); ++i) {
100                 TextEditor::Parenthesis paren = parenList.at(i);
101                 int position = block.position() + paren.pos;
102                 if (position < from || position >= end)
103                     continue;
104                 countBracket(open, close, paren.chr, errors, stillopen);
105             }
106         }
107         block = block.next();
108     }
109 }
110
111 QString AutoCompleter::autoComplete(QTextCursor &cursor, const QString &textToInsert) const
112 {
113     const bool checkBlockEnd = m_allowSkippingOfBlockEnd;
114     m_allowSkippingOfBlockEnd = false; // consume blockEnd.
115
116     if (m_surroundWithEnabled && cursor.hasSelection()) {
117         if (textToInsert == QLatin1String("("))
118             return cursor.selectedText() + QLatin1String(")");
119         if (textToInsert == QLatin1String("{")) {
120             //If the text span multiple lines, insert on different lines
121             QString str = cursor.selectedText();
122             if (str.contains(QChar::ParagraphSeparator)) {
123                 //Also, try to simulate auto-indent
124                 str = (str.startsWith(QChar::ParagraphSeparator) ? QString() : QString(QChar::ParagraphSeparator)) +
125                       str;
126                 if (str.endsWith(QChar::ParagraphSeparator))
127                     str += QLatin1String("}") + QString(QChar::ParagraphSeparator);
128                 else
129                     str += QString(QChar::ParagraphSeparator) + QLatin1String("}");
130             }
131             else {
132                 str += QLatin1String("}");
133             }
134             return str;
135         }
136         if (textToInsert == QLatin1String("["))
137             return cursor.selectedText() + QLatin1String("]");
138         if (textToInsert == QLatin1String("\""))
139             return cursor.selectedText() + QLatin1String("\"");
140         if (textToInsert == QLatin1String("'"))
141             return cursor.selectedText() + QLatin1String("'");
142     }
143
144     if (!m_autoParenthesesEnabled)
145         return QString();
146
147     if (!contextAllowsAutoParentheses(cursor, textToInsert))
148         return QString();
149
150     QTextDocument *doc = cursor.document();
151     const QString text = textToInsert;
152     const QChar lookAhead = doc->characterAt(cursor.selectionEnd());
153
154     const QChar character = textToInsert.at(0);
155     const QString parentheses = QLatin1String("()");
156     const QString brackets = QLatin1String("[]");
157     if (parentheses.contains(character) || brackets.contains(character)) {
158         QTextCursor tmp= cursor;
159         bool foundBlockStart = TextEditor::TextBlockUserData::findPreviousBlockOpenParenthesis(&tmp);
160         int blockStart = foundBlockStart ? tmp.position() : 0;
161         tmp = cursor;
162         bool foundBlockEnd = TextEditor::TextBlockUserData::findNextBlockClosingParenthesis(&tmp);
163         int blockEnd = foundBlockEnd ? tmp.position() : (cursor.document()->characterCount() - 1);
164         const QChar openChar = parentheses.contains(character) ? QLatin1Char('(') : QLatin1Char('[');
165         const QChar closeChar = parentheses.contains(character) ? QLatin1Char(')') : QLatin1Char(']');
166
167         int errors = 0;
168         int stillopen = 0;
169         countBrackets(cursor, blockStart, blockEnd, openChar, closeChar, &errors, &stillopen);
170         int errorsBeforeInsertion = errors + stillopen;
171         errors = 0;
172         stillopen = 0;
173         countBrackets(cursor, blockStart, cursor.position(), openChar, closeChar, &errors, &stillopen);
174         countBracket(openChar, closeChar, character, &errors, &stillopen);
175         countBrackets(cursor, cursor.position(), blockEnd, openChar, closeChar, &errors, &stillopen);
176         int errorsAfterInsertion = errors + stillopen;
177         if (errorsAfterInsertion < errorsBeforeInsertion)
178             return QString(); // insertion fixes parentheses or bracket errors, do not auto complete
179     }
180
181     int skippedChars = 0;
182     const QString autoText = insertMatchingBrace(cursor, text, lookAhead, &skippedChars);
183
184     if (checkBlockEnd && textToInsert.at(0) == QLatin1Char('}')) {
185         if (textToInsert.length() > 1)
186             qWarning() << "*** handle event compression";
187
188         int startPos = cursor.selectionEnd(), pos = startPos;
189         while (doc->characterAt(pos).isSpace())
190             ++pos;
191
192         if (doc->characterAt(pos) == QLatin1Char('}'))
193             skippedChars += (pos - startPos) + 1;
194     }
195
196     if (skippedChars) {
197         const int pos = cursor.position();
198         cursor.setPosition(pos + skippedChars);
199         cursor.setPosition(pos, QTextCursor::KeepAnchor);
200     }
201
202     return autoText;
203 }
204
205 bool AutoCompleter::autoBackspace(QTextCursor &cursor)
206 {
207     m_allowSkippingOfBlockEnd = false;
208
209     if (!m_autoParenthesesEnabled)
210         return false;
211
212     int pos = cursor.position();
213     if (pos == 0)
214         return false;
215     QTextCursor c = cursor;
216     c.setPosition(pos - 1);
217
218     QTextDocument *doc = cursor.document();
219     const QChar lookAhead = doc->characterAt(pos);
220     const QChar lookBehind = doc->characterAt(pos - 1);
221     const QChar lookFurtherBehind = doc->characterAt(pos - 2);
222
223     const QChar character = lookBehind;
224     if (character == QLatin1Char('(') || character == QLatin1Char('[')) {
225         QTextCursor tmp = cursor;
226         TextEditor::TextBlockUserData::findPreviousBlockOpenParenthesis(&tmp);
227         int blockStart = tmp.isNull() ? 0 : tmp.position();
228         tmp = cursor;
229         TextEditor::TextBlockUserData::findNextBlockClosingParenthesis(&tmp);
230         int blockEnd = tmp.isNull() ? (cursor.document()->characterCount()-1) : tmp.position();
231         QChar openChar = character;
232         QChar closeChar = (character == QLatin1Char('(')) ? QLatin1Char(')') : QLatin1Char(']');
233
234         int errors = 0;
235         int stillopen = 0;
236         countBrackets(cursor, blockStart, blockEnd, openChar, closeChar, &errors, &stillopen);
237         int errorsBeforeDeletion = errors + stillopen;
238         errors = 0;
239         stillopen = 0;
240         countBrackets(cursor, blockStart, pos - 1, openChar, closeChar, &errors, &stillopen);
241         countBrackets(cursor, pos, blockEnd, openChar, closeChar, &errors, &stillopen);
242         int errorsAfterDeletion = errors + stillopen;
243
244         if (errorsAfterDeletion < errorsBeforeDeletion)
245             return false; // insertion fixes parentheses or bracket errors, do not auto complete
246     }
247
248     // ### this code needs to be generalized
249     if    ((lookBehind == QLatin1Char('(') && lookAhead == QLatin1Char(')'))
250         || (lookBehind == QLatin1Char('[') && lookAhead == QLatin1Char(']'))
251         || (lookBehind == QLatin1Char('"') && lookAhead == QLatin1Char('"')
252             && lookFurtherBehind != QLatin1Char('\\'))
253         || (lookBehind == QLatin1Char('\'') && lookAhead == QLatin1Char('\'')
254             && lookFurtherBehind != QLatin1Char('\\'))) {
255         if (! isInComment(c)) {
256             cursor.beginEditBlock();
257             cursor.deleteChar();
258             cursor.deletePreviousChar();
259             cursor.endEditBlock();
260             return true;
261         }
262     }
263     return false;
264 }
265
266 int AutoCompleter::paragraphSeparatorAboutToBeInserted(QTextCursor &cursor,
267                                                        const TabSettings &tabSettings)
268 {
269     if (!m_autoParenthesesEnabled)
270         return 0;
271
272     QTextDocument *doc = cursor.document();
273     if (doc->characterAt(cursor.position() - 1) != QLatin1Char('{'))
274         return 0;
275
276     if (!contextAllowsAutoParentheses(cursor))
277         return 0;
278
279     // verify that we indeed do have an extra opening brace in the document
280     int braceDepth = BaseTextDocumentLayout::braceDepth(doc->lastBlock());
281
282     if (braceDepth <= 0)
283         return 0; // braces are all balanced or worse, no need to do anything
284
285     // we have an extra brace , let's see if we should close it
286
287     /* verify that the next block is not further intended compared to the current block.
288        This covers the following case:
289
290             if (condition) {|
291                 statement;
292     */
293     QTextBlock block = cursor.block();
294     int indentation = tabSettings.indentationColumn(block.text());
295
296     if (block.next().isValid()) { // not the last block
297         block = block.next();
298         //skip all empty blocks
299         while (block.isValid() && tabSettings.onlySpace(block.text()))
300             block = block.next();
301         if (block.isValid()
302                 && tabSettings.indentationColumn(block.text()) > indentation)
303             return 0;
304     }
305
306     const QString &textToInsert = insertParagraphSeparator(cursor);
307     int pos = cursor.position();
308     cursor.insertBlock();
309     cursor.insertText(textToInsert);
310     cursor.setPosition(pos);
311
312     m_allowSkippingOfBlockEnd = true;
313
314     return 1;
315 }
316
317 bool AutoCompleter::contextAllowsAutoParentheses(const QTextCursor &cursor,
318                                                  const QString &textToInsert) const
319 {
320     Q_UNUSED(cursor);
321     Q_UNUSED(textToInsert);
322     return false;
323 }
324
325 bool AutoCompleter::contextAllowsElectricCharacters(const QTextCursor &cursor) const
326 {
327     return contextAllowsAutoParentheses(cursor);
328 }
329
330 bool AutoCompleter::isInComment(const QTextCursor &cursor) const
331 {
332     Q_UNUSED(cursor);
333     return false;
334 }
335
336 QString AutoCompleter::insertMatchingBrace(const QTextCursor &cursor,
337                                            const QString &text,
338                                            QChar la,
339                                            int *skippedChars) const
340 {
341     Q_UNUSED(cursor);
342     Q_UNUSED(text);
343     Q_UNUSED(la);
344     Q_UNUSED(skippedChars);
345     return QString();
346 }
347
348 QString AutoCompleter::insertParagraphSeparator(const QTextCursor &cursor) const
349 {
350     Q_UNUSED(cursor);
351     return QString();
352 }