Skip to content
This repository was archived by the owner on May 4, 2023. It is now read-only.

Commit dc430b6

Browse files
committed
#60: Add checks to not save duplicate Ranges and RosieFixes
1 parent 20a839d commit dc430b6

1 file changed

Lines changed: 25 additions & 14 deletions

File tree

src/diagnostics/diagnostics.ts

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ import { getRulesFromCache } from "../rosie/rosieCache";
2121
const DIAGNOSTICS_TIMESTAMP: Map<string, number> = new Map();
2222
const FIXES_BY_DOCUMENT: Map<
2323
vscode.Uri,
24-
Map<vscode.Range, RosieFix[]>
24+
//Uses a string key (the JSON stringified vscode.Range) because Map.has() works based on === equality.
25+
// Having vscode.Range as key sometimes resulted in the same range added multiple times with the same
26+
// fixes in 'registerFixForDocument'.
27+
Map<string, [vscode.Range, RosieFix[]]>
2528
> = new Map();
2629

2730
/**
@@ -39,9 +42,9 @@ export const getFixesForDocument = (
3942
const fixesForDocument = FIXES_BY_DOCUMENT.get(documentUri);
4043
const result: RosieFix[] = [];
4144
if (fixesForDocument) {
42-
for (const k of fixesForDocument.keys()) {
43-
if (k.contains(range)) {
44-
fixesForDocument.get(k)?.forEach((f) => result.push(f));
45+
for (const rangeAndFixes of fixesForDocument.values()) {
46+
if (rangeAndFixes[0].contains(range)) {
47+
rangeAndFixes[1]?.forEach((f) => result.push(f));
4548
}
4649
}
4750
}
@@ -52,9 +55,12 @@ export const getFixesForDocument = (
5255
* Register a fix for a document and a range. When we analyze the file,
5356
* we store all the quick fixes in a Map so that we can retrieve them
5457
* later when the user hover the fixes.
55-
* @param documentUri
56-
* @param range
57-
* @param fix
58+
*
59+
* It makes sure that no duplicate ranges, and no duplicate fixes are added.
60+
*
61+
* @param documentUri - the URI of the analyzed VS Code document
62+
* @param range - the range we are at in the document
63+
* @param fix - the quick fix to register for this document and range
5864
*/
5965
const registerFixForDocument = (
6066
documentUri: vscode.Uri,
@@ -66,16 +72,21 @@ const registerFixForDocument = (
6672
FIXES_BY_DOCUMENT.set(documentUri, new Map());
6773
}
6874

69-
// Query the ranges saved for this document, and if the currently inspected range is not saved
75+
// Query the ranges saved for this document, and if the currently inspected range is not saved,
7076
// associate an empty list of fixes to it. Otherwise, add the fix for this range.
71-
const fixesForDocument = FIXES_BY_DOCUMENT.get(documentUri);
72-
if (!fixesForDocument?.has(range)) {
73-
FIXES_BY_DOCUMENT.get(documentUri)?.set(range, []);
77+
const rangeAndFixesForDocument = FIXES_BY_DOCUMENT.get(documentUri);
78+
let rangeString = JSON.stringify(range);
79+
if (!rangeAndFixesForDocument?.has(rangeString)) {
80+
rangeAndFixesForDocument?.set(rangeString, [range, []]);
7481
}
7582

76-
// This aims to prevent adding the same fix more than once
77-
if (!FIXES_BY_DOCUMENT.get(documentUri)?.get(range)?.includes(fix)) {
78-
FIXES_BY_DOCUMENT.get(documentUri)?.get(range)?.push(fix);
83+
if (rangeAndFixesForDocument?.get(rangeString)) {
84+
// @ts-ignore
85+
let fixesForRange = rangeAndFixesForDocument?.get(rangeString)[1];
86+
// If the fix hasn't been added to this range, add it.
87+
if (fixesForRange?.filter(f => JSON.stringify(f) === JSON.stringify(fix)).length === 0) {
88+
fixesForRange?.push(fix);
89+
}
7990
}
8091
};
8192

0 commit comments

Comments
 (0)