1414
1515namespace Microsoft . PowerShell . EditorServices . Handlers
1616{
17- internal class DocumentFormattingHandler : IDocumentFormattingHandler
17+ // TODO: Add IDocumentOnTypeFormatHandler to support on-type formatting.
18+ internal class DocumentFormattingHandlers : IDocumentFormattingHandler , IDocumentRangeFormattingHandler
1819 {
1920 private readonly ILogger _logger ;
2021 private readonly AnalysisService _analysisService ;
2122 private readonly ConfigurationService _configurationService ;
2223 private readonly WorkspaceService _workspaceService ;
23- private DocumentFormattingCapability _capability ;
2424
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 )
2633 {
27- _logger = factory . CreateLogger < DocumentFormattingHandler > ( ) ;
34+ _logger = factory . CreateLogger < DocumentFormattingHandlers > ( ) ;
2835 _analysisService = analysisService ;
2936 _configurationService = configurationService ;
3037 _workspaceService = workspaceService ;
@@ -43,7 +50,8 @@ public async Task<TextEditContainer> Handle(DocumentFormattingParams request, Ca
4350 var scriptFile = _workspaceService . GetFile ( request . TextDocument . Uri ) ;
4451 var pssaSettings = _configurationService . CurrentSettings . CodeFormatting . GetPSSASettingsHashtable (
4552 ( int ) request . Options . TabSize ,
46- request . Options . InsertSpaces ) ;
53+ request . Options . InsertSpaces ,
54+ _logger ) ;
4755
4856
4957 // TODO raise an error event in case format returns null
@@ -79,42 +87,13 @@ public async Task<TextEditContainer> Handle(DocumentFormattingParams request, Ca
7987 } ) ;
8088 }
8189
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-
11290 public async Task < TextEditContainer > Handle ( DocumentRangeFormattingParams request , CancellationToken cancellationToken )
11391 {
11492 var scriptFile = _workspaceService . GetFile ( request . TextDocument . Uri ) ;
11593 var pssaSettings = _configurationService . CurrentSettings . CodeFormatting . GetPSSASettingsHashtable (
11694 ( int ) request . Options . TabSize ,
117- request . Options . InsertSpaces ) ;
95+ request . Options . InsertSpaces ,
96+ _logger ) ;
11897
11998 // TODO raise an error event in case format returns null;
12099 string formattedScript ;
@@ -137,17 +116,24 @@ public async Task<TextEditContainer> Handle(DocumentRangeFormattingParams reques
137116 } ;
138117
139118 Range range = request . Range ;
140- var rangeList = range == null ? null : new int [ ] {
119+ var rangeList = range == null ? null : new int [ ]
120+ {
141121 ( int ) range . Start . Line + 1 ,
142122 ( int ) range . Start . Character + 1 ,
143123 ( int ) range . End . Line + 1 ,
144- ( int ) range . End . Character + 1 } ;
124+ ( int ) range . End . Character + 1
125+ } ;
145126
146127 formattedScript = await _analysisService . FormatAsync (
147128 scriptFile . Contents ,
148129 pssaSettings ,
149130 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+ }
151137
152138 return new TextEditContainer ( new TextEdit
153139 {
@@ -156,9 +142,14 @@ public async Task<TextEditContainer> Handle(DocumentRangeFormattingParams reques
156142 } ) ;
157143 }
158144
145+ public void SetCapability ( DocumentFormattingCapability capability )
146+ {
147+ _documentFormattingCapability = capability ;
148+ }
149+
159150 public void SetCapability ( DocumentRangeFormattingCapability capability )
160151 {
161- _capability = capability ;
152+ _documentRangeFormattingCapability = capability ;
162153 }
163154 }
164155}
0 commit comments