2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2010-06-14 14:52:43 +02:00
|
|
|
|
|
|
|
|
#include "qmljsrefactoringchanges.h"
|
2023-11-20 11:28:41 +01:00
|
|
|
|
2010-11-11 10:05:05 +01:00
|
|
|
#include "qmljsqtstylecodeformatter.h"
|
2011-09-09 10:55:11 +02:00
|
|
|
#include "qmljsmodelmanager.h"
|
2012-01-12 13:23:48 +01:00
|
|
|
#include "qmljsindenter.h"
|
2023-11-20 11:28:41 +01:00
|
|
|
#include "qmljstoolsconstants.h"
|
2010-06-14 14:52:43 +02:00
|
|
|
|
2010-09-30 16:12:24 +02:00
|
|
|
#include <qmljs/parser/qmljsast_p.h>
|
2014-09-26 09:14:03 +02:00
|
|
|
#include <texteditor/textdocument.h>
|
2010-08-12 11:34:48 +02:00
|
|
|
#include <texteditor/tabsettings.h>
|
2011-02-01 14:13:54 +01:00
|
|
|
#include <projectexplorer/editorconfiguration.h>
|
2010-08-12 11:34:48 +02:00
|
|
|
|
2010-06-14 14:52:43 +02:00
|
|
|
using namespace QmlJS;
|
|
|
|
|
|
2015-02-03 23:48:57 +02:00
|
|
|
namespace QmlJSTools {
|
|
|
|
|
|
2023-11-16 17:40:08 +01:00
|
|
|
class QmlJSRefactoringChangesData
|
2011-08-17 11:35:57 +02:00
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
QmlJSRefactoringChangesData(ModelManagerInterface *modelManager,
|
|
|
|
|
const Snapshot &snapshot)
|
|
|
|
|
: m_modelManager(modelManager)
|
|
|
|
|
, m_snapshot(snapshot)
|
|
|
|
|
{}
|
|
|
|
|
|
2015-02-03 23:48:57 +02:00
|
|
|
ModelManagerInterface *m_modelManager;
|
|
|
|
|
Snapshot m_snapshot;
|
2011-08-17 11:35:57 +02:00
|
|
|
};
|
|
|
|
|
|
2010-06-14 14:52:43 +02:00
|
|
|
QmlJSRefactoringChanges::QmlJSRefactoringChanges(ModelManagerInterface *modelManager,
|
|
|
|
|
const Snapshot &snapshot)
|
2023-11-16 17:33:35 +01:00
|
|
|
: m_data(new QmlJSRefactoringChangesData(modelManager, snapshot))
|
2010-06-14 14:52:43 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-16 15:45:02 +01:00
|
|
|
TextEditor::RefactoringFilePtr QmlJSRefactoringChanges::file(const Utils::FilePath &filePath) const
|
2010-08-13 12:49:11 +02:00
|
|
|
{
|
2023-11-17 13:36:46 +01:00
|
|
|
return qmlJSFile(filePath);
|
2010-08-13 12:49:11 +02:00
|
|
|
}
|
|
|
|
|
|
2023-11-16 15:45:02 +01:00
|
|
|
QmlJSRefactoringFilePtr QmlJSRefactoringChanges::qmlJSFile(const Utils::FilePath &filePath) const
|
|
|
|
|
{
|
2023-11-17 13:36:46 +01:00
|
|
|
return QmlJSRefactoringFilePtr(new QmlJSRefactoringFile(filePath, m_data));
|
2023-11-16 15:45:02 +01:00
|
|
|
}
|
|
|
|
|
|
2011-08-17 11:35:57 +02:00
|
|
|
QmlJSRefactoringFilePtr QmlJSRefactoringChanges::file(
|
2014-09-26 11:37:54 +02:00
|
|
|
TextEditor::TextEditorWidget *editor, const Document::Ptr &document)
|
2010-08-13 12:49:11 +02:00
|
|
|
{
|
2011-08-17 11:35:57 +02:00
|
|
|
return QmlJSRefactoringFilePtr(new QmlJSRefactoringFile(editor, document));
|
2010-08-13 12:49:11 +02:00
|
|
|
}
|
|
|
|
|
|
2011-08-17 11:35:57 +02:00
|
|
|
const Snapshot &QmlJSRefactoringChanges::snapshot() const
|
2010-08-12 11:34:48 +02:00
|
|
|
{
|
2023-11-16 17:33:35 +01:00
|
|
|
return m_data->m_snapshot;
|
2010-08-12 13:46:18 +02:00
|
|
|
}
|
2010-08-13 12:49:11 +02:00
|
|
|
|
2021-05-28 12:02:36 +02:00
|
|
|
QmlJSRefactoringFile::QmlJSRefactoringFile(
|
2023-11-16 17:24:51 +01:00
|
|
|
const Utils::FilePath &filePath, const QSharedPointer<QmlJSRefactoringChangesData> &data)
|
|
|
|
|
: RefactoringFile(filePath), m_data(data)
|
2011-09-09 10:55:11 +02:00
|
|
|
{
|
|
|
|
|
// the RefactoringFile is invalid if its not for a file with qml or js code
|
2022-06-20 12:35:13 +02:00
|
|
|
if (ModelManagerInterface::guessLanguageOfFile(filePath) == Dialect::NoLanguage)
|
2023-11-17 13:14:13 +01:00
|
|
|
invalidate();
|
2011-09-09 10:55:11 +02:00
|
|
|
}
|
2010-08-13 12:49:11 +02:00
|
|
|
|
2015-02-03 23:48:57 +02:00
|
|
|
QmlJSRefactoringFile::QmlJSRefactoringFile(TextEditor::TextEditorWidget *editor, Document::Ptr document)
|
2011-08-17 11:35:57 +02:00
|
|
|
: RefactoringFile(editor)
|
2010-08-13 12:49:11 +02:00
|
|
|
, m_qmljsDocument(document)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Document::Ptr QmlJSRefactoringFile::qmljsDocument() const
|
|
|
|
|
{
|
|
|
|
|
if (!m_qmljsDocument) {
|
|
|
|
|
const QString source = document()->toPlainText();
|
2023-11-16 17:24:51 +01:00
|
|
|
const Snapshot &snapshot = m_data->m_snapshot;
|
2010-08-13 12:49:11 +02:00
|
|
|
|
2022-06-20 12:35:13 +02:00
|
|
|
Document::MutablePtr newDoc
|
|
|
|
|
= snapshot.documentFromSource(source,
|
|
|
|
|
filePath(),
|
|
|
|
|
ModelManagerInterface::guessLanguageOfFile(filePath()));
|
2011-11-03 13:47:03 +01:00
|
|
|
newDoc->parse();
|
|
|
|
|
m_qmljsDocument = newDoc;
|
2010-08-13 12:49:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return m_qmljsDocument;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-07 15:52:58 +03:00
|
|
|
QString QmlJSRefactoringFile::qmlImports() const
|
|
|
|
|
{
|
|
|
|
|
QString imports;
|
|
|
|
|
QmlJS::AST::UiProgram *prog = qmljsDocument()->qmlProgram();
|
|
|
|
|
if (prog && prog->headers) {
|
|
|
|
|
const unsigned int start = startOf(prog->headers->firstSourceLocation());
|
|
|
|
|
const unsigned int end = startOf(prog->members->member->firstSourceLocation());
|
|
|
|
|
imports = textOf(start, end);
|
|
|
|
|
}
|
|
|
|
|
return imports;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-28 17:51:32 +01:00
|
|
|
unsigned QmlJSRefactoringFile::startOf(const SourceLocation &loc) const
|
2010-08-13 12:49:11 +02:00
|
|
|
{
|
|
|
|
|
return position(loc.startLine, loc.startColumn);
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-30 16:12:24 +02:00
|
|
|
bool QmlJSRefactoringFile::isCursorOn(AST::UiObjectMember *ast) const
|
|
|
|
|
{
|
|
|
|
|
const unsigned pos = cursor().position();
|
|
|
|
|
|
|
|
|
|
return ast->firstSourceLocation().begin() <= pos
|
|
|
|
|
&& pos <= ast->lastSourceLocation().end();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool QmlJSRefactoringFile::isCursorOn(AST::UiQualifiedId *ast) const
|
|
|
|
|
{
|
|
|
|
|
const unsigned pos = cursor().position();
|
|
|
|
|
|
|
|
|
|
if (ast->identifierToken.begin() > pos)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
AST::UiQualifiedId *last = ast;
|
|
|
|
|
while (last->next)
|
|
|
|
|
last = last->next;
|
|
|
|
|
|
|
|
|
|
return pos <= ast->identifierToken.end();
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-28 17:51:32 +01:00
|
|
|
bool QmlJSRefactoringFile::isCursorOn(SourceLocation loc) const
|
2011-10-19 14:27:40 +02:00
|
|
|
{
|
|
|
|
|
const unsigned pos = cursor().position();
|
|
|
|
|
return pos >= loc.begin() && pos <= loc.end();
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-17 11:35:57 +02:00
|
|
|
void QmlJSRefactoringFile::fileChanged()
|
2010-08-13 12:49:11 +02:00
|
|
|
{
|
2023-11-17 13:14:13 +01:00
|
|
|
QTC_ASSERT(!filePath().isEmpty(), return);
|
2011-08-17 11:35:57 +02:00
|
|
|
m_qmljsDocument.clear();
|
2023-11-16 17:24:51 +01:00
|
|
|
m_data->m_modelManager->updateSourceFiles({filePath()}, true);
|
2023-11-16 16:09:26 +01:00
|
|
|
}
|
|
|
|
|
|
2023-11-20 11:28:41 +01:00
|
|
|
Utils::Id QmlJSRefactoringFile::indenterId() const
|
2023-11-16 16:09:26 +01:00
|
|
|
{
|
2023-11-20 11:28:41 +01:00
|
|
|
return Constants::QML_JS_SETTINGS_ID;
|
2010-08-13 12:49:11 +02:00
|
|
|
}
|
2015-02-03 23:48:57 +02:00
|
|
|
|
|
|
|
|
} // namespace QmlJSTools
|