OSDN Git Service

70e2f4cf8e2f0b8aa4a3967078019779adead556
[qt-creator-jp/qt-creator-jp.git] / src / plugins / cppeditor / cppautocompleter.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 "cppautocompleter.h"
35
36 #include <Token.h>
37
38 #include <cplusplus/SimpleLexer.h>
39 #include <cplusplus/MatchingText.h>
40 #include <cplusplus/BackwardsScanner.h>
41
42 #include <QtCore/QLatin1Char>
43 #include <QtGui/QTextCursor>
44
45 using namespace CppEditor;
46 using namespace Internal;
47 using namespace CPlusPlus;
48
49 CppAutoCompleter::CppAutoCompleter()
50 {}
51
52 CppAutoCompleter::~CppAutoCompleter()
53 {}
54
55 bool CppAutoCompleter::contextAllowsAutoParentheses(const QTextCursor &cursor,
56                                                     const QString &textToInsert) const
57 {
58     QChar ch;
59
60     if (! textToInsert.isEmpty())
61         ch = textToInsert.at(0);
62
63     if (! (MatchingText::shouldInsertMatchingText(cursor)
64            || ch == QLatin1Char('\'')
65            || ch == QLatin1Char('"')))
66         return false;
67     else if (isInComment(cursor))
68         return false;
69
70     return true;
71 }
72
73 bool CppAutoCompleter::contextAllowsElectricCharacters(const QTextCursor &cursor) const
74 {
75     const Token tk = SimpleLexer::tokenAt(cursor.block().text(), cursor.positionInBlock(),
76                                           BackwardsScanner::previousBlockState(cursor.block()));
77
78     // XXX Duplicated from CPPEditor::isInComment to avoid tokenizing twice
79     if (tk.isComment()) {
80         const unsigned pos = cursor.selectionEnd() - cursor.block().position();
81
82         if (pos == tk.end()) {
83             if (tk.is(T_CPP_COMMENT) || tk.is(T_CPP_DOXY_COMMENT))
84                 return false;
85
86             const int state = cursor.block().userState() & 0xFF;
87             if (state > 0)
88                 return false;
89         }
90
91         if (pos < tk.end())
92             return false;
93     }
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)) {
96
97         const unsigned pos = cursor.selectionEnd() - cursor.block().position();
98         if (pos <= tk.end())
99             return false;
100     }
101
102     return true;
103 }
104
105 bool CppAutoCompleter::isInComment(const QTextCursor &cursor) const
106 {
107     const Token tk = SimpleLexer::tokenAt(cursor.block().text(), cursor.positionInBlock(),
108                                           BackwardsScanner::previousBlockState(cursor.block()));
109
110     if (tk.isComment()) {
111         const unsigned pos = cursor.selectionEnd() - cursor.block().position();
112
113         if (pos == tk.end()) {
114             if (tk.is(T_CPP_COMMENT) || tk.is(T_CPP_DOXY_COMMENT))
115                 return true;
116
117             const int state = cursor.block().userState() & 0xFF;
118             if (state > 0)
119                 return true;
120         }
121
122         if (pos < tk.end())
123             return true;
124     }
125
126     return false;
127 }
128
129 QString CppAutoCompleter::insertMatchingBrace(const QTextCursor &cursor,
130                                                 const QString &text,
131                                                 QChar la,
132                                                 int *skippedChars) const
133 {
134     MatchingText m;
135     return m.insertMatchingBrace(cursor, text, la, skippedChars);
136 }
137
138 QString CppAutoCompleter::insertParagraphSeparator(const QTextCursor &cursor) const
139 {
140     MatchingText m;
141     return m.insertParagraphSeparator(cursor);
142 }