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 "cppautocompleter.h"
38 #include <cplusplus/SimpleLexer.h>
39 #include <cplusplus/MatchingText.h>
40 #include <cplusplus/BackwardsScanner.h>
42 #include <QtCore/QLatin1Char>
43 #include <QtGui/QTextCursor>
45 using namespace CppEditor;
46 using namespace Internal;
47 using namespace CPlusPlus;
49 CppAutoCompleter::CppAutoCompleter()
52 CppAutoCompleter::~CppAutoCompleter()
55 bool CppAutoCompleter::contextAllowsAutoParentheses(const QTextCursor &cursor,
56 const QString &textToInsert) const
60 if (! textToInsert.isEmpty())
61 ch = textToInsert.at(0);
63 if (! (MatchingText::shouldInsertMatchingText(cursor)
64 || ch == QLatin1Char('\'')
65 || ch == QLatin1Char('"')))
67 else if (isInComment(cursor))
73 bool CppAutoCompleter::contextAllowsElectricCharacters(const QTextCursor &cursor) const
75 const Token tk = SimpleLexer::tokenAt(cursor.block().text(), cursor.positionInBlock(),
76 BackwardsScanner::previousBlockState(cursor.block()));
78 // XXX Duplicated from CPPEditor::isInComment to avoid tokenizing twice
80 const unsigned pos = cursor.selectionEnd() - cursor.block().position();
82 if (pos == tk.end()) {
83 if (tk.is(T_CPP_COMMENT) || tk.is(T_CPP_DOXY_COMMENT))
86 const int state = cursor.block().userState() & 0xFF;
94 else if (tk.is(T_STRING_LITERAL) || tk.is(T_WIDE_STRING_LITERAL)
95 || tk.is(T_CHAR_LITERAL) || tk.is(T_WIDE_CHAR_LITERAL)) {
97 const unsigned pos = cursor.selectionEnd() - cursor.block().position();
105 bool CppAutoCompleter::isInComment(const QTextCursor &cursor) const
107 const Token tk = SimpleLexer::tokenAt(cursor.block().text(), cursor.positionInBlock(),
108 BackwardsScanner::previousBlockState(cursor.block()));
110 if (tk.isComment()) {
111 const unsigned pos = cursor.selectionEnd() - cursor.block().position();
113 if (pos == tk.end()) {
114 if (tk.is(T_CPP_COMMENT) || tk.is(T_CPP_DOXY_COMMENT))
117 const int state = cursor.block().userState() & 0xFF;
129 QString CppAutoCompleter::insertMatchingBrace(const QTextCursor &cursor,
132 int *skippedChars) const
135 return m.insertMatchingBrace(cursor, text, la, skippedChars);
138 QString CppAutoCompleter::insertParagraphSeparator(const QTextCursor &cursor) const
141 return m.insertParagraphSeparator(cursor);