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 "qmljsrefactoringchanges.h"
34 #include "qmljsqtstylecodeformatter.h"
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>
42 using namespace QmlJS;
43 using namespace QmlJSTools;
45 QmlJSRefactoringChanges::QmlJSRefactoringChanges(ModelManagerInterface *modelManager,
46 const Snapshot &snapshot)
47 : m_modelManager(modelManager)
48 , m_snapshot(snapshot)
50 Q_ASSERT(modelManager);
53 const Snapshot &QmlJSRefactoringChanges::snapshot() const
58 QmlJSRefactoringFile QmlJSRefactoringChanges::file(const QString &fileName)
60 return QmlJSRefactoringFile(fileName, this);
63 void QmlJSRefactoringChanges::indentSelection(const QTextCursor &selection,
64 const QString &fileName,
65 const TextEditor::BaseTextEditorWidget *textEditor) const
67 // ### shares code with QmlJSTextEditor::indent
68 QTextDocument *doc = selection.document();
70 QTextBlock block = doc->findBlock(selection.selectionStart());
71 const QTextBlock end = doc->findBlock(selection.selectionEnd()).next();
73 const TextEditor::TabSettings &tabSettings =
74 ProjectExplorer::actualTabSettings(fileName, textEditor);
75 QtStyleCodeFormatter codeFormatter(tabSettings);
76 codeFormatter.updateStateUntil(block);
79 tabSettings.indentLine(block, codeFormatter.indentFor(block));
80 codeFormatter.updateLineStateChange(block);
82 } while (block.isValid() && block != end);
85 void QmlJSRefactoringChanges::fileChanged(const QString &fileName)
87 m_modelManager->updateSourceFiles(QStringList(fileName), true);
91 QmlJSRefactoringFile::QmlJSRefactoringFile()
94 QmlJSRefactoringFile::QmlJSRefactoringFile(const QString &fileName, QmlJSRefactoringChanges *refactoringChanges)
95 : RefactoringFile(fileName, refactoringChanges)
98 QmlJSRefactoringFile::QmlJSRefactoringFile(TextEditor::BaseTextEditorWidget *editor, QmlJS::Document::Ptr document)
100 , m_qmljsDocument(document)
102 m_fileName = document->fileName();
106 Document::Ptr QmlJSRefactoringFile::qmljsDocument() const
108 if (!m_qmljsDocument) {
109 const QString source = document()->toPlainText();
110 const QString name = fileName();
111 const Snapshot &snapshot = refactoringChanges()->snapshot();
113 m_qmljsDocument = snapshot.documentFromSource(source, name);
114 m_qmljsDocument->parse();
117 return m_qmljsDocument;
120 unsigned QmlJSRefactoringFile::startOf(const AST::SourceLocation &loc) const
122 return position(loc.startLine, loc.startColumn);
125 bool QmlJSRefactoringFile::isCursorOn(AST::UiObjectMember *ast) const
127 const unsigned pos = cursor().position();
129 return ast->firstSourceLocation().begin() <= pos
130 && pos <= ast->lastSourceLocation().end();
133 bool QmlJSRefactoringFile::isCursorOn(AST::UiQualifiedId *ast) const
135 const unsigned pos = cursor().position();
137 if (ast->identifierToken.begin() > pos)
140 AST::UiQualifiedId *last = ast;
144 return pos <= ast->identifierToken.end();
147 QmlJSRefactoringChanges *QmlJSRefactoringFile::refactoringChanges() const
149 return static_cast<QmlJSRefactoringChanges *>(m_refactoringChanges);