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 **************************************************************************/
34 #include "qmljsrefactoringchanges.h"
35 #include "qmljsqtstylecodeformatter.h"
37 #include <qmljs/parser/qmljsast_p.h>
38 #include <qmljs/qmljsmodelmanagerinterface.h>
39 #include <texteditor/texteditorsettings.h>
40 #include <texteditor/tabsettings.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) const
65 // ### shares code with QmlJSTextEditor::indent
66 QTextDocument *doc = selection.document();
68 QTextBlock block = doc->findBlock(selection.selectionStart());
69 const QTextBlock end = doc->findBlock(selection.selectionEnd()).next();
71 const TextEditor::TabSettings &tabSettings(TextEditor::TextEditorSettings::instance()->tabSettings());
72 QtStyleCodeFormatter codeFormatter(tabSettings);
73 codeFormatter.updateStateUntil(block);
76 tabSettings.indentLine(block, codeFormatter.indentFor(block));
77 codeFormatter.updateLineStateChange(block);
79 } while (block.isValid() && block != end);
82 void QmlJSRefactoringChanges::fileChanged(const QString &fileName)
84 m_modelManager->updateSourceFiles(QStringList(fileName), true);
88 QmlJSRefactoringFile::QmlJSRefactoringFile()
91 QmlJSRefactoringFile::QmlJSRefactoringFile(const QString &fileName, QmlJSRefactoringChanges *refactoringChanges)
92 : RefactoringFile(fileName, refactoringChanges)
95 QmlJSRefactoringFile::QmlJSRefactoringFile(TextEditor::BaseTextEditor *editor, QmlJS::Document::Ptr document)
97 , m_qmljsDocument(document)
99 m_fileName = document->fileName();
103 Document::Ptr QmlJSRefactoringFile::qmljsDocument() const
105 if (!m_qmljsDocument) {
106 const QString source = document()->toPlainText();
107 const QString name = fileName();
108 const Snapshot &snapshot = refactoringChanges()->snapshot();
110 m_qmljsDocument = snapshot.documentFromSource(source, name);
111 m_qmljsDocument->parse();
114 return m_qmljsDocument;
117 unsigned QmlJSRefactoringFile::startOf(const AST::SourceLocation &loc) const
119 return position(loc.startLine, loc.startColumn);
122 bool QmlJSRefactoringFile::isCursorOn(AST::UiObjectMember *ast) const
124 const unsigned pos = cursor().position();
126 return ast->firstSourceLocation().begin() <= pos
127 && pos <= ast->lastSourceLocation().end();
130 bool QmlJSRefactoringFile::isCursorOn(AST::UiQualifiedId *ast) const
132 const unsigned pos = cursor().position();
134 if (ast->identifierToken.begin() > pos)
137 AST::UiQualifiedId *last = ast;
141 return pos <= ast->identifierToken.end();
144 QmlJSRefactoringChanges *QmlJSRefactoringFile::refactoringChanges() const
146 return static_cast<QmlJSRefactoringChanges *>(m_refactoringChanges);