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 "autocompleter.h"
35 #include "basetextdocumentlayout.h"
36 #include "texteditorsettings.h"
37 #include "tabsettings.h"
39 #include <QtCore/QDebug>
40 #include <QtGui/QTextCursor>
42 using namespace TextEditor;
44 AutoCompleter::AutoCompleter() :
45 m_allowSkippingOfBlockEnd(false),
46 m_surroundWithEnabled(true),
47 m_autoParenthesesEnabled(true)
50 AutoCompleter::~AutoCompleter()
53 void AutoCompleter::setAutoParenthesesEnabled(bool b)
55 m_autoParenthesesEnabled = b;
58 bool AutoCompleter::isAutoParenthesesEnabled() const
60 return m_autoParenthesesEnabled;
63 void AutoCompleter::setSurroundWithEnabled(bool b)
65 m_surroundWithEnabled = b;
68 bool AutoCompleter::isSurroundWithEnabled() const
70 return m_surroundWithEnabled;
73 void AutoCompleter::countBracket(QChar open, QChar close, QChar c, int *errors, int *stillopen)
81 *errors += -1 * (*stillopen);
86 void AutoCompleter::countBrackets(QTextCursor cursor,
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)
104 countBracket(open, close, paren.chr, errors, stillopen);
107 block = block.next();
111 QString AutoCompleter::autoComplete(QTextCursor &cursor, const QString &textToInsert) const
113 const bool checkBlockEnd = m_allowSkippingOfBlockEnd;
114 m_allowSkippingOfBlockEnd = false; // consume blockEnd.
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)) +
126 if (str.endsWith(QChar::ParagraphSeparator))
127 str += QLatin1String("}") + QString(QChar::ParagraphSeparator);
129 str += QString(QChar::ParagraphSeparator) + QLatin1String("}");
132 str += QLatin1String("}");
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("'");
144 if (!m_autoParenthesesEnabled)
147 if (!contextAllowsAutoParentheses(cursor, textToInsert))
150 QTextDocument *doc = cursor.document();
151 const QString text = textToInsert;
152 const QChar lookAhead = doc->characterAt(cursor.selectionEnd());
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;
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(']');
169 countBrackets(cursor, blockStart, blockEnd, openChar, closeChar, &errors, &stillopen);
170 int errorsBeforeInsertion = errors + stillopen;
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
181 int skippedChars = 0;
182 const QString autoText = insertMatchingBrace(cursor, text, lookAhead, &skippedChars);
184 if (checkBlockEnd && textToInsert.at(0) == QLatin1Char('}')) {
185 if (textToInsert.length() > 1)
186 qWarning() << "*** handle event compression";
188 int startPos = cursor.selectionEnd(), pos = startPos;
189 while (doc->characterAt(pos).isSpace())
192 if (doc->characterAt(pos) == QLatin1Char('}'))
193 skippedChars += (pos - startPos) + 1;
197 const int pos = cursor.position();
198 cursor.setPosition(pos + skippedChars);
199 cursor.setPosition(pos, QTextCursor::KeepAnchor);
205 bool AutoCompleter::autoBackspace(QTextCursor &cursor)
207 m_allowSkippingOfBlockEnd = false;
209 if (!m_autoParenthesesEnabled)
212 int pos = cursor.position();
215 QTextCursor c = cursor;
216 c.setPosition(pos - 1);
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);
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();
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(']');
236 countBrackets(cursor, blockStart, blockEnd, openChar, closeChar, &errors, &stillopen);
237 int errorsBeforeDeletion = errors + stillopen;
240 countBrackets(cursor, blockStart, pos - 1, openChar, closeChar, &errors, &stillopen);
241 countBrackets(cursor, pos, blockEnd, openChar, closeChar, &errors, &stillopen);
242 int errorsAfterDeletion = errors + stillopen;
244 if (errorsAfterDeletion < errorsBeforeDeletion)
245 return false; // insertion fixes parentheses or bracket errors, do not auto complete
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();
258 cursor.deletePreviousChar();
259 cursor.endEditBlock();
266 int AutoCompleter::paragraphSeparatorAboutToBeInserted(QTextCursor &cursor,
267 const TabSettings &tabSettings)
269 if (!m_autoParenthesesEnabled)
272 QTextDocument *doc = cursor.document();
273 if (doc->characterAt(cursor.position() - 1) != QLatin1Char('{'))
276 if (!contextAllowsAutoParentheses(cursor))
279 // verify that we indeed do have an extra opening brace in the document
280 int braceDepth = BaseTextDocumentLayout::braceDepth(doc->lastBlock());
283 return 0; // braces are all balanced or worse, no need to do anything
285 // we have an extra brace , let's see if we should close it
287 /* verify that the next block is not further intended compared to the current block.
288 This covers the following case:
293 QTextBlock block = cursor.block();
294 int indentation = tabSettings.indentationColumn(block.text());
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();
302 && tabSettings.indentationColumn(block.text()) > indentation)
306 const QString &textToInsert = insertParagraphSeparator(cursor);
307 int pos = cursor.position();
308 cursor.insertBlock();
309 cursor.insertText(textToInsert);
310 cursor.setPosition(pos);
312 m_allowSkippingOfBlockEnd = true;
317 bool AutoCompleter::contextAllowsAutoParentheses(const QTextCursor &cursor,
318 const QString &textToInsert) const
321 Q_UNUSED(textToInsert);
325 bool AutoCompleter::contextAllowsElectricCharacters(const QTextCursor &cursor) const
327 return contextAllowsAutoParentheses(cursor);
330 bool AutoCompleter::isInComment(const QTextCursor &cursor) const
336 QString AutoCompleter::insertMatchingBrace(const QTextCursor &cursor,
339 int *skippedChars) const
344 Q_UNUSED(skippedChars);
348 QString AutoCompleter::insertParagraphSeparator(const QTextCursor &cursor) const