OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qmljstools / qmljsrefactoringchanges.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 "qmljsrefactoringchanges.h"
35 #include "qmljsqtstylecodeformatter.h"
36
37 #include <qmljs/parser/qmljsast_p.h>
38 #include <qmljs/qmljsmodelmanagerinterface.h>
39 #include <texteditor/texteditorsettings.h>
40 #include <texteditor/tabsettings.h>
41
42 using namespace QmlJS;
43 using namespace QmlJSTools;
44
45 QmlJSRefactoringChanges::QmlJSRefactoringChanges(ModelManagerInterface *modelManager,
46                                                  const Snapshot &snapshot)
47     : m_modelManager(modelManager)
48     , m_snapshot(snapshot)
49 {
50     Q_ASSERT(modelManager);
51 }
52
53 const Snapshot &QmlJSRefactoringChanges::snapshot() const
54 {
55     return m_snapshot;
56 }
57
58 QmlJSRefactoringFile QmlJSRefactoringChanges::file(const QString &fileName)
59 {
60     return QmlJSRefactoringFile(fileName, this);
61 }
62
63 void QmlJSRefactoringChanges::indentSelection(const QTextCursor &selection) const
64 {
65     // ### shares code with QmlJSTextEditor::indent
66     QTextDocument *doc = selection.document();
67
68     QTextBlock block = doc->findBlock(selection.selectionStart());
69     const QTextBlock end = doc->findBlock(selection.selectionEnd()).next();
70
71     const TextEditor::TabSettings &tabSettings(TextEditor::TextEditorSettings::instance()->tabSettings());
72     QtStyleCodeFormatter codeFormatter(tabSettings);
73     codeFormatter.updateStateUntil(block);
74
75     do {
76         tabSettings.indentLine(block, codeFormatter.indentFor(block));
77         codeFormatter.updateLineStateChange(block);
78         block = block.next();
79     } while (block.isValid() && block != end);
80 }
81
82 void QmlJSRefactoringChanges::fileChanged(const QString &fileName)
83 {
84     m_modelManager->updateSourceFiles(QStringList(fileName), true);
85 }
86
87
88 QmlJSRefactoringFile::QmlJSRefactoringFile()
89 { }
90
91 QmlJSRefactoringFile::QmlJSRefactoringFile(const QString &fileName, QmlJSRefactoringChanges *refactoringChanges)
92     : RefactoringFile(fileName, refactoringChanges)
93 { }
94
95 QmlJSRefactoringFile::QmlJSRefactoringFile(TextEditor::BaseTextEditor *editor, QmlJS::Document::Ptr document)
96     : RefactoringFile()
97     , m_qmljsDocument(document)
98 {
99     m_fileName = document->fileName();
100     m_editor = editor;
101 }
102
103 Document::Ptr QmlJSRefactoringFile::qmljsDocument() const
104 {
105     if (!m_qmljsDocument) {
106         const QString source = document()->toPlainText();
107         const QString name = fileName();
108         const Snapshot &snapshot = refactoringChanges()->snapshot();
109
110         m_qmljsDocument = snapshot.documentFromSource(source, name);
111         m_qmljsDocument->parse();
112     }
113
114     return m_qmljsDocument;
115 }
116
117 unsigned QmlJSRefactoringFile::startOf(const AST::SourceLocation &loc) const
118 {
119     return position(loc.startLine, loc.startColumn);
120 }
121
122 bool QmlJSRefactoringFile::isCursorOn(AST::UiObjectMember *ast) const
123 {
124     const unsigned pos = cursor().position();
125
126     return ast->firstSourceLocation().begin() <= pos
127             && pos <= ast->lastSourceLocation().end();
128 }
129
130 bool QmlJSRefactoringFile::isCursorOn(AST::UiQualifiedId *ast) const
131 {
132     const unsigned pos = cursor().position();
133
134     if (ast->identifierToken.begin() > pos)
135         return false;
136
137     AST::UiQualifiedId *last = ast;
138     while (last->next)
139         last = last->next;
140
141     return pos <= ast->identifierToken.end();
142 }
143
144 QmlJSRefactoringChanges *QmlJSRefactoringFile::refactoringChanges() const
145 {
146     return static_cast<QmlJSRefactoringChanges *>(m_refactoringChanges);
147 }