OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / cppeditor / cppqtstyleindenter.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 "cppqtstyleindenter.h"
34
35 #include <cpptools/cppcodeformatter.h>
36 #include <texteditor/basetexteditor.h>
37 #include <texteditor/tabsettings.h>
38
39 #include <QtCore/QChar>
40 #include <QtGui/QTextDocument>
41 #include <QtGui/QTextBlock>
42 #include <QtGui/QTextCursor>
43
44 using namespace CppEditor;
45 using namespace Internal;
46
47 CppQtStyleIndenter::CppQtStyleIndenter()
48 {}
49
50 CppQtStyleIndenter::~CppQtStyleIndenter()
51 {}
52
53 bool CppQtStyleIndenter::isElectricCharacter(const QChar &ch) const
54 {
55     if (ch == QLatin1Char('{') ||
56         ch == QLatin1Char('}') ||
57         ch == QLatin1Char(':') ||
58         ch == QLatin1Char('#')) {
59         return true;
60     }
61     return false;
62 }
63
64 static bool colonIsElectric(const QString &text)
65 {
66     // switch cases and access declarations should be reindented
67     if (text.contains(QLatin1String("case"))
68             || text.contains(QLatin1String("default"))
69             || text.contains(QLatin1String("public"))
70             || text.contains(QLatin1String("private"))
71             || text.contains(QLatin1String("protected"))
72             || text.contains(QLatin1String("signals"))) {
73         return true;
74     }
75
76     // lines that start with : might have a constructor initializer list
77     const QString trimmedtext = text.trimmed();
78     if (!trimmedtext.isEmpty() && trimmedtext.at(0) == QLatin1Char(':'))
79         return true;
80
81     return false;
82 }
83
84 void CppQtStyleIndenter::indentBlock(QTextDocument *doc,
85                                      const QTextBlock &block,
86                                      const QChar &typedChar,
87                                      TextEditor::BaseTextEditorWidget *editor)
88 {
89     Q_UNUSED(doc)
90
91     const TextEditor::TabSettings &ts = editor->tabSettings();
92     CppTools::QtStyleCodeFormatter codeFormatter(ts);
93
94     codeFormatter.updateStateUntil(block);
95     int indent;
96     int padding;
97     codeFormatter.indentFor(block, &indent, &padding);
98
99     if (isElectricCharacter(typedChar)) {
100         // : should not be electric for labels
101         if (typedChar == QLatin1Char(':') && !colonIsElectric(block.text()))
102             return;
103
104         // only reindent the current line when typing electric characters if the
105         // indent is the same it would be if the line were empty
106         int newlineIndent;
107         int newlinePadding;
108         codeFormatter.indentForNewLineAfter(block.previous(), &newlineIndent, &newlinePadding);
109         if (ts.indentationColumn(block.text()) != newlineIndent + newlinePadding)
110             return;
111     }
112
113     ts.indentLine(block, indent + padding, padding);
114 }
115
116 void CppQtStyleIndenter::indent(QTextDocument *doc,
117                                 const QTextCursor &cursor,
118                                 const QChar &typedChar,
119                                 TextEditor::BaseTextEditorWidget *editor)
120 {
121     if (cursor.hasSelection()) {
122         QTextBlock block = doc->findBlock(cursor.selectionStart());
123         const QTextBlock end = doc->findBlock(cursor.selectionEnd()).next();
124
125         const TextEditor::TabSettings &ts = editor->tabSettings();
126         CppTools::QtStyleCodeFormatter codeFormatter(ts);
127         codeFormatter.updateStateUntil(block);
128
129         QTextCursor tc = editor->textCursor();
130         tc.beginEditBlock();
131         do {
132             int indent;
133             int padding;
134             codeFormatter.indentFor(block, &indent, &padding);
135             ts.indentLine(block, indent + padding, padding);
136             codeFormatter.updateLineStateChange(block);
137             block = block.next();
138         } while (block.isValid() && block != end);
139         tc.endEditBlock();
140     } else {
141         indentBlock(doc, cursor.block(), typedChar, editor);
142     }
143 }