1919
2020namespace Microsoft . PowerShell . EditorServices . Handlers
2121{
22- // TODO: Use ABCs.
23- internal class PsesCodeLensHandlers : ICodeLensHandler , ICodeLensResolveHandler
22+ internal class PsesCodeLensHandlers : CodeLensHandlerBase
2423 {
2524 private readonly ILogger _logger ;
2625 private readonly SymbolsService _symbolsService ;
2726 private readonly WorkspaceService _workspaceService ;
28- private CodeLensCapability _capability ;
29- private readonly Guid _id = Guid . NewGuid ( ) ;
30- Guid ICanBeIdentifiedHandler . Id => _id ;
3127
32- public PsesCodeLensHandlers ( ILoggerFactory factory , SymbolsService symbolsService , WorkspaceService workspaceService , ConfigurationService configurationService )
28+ public PsesCodeLensHandlers ( ILoggerFactory factory , SymbolsService symbolsService , WorkspaceService workspaceService )
3329 {
3430 _logger = factory . CreateLogger < PsesCodeLensHandlers > ( ) ;
3531 _workspaceService = workspaceService ;
3632 _symbolsService = symbolsService ;
3733 }
3834
39- public CodeLensRegistrationOptions GetRegistrationOptions ( CodeLensCapability capability , ClientCapabilities clientCapabilities ) => new CodeLensRegistrationOptions
35+ protected override CodeLensRegistrationOptions CreateRegistrationOptions ( CodeLensCapability capability , ClientCapabilities clientCapabilities ) => new ( )
4036 {
4137 DocumentSelector = LspUtils . PowerShellDocumentSelector ,
4238 ResolveProvider = true
4339 } ;
4440
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 )
5142 {
5243 ScriptFile scriptFile = _workspaceService . GetFile ( request . TextDocument . Uri ) ;
53-
5444 CodeLens [ ] codeLensResults = ProvideCodeLenses ( scriptFile ) ;
55-
5645 return Task . FromResult ( new CodeLensContainer ( codeLensResults ) ) ;
5746 }
5847
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 )
6649 {
6750 // TODO: Catch deserializtion exception on bad object
6851 CodeLensData codeLensData = request . Data . ToObject < CodeLensData > ( ) ;
6952
7053 ICodeLensProvider originalProvider = _symbolsService
7154 . GetCodeLensProviders ( )
72- . FirstOrDefault ( provider => provider . ProviderId . Equals ( codeLensData . ProviderId ) ) ;
55+ . FirstOrDefault ( provider => provider . ProviderId . Equals ( codeLensData . ProviderId , StringComparison . Ordinal ) ) ;
7356
74- ScriptFile scriptFile =
75- _workspaceService . GetFile (
76- codeLensData . Uri ) ;
57+ ScriptFile scriptFile = _workspaceService . GetFile ( codeLensData . Uri ) ;
7758
7859 return originalProvider . ResolveCodeLens ( request , scriptFile ) ;
7960 }
8061
81- public void SetCapability ( CodeLensCapability capability )
82- {
83- _capability = capability ;
84- }
85-
8662 /// <summary>
8763 /// Get all the CodeLenses for a given script file.
8864 /// </summary>
@@ -104,30 +80,23 @@ private CodeLens[] ProvideCodeLenses(ScriptFile scriptFile)
10480 /// An IEnumerable containing the results of all providers
10581 /// that were invoked successfully.
10682 /// </returns>
107- private IEnumerable < TResult > InvokeProviders < TResult > (
108- Func < ICodeLensProvider , TResult > invokeFunc )
83+ private IEnumerable < TResult > InvokeProviders < TResult > ( Func < ICodeLensProvider , TResult > invokeFunc )
10984 {
110- Stopwatch invokeTimer = new Stopwatch ( ) ;
111- List < TResult > providerResults = new List < TResult > ( ) ;
85+ Stopwatch invokeTimer = new ( ) ;
86+ List < TResult > providerResults = new ( ) ;
11287
11388 foreach ( ICodeLensProvider provider in _symbolsService . GetCodeLensProviders ( ) )
11489 {
11590 try
11691 {
11792 invokeTimer . Restart ( ) ;
118-
11993 providerResults . Add ( invokeFunc ( provider ) ) ;
120-
12194 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.") ;
12596 }
12697 catch ( Exception e )
12798 {
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 ) ;
131100 }
132101 }
133102
0 commit comments