TextEditor: Simplify TextMarkRegistry further

Change-Id: I61bbc99f0f8e378edbfb5733bffce7b424a36f2e
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
hjk
2024-01-17 16:59:47 +01:00
parent 1de4590dde
commit 89d5c69a79

View File

@@ -30,7 +30,6 @@ namespace TextEditor {
class TextMarkRegistry : public QObject class TextMarkRegistry : public QObject
{ {
Q_OBJECT
public: public:
TextMarkRegistry(QObject *parent); TextMarkRegistry(QObject *parent);
@@ -45,10 +44,9 @@ private:
const FilePath &newPath); const FilePath &newPath);
void allDocumentsRenamed(const FilePath &oldPath, const FilePath &newPath); void allDocumentsRenamed(const FilePath &oldPath, const FilePath &newPath);
QHash<Utils::FilePath, QSet<TextMark *> > m_marks;
}; };
TextMarkRegistry *m_instance = nullptr; static QHash<FilePath, QSet<TextMark *>> s_marks;
class AnnotationColors class AnnotationColors
{ {
@@ -463,8 +461,6 @@ void TextMark::setIsLocationMarker(bool newIsLocationMarker)
TextMarkRegistry::TextMarkRegistry(QObject *parent) TextMarkRegistry::TextMarkRegistry(QObject *parent)
: QObject(parent) : QObject(parent)
{ {
m_instance = this;
connect(EditorManager::instance(), &EditorManager::editorOpened, connect(EditorManager::instance(), &EditorManager::editorOpened,
this, &TextMarkRegistry::editorOpened); this, &TextMarkRegistry::editorOpened);
@@ -481,14 +477,14 @@ void TextMarkRegistry::add(TextMark *mark)
void TextMarkRegistry::add(TextMark *mark, TextDocument *document) void TextMarkRegistry::add(TextMark *mark, TextDocument *document)
{ {
m_instance->m_marks[mark->filePath()].insert(mark); s_marks[mark->filePath()].insert(mark);
if (document) if (document)
document->addMark(mark); document->addMark(mark);
} }
bool TextMarkRegistry::remove(TextMark *mark) bool TextMarkRegistry::remove(TextMark *mark)
{ {
return m_instance->m_marks[mark->filePath()].remove(mark); return s_marks[mark->filePath()].remove(mark);
} }
void TextMarkRegistry::editorOpened(IEditor *editor) void TextMarkRegistry::editorOpened(IEditor *editor)
@@ -496,10 +492,10 @@ void TextMarkRegistry::editorOpened(IEditor *editor)
auto document = qobject_cast<TextDocument *>(editor ? editor->document() : nullptr); auto document = qobject_cast<TextDocument *>(editor ? editor->document() : nullptr);
if (!document) if (!document)
return; return;
if (!m_marks.contains(document->filePath())) if (!s_marks.contains(document->filePath()))
return; return;
const QSet<TextMark *> marks = m_marks.value(document->filePath()); const QSet<TextMark *> marks = s_marks.value(document->filePath());
for (TextMark *mark : marks) for (TextMark *mark : marks)
document->addMark(mark); document->addMark(mark);
} }
@@ -511,7 +507,7 @@ void TextMarkRegistry::documentRenamed(IDocument *document,
auto baseTextDocument = qobject_cast<TextDocument *>(document); auto baseTextDocument = qobject_cast<TextDocument *>(document);
if (!baseTextDocument) if (!baseTextDocument)
return; return;
if (!m_marks.contains(oldPath)) if (!s_marks.contains(oldPath))
return; return;
QSet<TextMark *> toBeMoved; QSet<TextMark *> toBeMoved;
@@ -519,8 +515,8 @@ void TextMarkRegistry::documentRenamed(IDocument *document,
for (TextMark *mark : marks) for (TextMark *mark : marks)
toBeMoved.insert(mark); toBeMoved.insert(mark);
m_marks[oldPath].subtract(toBeMoved); s_marks[oldPath].subtract(toBeMoved);
m_marks[newPath].unite(toBeMoved); s_marks[newPath].unite(toBeMoved);
for (TextMark *mark : std::as_const(toBeMoved)) for (TextMark *mark : std::as_const(toBeMoved))
mark->updateFilePath(newPath); mark->updateFilePath(newPath);
@@ -528,13 +524,13 @@ void TextMarkRegistry::documentRenamed(IDocument *document,
void TextMarkRegistry::allDocumentsRenamed(const FilePath &oldPath, const FilePath &newPath) void TextMarkRegistry::allDocumentsRenamed(const FilePath &oldPath, const FilePath &newPath)
{ {
if (!m_marks.contains(oldPath)) if (!s_marks.contains(oldPath))
return; return;
const QSet<TextMark *> oldFileNameMarks = m_marks.value(oldPath); const QSet<TextMark *> oldFileNameMarks = s_marks.value(oldPath);
m_marks[newPath].unite(oldFileNameMarks); s_marks[newPath].unite(oldFileNameMarks);
m_marks[oldPath].clear(); s_marks[oldPath].clear();
for (TextMark *mark : oldFileNameMarks) for (TextMark *mark : oldFileNameMarks)
mark->updateFilePath(newPath); mark->updateFilePath(newPath);
@@ -574,5 +570,3 @@ void setupTextMarkRegistry(QObject *guard)
} }
} // namespace TextEditor } // namespace TextEditor
#include "textmark.moc"