4
4
using System ;
5
5
using System . Collections . Generic ;
6
6
using System . Text ;
7
+ using System . Threading ;
7
8
using System . Threading . Tasks ;
8
9
using Microsoft . PowerShell . EditorServices . Services ;
9
10
using Microsoft . PowerShell . EditorServices . Services . Symbols ;
@@ -53,12 +54,14 @@ public ReferencesCodeLensProvider(WorkspaceService workspaceService, SymbolsServ
53
54
/// Get all reference code lenses for a given script file.
54
55
/// </summary>
55
56
/// <param name="scriptFile">The PowerShell script file to get code lenses for.</param>
57
+ /// <param name="cancellationToken"></param>
56
58
/// <returns>An array of CodeLenses describing all functions in the given script file.</returns>
57
- public CodeLens [ ] ProvideCodeLenses ( ScriptFile scriptFile )
59
+ public CodeLens [ ] ProvideCodeLenses ( ScriptFile scriptFile , CancellationToken cancellationToken )
58
60
{
59
61
List < CodeLens > acc = new ( ) ;
60
62
foreach ( SymbolReference sym in _symbolProvider . ProvideDocumentSymbols ( scriptFile ) )
61
63
{
64
+ cancellationToken . ThrowIfCancellationRequested ( ) ;
62
65
if ( sym . SymbolType == SymbolType . Function )
63
66
{
64
67
acc . Add ( new CodeLens
@@ -68,7 +71,7 @@ public CodeLens[] ProvideCodeLenses(ScriptFile scriptFile)
68
71
Uri = scriptFile . DocumentUri ,
69
72
ProviderId = nameof ( ReferencesCodeLensProvider )
70
73
} , LspSerializer . Instance . JsonSerializer ) ,
71
- Range = sym . ScriptRegion . ToRange ( )
74
+ Range = sym . ScriptRegion . ToRange ( ) ,
72
75
} ) ;
73
76
}
74
77
}
@@ -81,8 +84,12 @@ public CodeLens[] ProvideCodeLenses(ScriptFile scriptFile)
81
84
/// </summary>
82
85
/// <param name="codeLens">The old code lens to get updated references for.</param>
83
86
/// <param name="scriptFile"></param>
87
+ /// <param name="cancellationToken"></param>
84
88
/// <returns>A new code lens object describing the same data as the old one but with updated references.</returns>
85
- public async Task < CodeLens > ResolveCodeLens ( CodeLens codeLens , ScriptFile scriptFile )
89
+ public async Task < CodeLens > ResolveCodeLens (
90
+ CodeLens codeLens ,
91
+ ScriptFile scriptFile ,
92
+ CancellationToken cancellationToken )
86
93
{
87
94
ScriptFile [ ] references = _workspaceService . ExpandScriptReferences (
88
95
scriptFile ) ;
@@ -93,9 +100,10 @@ public async Task<CodeLens> ResolveCodeLens(CodeLens codeLens, ScriptFile script
93
100
codeLens . Range . Start . Character + 1 ) ;
94
101
95
102
List < SymbolReference > referencesResult = await _symbolsService . FindReferencesOfSymbol (
96
- foundSymbol ,
97
- references ,
98
- _workspaceService ) . ConfigureAwait ( false ) ;
103
+ foundSymbol ,
104
+ references ,
105
+ _workspaceService ,
106
+ cancellationToken ) . ConfigureAwait ( false ) ;
99
107
100
108
Location [ ] referenceLocations ;
101
109
if ( referencesResult == null )
@@ -107,6 +115,10 @@ public async Task<CodeLens> ResolveCodeLens(CodeLens codeLens, ScriptFile script
107
115
List < Location > acc = new ( ) ;
108
116
foreach ( SymbolReference foundReference in referencesResult )
109
117
{
118
+ // This async method is pretty dense with synchronous code
119
+ // so it's helpful to add some yields.
120
+ await Task . Yield ( ) ;
121
+ cancellationToken . ThrowIfCancellationRequested ( ) ;
110
122
if ( IsReferenceDefinition ( foundSymbol , foundReference ) )
111
123
{
112
124
continue ;
@@ -140,9 +152,9 @@ public async Task<CodeLens> ResolveCodeLens(CodeLens codeLens, ScriptFile script
140
152
Title = GetReferenceCountHeader ( referenceLocations . Length ) ,
141
153
Arguments = JArray . FromObject ( new object [ ]
142
154
{
143
- scriptFile . DocumentUri ,
144
- codeLens . Range . Start ,
145
- referenceLocations
155
+ scriptFile . DocumentUri ,
156
+ codeLens . Range . Start ,
157
+ referenceLocations
146
158
} ,
147
159
LspSerializer . Instance . JsonSerializer )
148
160
}
0 commit comments