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 "cppqtstyleindenter.h"
35 #include <cpptools/cppcodeformatter.h>
36 #include <texteditor/basetexteditor.h>
37 #include <texteditor/tabsettings.h>
39 #include <QtCore/QChar>
40 #include <QtGui/QTextDocument>
41 #include <QtGui/QTextBlock>
42 #include <QtGui/QTextCursor>
44 using namespace CppEditor;
45 using namespace Internal;
47 CppQtStyleIndenter::CppQtStyleIndenter()
50 CppQtStyleIndenter::~CppQtStyleIndenter()
53 bool CppQtStyleIndenter::isElectricCharacter(const QChar &ch) const
55 if (ch == QLatin1Char('{') ||
56 ch == QLatin1Char('}') ||
57 ch == QLatin1Char(':') ||
58 ch == QLatin1Char('#')) {
64 static bool colonIsElectric(const QString &text)
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"))) {
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(':'))
84 void CppQtStyleIndenter::indentBlock(QTextDocument *doc,
85 const QTextBlock &block,
86 const QChar &typedChar,
87 TextEditor::BaseTextEditorWidget *editor)
91 const TextEditor::TabSettings &ts = editor->tabSettings();
92 CppTools::QtStyleCodeFormatter codeFormatter(ts);
94 codeFormatter.updateStateUntil(block);
97 codeFormatter.indentFor(block, &indent, &padding);
99 if (isElectricCharacter(typedChar)) {
100 // : should not be electric for labels
101 if (typedChar == QLatin1Char(':') && !colonIsElectric(block.text()))
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
108 codeFormatter.indentForNewLineAfter(block.previous(), &newlineIndent, &newlinePadding);
109 if (ts.indentationColumn(block.text()) != newlineIndent + newlinePadding)
113 ts.indentLine(block, indent + padding, padding);
116 void CppQtStyleIndenter::indent(QTextDocument *doc,
117 const QTextCursor &cursor,
118 const QChar &typedChar,
119 TextEditor::BaseTextEditorWidget *editor)
121 if (cursor.hasSelection()) {
122 QTextBlock block = doc->findBlock(cursor.selectionStart());
123 const QTextBlock end = doc->findBlock(cursor.selectionEnd()).next();
125 const TextEditor::TabSettings &ts = editor->tabSettings();
126 CppTools::QtStyleCodeFormatter codeFormatter(ts);
127 codeFormatter.updateStateUntil(block);
129 QTextCursor tc = editor->textCursor();
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);
141 indentBlock(doc, cursor.block(), typedChar, editor);