1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #include "cppautocompleter.h"
37 #include <cplusplus/SimpleLexer.h>
38 #include <cplusplus/MatchingText.h>
39 #include <cplusplus/BackwardsScanner.h>
41 #include <QtCore/QLatin1Char>
42 #include <QtGui/QTextCursor>
44 using namespace CppEditor;
45 using namespace Internal;
46 using namespace CPlusPlus;
48 CppAutoCompleter::CppAutoCompleter()
51 CppAutoCompleter::~CppAutoCompleter()
54 bool CppAutoCompleter::contextAllowsAutoParentheses(const QTextCursor &cursor,
55 const QString &textToInsert) const
59 if (! textToInsert.isEmpty())
60 ch = textToInsert.at(0);
62 if (! (MatchingText::shouldInsertMatchingText(cursor)
63 || ch == QLatin1Char('\'')
64 || ch == QLatin1Char('"')))
66 else if (isInComment(cursor))
72 bool CppAutoCompleter::contextAllowsElectricCharacters(const QTextCursor &cursor) const
74 const Token tk = SimpleLexer::tokenAt(cursor.block().text(), cursor.positionInBlock(),
75 BackwardsScanner::previousBlockState(cursor.block()));
77 // XXX Duplicated from CPPEditor::isInComment to avoid tokenizing twice
79 const unsigned pos = cursor.selectionEnd() - cursor.block().position();
81 if (pos == tk.end()) {
82 if (tk.is(T_CPP_COMMENT) || tk.is(T_CPP_DOXY_COMMENT))
85 const int state = cursor.block().userState() & 0xFF;
93 else if (tk.is(T_STRING_LITERAL) || tk.is(T_WIDE_STRING_LITERAL)
94 || tk.is(T_CHAR_LITERAL) || tk.is(T_WIDE_CHAR_LITERAL)) {
96 const unsigned pos = cursor.selectionEnd() - cursor.block().position();
104 bool CppAutoCompleter::isInComment(const QTextCursor &cursor) const
106 const Token tk = SimpleLexer::tokenAt(cursor.block().text(), cursor.positionInBlock(),
107 BackwardsScanner::previousBlockState(cursor.block()));
109 if (tk.isComment()) {
110 const unsigned pos = cursor.selectionEnd() - cursor.block().position();
112 if (pos == tk.end()) {
113 if (tk.is(T_CPP_COMMENT) || tk.is(T_CPP_DOXY_COMMENT))
116 const int state = cursor.block().userState() & 0xFF;
128 QString CppAutoCompleter::insertMatchingBrace(const QTextCursor &cursor,
131 int *skippedChars) const
134 return m.insertMatchingBrace(cursor, text, la, skippedChars);
137 QString CppAutoCompleter::insertParagraphSeparator(const QTextCursor &cursor) const
140 return m.insertParagraphSeparator(cursor);