OSDN Git Service

Update license.
[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 (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
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.
18 **
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.
22 **
23 ** Other Usage
24 **
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.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #include "cppautocompleter.h"
34
35 #include <Token.h>
36
37 #include <cplusplus/SimpleLexer.h>
38 #include <cplusplus/MatchingText.h>
39 #include <cplusplus/BackwardsScanner.h>
40
41 #include <QtCore/QLatin1Char>
42 #include <QtGui/QTextCursor>
43
44 using namespace CppEditor;
45 using namespace Internal;
46 using namespace CPlusPlus;
47
48 CppAutoCompleter::CppAutoCompleter()
49 {}
50
51 CppAutoCompleter::~CppAutoCompleter()
52 {}
53
54 bool CppAutoCompleter::contextAllowsAutoParentheses(const QTextCursor &cursor,
55                                                     const QString &textToInsert) const
56 {
57     QChar ch;
58
59     if (! textToInsert.isEmpty())
60         ch = textToInsert.at(0);
61
62     if (! (MatchingText::shouldInsertMatchingText(cursor)
63            || ch == QLatin1Char('\'')
64            || ch == QLatin1Char('"')))
65         return false;
66     else if (isInComment(cursor))
67         return false;
68
69     return true;
70 }
71
72 bool CppAutoCompleter::contextAllowsElectricCharacters(const QTextCursor &cursor) const
73 {
74     const Token tk = SimpleLexer::tokenAt(cursor.block().text(), cursor.positionInBlock(),
75                                           BackwardsScanner::previousBlockState(cursor.block()));
76
77     // XXX Duplicated from CPPEditor::isInComment to avoid tokenizing twice
78     if (tk.isComment()) {
79         const unsigned pos = cursor.selectionEnd() - cursor.block().position();
80
81         if (pos == tk.end()) {
82             if (tk.is(T_CPP_COMMENT) || tk.is(T_CPP_DOXY_COMMENT))
83                 return false;
84
85             const int state = cursor.block().userState() & 0xFF;
86             if (state > 0)
87                 return false;
88         }
89
90         if (pos < tk.end())
91             return false;
92     }
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)) {
95
96         const unsigned pos = cursor.selectionEnd() - cursor.block().position();
97         if (pos <= tk.end())
98             return false;
99     }
100
101     return true;
102 }
103
104 bool CppAutoCompleter::isInComment(const QTextCursor &cursor) const
105 {
106     const Token tk = SimpleLexer::tokenAt(cursor.block().text(), cursor.positionInBlock(),
107                                           BackwardsScanner::previousBlockState(cursor.block()));
108
109     if (tk.isComment()) {
110         const unsigned pos = cursor.selectionEnd() - cursor.block().position();
111
112         if (pos == tk.end()) {
113             if (tk.is(T_CPP_COMMENT) || tk.is(T_CPP_DOXY_COMMENT))
114                 return true;
115
116             const int state = cursor.block().userState() & 0xFF;
117             if (state > 0)
118                 return true;
119         }
120
121         if (pos < tk.end())
122             return true;
123     }
124
125     return false;
126 }
127
128 QString CppAutoCompleter::insertMatchingBrace(const QTextCursor &cursor,
129                                                 const QString &text,
130                                                 QChar la,
131                                                 int *skippedChars) const
132 {
133     MatchingText m;
134     return m.insertMatchingBrace(cursor, text, la, skippedChars);
135 }
136
137 QString CppAutoCompleter::insertParagraphSeparator(const QTextCursor &cursor) const
138 {
139     MatchingText m;
140     return m.insertParagraphSeparator(cursor);
141 }