-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHighlightService.cpp
More file actions
97 lines (81 loc) · 2.95 KB
/
Copy pathHighlightService.cpp
File metadata and controls
97 lines (81 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "syntax/HighlightService.h"
#include "editor/EditorWidget.h"
#include "syntax/HighlightApplier.h"
#include <algorithm>
namespace cold {
HighlightService::HighlightService(QObject* parent) : QObject(parent) {}
void HighlightService::bindEditor(const QString& docKey, EditorWidget* editor) {
if (docKey.isEmpty() || !editor) {
return;
}
pruneEditors(docKey);
QVector<QPointer<EditorWidget>>& editors = m_editors[docKey];
for (const QPointer<EditorWidget>& existing : std::as_const(editors)) {
if (existing == editor) {
return;
}
}
editors.push_back(editor);
}
void HighlightService::unbindEditor(const QString& docKey, EditorWidget* editor) {
if (!m_editors.contains(docKey)) {
return;
}
QVector<QPointer<EditorWidget>>& editors = m_editors[docKey];
editors.erase(std::remove_if(editors.begin(), editors.end(),
[editor](const auto& candidate) {
return candidate.isNull() || candidate.data() == editor;
}),
editors.end());
if (editors.isEmpty()) {
m_editors.remove(docKey);
}
}
void HighlightService::unbindEditor(const QString& docKey) {
m_editors.remove(docKey);
}
void HighlightService::removeDocument(const QString& docKey) {
const uint64_t nextVersion = m_appliedVersion.value(docKey, 0) + 1;
QVector<QPointer<EditorWidget>> editors = m_editors.take(docKey);
m_appliedVersion.remove(docKey);
HighlightSnapshot empty;
empty.docKey = docKey;
empty.version = nextVersion;
for (const QPointer<EditorWidget>& editor : std::as_const(editors)) {
if (editor) {
m_applier.applyToEditor(editor, empty);
}
}
}
void HighlightService::applySnapshotFromLsp(const QString& docKey,
const HighlightSnapshot& snapshot) {
Q_UNUSED(docKey);
applySnapshot(snapshot);
}
void HighlightService::applySnapshot(const HighlightSnapshot& snapshot) {
const uint64_t applied = m_appliedVersion.value(snapshot.docKey, 0);
if (snapshot.version <= applied) {
return;
}
m_appliedVersion[snapshot.docKey] = snapshot.version;
pruneEditors(snapshot.docKey);
const QVector<QPointer<EditorWidget>> editors = m_editors.value(snapshot.docKey);
for (const QPointer<EditorWidget>& editor : editors) {
if (editor) {
m_applier.applyToEditor(editor, snapshot);
}
}
}
void HighlightService::pruneEditors(const QString& docKey) {
if (!m_editors.contains(docKey)) {
return;
}
QVector<QPointer<EditorWidget>>& editors = m_editors[docKey];
editors.erase(std::remove_if(editors.begin(), editors.end(),
[](const auto& editor) { return editor.isNull(); }),
editors.end());
if (editors.isEmpty()) {
m_editors.remove(docKey);
}
}
} // namespace cold