13
13
namespace Microsoft . PowerShell . EditorServices . Handlers
14
14
{
15
15
// TODO: Add IDocumentOnTypeFormatHandler to support on-type formatting.
16
- // TODO: Use ABCs.
17
- internal class PsesDocumentFormattingHandlers : IDocumentFormattingHandler , IDocumentRangeFormattingHandler
16
+ internal class PsesDocumentFormattingHandler : DocumentFormattingHandlerBase
18
17
{
19
18
private readonly ILogger _logger ;
20
19
private readonly AnalysisService _analysisService ;
21
20
private readonly ConfigurationService _configurationService ;
22
21
private readonly WorkspaceService _workspaceService ;
23
22
24
- private DocumentFormattingCapability _documentFormattingCapability ;
25
- private DocumentRangeFormattingCapability _documentRangeFormattingCapability ;
26
-
27
- public PsesDocumentFormattingHandlers (
23
+ public PsesDocumentFormattingHandler (
28
24
ILoggerFactory factory ,
29
25
AnalysisService analysisService ,
30
26
ConfigurationService configurationService ,
31
27
WorkspaceService workspaceService )
32
28
{
33
- _logger = factory . CreateLogger < PsesDocumentFormattingHandlers > ( ) ;
29
+ _logger = factory . CreateLogger < PsesDocumentFormattingHandler > ( ) ;
34
30
_analysisService = analysisService ;
35
31
_configurationService = configurationService ;
36
32
_workspaceService = workspaceService ;
37
33
}
38
34
39
- public DocumentFormattingRegistrationOptions GetRegistrationOptions ( DocumentFormattingCapability capability , ClientCapabilities clientCapabilities ) => new DocumentFormattingRegistrationOptions
40
- {
41
- DocumentSelector = LspUtils . PowerShellDocumentSelector
42
- } ;
43
-
44
- public DocumentRangeFormattingRegistrationOptions GetRegistrationOptions ( DocumentRangeFormattingCapability capability , ClientCapabilities clientCapabilities ) => new DocumentRangeFormattingRegistrationOptions
35
+ protected override DocumentFormattingRegistrationOptions CreateRegistrationOptions ( DocumentFormattingCapability capability , ClientCapabilities clientCapabilities ) => new ( )
45
36
{
46
37
DocumentSelector = LspUtils . PowerShellDocumentSelector
47
38
} ;
48
39
49
- public async Task < TextEditContainer > Handle ( DocumentFormattingParams request , CancellationToken cancellationToken )
40
+ public override async Task < TextEditContainer > Handle ( DocumentFormattingParams request , CancellationToken cancellationToken )
50
41
{
51
- var scriptFile = _workspaceService . GetFile ( request . TextDocument . Uri ) ;
52
- var pssaSettings = _configurationService . CurrentSettings . CodeFormatting . GetPSSASettingsHashtable (
53
- ( int ) request . Options . TabSize ,
42
+ Services . TextDocument . ScriptFile scriptFile = _workspaceService . GetFile ( request . TextDocument . Uri ) ;
43
+ System . Collections . Hashtable pssaSettings = _configurationService . CurrentSettings . CodeFormatting . GetPSSASettingsHashtable (
44
+ request . Options . TabSize ,
54
45
request . Options . InsertSpaces ,
55
46
_logger ) ;
56
47
57
-
58
- // TODO raise an error event in case format returns null
48
+ // TODO: Raise an error event in case format returns null.
59
49
string formattedScript ;
60
50
Range editRange ;
61
- var extent = scriptFile . ScriptAst . Extent ;
51
+ System . Management . Automation . Language . IScriptExtent extent = scriptFile . ScriptAst . Extent ;
62
52
63
- // todo create an extension for converting range to script extent
53
+ // TODO: Create an extension for converting range to script extent.
64
54
editRange = new Range
65
55
{
66
56
Start = new Position
@@ -79,29 +69,59 @@ public async Task<TextEditContainer> Handle(DocumentFormattingParams request, Ca
79
69
scriptFile . Contents ,
80
70
pssaSettings ,
81
71
null ) . ConfigureAwait ( false ) ;
82
- formattedScript = formattedScript ?? scriptFile . Contents ;
72
+
73
+ if ( formattedScript is null )
74
+ {
75
+ _logger . LogWarning ( $ "Formatting returned null. Returning original contents for file: { scriptFile . DocumentUri } ") ;
76
+ formattedScript = scriptFile . Contents ;
77
+ }
83
78
84
79
return new TextEditContainer ( new TextEdit
85
80
{
86
81
NewText = formattedScript ,
87
82
Range = editRange
88
83
} ) ;
89
84
}
85
+ }
90
86
91
- public async Task < TextEditContainer > Handle ( DocumentRangeFormattingParams request , CancellationToken cancellationToken )
87
+ internal class PsesDocumentRangeFormattingHandler : DocumentRangeFormattingHandlerBase
88
+ {
89
+ private readonly ILogger _logger ;
90
+ private readonly AnalysisService _analysisService ;
91
+ private readonly ConfigurationService _configurationService ;
92
+ private readonly WorkspaceService _workspaceService ;
93
+
94
+ public PsesDocumentRangeFormattingHandler (
95
+ ILoggerFactory factory ,
96
+ AnalysisService analysisService ,
97
+ ConfigurationService configurationService ,
98
+ WorkspaceService workspaceService )
99
+ {
100
+ _logger = factory . CreateLogger < PsesDocumentRangeFormattingHandler > ( ) ;
101
+ _analysisService = analysisService ;
102
+ _configurationService = configurationService ;
103
+ _workspaceService = workspaceService ;
104
+ }
105
+
106
+ protected override DocumentRangeFormattingRegistrationOptions CreateRegistrationOptions ( DocumentRangeFormattingCapability capability , ClientCapabilities clientCapabilities ) => new ( )
107
+ {
108
+ DocumentSelector = LspUtils . PowerShellDocumentSelector
109
+ } ;
110
+
111
+ public override async Task < TextEditContainer > Handle ( DocumentRangeFormattingParams request , CancellationToken cancellationToken )
92
112
{
93
- var scriptFile = _workspaceService . GetFile ( request . TextDocument . Uri ) ;
94
- var pssaSettings = _configurationService . CurrentSettings . CodeFormatting . GetPSSASettingsHashtable (
95
- ( int ) request . Options . TabSize ,
113
+ Services . TextDocument . ScriptFile scriptFile = _workspaceService . GetFile ( request . TextDocument . Uri ) ;
114
+ System . Collections . Hashtable pssaSettings = _configurationService . CurrentSettings . CodeFormatting . GetPSSASettingsHashtable (
115
+ request . Options . TabSize ,
96
116
request . Options . InsertSpaces ,
97
117
_logger ) ;
98
118
99
- // TODO raise an error event in case format returns null;
119
+ // TODO: Raise an error event in case format returns null.
100
120
string formattedScript ;
101
121
Range editRange ;
102
- var extent = scriptFile . ScriptAst . Extent ;
122
+ System . Management . Automation . Language . IScriptExtent extent = scriptFile . ScriptAst . Extent ;
103
123
104
- // TODO create an extension for converting range to script extent
124
+ // TODO: Create an extension for converting range to script extent.
105
125
editRange = new Range
106
126
{
107
127
Start = new Position
@@ -117,7 +137,7 @@ public async Task<TextEditContainer> Handle(DocumentRangeFormattingParams reques
117
137
} ;
118
138
119
139
Range range = request . Range ;
120
- var rangeList = range == null ? null : new int [ ]
140
+ int [ ] rangeList = range == null ? null : new int [ ]
121
141
{
122
142
range . Start . Line + 1 ,
123
143
range . Start . Character + 1 ,
@@ -130,9 +150,9 @@ public async Task<TextEditContainer> Handle(DocumentRangeFormattingParams reques
130
150
pssaSettings ,
131
151
rangeList ) . ConfigureAwait ( false ) ;
132
152
133
- if ( formattedScript == null )
153
+ if ( formattedScript is null )
134
154
{
135
- _logger . LogWarning ( "Formatting returned null. Returning original contents for file: {0}" , scriptFile . DocumentUri ) ;
155
+ _logger . LogWarning ( $ "Formatting returned null. Returning original contents for file: { scriptFile . DocumentUri } " ) ;
136
156
formattedScript = scriptFile . Contents ;
137
157
}
138
158
@@ -142,15 +162,5 @@ public async Task<TextEditContainer> Handle(DocumentRangeFormattingParams reques
142
162
Range = editRange
143
163
} ) ;
144
164
}
145
-
146
- public void SetCapability ( DocumentFormattingCapability capability )
147
- {
148
- _documentFormattingCapability = capability ;
149
- }
150
-
151
- public void SetCapability ( DocumentRangeFormattingCapability capability )
152
- {
153
- _documentRangeFormattingCapability = capability ;
154
- }
155
165
}
156
166
}
0 commit comments