3
3
4
4
using System ;
5
5
using System . Collections . Generic ;
6
- using System . Linq ;
7
6
using System . Threading ;
8
7
using System . Threading . Tasks ;
9
8
using Microsoft . Extensions . Logging ;
10
9
using Microsoft . PowerShell . EditorServices . Services ;
11
10
using Microsoft . PowerShell . EditorServices . Services . TextDocument ;
12
11
using Microsoft . PowerShell . EditorServices . Utility ;
13
- using Newtonsoft . Json . Linq ;
14
12
using OmniSharp . Extensions . LanguageServer . Protocol . Client . Capabilities ;
15
13
using OmniSharp . Extensions . LanguageServer . Protocol . Document ;
16
14
using OmniSharp . Extensions . LanguageServer . Protocol . Models ;
@@ -21,21 +19,22 @@ internal class PsesCodeActionHandler : CodeActionHandlerBase
21
19
{
22
20
private readonly ILogger _logger ;
23
21
private readonly AnalysisService _analysisService ;
24
- private readonly WorkspaceService _workspaceService ;
25
22
26
- public PsesCodeActionHandler ( ILoggerFactory factory , AnalysisService analysisService , WorkspaceService workspaceService )
23
+ public PsesCodeActionHandler ( ILoggerFactory factory , AnalysisService analysisService )
27
24
{
28
25
_logger = factory . CreateLogger < PsesCodeActionHandler > ( ) ;
29
26
_analysisService = analysisService ;
30
- _workspaceService = workspaceService ;
31
27
}
32
28
33
- protected override CodeActionRegistrationOptions CreateRegistrationOptions ( CodeActionCapability capability , ClientCapabilities clientCapabilities ) => new CodeActionRegistrationOptions
29
+ protected override CodeActionRegistrationOptions CreateRegistrationOptions ( CodeActionCapability capability , ClientCapabilities clientCapabilities )
30
+ {
31
+ return new ( )
34
32
{
35
- // TODO: What do we do with the arguments?
36
- DocumentSelector = LspUtils . PowerShellDocumentSelector ,
37
- CodeActionKinds = new CodeActionKind [ ] { CodeActionKind . QuickFix }
38
- } ;
33
+ // TODO: What do we do with the arguments?
34
+ DocumentSelector = LspUtils . PowerShellDocumentSelector ,
35
+ CodeActionKinds = new CodeActionKind [ ] { CodeActionKind . QuickFix }
36
+ } ;
37
+ }
39
38
40
39
// TODO: Either fix or ignore "method lacks 'await'" warning.
41
40
public override async Task < CodeAction > Handle ( CodeAction request , CancellationToken cancellationToken )
@@ -65,7 +64,7 @@ public override async Task<CommandOrCodeActionContainer> Handle(CodeActionParams
65
64
return Array . Empty < CommandOrCodeAction > ( ) ;
66
65
}
67
66
68
- var codeActions = new List < CommandOrCodeAction > ( ) ;
67
+ List < CommandOrCodeAction > codeActions = new ( ) ;
69
68
70
69
// If there are any code fixes, send these commands first so they appear at top of "Code Fix" menu in the client UI.
71
70
foreach ( Diagnostic diagnostic in request . Context . Diagnostics )
@@ -81,31 +80,34 @@ public override async Task<CommandOrCodeActionContainer> Handle(CodeActionParams
81
80
string diagnosticId = AnalysisService . GetUniqueIdFromDiagnostic ( diagnostic ) ;
82
81
if ( corrections . TryGetValue ( diagnosticId , out MarkerCorrection correction ) )
83
82
{
84
- codeActions . Add ( new CodeAction
83
+ foreach ( ScriptRegion edit in correction . Edits )
85
84
{
86
- Title = correction . Name ,
87
- Kind = CodeActionKind . QuickFix ,
88
- Edit = new WorkspaceEdit
85
+ codeActions . Add ( new CodeAction
89
86
{
90
- DocumentChanges = new Container < WorkspaceEditDocumentChange > (
91
- new WorkspaceEditDocumentChange (
92
- new TextDocumentEdit
93
- {
94
- TextDocument = new OptionalVersionedTextDocumentIdentifier
87
+ Title = correction . Name ,
88
+ Kind = CodeActionKind . QuickFix ,
89
+ Edit = new WorkspaceEdit
90
+ {
91
+ DocumentChanges = new Container < WorkspaceEditDocumentChange > (
92
+ new WorkspaceEditDocumentChange (
93
+ new TextDocumentEdit
95
94
{
96
- Uri = request . TextDocument . Uri
97
- } ,
98
- Edits = new TextEditContainer ( correction . Edits . Select ( ScriptRegion . ToTextEdit ) )
99
- } ) )
100
- }
101
- } ) ;
95
+ TextDocument = new OptionalVersionedTextDocumentIdentifier
96
+ {
97
+ Uri = request . TextDocument . Uri
98
+ } ,
99
+ Edits = new TextEditContainer ( ScriptRegion . ToTextEdit ( edit ) )
100
+ } ) )
101
+ }
102
+ } ) ;
103
+ }
102
104
}
103
105
}
104
106
105
107
// Add "show documentation" commands last so they appear at the bottom of the client UI.
106
108
// These commands do not require code fixes. Sometimes we get a batch of diagnostics
107
109
// to create commands for. No need to create multiple show doc commands for the same rule.
108
- var ruleNamesProcessed = new HashSet < string > ( ) ;
110
+ HashSet < string > ruleNamesProcessed = new ( ) ;
109
111
foreach ( Diagnostic diagnostic in request . Context . Diagnostics )
110
112
{
111
113
if (
@@ -119,8 +121,8 @@ public override async Task<CommandOrCodeActionContainer> Handle(CodeActionParams
119
121
if ( string . Equals ( diagnostic . Source , "PSScriptAnalyzer" , StringComparison . OrdinalIgnoreCase ) &&
120
122
! ruleNamesProcessed . Contains ( diagnostic . Code ? . String ) )
121
123
{
122
- ruleNamesProcessed . Add ( diagnostic . Code ? . String ) ;
123
- var title = $ "Show documentation for: { diagnostic . Code ? . String } ";
124
+ _ = ruleNamesProcessed . Add ( diagnostic . Code ? . String ) ;
125
+ string title = $ "Show documentation for: { diagnostic . Code ? . String } ";
124
126
codeActions . Add ( new CodeAction
125
127
{
126
128
Title = title ,
@@ -132,7 +134,7 @@ public override async Task<CommandOrCodeActionContainer> Handle(CodeActionParams
132
134
{
133
135
Title = title ,
134
136
Name = "PowerShell.ShowCodeActionDocumentation" ,
135
- Arguments = JArray . FromObject ( new [ ] { diagnostic . Code ? . String } )
137
+ Arguments = Newtonsoft . Json . Linq . JArray . FromObject ( new [ ] { diagnostic . Code ? . String } )
136
138
}
137
139
} ) ;
138
140
}
0 commit comments