14
14
15
15
namespace Microsoft . PowerShell . EditorServices . Handlers
16
16
{
17
- internal class DocumentFormattingHandler : IDocumentFormattingHandler
17
+ // TODO: Add IDocumentOnTypeFormatHandler to support on-type formatting.
18
+ internal class DocumentFormattingHandlers : IDocumentFormattingHandler , IDocumentRangeFormattingHandler
18
19
{
19
20
private readonly ILogger _logger ;
20
21
private readonly AnalysisService _analysisService ;
21
22
private readonly ConfigurationService _configurationService ;
22
23
private readonly WorkspaceService _workspaceService ;
23
- private DocumentFormattingCapability _capability ;
24
24
25
- public DocumentFormattingHandler ( ILoggerFactory factory , AnalysisService analysisService , ConfigurationService configurationService , WorkspaceService workspaceService )
25
+ private DocumentFormattingCapability _documentFormattingCapability ;
26
+ private DocumentRangeFormattingCapability _documentRangeFormattingCapability ;
27
+
28
+ public DocumentFormattingHandlers (
29
+ ILoggerFactory factory ,
30
+ AnalysisService analysisService ,
31
+ ConfigurationService configurationService ,
32
+ WorkspaceService workspaceService )
26
33
{
27
- _logger = factory . CreateLogger < DocumentFormattingHandler > ( ) ;
34
+ _logger = factory . CreateLogger < DocumentFormattingHandlers > ( ) ;
28
35
_analysisService = analysisService ;
29
36
_configurationService = configurationService ;
30
37
_workspaceService = workspaceService ;
@@ -43,7 +50,8 @@ public async Task<TextEditContainer> Handle(DocumentFormattingParams request, Ca
43
50
var scriptFile = _workspaceService . GetFile ( request . TextDocument . Uri ) ;
44
51
var pssaSettings = _configurationService . CurrentSettings . CodeFormatting . GetPSSASettingsHashtable (
45
52
( int ) request . Options . TabSize ,
46
- request . Options . InsertSpaces ) ;
53
+ request . Options . InsertSpaces ,
54
+ _logger ) ;
47
55
48
56
49
57
// TODO raise an error event in case format returns null
@@ -79,42 +87,13 @@ public async Task<TextEditContainer> Handle(DocumentFormattingParams request, Ca
79
87
} ) ;
80
88
}
81
89
82
- public void SetCapability ( DocumentFormattingCapability capability )
83
- {
84
- _capability = capability ;
85
- }
86
- }
87
-
88
- internal class DocumentRangeFormattingHandler : IDocumentRangeFormattingHandler
89
- {
90
- private readonly ILogger _logger ;
91
- private readonly AnalysisService _analysisService ;
92
- private readonly ConfigurationService _configurationService ;
93
- private readonly WorkspaceService _workspaceService ;
94
- private DocumentRangeFormattingCapability _capability ;
95
-
96
- public DocumentRangeFormattingHandler ( ILoggerFactory factory , AnalysisService analysisService , ConfigurationService configurationService , WorkspaceService workspaceService )
97
- {
98
- _logger = factory . CreateLogger < DocumentRangeFormattingHandler > ( ) ;
99
- _analysisService = analysisService ;
100
- _configurationService = configurationService ;
101
- _workspaceService = workspaceService ;
102
- }
103
-
104
- public TextDocumentRegistrationOptions GetRegistrationOptions ( )
105
- {
106
- return new TextDocumentRegistrationOptions
107
- {
108
- DocumentSelector = LspUtils . PowerShellDocumentSelector
109
- } ;
110
- }
111
-
112
90
public async Task < TextEditContainer > Handle ( DocumentRangeFormattingParams request , CancellationToken cancellationToken )
113
91
{
114
92
var scriptFile = _workspaceService . GetFile ( request . TextDocument . Uri ) ;
115
93
var pssaSettings = _configurationService . CurrentSettings . CodeFormatting . GetPSSASettingsHashtable (
116
94
( int ) request . Options . TabSize ,
117
- request . Options . InsertSpaces ) ;
95
+ request . Options . InsertSpaces ,
96
+ _logger ) ;
118
97
119
98
// TODO raise an error event in case format returns null;
120
99
string formattedScript ;
@@ -137,17 +116,24 @@ public async Task<TextEditContainer> Handle(DocumentRangeFormattingParams reques
137
116
} ;
138
117
139
118
Range range = request . Range ;
140
- var rangeList = range == null ? null : new int [ ] {
119
+ var rangeList = range == null ? null : new int [ ]
120
+ {
141
121
( int ) range . Start . Line + 1 ,
142
122
( int ) range . Start . Character + 1 ,
143
123
( int ) range . End . Line + 1 ,
144
- ( int ) range . End . Character + 1 } ;
124
+ ( int ) range . End . Character + 1
125
+ } ;
145
126
146
127
formattedScript = await _analysisService . FormatAsync (
147
128
scriptFile . Contents ,
148
129
pssaSettings ,
149
130
rangeList ) . ConfigureAwait ( false ) ;
150
- formattedScript = formattedScript ?? scriptFile . Contents ;
131
+
132
+ if ( formattedScript == null )
133
+ {
134
+ _logger . LogWarning ( "Formatting returned null. Returning original contents for file: {0}" , scriptFile . DocumentUri ) ;
135
+ formattedScript = scriptFile . Contents ;
136
+ }
151
137
152
138
return new TextEditContainer ( new TextEdit
153
139
{
@@ -156,9 +142,14 @@ public async Task<TextEditContainer> Handle(DocumentRangeFormattingParams reques
156
142
} ) ;
157
143
}
158
144
145
+ public void SetCapability ( DocumentFormattingCapability capability )
146
+ {
147
+ _documentFormattingCapability = capability ;
148
+ }
149
+
159
150
public void SetCapability ( DocumentRangeFormattingCapability capability )
160
151
{
161
- _capability = capability ;
152
+ _documentRangeFormattingCapability = capability ;
162
153
}
163
154
}
164
155
}
0 commit comments