Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ce82fac

Browse files
authored
fix(vscode): flatten reactivity visualization decorators (#5642)
1 parent 29570bd commit ce82fac

File tree

1 file changed

+40
-20
lines changed

1 file changed

+40
-20
lines changed

extensions/vscode/lib/reactivityVisualization.ts

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { getReactiveReferences } from '@vue/typescript-plugin/lib/requests/
22
import * as vscode from 'vscode';
33
import { config } from './config';
44

5-
const dependenciesDecorations = vscode.window.createTextEditorDecorationType({
5+
const dependencyDecorations = vscode.window.createTextEditorDecorationType({
66
isWholeLine: true,
77
backgroundColor: 'rgba(120,120,255,0.1)',
88
border: '1px solid rgba(120,120,255,0.6)',
@@ -12,7 +12,7 @@ const dependenciesDecorations = vscode.window.createTextEditorDecorationType({
1212
// color: 'rgba(120,120,255,0.6)',
1313
// },
1414
});
15-
const subscribersDecorations = vscode.window.createTextEditorDecorationType({
15+
const dependentDecorations = vscode.window.createTextEditorDecorationType({
1616
// outlineColor: 'rgba(80,200,80,0.6)',
1717
// outlineStyle: 'dashed',
1818
// borderRadius: '3px',
@@ -62,8 +62,8 @@ export function activate(
6262
return;
6363
}
6464
if (!config.editor.reactivityVisualization) {
65-
editor.setDecorations(dependenciesDecorations, []);
66-
editor.setDecorations(subscribersDecorations, []);
65+
editor.setDecorations(dependencyDecorations, []);
66+
editor.setDecorations(dependentDecorations, []);
6767
return;
6868
}
6969

@@ -82,27 +82,47 @@ export function activate(
8282
{ isAsync: true, lowPriority: true },
8383
);
8484
editor.setDecorations(
85-
dependenciesDecorations,
86-
result?.body?.dependencyRanges.map(range =>
87-
new vscode.Range(
88-
document.positionAt(range.start),
89-
document.positionAt(range.end),
90-
)
91-
) ?? [],
85+
dependencyDecorations,
86+
getFlatRanges(document, result?.body?.dependencyRanges ?? []),
9287
);
9388
editor.setDecorations(
94-
subscribersDecorations,
95-
result?.body?.dependentRanges.map(range =>
96-
new vscode.Range(
97-
document.positionAt(range.start),
98-
document.positionAt(range.end),
99-
)
100-
) ?? [],
89+
dependentDecorations,
90+
getFlatRanges(document, result?.body?.dependentRanges ?? []),
10191
);
10292
}
10393
catch {
104-
editor.setDecorations(dependenciesDecorations, []);
105-
editor.setDecorations(subscribersDecorations, []);
94+
editor.setDecorations(dependencyDecorations, []);
95+
editor.setDecorations(dependentDecorations, []);
10696
}
10797
}
10898
}
99+
100+
function getFlatRanges(document: vscode.TextDocument, ranges: {
101+
start: number;
102+
end: number;
103+
}[]) {
104+
const documentRanges = ranges
105+
.map(range =>
106+
new vscode.Range(
107+
document.positionAt(range.start),
108+
document.positionAt(range.end),
109+
)
110+
)
111+
.sort((a, b) => a.start.compareTo(b.start));
112+
113+
for (let i = 1; i < documentRanges.length; i++) {
114+
const prev = documentRanges[i - 1]!;
115+
const curr = documentRanges[i]!;
116+
117+
if (prev.end.compareTo(curr.start) >= 0) {
118+
if (curr.end.compareTo(prev.end) <= 0) {
119+
documentRanges.splice(i, 1);
120+
}
121+
else {
122+
documentRanges.splice(i - 1, 2, new vscode.Range(prev.start, curr.end));
123+
}
124+
i--;
125+
}
126+
}
127+
return documentRanges;
128+
}

0 commit comments

Comments
 (0)