@@ -21,7 +21,10 @@ import { getRulesFromCache } from "../rosie/rosieCache";
2121const DIAGNOSTICS_TIMESTAMP : Map < string , number > = new Map ( ) ;
2222const 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,24 +55,39 @@ 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 */
5965const registerFixForDocument = (
6066 documentUri : vscode . Uri ,
6167 range : vscode . Range ,
6268 fix : RosieFix
6369) : void => {
70+ // If there is no range or fix saved for this document, save the document
6471 if ( ! FIXES_BY_DOCUMENT . has ( documentUri ) ) {
6572 FIXES_BY_DOCUMENT . set ( documentUri , new Map ( ) ) ;
6673 }
67- const fixesForDocument = FIXES_BY_DOCUMENT . get ( documentUri ) ;
68- if ( ! fixesForDocument ?. has ( range ) ) {
69- FIXES_BY_DOCUMENT . get ( documentUri ) ?. set ( range , [ ] ) ;
74+
75+ // Query the ranges saved for this document, and if the currently inspected range is not saved,
76+ // associate an empty list of fixes to it. Otherwise, add the fix for this range.
77+ const rangeAndFixesForDocument = FIXES_BY_DOCUMENT . get ( documentUri ) ;
78+ const rangeString = JSON . stringify ( range ) ;
79+ if ( ! rangeAndFixesForDocument ?. has ( rangeString ) ) {
80+ rangeAndFixesForDocument ?. set ( rangeString , [ range , [ ] ] ) ;
7081 }
7182
72- 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+ }
90+ }
7391} ;
7492
7593/**
0 commit comments