@@ -21,20 +21,12 @@ import { updateRangeInfos } from "./updateRangeInfos";
2121export class RangeUpdater {
2222 private rangeInfoLists : Map < string , FullRangeInfo [ ] [ ] > = new Map ( ) ;
2323 private replaceEditLists : Map < string , Edit [ ] [ ] > = new Map ( ) ;
24- private disposable ! : Disposable ;
24+ private disposables : Disposable [ ] = [ ] ;
2525
2626 constructor ( ) {
2727 this . listenForDocumentChanges ( ) ;
2828 }
2929
30- private getDocumentRangeInfoLists ( document : TextDocument ) {
31- return getDefault ( this . rangeInfoLists , document . uri . toString ( ) , ( ) => [ ] ) ;
32- }
33-
34- private getDocumentReplaceEditLists ( document : TextDocument ) {
35- return getDefault ( this . replaceEditLists , document . uri . toString ( ) , ( ) => [ ] ) ;
36- }
37-
3830 /**
3931 * Registers a list of range infos to be kept up to date. It is ok to
4032 * add to this list after registering it; any items in the list at the time of
@@ -49,11 +41,26 @@ export class RangeUpdater {
4941 document : TextDocument ,
5042 rangeInfoList : FullRangeInfo [ ] ,
5143 ) : ( ) => void {
52- const documentRangeInfoLists = this . getDocumentRangeInfoLists ( document ) ;
44+ const key = document . uri . toString ( ) ;
45+ const documentRangeInfoLists = getDefault (
46+ this . rangeInfoLists ,
47+ key ,
48+ ( ) => [ ] ,
49+ ) ;
5350
5451 documentRangeInfoLists . push ( rangeInfoList ) ;
5552
56- return ( ) => pull ( documentRangeInfoLists , rangeInfoList ) ;
53+ return ( ) => {
54+ pull ( documentRangeInfoLists , rangeInfoList ) ;
55+ // If there are no more lists for this document, remove the entry from the map to avoid memory leaks
56+ // Note that we check that the value in the map is the same array that we modified before deleting.
57+ if (
58+ documentRangeInfoLists . length === 0 &&
59+ this . rangeInfoLists . get ( key ) === documentRangeInfoLists
60+ ) {
61+ this . rangeInfoLists . delete ( key ) ;
62+ }
63+ } ;
5764 }
5865
5966 /**
@@ -76,15 +83,29 @@ export class RangeUpdater {
7683 document : TextDocument ,
7784 replaceEditList : Edit [ ] ,
7885 ) : ( ) => void {
79- const documentReplaceEditLists = this . getDocumentReplaceEditLists ( document ) ;
86+ const key = document . uri . toString ( ) ;
87+ const documentReplaceEditLists = getDefault (
88+ this . replaceEditLists ,
89+ key ,
90+ ( ) => [ ] ,
91+ ) ;
8092
8193 documentReplaceEditLists . push ( replaceEditList ) ;
8294
83- return ( ) => pull ( documentReplaceEditLists , replaceEditList ) ;
95+ return ( ) => {
96+ pull ( documentReplaceEditLists , replaceEditList ) ;
97+ if (
98+ documentReplaceEditLists . length === 0 &&
99+ this . replaceEditLists . get ( key ) === documentReplaceEditLists
100+ ) {
101+ this . replaceEditLists . delete ( key ) ;
102+ }
103+ } ;
84104 }
85105
86106 private * documentRangeInfoGenerator ( document : TextDocument ) {
87- const documentRangeInfoLists = this . getDocumentRangeInfoLists ( document ) ;
107+ const documentRangeInfoLists =
108+ this . rangeInfoLists . get ( document . uri . toString ( ) ) ?? [ ] ;
88109
89110 for ( const rangeInfoLists of documentRangeInfoLists ) {
90111 for ( const rangeInfo of rangeInfoLists ) {
@@ -94,11 +115,10 @@ export class RangeUpdater {
94115 }
95116
96117 private listenForDocumentChanges ( ) {
97- this . disposable = ide ( ) . onDidChangeTextDocument (
98- ( event : TextDocumentChangeEvent ) => {
99- const documentReplaceEditLists = this . getDocumentReplaceEditLists (
100- event . document ,
101- ) ;
118+ this . disposables . push (
119+ ide ( ) . onDidChangeTextDocument ( ( event : TextDocumentChangeEvent ) => {
120+ const documentReplaceEditLists =
121+ this . replaceEditLists . get ( event . document . uri . toString ( ) ) ?? [ ] ;
102122
103123 const extendedEvent : ExtendedTextDocumentChangeEvent = {
104124 ...event ,
@@ -116,12 +136,18 @@ export class RangeUpdater {
116136 extendedEvent ,
117137 this . documentRangeInfoGenerator ( event . document ) ,
118138 ) ;
119- } ,
139+ } ) ,
140+
141+ ide ( ) . onDidCloseTextDocument ( ( document ) => {
142+ const key = document . uri . toString ( ) ;
143+ this . rangeInfoLists . delete ( key ) ;
144+ this . replaceEditLists . delete ( key ) ;
145+ } ) ,
120146 ) ;
121147 }
122148
123149 dispose ( ) {
124- this . disposable . dispose ( ) ;
150+ this . disposables . forEach ( ( disposable ) => disposable . dispose ( ) ) ;
125151 }
126152}
127153
0 commit comments