TextEditor: Introduce PlainRefactoringFileFactory

This makes it immediately clear that there are more specialized variants
available, which helps users make a conscious decision.

Change-Id: I35feb4bed2d91fb4f83ede6e731d9ce89fd4af3f
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Christian Kandeler
2023-11-17 12:56:27 +01:00
parent 78b9edd251
commit cf74409bac
8 changed files with 23 additions and 20 deletions

View File

@@ -46,7 +46,7 @@ static FileToFixits fixitsPerFile(const QList<ClangFixIt> &fixIts)
void ClangFixItOperation::perform() void ClangFixItOperation::perform()
{ {
const TextEditor::RefactoringFileFactory refactoringChanges; const TextEditor::PlainRefactoringFileFactory refactoringChanges;
const FileToFixits fileToFixIts = fixitsPerFile(fixIts); const FileToFixits fileToFixIts = fixitsPerFile(fixIts);
for (auto i = fileToFixIts.cbegin(), end = fileToFixIts.cend(); i != end; ++i) { for (auto i = fileToFixIts.cbegin(), end = fileToFixIts.cend(); i != end; ++i) {

View File

@@ -41,7 +41,7 @@ static Range toRange(const QTextDocument *doc, DiagnosticRange locations)
void ClangToolQuickFixOperation::perform() void ClangToolQuickFixOperation::perform()
{ {
TextEditor::RefactoringFileFactory changes; TextEditor::PlainRefactoringFileFactory changes;
QMap<QString, TextEditor::RefactoringFilePtr> refactoringFiles; QMap<QString, TextEditor::RefactoringFilePtr> refactoringFiles;
for (const ExplainingStep &step : m_diagnostic.explainingSteps) { for (const ExplainingStep &step : m_diagnostic.explainingSteps) {

View File

@@ -1839,7 +1839,7 @@ void CppModelManager::renameIncludes(const FilePath &oldFilePath, const FilePath
if (oldFilePath.absolutePath() != newFilePath.absolutePath()) if (oldFilePath.absolutePath() != newFilePath.absolutePath())
return; return;
const TextEditor::RefactoringFileFactory changes; const TextEditor::PlainRefactoringFileFactory changes;
QString oldFileName = oldFilePath.fileName(); QString oldFileName = oldFilePath.fileName();
QString newFileName = newFilePath.fileName(); QString newFileName = newFilePath.fileName();

View File

@@ -1739,7 +1739,7 @@ void Client::log(const QString &message) const
TextEditor::RefactoringFilePtr Client::createRefactoringFile(const FilePath &filePath) const TextEditor::RefactoringFilePtr Client::createRefactoringFile(const FilePath &filePath) const
{ {
return TextEditor::RefactoringFileFactory().file(filePath); return TextEditor::PlainRefactoringFileFactory().file(filePath);
} }
void Client::setCompletionResultsLimit(int limit) void Client::setCompletionResultsLimit(int limit)

View File

@@ -539,7 +539,7 @@ FilePaths BaseFileFind::replaceAll(const QString &text, const SearchResultItems
if (items.isEmpty()) if (items.isEmpty())
return {}; return {};
RefactoringFileFactory refactoring; PlainRefactoringFileFactory refactoring;
QHash<FilePath, SearchResultItems> changes; QHash<FilePath, SearchResultItems> changes;
for (const SearchResultItem &item : items) for (const SearchResultItem &item : items)

View File

@@ -26,13 +26,6 @@ using namespace Utils;
namespace TextEditor { namespace TextEditor {
RefactoringFileFactory::~RefactoringFileFactory() = default;
RefactoringFilePtr RefactoringFileFactory::file(const FilePath &filePath) const
{
return RefactoringFilePtr(new RefactoringFile(filePath));
}
RefactoringFile::RefactoringFile(QTextDocument *document, const FilePath &filePath) RefactoringFile::RefactoringFile(QTextDocument *document, const FilePath &filePath)
: m_filePath(filePath) : m_filePath(filePath)
, m_document(document) , m_document(document)
@@ -474,4 +467,11 @@ RefactoringSelections RefactoringFile::rangesToSelections(QTextDocument *documen
return selections; return selections;
} }
RefactoringFileFactory::~RefactoringFileFactory() = default;
RefactoringFilePtr PlainRefactoringFileFactory::file(const Utils::FilePath &filePath) const
{
return RefactoringFilePtr(new RefactoringFile(filePath));
}
} // namespace TextEditor } // namespace TextEditor

View File

@@ -20,12 +20,12 @@ class QTextDocument;
QT_END_NAMESPACE QT_END_NAMESPACE
namespace TextEditor { namespace TextEditor {
class TextDocument; class PlainRefactoringFileFactory;
class TextEditorWidget;
class RefactoringFile; class RefactoringFile;
using RefactoringFilePtr = QSharedPointer<RefactoringFile>; using RefactoringFilePtr = QSharedPointer<RefactoringFile>;
class RefactoringFileFactory;
using RefactoringSelections = QVector<QPair<QTextCursor, QTextCursor>>; using RefactoringSelections = QVector<QPair<QTextCursor, QTextCursor>>;
class TextDocument;
class TextEditorWidget;
// ### listen to the m_editor::destroyed signal? // ### listen to the m_editor::destroyed signal?
class TEXTEDITOR_EXPORT RefactoringFile class TEXTEDITOR_EXPORT RefactoringFile
@@ -103,17 +103,20 @@ protected:
bool m_appliedOnce = false; bool m_appliedOnce = false;
bool m_formattingEnabled = false; bool m_formattingEnabled = false;
friend class RefactoringFileFactory; // access to constructor friend class PlainRefactoringFileFactory; // access to constructor
}; };
class TEXTEDITOR_EXPORT RefactoringFileFactory class TEXTEDITOR_EXPORT RefactoringFileFactory
{ {
public: public:
virtual ~RefactoringFileFactory(); virtual ~RefactoringFileFactory();
virtual RefactoringFilePtr file(const Utils::FilePath &filePath) const = 0;
};
// TODO: Make pure virtual and introduce dedicated subclass for generic refactoring, class TEXTEDITOR_EXPORT PlainRefactoringFileFactory final : public RefactoringFileFactory
// so no one instantiates this one by mistake. {
virtual RefactoringFilePtr file(const Utils::FilePath &filePath) const; public:
RefactoringFilePtr file(const Utils::FilePath &filePath) const override;
}; };
} // namespace TextEditor } // namespace TextEditor

View File

@@ -514,7 +514,7 @@ bool TextDocument::applyChangeSet(const ChangeSet &changeSet)
{ {
if (changeSet.isEmpty()) if (changeSet.isEmpty())
return true; return true;
RefactoringFileFactory changes; PlainRefactoringFileFactory changes;
const RefactoringFilePtr file = changes.file(filePath()); const RefactoringFilePtr file = changes.file(filePath());
file->setChangeSet(changeSet); file->setChangeSet(changeSet);
return file->apply(); return file->apply();