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"
|
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"
|
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 {
|
|
|
|
|
|
|
|
|
|
class QmlJSRefactoringChangesData : public TextEditor::RefactoringChangesData
|
2011-08-17 11:35:57 +02:00
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
QmlJSRefactoringChangesData(ModelManagerInterface *modelManager,
|
|
|
|
|
const Snapshot &snapshot)
|
|
|
|
|
: m_modelManager(modelManager)
|
|
|
|
|
, m_snapshot(snapshot)
|
|
|
|
|
{}
|
|
|
|
|
|
2018-07-11 07:31:38 +02:00
|
|
|
void indentSelection(const QTextCursor &selection,
|
2021-05-28 12:02:36 +02:00
|
|
|
const Utils::FilePath &filePath,
|
2018-07-11 07:31:38 +02:00
|
|
|
const TextEditor::TextDocument *textDocument) const override
|
2011-08-17 11:35:57 +02:00
|
|
|
{
|
|
|
|
|
// ### shares code with QmlJSTextEditor::indent
|
|
|
|
|
QTextDocument *doc = selection.document();
|
|
|
|
|
|
|
|
|
|
QTextBlock block = doc->findBlock(selection.selectionStart());
|
|
|
|
|
const QTextBlock end = doc->findBlock(selection.selectionEnd()).next();
|
|
|
|
|
|
|
|
|
|
const TextEditor::TabSettings &tabSettings =
|
2022-02-03 15:14:24 +01:00
|
|
|
ProjectExplorer::actualTabSettings(filePath, textDocument);
|
2011-10-17 13:56:48 +02:00
|
|
|
CreatorCodeFormatter codeFormatter(tabSettings);
|
2011-08-17 11:35:57 +02:00
|
|
|
codeFormatter.updateStateUntil(block);
|
|
|
|
|
do {
|
2020-08-01 02:20:32 +02:00
|
|
|
int depth = codeFormatter.indentFor(block);
|
|
|
|
|
if (depth != -1) {
|
|
|
|
|
if (QStringView(block.text()).trimmed().isEmpty()) {
|
|
|
|
|
// we do not want to indent empty lines (as one is indentent when pressing tab
|
|
|
|
|
// assuming that the user will start writing something), and get rid of that
|
|
|
|
|
// space if one had pressed tab in an empty line just before refactoring.
|
|
|
|
|
// If depth == -1 (inside a multiline string for example) leave the spaces.
|
|
|
|
|
depth = 0;
|
|
|
|
|
}
|
2011-10-24 12:29:54 +02:00
|
|
|
tabSettings.indentLine(block, depth);
|
2020-08-01 02:20:32 +02:00
|
|
|
}
|
2011-08-17 11:35:57 +02:00
|
|
|
codeFormatter.updateLineStateChange(block);
|
|
|
|
|
block = block.next();
|
|
|
|
|
} while (block.isValid() && block != end);
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-11 07:31:38 +02:00
|
|
|
void reindentSelection(const QTextCursor &selection,
|
2021-05-28 12:02:36 +02:00
|
|
|
const Utils::FilePath &filePath,
|
2018-07-11 07:31:38 +02:00
|
|
|
const TextEditor::TextDocument *textDocument) const override
|
2012-01-12 13:23:48 +01:00
|
|
|
{
|
|
|
|
|
const TextEditor::TabSettings &tabSettings =
|
2022-02-03 15:14:24 +01:00
|
|
|
ProjectExplorer::actualTabSettings(filePath, textDocument);
|
2012-01-12 13:23:48 +01:00
|
|
|
|
2019-01-16 09:37:54 +01:00
|
|
|
QmlJSEditor::Internal::Indenter indenter(selection.document());
|
|
|
|
|
indenter.reindent(selection, tabSettings);
|
2012-01-12 13:23:48 +01:00
|
|
|
}
|
|
|
|
|
|
2021-05-28 12:02:36 +02:00
|
|
|
void fileChanged(const Utils::FilePath &filePath) override
|
2011-08-17 11:35:57 +02:00
|
|
|
{
|
2022-06-20 12:35:13 +02:00
|
|
|
m_modelManager->updateSourceFiles({filePath}, true);
|
2011-08-17 11:35:57 +02:00
|
|
|
}
|
|
|
|
|
|
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)
|
2011-08-17 11:35:57 +02:00
|
|
|
: RefactoringChanges(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
|
|
|
{
|
2021-05-28 12:02:36 +02:00
|
|
|
return QmlJSRefactoringFilePtr(new QmlJSRefactoringFile(filePath, m_data));
|
2010-08-13 12:49:11 +02:00
|
|
|
}
|
|
|
|
|
|
2023-11-16 15:45:02 +01:00
|
|
|
QmlJSRefactoringFilePtr QmlJSRefactoringChanges::qmlJSFile(const Utils::FilePath &filePath) const
|
|
|
|
|
{
|
|
|
|
|
return file(filePath).staticCast<QmlJSRefactoringFile>();
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
{
|
2011-08-17 11:35:57 +02:00
|
|
|
return data()->m_snapshot;
|
2010-08-12 11:34:48 +02:00
|
|
|
}
|
2010-08-12 13:46:18 +02:00
|
|
|
|
2011-08-17 11:35:57 +02:00
|
|
|
QmlJSRefactoringChangesData *QmlJSRefactoringChanges::data() const
|
2010-08-12 13:46:18 +02:00
|
|
|
{
|
2011-08-17 11:35:57 +02:00
|
|
|
return static_cast<QmlJSRefactoringChangesData *>(m_data.data());
|
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(
|
|
|
|
|
const Utils::FilePath &filePath, const QSharedPointer<TextEditor::RefactoringChangesData> &data)
|
|
|
|
|
: RefactoringFile(filePath, 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)
|
2021-05-28 12:02:36 +02:00
|
|
|
m_filePath.clear();
|
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)
|
|
|
|
|
{
|
2016-12-14 18:32:36 +03:00
|
|
|
if (document)
|
2022-06-20 12:35:13 +02:00
|
|
|
m_filePath = document->fileName();
|
2010-08-13 12:49:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Document::Ptr QmlJSRefactoringFile::qmljsDocument() const
|
|
|
|
|
{
|
|
|
|
|
if (!m_qmljsDocument) {
|
|
|
|
|
const QString source = document()->toPlainText();
|
2011-08-17 11:35:57 +02:00
|
|
|
const Snapshot &snapshot = 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
|
|
|
QmlJSRefactoringChangesData *QmlJSRefactoringFile::data() const
|
|
|
|
|
{
|
|
|
|
|
return static_cast<QmlJSRefactoringChangesData *>(m_data.data());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QmlJSRefactoringFile::fileChanged()
|
2010-08-13 12:49:11 +02:00
|
|
|
{
|
2011-08-17 11:35:57 +02:00
|
|
|
m_qmljsDocument.clear();
|
|
|
|
|
RefactoringFile::fileChanged();
|
2010-08-13 12:49:11 +02:00
|
|
|
}
|
2015-02-03 23:48:57 +02:00
|
|
|
|
|
|
|
|
} // namespace QmlJSTools
|