19
19
20
20
namespace Microsoft . PowerShell . EditorServices . Handlers
21
21
{
22
- // TODO: Use ABCs.
23
- internal class PsesCodeLensHandlers : ICodeLensHandler , ICodeLensResolveHandler
22
+ internal class PsesCodeLensHandlers : CodeLensHandlerBase
24
23
{
25
24
private readonly ILogger _logger ;
26
25
private readonly SymbolsService _symbolsService ;
27
26
private readonly WorkspaceService _workspaceService ;
28
- private CodeLensCapability _capability ;
29
- private readonly Guid _id = Guid . NewGuid ( ) ;
30
- Guid ICanBeIdentifiedHandler . Id => _id ;
31
27
32
- public PsesCodeLensHandlers ( ILoggerFactory factory , SymbolsService symbolsService , WorkspaceService workspaceService , ConfigurationService configurationService )
28
+ public PsesCodeLensHandlers ( ILoggerFactory factory , SymbolsService symbolsService , WorkspaceService workspaceService )
33
29
{
34
30
_logger = factory . CreateLogger < PsesCodeLensHandlers > ( ) ;
35
31
_workspaceService = workspaceService ;
36
32
_symbolsService = symbolsService ;
37
33
}
38
34
39
- public CodeLensRegistrationOptions GetRegistrationOptions ( CodeLensCapability capability , ClientCapabilities clientCapabilities ) => new CodeLensRegistrationOptions
35
+ protected override CodeLensRegistrationOptions CreateRegistrationOptions ( CodeLensCapability capability , ClientCapabilities clientCapabilities ) => new ( )
40
36
{
41
37
DocumentSelector = LspUtils . PowerShellDocumentSelector ,
42
38
ResolveProvider = true
43
39
} ;
44
40
45
- public void SetCapability ( CodeLensCapability capability , ClientCapabilities clientCapabilities )
46
- {
47
- _capability = capability ;
48
- }
49
-
50
- public Task < CodeLensContainer > Handle ( CodeLensParams request , CancellationToken cancellationToken )
41
+ public override Task < CodeLensContainer > Handle ( CodeLensParams request , CancellationToken cancellationToken )
51
42
{
52
43
ScriptFile scriptFile = _workspaceService . GetFile ( request . TextDocument . Uri ) ;
53
-
54
44
CodeLens [ ] codeLensResults = ProvideCodeLenses ( scriptFile ) ;
55
-
56
45
return Task . FromResult ( new CodeLensContainer ( codeLensResults ) ) ;
57
46
}
58
47
59
- public bool CanResolve ( CodeLens value )
60
- {
61
- CodeLensData codeLensData = value . Data . ToObject < CodeLensData > ( ) ;
62
- return value ? . Data != null && _symbolsService . GetCodeLensProviders ( ) . Any ( provider => provider . ProviderId . Equals ( codeLensData . ProviderId ) ) ;
63
- }
64
-
65
- public Task < CodeLens > Handle ( CodeLens request , CancellationToken cancellationToken )
48
+ public override Task < CodeLens > Handle ( CodeLens request , CancellationToken cancellationToken )
66
49
{
67
50
// TODO: Catch deserializtion exception on bad object
68
51
CodeLensData codeLensData = request . Data . ToObject < CodeLensData > ( ) ;
69
52
70
53
ICodeLensProvider originalProvider = _symbolsService
71
54
. GetCodeLensProviders ( )
72
- . FirstOrDefault ( provider => provider . ProviderId . Equals ( codeLensData . ProviderId ) ) ;
55
+ . FirstOrDefault ( provider => provider . ProviderId . Equals ( codeLensData . ProviderId , StringComparison . Ordinal ) ) ;
73
56
74
- ScriptFile scriptFile =
75
- _workspaceService . GetFile (
76
- codeLensData . Uri ) ;
57
+ ScriptFile scriptFile = _workspaceService . GetFile ( codeLensData . Uri ) ;
77
58
78
59
return originalProvider . ResolveCodeLens ( request , scriptFile ) ;
79
60
}
80
61
81
- public void SetCapability ( CodeLensCapability capability )
82
- {
83
- _capability = capability ;
84
- }
85
-
86
62
/// <summary>
87
63
/// Get all the CodeLenses for a given script file.
88
64
/// </summary>
@@ -104,30 +80,23 @@ private CodeLens[] ProvideCodeLenses(ScriptFile scriptFile)
104
80
/// An IEnumerable containing the results of all providers
105
81
/// that were invoked successfully.
106
82
/// </returns>
107
- private IEnumerable < TResult > InvokeProviders < TResult > (
108
- Func < ICodeLensProvider , TResult > invokeFunc )
83
+ private IEnumerable < TResult > InvokeProviders < TResult > ( Func < ICodeLensProvider , TResult > invokeFunc )
109
84
{
110
- Stopwatch invokeTimer = new Stopwatch ( ) ;
111
- List < TResult > providerResults = new List < TResult > ( ) ;
85
+ Stopwatch invokeTimer = new ( ) ;
86
+ List < TResult > providerResults = new ( ) ;
112
87
113
88
foreach ( ICodeLensProvider provider in _symbolsService . GetCodeLensProviders ( ) )
114
89
{
115
90
try
116
91
{
117
92
invokeTimer . Restart ( ) ;
118
-
119
93
providerResults . Add ( invokeFunc ( provider ) ) ;
120
-
121
94
invokeTimer . Stop ( ) ;
122
-
123
- this . _logger . LogTrace (
124
- $ "Invocation of provider '{ provider . GetType ( ) . Name } ' completed in { invokeTimer . ElapsedMilliseconds } ms.") ;
95
+ _logger . LogTrace ( $ "Invocation of provider '{ provider . GetType ( ) . Name } ' completed in { invokeTimer . ElapsedMilliseconds } ms.") ;
125
96
}
126
97
catch ( Exception e )
127
98
{
128
- this . _logger . LogException (
129
- $ "Exception caught while invoking provider { provider . GetType ( ) . Name } :",
130
- e ) ;
99
+ _logger . LogException ( $ "Exception caught while invoking provider { provider . GetType ( ) . Name } :", e ) ;
131
100
}
132
101
}
133
102
0 commit comments