1313namespace Microsoft . PowerShell . EditorServices . Handlers
1414{
1515 // TODO: Add IDocumentOnTypeFormatHandler to support on-type formatting.
16- // TODO: Use ABCs.
17- internal class PsesDocumentFormattingHandlers : IDocumentFormattingHandler , IDocumentRangeFormattingHandler
16+ internal class PsesDocumentFormattingHandler : DocumentFormattingHandlerBase
1817 {
1918 private readonly ILogger _logger ;
2019 private readonly AnalysisService _analysisService ;
2120 private readonly ConfigurationService _configurationService ;
2221 private readonly WorkspaceService _workspaceService ;
2322
24- private DocumentFormattingCapability _documentFormattingCapability ;
25- private DocumentRangeFormattingCapability _documentRangeFormattingCapability ;
26-
27- public PsesDocumentFormattingHandlers (
23+ public PsesDocumentFormattingHandler (
2824 ILoggerFactory factory ,
2925 AnalysisService analysisService ,
3026 ConfigurationService configurationService ,
3127 WorkspaceService workspaceService )
3228 {
33- _logger = factory . CreateLogger < PsesDocumentFormattingHandlers > ( ) ;
29+ _logger = factory . CreateLogger < PsesDocumentFormattingHandler > ( ) ;
3430 _analysisService = analysisService ;
3531 _configurationService = configurationService ;
3632 _workspaceService = workspaceService ;
3733 }
3834
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 ( )
4536 {
4637 DocumentSelector = LspUtils . PowerShellDocumentSelector
4738 } ;
4839
49- public async Task < TextEditContainer > Handle ( DocumentFormattingParams request , CancellationToken cancellationToken )
40+ public override async Task < TextEditContainer > Handle ( DocumentFormattingParams request , CancellationToken cancellationToken )
5041 {
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 ,
5445 request . Options . InsertSpaces ,
5546 _logger ) ;
5647
57-
58- // TODO raise an error event in case format returns null
48+ // TODO: Raise an error event in case format returns null.
5949 string formattedScript ;
6050 Range editRange ;
61- var extent = scriptFile . ScriptAst . Extent ;
51+ System . Management . Automation . Language . IScriptExtent extent = scriptFile . ScriptAst . Extent ;
6252
63- // todo create an extension for converting range to script extent
53+ // TODO: Create an extension for converting range to script extent.
6454 editRange = new Range
6555 {
6656 Start = new Position
@@ -79,29 +69,59 @@ public async Task<TextEditContainer> Handle(DocumentFormattingParams request, Ca
7969 scriptFile . Contents ,
8070 pssaSettings ,
8171 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+ }
8378
8479 return new TextEditContainer ( new TextEdit
8580 {
8681 NewText = formattedScript ,
8782 Range = editRange
8883 } ) ;
8984 }
85+ }
9086
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 )
92112 {
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 ,
96116 request . Options . InsertSpaces ,
97117 _logger ) ;
98118
99- // TODO raise an error event in case format returns null;
119+ // TODO: Raise an error event in case format returns null.
100120 string formattedScript ;
101121 Range editRange ;
102- var extent = scriptFile . ScriptAst . Extent ;
122+ System . Management . Automation . Language . IScriptExtent extent = scriptFile . ScriptAst . Extent ;
103123
104- // TODO create an extension for converting range to script extent
124+ // TODO: Create an extension for converting range to script extent.
105125 editRange = new Range
106126 {
107127 Start = new Position
@@ -117,7 +137,7 @@ public async Task<TextEditContainer> Handle(DocumentRangeFormattingParams reques
117137 } ;
118138
119139 Range range = request . Range ;
120- var rangeList = range == null ? null : new int [ ]
140+ int [ ] rangeList = range == null ? null : new int [ ]
121141 {
122142 range . Start . Line + 1 ,
123143 range . Start . Character + 1 ,
@@ -130,9 +150,9 @@ public async Task<TextEditContainer> Handle(DocumentRangeFormattingParams reques
130150 pssaSettings ,
131151 rangeList ) . ConfigureAwait ( false ) ;
132152
133- if ( formattedScript == null )
153+ if ( formattedScript is null )
134154 {
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 } " ) ;
136156 formattedScript = scriptFile . Contents ;
137157 }
138158
@@ -142,15 +162,5 @@ public async Task<TextEditContainer> Handle(DocumentRangeFormattingParams reques
142162 Range = editRange
143163 } ) ;
144164 }
145-
146- public void SetCapability ( DocumentFormattingCapability capability )
147- {
148- _documentFormattingCapability = capability ;
149- }
150-
151- public void SetCapability ( DocumentRangeFormattingCapability capability )
152- {
153- _documentRangeFormattingCapability = capability ;
154- }
155165 }
156166}
0 commit comments