OSDN Git Service

Update license.
[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 (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 "qmljsrefactoringchanges.h"
34 #include "qmljsqtstylecodeformatter.h"
35
36 #include <qmljs/parser/qmljsast_p.h>
37 #include <qmljs/qmljsmodelmanagerinterface.h>
38 #include <texteditor/texteditorsettings.h>
39 #include <texteditor/tabsettings.h>
40 #include <projectexplorer/editorconfiguration.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,
64                                               const QString &fileName,
65                                               const TextEditor::BaseTextEditorWidget *textEditor) const
66 {
67     // ### shares code with QmlJSTextEditor::indent
68     QTextDocument *doc = selection.document();
69
70     QTextBlock block = doc->findBlock(selection.selectionStart());
71     const QTextBlock end = doc->findBlock(selection.selectionEnd()).next();
72
73     const TextEditor::TabSettings &tabSettings =
74         ProjectExplorer::actualTabSettings(fileName, textEditor);
75     QtStyleCodeFormatter codeFormatter(tabSettings);
76     codeFormatter.updateStateUntil(block);
77
78     do {
79         tabSettings.indentLine(block, codeFormatter.indentFor(block));
80         codeFormatter.updateLineStateChange(block);
81         block = block.next();
82     } while (block.isValid() && block != end);
83 }
84
85 void QmlJSRefactoringChanges::fileChanged(const QString &fileName)
86 {
87     m_modelManager->updateSourceFiles(QStringList(fileName), true);
88 }
89
90
91 QmlJSRefactoringFile::QmlJSRefactoringFile()
92 { }
93
94 QmlJSRefactoringFile::QmlJSRefactoringFile(const QString &fileName, QmlJSRefactoringChanges *refactoringChanges)
95     : RefactoringFile(fileName, refactoringChanges)
96 { }
97
98 QmlJSRefactoringFile::QmlJSRefactoringFile(TextEditor::BaseTextEditorWidget *editor, QmlJS::Document::Ptr document)
99     : RefactoringFile()
100     , m_qmljsDocument(document)
101 {
102     m_fileName = document->fileName();
103     m_editor = editor;
104 }
105
106 Document::Ptr QmlJSRefactoringFile::qmljsDocument() const
107 {
108     if (!m_qmljsDocument) {
109         const QString source = document()->toPlainText();
110         const QString name = fileName();
111         const Snapshot &snapshot = refactoringChanges()->snapshot();
112
113         m_qmljsDocument = snapshot.documentFromSource(source, name);
114         m_qmljsDocument->parse();
115     }
116
117     return m_qmljsDocument;
118 }
119
120 unsigned QmlJSRefactoringFile::startOf(const AST::SourceLocation &loc) const
121 {
122     return position(loc.startLine, loc.startColumn);
123 }
124
125 bool QmlJSRefactoringFile::isCursorOn(AST::UiObjectMember *ast) const
126 {
127     const unsigned pos = cursor().position();
128
129     return ast->firstSourceLocation().begin() <= pos
130             && pos <= ast->lastSourceLocation().end();
131 }
132
133 bool QmlJSRefactoringFile::isCursorOn(AST::UiQualifiedId *ast) const
134 {
135     const unsigned pos = cursor().position();
136
137     if (ast->identifierToken.begin() > pos)
138         return false;
139
140     AST::UiQualifiedId *last = ast;
141     while (last->next)
142         last = last->next;
143
144     return pos <= ast->identifierToken.end();
145 }
146
147 QmlJSRefactoringChanges *QmlJSRefactoringFile::refactoringChanges() const
148 {
149     return static_cast<QmlJSRefactoringChanges *>(m_refactoringChanges);
150 }