8
8
9
9
namespace Microsoft . AspNetCore . Razor . LanguageServer ;
10
10
11
- internal class CodeDocumentReferenceHolder : DocumentProcessedListener
11
+ internal class CodeDocumentReferenceHolder : IDocumentProcessedListener
12
12
{
13
- private Dictionary < DocumentKey , RazorCodeDocument > _codeDocumentCache ;
14
- private IProjectSnapshotManager ? _projectManager ;
13
+ private readonly IProjectSnapshotManager _projectManager ;
14
+ private readonly Dictionary < DocumentKey , RazorCodeDocument > _codeDocumentCache ;
15
15
16
- public CodeDocumentReferenceHolder ( )
16
+ public CodeDocumentReferenceHolder ( IProjectSnapshotManager projectManager )
17
17
{
18
- _codeDocumentCache = new ( ) ;
18
+ _projectManager = projectManager ;
19
+ _codeDocumentCache = [ ] ;
20
+
21
+ _projectManager . Changed += ProjectManager_Changed ;
19
22
}
20
23
21
- public override void DocumentProcessed ( RazorCodeDocument codeDocument , IDocumentSnapshot documentSnapshot )
24
+ public void DocumentProcessed ( RazorCodeDocument codeDocument , IDocumentSnapshot documentSnapshot )
22
25
{
23
26
// We capture a reference to the code document after a document has been processed in order to ensure that
24
27
// latest document state information is readily available without re-computation. The DocumentState type
25
28
// (brains of DocumentSnapshot) will garbage collect its generated output aggressively and due to the
26
29
// nature of LSP being heavily asynchronous (multiple requests for single keystrokes) we don't want to cause
27
30
// multiple parses/regenerations across LSP requests that are all for the same document version.
28
- var key = new DocumentKey ( documentSnapshot . Project . Key , documentSnapshot . FilePath . AssumeNotNull ( ) ) ;
29
- _codeDocumentCache [ key ] = codeDocument ;
30
- }
31
-
32
- public override void Initialize ( IProjectSnapshotManager projectManager )
33
- {
34
- _projectManager = projectManager ;
35
- _projectManager . Changed += ProjectManager_Changed ;
31
+ lock ( _codeDocumentCache )
32
+ {
33
+ var key = new DocumentKey ( documentSnapshot . Project . Key , documentSnapshot . FilePath . AssumeNotNull ( ) ) ;
34
+ _codeDocumentCache [ key ] = codeDocument ;
35
+ }
36
36
}
37
37
38
38
private void ProjectManager_Changed ( object ? sender , ProjectChangeEventArgs args )
@@ -45,26 +45,38 @@ private void ProjectManager_Changed(object? sender, ProjectChangeEventArgs args)
45
45
case ProjectChangeKind . ProjectChanged :
46
46
foreach ( var documentFilePath in args . Newer ! . DocumentFilePaths )
47
47
{
48
- var key = new DocumentKey ( args . Newer . Key , documentFilePath ) ;
49
- _codeDocumentCache . Remove ( key ) ;
48
+ lock ( _codeDocumentCache )
49
+ {
50
+ var key = new DocumentKey ( args . Newer . Key , documentFilePath ) ;
51
+ _codeDocumentCache . Remove ( key ) ;
52
+ }
50
53
}
51
54
52
55
break ;
56
+
53
57
case ProjectChangeKind . ProjectRemoved :
54
58
foreach ( var documentFilePath in args . Older ! . DocumentFilePaths )
55
59
{
56
- var key = new DocumentKey ( args . Older . Key , documentFilePath ) ;
57
- _codeDocumentCache . Remove ( key ) ;
60
+ lock ( _codeDocumentCache )
61
+ {
62
+ var key = new DocumentKey ( args . Older . Key , documentFilePath ) ;
63
+ _codeDocumentCache . Remove ( key ) ;
64
+ }
58
65
}
59
66
60
67
break ;
68
+
61
69
case ProjectChangeKind . DocumentChanged :
62
70
case ProjectChangeKind . DocumentRemoved :
63
71
{
64
- var key = new DocumentKey ( args . ProjectKey , args . DocumentFilePath . AssumeNotNull ( ) ) ;
65
- _codeDocumentCache . Remove ( key ) ;
66
- break ;
72
+ lock ( _codeDocumentCache )
73
+ {
74
+ var key = new DocumentKey ( args . ProjectKey , args . DocumentFilePath . AssumeNotNull ( ) ) ;
75
+ _codeDocumentCache . Remove ( key ) ;
76
+ }
67
77
}
78
+
79
+ break ;
68
80
}
69
81
}
70
82
}
0 commit comments