OSDN Git Service

dd4aaed7aa483d12f26ef07aac42053c2d2b3f03
[qt-creator-jp/qt-creator-jp.git] / src / plugins / texteditor / indenter.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 "indenter.h"
35 #include "basetexteditor.h"
36 #include "tabsettings.h"
37
38 using namespace TextEditor;
39
40 Indenter::Indenter()
41 {}
42
43 Indenter::~Indenter()
44 {}
45
46 bool Indenter::isElectricCharacter(const QChar &) const
47 {
48     return false;
49 }
50
51 void Indenter::indentBlock(QTextDocument *doc,
52                              const QTextBlock &block,
53                              const QChar &typedChar,
54                              BaseTextEditorWidget *editor)
55 {
56     Q_UNUSED(doc);
57     Q_UNUSED(block);
58     Q_UNUSED(typedChar);
59     Q_UNUSED(editor);
60 }
61
62 void Indenter::indent(QTextDocument *doc,
63                       const QTextCursor &cursor,
64                       const QChar &typedChar,
65                       BaseTextEditorWidget *editor)
66 {
67     if (cursor.hasSelection()) {
68         QTextBlock block = doc->findBlock(cursor.selectionStart());
69         const QTextBlock end = doc->findBlock(cursor.selectionEnd()).next();
70         do {
71             indentBlock(doc, block, typedChar, editor);
72             block = block.next();
73         } while (block.isValid() && block != end);
74     } else {
75         indentBlock(doc, cursor.block(), typedChar, editor);
76     }
77 }
78
79 void Indenter::reindent(QTextDocument *doc, const QTextCursor &cursor, BaseTextEditorWidget *editor)
80 {
81     if (cursor.hasSelection()) {
82         QTextBlock block = doc->findBlock(cursor.selectionStart());
83         const QTextBlock end = doc->findBlock(cursor.selectionEnd()).next();
84
85         const TabSettings &ts = editor->tabSettings();
86
87         // skip empty blocks
88         while (block.isValid() && block != end) {
89             QString bt = block.text();
90             if (ts.firstNonSpace(bt) < bt.size())
91                 break;
92             indentBlock(doc, block, QChar::Null, editor);
93             block = block.next();
94         }
95
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;
100
101         block = block.next();
102         while (block.isValid() && block != end) {
103             ts.reindentLine(block, delta);
104             block = block.next();
105         }
106     } else {
107         indentBlock(doc, cursor.block(), QChar::Null, editor);
108     }
109 }