TextEditor: Clean up RefactoringFile interface

Change-Id: I1e415b6b51065d16bc49a25118931723b600c00b
Reviewed-by: David Schulz <david.schulz@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
Christian Kandeler
2023-11-17 13:14:13 +01:00
parent cf74409bac
commit ac952955a4
3 changed files with 19 additions and 18 deletions

View File

@@ -76,19 +76,19 @@ CppRefactoringFile::CppRefactoringFile(const FilePath &filePath, const QSharedPo
{ {
const Snapshot &snapshot = data->m_snapshot; const Snapshot &snapshot = data->m_snapshot;
m_cppDocument = snapshot.document(filePath); m_cppDocument = snapshot.document(filePath);
m_formattingEnabled = true; enableFormatting();
} }
CppRefactoringFile::CppRefactoringFile(QTextDocument *document, const FilePath &filePath) CppRefactoringFile::CppRefactoringFile(QTextDocument *document, const FilePath &filePath)
: RefactoringFile(document, filePath) : RefactoringFile(document, filePath)
{ {
m_formattingEnabled = true; enableFormatting();
} }
CppRefactoringFile::CppRefactoringFile(TextEditor::TextEditorWidget *editor) CppRefactoringFile::CppRefactoringFile(TextEditor::TextEditorWidget *editor)
: RefactoringFile(editor) : RefactoringFile(editor)
{ {
m_formattingEnabled = true; enableFormatting();
} }
Document::Ptr CppRefactoringFile::cppDocument() const Document::Ptr CppRefactoringFile::cppDocument() const
@@ -235,7 +235,7 @@ const Token &CppRefactoringFile::tokenAt(unsigned index) const
void CppRefactoringFile::fileChanged() void CppRefactoringFile::fileChanged()
{ {
QTC_ASSERT(!m_filePath.isEmpty(), return); QTC_ASSERT(!filePath().isEmpty(), return);
m_cppDocument.clear(); m_cppDocument.clear();
CppModelManager::updateSourceFiles({filePath()}); CppModelManager::updateSourceFiles({filePath()});
} }

View File

@@ -61,7 +61,7 @@ QmlJSRefactoringFile::QmlJSRefactoringFile(
{ {
// the RefactoringFile is invalid if its not for a file with qml or js code // the RefactoringFile is invalid if its not for a file with qml or js code
if (ModelManagerInterface::guessLanguageOfFile(filePath) == Dialect::NoLanguage) if (ModelManagerInterface::guessLanguageOfFile(filePath) == Dialect::NoLanguage)
m_filePath.clear(); invalidate();
} }
QmlJSRefactoringFile::QmlJSRefactoringFile(TextEditor::TextEditorWidget *editor, Document::Ptr document) QmlJSRefactoringFile::QmlJSRefactoringFile(TextEditor::TextEditorWidget *editor, Document::Ptr document)
@@ -69,7 +69,7 @@ QmlJSRefactoringFile::QmlJSRefactoringFile(TextEditor::TextEditorWidget *editor,
, m_qmljsDocument(document) , m_qmljsDocument(document)
{ {
if (document) if (document)
m_filePath = document->fileName(); setFilePath(document->fileName()); // TODO: Is this really a different file path than in the editor?
} }
Document::Ptr QmlJSRefactoringFile::qmljsDocument() const Document::Ptr QmlJSRefactoringFile::qmljsDocument() const
@@ -136,7 +136,7 @@ bool QmlJSRefactoringFile::isCursorOn(SourceLocation loc) const
void QmlJSRefactoringFile::fileChanged() void QmlJSRefactoringFile::fileChanged()
{ {
QTC_ASSERT(!m_filePath.isEmpty(), return); QTC_ASSERT(!filePath().isEmpty(), return);
m_qmljsDocument.clear(); m_qmljsDocument.clear();
m_data->m_modelManager->updateSourceFiles({filePath()}, true); m_data->m_modelManager->updateSourceFiles({filePath()}, true);
} }

View File

@@ -31,6 +31,7 @@ class TextEditorWidget;
class TEXTEDITOR_EXPORT RefactoringFile class TEXTEDITOR_EXPORT RefactoringFile
{ {
Q_DISABLE_COPY(RefactoringFile) Q_DISABLE_COPY(RefactoringFile)
friend class PlainRefactoringFileFactory; // access to constructor
public: public:
using Range = Utils::ChangeSet::Range; using Range = Utils::ChangeSet::Range;
@@ -39,8 +40,7 @@ public:
bool isValid() const; bool isValid() const;
const QTextDocument *document() const; const QTextDocument *document() const;
// mustn't use the cursor to change the document const QTextCursor cursor() const; // mustn't use the cursor to change the document
const QTextCursor cursor() const;
Utils::FilePath filePath() const; Utils::FilePath filePath() const;
TextEditorWidget *editor() const; TextEditorWidget *editor() const;
@@ -69,10 +69,16 @@ protected:
RefactoringFile(TextEditorWidget *editor); RefactoringFile(TextEditorWidget *editor);
RefactoringFile(const Utils::FilePath &filePath); RefactoringFile(const Utils::FilePath &filePath);
QTextDocument *mutableDocument() const; void invalidate() { m_filePath.clear(); }
void setFilePath(const Utils::FilePath &filePath) { m_filePath = filePath; } // FIXME: Really necessary?
void enableFormatting() { m_formattingEnabled = true; }
// derived classes may want to clear language specific extra data private:
virtual void fileChanged() {} virtual void fileChanged() {} // derived classes may want to clear language specific extra data
virtual void indentSelection(const QTextCursor &selection,
const TextDocument *textDocument) const;
virtual void reindentSelection(const QTextCursor &selection,
const TextDocument *textDocument) const;
enum IndentType {Indent, Reindent}; enum IndentType {Indent, Reindent};
void indentOrReindent(const RefactoringSelections &ranges, IndentType indent); void indentOrReindent(const RefactoringSelections &ranges, IndentType indent);
@@ -84,10 +90,7 @@ protected:
static RefactoringSelections rangesToSelections(QTextDocument *document, static RefactoringSelections rangesToSelections(QTextDocument *document,
const QList<Range> &ranges); const QList<Range> &ranges);
virtual void indentSelection(const QTextCursor &selection, QTextDocument *mutableDocument() const;
const TextDocument *textDocument) const;
virtual void reindentSelection(const QTextCursor &selection,
const TextDocument *textDocument) const;
Utils::FilePath m_filePath; Utils::FilePath m_filePath;
mutable Utils::TextFileFormat m_textFileFormat; mutable Utils::TextFileFormat m_textFileFormat;
@@ -102,8 +105,6 @@ protected:
int m_editorCursorPosition = -1; int m_editorCursorPosition = -1;
bool m_appliedOnce = false; bool m_appliedOnce = false;
bool m_formattingEnabled = false; bool m_formattingEnabled = false;
friend class PlainRefactoringFileFactory; // access to constructor
}; };
class TEXTEDITOR_EXPORT RefactoringFileFactory class TEXTEDITOR_EXPORT RefactoringFileFactory