1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and Digia.  For licensing terms and
13 ** conditions see http://qt.digia.com/licensing.  For further information
14 ** use the contact form at http://qt.digia.com/contact-us.
15 **
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file.  Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 **
24 ** In addition, as a special exception, Digia gives you certain additional
25 ** rights.  These rights are described in the Digia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27 **
28 ****************************************************************************/
29 
30 #include "autocompleter.h"
31 #include "basetextdocumentlayout.h"
32 #include "tabsettings.h"
33 
34 #include <QDebug>
35 #include <QTextCursor>
36 
37 using namespace TextEditor;
38 
AutoCompleter()39 AutoCompleter::AutoCompleter() :
40     m_allowSkippingOfBlockEnd(false),
41     m_surroundWithEnabled(true),
42     m_autoParenthesesEnabled(true)
43 {}
44 
~AutoCompleter()45 AutoCompleter::~AutoCompleter()
46 {}
47 
setAutoParenthesesEnabled(bool b)48 void AutoCompleter::setAutoParenthesesEnabled(bool b)
49 {
50     m_autoParenthesesEnabled = b;
51 }
52 
isAutoParenthesesEnabled() const53 bool AutoCompleter::isAutoParenthesesEnabled() const
54 {
55     return m_autoParenthesesEnabled;
56 }
57 
setSurroundWithEnabled(bool b)58 void AutoCompleter::setSurroundWithEnabled(bool b)
59 {
60     m_surroundWithEnabled = b;
61 }
62 
isSurroundWithEnabled() const63 bool AutoCompleter::isSurroundWithEnabled() const
64 {
65     return m_surroundWithEnabled;
66 }
67 
countBracket(QChar open,QChar close,QChar c,int * errors,int * stillopen)68 void AutoCompleter::countBracket(QChar open, QChar close, QChar c, int *errors, int *stillopen)
69 {
70     if (c == open)
71         ++*stillopen;
72     else if (c == close)
73         --*stillopen;
74 
75     if (*stillopen < 0) {
76         *errors += -1 * (*stillopen);
77         *stillopen = 0;
78     }
79 }
80 
countBrackets(QTextCursor cursor,int from,int end,QChar open,QChar close,int * errors,int * stillopen)81 void AutoCompleter::countBrackets(QTextCursor cursor,
82                                   int from,
83                                   int end,
84                                   QChar open,
85                                   QChar close,
86                                   int *errors,
87                                   int *stillopen)
88 {
89     cursor.setPosition(from);
90     QTextBlock block = cursor.block();
91     while (block.isValid() && block.position() < end) {
92         TextEditor::Parentheses parenList = TextEditor::BaseTextDocumentLayout::parentheses(block);
93         if (!parenList.isEmpty() && !TextEditor::BaseTextDocumentLayout::ifdefedOut(block)) {
94             for (int i = 0; i < parenList.count(); ++i) {
95                 TextEditor::Parenthesis paren = parenList.at(i);
96                 int position = block.position() + paren.pos;
97                 if (position < from || position >= end)
98                     continue;
99                 countBracket(open, close, paren.chr, errors, stillopen);
100             }
101         }
102         block = block.next();
103     }
104 }
105 
autoComplete(QTextCursor & cursor,const QString & textToInsert) const106 QString AutoCompleter::autoComplete(QTextCursor &cursor, const QString &textToInsert) const
107 {
108     const bool checkBlockEnd = m_allowSkippingOfBlockEnd;
109     m_allowSkippingOfBlockEnd = false; // consume blockEnd.
110 
111     if (m_surroundWithEnabled && cursor.hasSelection()) {
112         if (textToInsert == QLatin1String("("))
113             return cursor.selectedText() + QLatin1String(")");
114         if (textToInsert == QLatin1String("{")) {
115             //If the text span multiple lines, insert on different lines
116             QString str = cursor.selectedText();
117             if (str.contains(QChar::ParagraphSeparator)) {
118                 //Also, try to simulate auto-indent
119                 str = (str.startsWith(QChar::ParagraphSeparator) ? QString() : QString(QChar::ParagraphSeparator)) +
120                       str;
121                 if (str.endsWith(QChar::ParagraphSeparator))
122                     str += QLatin1String("}") + QString(QChar::ParagraphSeparator);
123                 else
124                     str += QString(QChar::ParagraphSeparator) + QLatin1String("}");
125             } else {
126                 str += QLatin1String("}");
127             }
128             return str;
129         }
130         if (textToInsert == QLatin1String("["))
131             return cursor.selectedText() + QLatin1String("]");
132         if (textToInsert == QLatin1String("\""))
133             return cursor.selectedText() + QLatin1String("\"");
134         if (textToInsert == QLatin1String("'"))
135             return cursor.selectedText() + QLatin1String("'");
136     }
137 
138     if (!m_autoParenthesesEnabled)
139         return QString();
140 
141     if (!contextAllowsAutoParentheses(cursor, textToInsert))
142         return QString();
143 
144     QTextDocument *doc = cursor.document();
145     const QString text = textToInsert;
146     const QChar lookAhead = doc->characterAt(cursor.selectionEnd());
147 
148     const QChar character = textToInsert.at(0);
149     const QString parentheses = QLatin1String("()");
150     const QString brackets = QLatin1String("[]");
151     if (parentheses.contains(character) || brackets.contains(character)) {
152         QTextCursor tmp= cursor;
153         bool foundBlockStart = TextEditor::TextBlockUserData::findPreviousBlockOpenParenthesis(&tmp);
154         int blockStart = foundBlockStart ? tmp.position() : 0;
155         tmp = cursor;
156         bool foundBlockEnd = TextEditor::TextBlockUserData::findNextBlockClosingParenthesis(&tmp);
157         int blockEnd = foundBlockEnd ? tmp.position() : (cursor.document()->characterCount() - 1);
158         const QChar openChar = parentheses.contains(character) ? QLatin1Char('(') : QLatin1Char('[');
159         const QChar closeChar = parentheses.contains(character) ? QLatin1Char(')') : QLatin1Char(']');
160 
161         int errors = 0;
162         int stillopen = 0;
163         countBrackets(cursor, blockStart, blockEnd, openChar, closeChar, &errors, &stillopen);
164         int errorsBeforeInsertion = errors + stillopen;
165         errors = 0;
166         stillopen = 0;
167         countBrackets(cursor, blockStart, cursor.position(), openChar, closeChar, &errors, &stillopen);
168         countBracket(openChar, closeChar, character, &errors, &stillopen);
169         countBrackets(cursor, cursor.position(), blockEnd, openChar, closeChar, &errors, &stillopen);
170         int errorsAfterInsertion = errors + stillopen;
171         if (errorsAfterInsertion < errorsBeforeInsertion)
172             return QString(); // insertion fixes parentheses or bracket errors, do not auto complete
173     }
174 
175     int skippedChars = 0;
176     const QString autoText = insertMatchingBrace(cursor, text, lookAhead, &skippedChars);
177 
178     if (checkBlockEnd && textToInsert.at(0) == QLatin1Char('}')) {
179         if (textToInsert.length() > 1)
180             qWarning() << "*** handle event compression";
181 
182         int startPos = cursor.selectionEnd(), pos = startPos;
183         while (doc->characterAt(pos).isSpace())
184             ++pos;
185 
186         if (doc->characterAt(pos) == QLatin1Char('}'))
187             skippedChars += (pos - startPos) + 1;
188     }
189 
190     if (skippedChars) {
191         const int pos = cursor.position();
192         cursor.setPosition(pos + skippedChars);
193         cursor.setPosition(pos, QTextCursor::KeepAnchor);
194     }
195 
196     return autoText;
197 }
198 
autoBackspace(QTextCursor & cursor)199 bool AutoCompleter::autoBackspace(QTextCursor &cursor)
200 {
201     m_allowSkippingOfBlockEnd = false;
202 
203     if (!m_autoParenthesesEnabled)
204         return false;
205 
206     int pos = cursor.position();
207     if (pos == 0)
208         return false;
209     QTextCursor c = cursor;
210     c.setPosition(pos - 1);
211 
212     QTextDocument *doc = cursor.document();
213     const QChar lookAhead = doc->characterAt(pos);
214     const QChar lookBehind = doc->characterAt(pos - 1);
215     const QChar lookFurtherBehind = doc->characterAt(pos - 2);
216 
217     const QChar character = lookBehind;
218     if (character == QLatin1Char('(') || character == QLatin1Char('[')) {
219         QTextCursor tmp = cursor;
220         TextEditor::TextBlockUserData::findPreviousBlockOpenParenthesis(&tmp);
221         int blockStart = tmp.isNull() ? 0 : tmp.position();
222         tmp = cursor;
223         TextEditor::TextBlockUserData::findNextBlockClosingParenthesis(&tmp);
224         int blockEnd = tmp.isNull() ? (cursor.document()->characterCount()-1) : tmp.position();
225         QChar openChar = character;
226         QChar closeChar = (character == QLatin1Char('(')) ? QLatin1Char(')') : QLatin1Char(']');
227 
228         int errors = 0;
229         int stillopen = 0;
230         countBrackets(cursor, blockStart, blockEnd, openChar, closeChar, &errors, &stillopen);
231         int errorsBeforeDeletion = errors + stillopen;
232         errors = 0;
233         stillopen = 0;
234         countBrackets(cursor, blockStart, pos - 1, openChar, closeChar, &errors, &stillopen);
235         countBrackets(cursor, pos, blockEnd, openChar, closeChar, &errors, &stillopen);
236         int errorsAfterDeletion = errors + stillopen;
237 
238         if (errorsAfterDeletion < errorsBeforeDeletion)
239             return false; // insertion fixes parentheses or bracket errors, do not auto complete
240     }
241 
242     // ### this code needs to be generalized
243     if    ((lookBehind == QLatin1Char('(') && lookAhead == QLatin1Char(')'))
244         || (lookBehind == QLatin1Char('[') && lookAhead == QLatin1Char(']'))
245         || (lookBehind == QLatin1Char('"') && lookAhead == QLatin1Char('"')
246             && lookFurtherBehind != QLatin1Char('\\'))
247         || (lookBehind == QLatin1Char('\'') && lookAhead == QLatin1Char('\'')
248             && lookFurtherBehind != QLatin1Char('\\'))) {
249         if (! isInComment(c)) {
250             cursor.beginEditBlock();
251             cursor.deleteChar();
252             cursor.deletePreviousChar();
253             cursor.endEditBlock();
254             return true;
255         }
256     }
257     return false;
258 }
259 
paragraphSeparatorAboutToBeInserted(QTextCursor & cursor,const TabSettings & tabSettings)260 int AutoCompleter::paragraphSeparatorAboutToBeInserted(QTextCursor &cursor,
261                                                        const TabSettings &tabSettings)
262 {
263     if (!m_autoParenthesesEnabled)
264         return 0;
265 
266     QTextDocument *doc = cursor.document();
267     if (doc->characterAt(cursor.position() - 1) != QLatin1Char('{'))
268         return 0;
269 
270     if (!contextAllowsAutoParentheses(cursor))
271         return 0;
272 
273     // verify that we indeed do have an extra opening brace in the document
274     QTextBlock block = cursor.block();
275     const QString textFromCusror = block.text().mid(cursor.positionInBlock()).trimmed();
276     int braceDepth = BaseTextDocumentLayout::braceDepth(doc->lastBlock());
277 
278     if (braceDepth <= 0 && (textFromCusror.isEmpty() || textFromCusror.at(0) != QLatin1Char('}')))
279         return 0; // braces are all balanced or worse, no need to do anything and separator inserted not between '{' and '}'
280 
281     // we have an extra brace , let's see if we should close it
282 
283     /* verify that the next block is not further intended compared to the current block.
284        This covers the following case:
285 
286             if (condition) {|
287                 statement;
288     */
289     int indentation = tabSettings.indentationColumn(block.text());
290 
291     if (block.next().isValid()) { // not the last block
292         block = block.next();
293         //skip all empty blocks
294         while (block.isValid() && tabSettings.onlySpace(block.text()))
295             block = block.next();
296         if (block.isValid()
297                 && tabSettings.indentationColumn(block.text()) > indentation)
298             return 0;
299     }
300 
301     const QString &textToInsert = insertParagraphSeparator(cursor);
302     int pos = cursor.position();
303     cursor.insertBlock();
304     cursor.insertText(textToInsert);
305     cursor.setPosition(pos);
306 
307     // if we actually insert a separator, allow it to be overwritten if
308     // user types it
309     if (!textToInsert.isEmpty())
310         m_allowSkippingOfBlockEnd = true;
311 
312     return 1;
313 }
314 
contextAllowsAutoParentheses(const QTextCursor & cursor,const QString & textToInsert) const315 bool AutoCompleter::contextAllowsAutoParentheses(const QTextCursor &cursor,
316                                                  const QString &textToInsert) const
317 {
318     Q_UNUSED(cursor);
319     Q_UNUSED(textToInsert);
320     return true;
321 }
322 
contextAllowsElectricCharacters(const QTextCursor & cursor) const323 bool AutoCompleter::contextAllowsElectricCharacters(const QTextCursor &cursor) const
324 {
325     return contextAllowsAutoParentheses(cursor);
326 }
327 
isInComment(const QTextCursor & cursor) const328 bool AutoCompleter::isInComment(const QTextCursor &cursor) const
329 {
330     Q_UNUSED(cursor);
331     return false;
332 }
333 
insertMatchingBrace(const QTextCursor & cursor,const QString & text,QChar la,int * skippedChars) const334 QString AutoCompleter::insertMatchingBrace(const QTextCursor &cursor,
335                                            const QString &text,
336                                            QChar la,
337                                            int *skippedChars) const
338 {
339     Q_UNUSED(cursor);
340     Q_UNUSED(text);
341     Q_UNUSED(la);
342     Q_UNUSED(skippedChars);
343     return QString();
344 }
345 
insertParagraphSeparator(const QTextCursor & cursor) const346 QString AutoCompleter::insertParagraphSeparator(const QTextCursor &cursor) const
347 {
348     Q_UNUSED(cursor);
349     return QString();
350 }
351