1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
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
16 ** GNU Lesser General Public License Usage
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.
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.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
35 #include "basetexteditor.h"
36 #include "tabsettings.h"
38 using namespace TextEditor;
46 bool Indenter::isElectricCharacter(const QChar &) const
51 void Indenter::indentBlock(QTextDocument *doc,
52 const QTextBlock &block,
53 const QChar &typedChar,
54 BaseTextEditorWidget *editor)
62 void Indenter::indent(QTextDocument *doc,
63 const QTextCursor &cursor,
64 const QChar &typedChar,
65 BaseTextEditorWidget *editor)
67 if (cursor.hasSelection()) {
68 QTextBlock block = doc->findBlock(cursor.selectionStart());
69 const QTextBlock end = doc->findBlock(cursor.selectionEnd()).next();
71 indentBlock(doc, block, typedChar, editor);
73 } while (block.isValid() && block != end);
75 indentBlock(doc, cursor.block(), typedChar, editor);
79 void Indenter::reindent(QTextDocument *doc, const QTextCursor &cursor, BaseTextEditorWidget *editor)
81 if (cursor.hasSelection()) {
82 QTextBlock block = doc->findBlock(cursor.selectionStart());
83 const QTextBlock end = doc->findBlock(cursor.selectionEnd()).next();
85 const TabSettings &ts = editor->tabSettings();
88 while (block.isValid() && block != end) {
89 QString bt = block.text();
90 if (ts.firstNonSpace(bt) < bt.size())
92 indentBlock(doc, block, QChar::Null, editor);
96 int previousIndentation = ts.indentationColumn(block.text());
97 indentBlock(doc, block, QChar::Null, editor);
98 int currentIndentation = ts.indentationColumn(block.text());
99 int delta = currentIndentation - previousIndentation;
101 block = block.next();
102 while (block.isValid() && block != end) {
103 ts.reindentLine(block, delta);
104 block = block.next();
107 indentBlock(doc, cursor.block(), QChar::Null, editor);