@@ -104,6 +104,9 @@ public void Start()
104
104
this . messageHandlers . SetRequestHandler ( WorkspaceSymbolRequest . Type , this . HandleWorkspaceSymbolRequest ) ;
105
105
this . messageHandlers . SetRequestHandler ( CodeActionRequest . Type , this . HandleCodeActionRequest ) ;
106
106
this . messageHandlers . SetRequestHandler ( DocumentFormattingRequest . Type , this . HandleDocumentFormattingRequest ) ;
107
+ this . messageHandlers . SetRequestHandler (
108
+ DocumentRangeFormattingRequest . Type ,
109
+ this . HandleDocumentRangeFormattingRequest ) ;
107
110
108
111
this . messageHandlers . SetRequestHandler ( ShowOnlineHelpRequest . Type , this . HandleShowOnlineHelpRequest ) ;
109
112
this . messageHandlers . SetRequestHandler ( ExpandAliasRequest . Type , this . HandleExpandAliasRequest ) ;
@@ -197,7 +200,8 @@ await requestContext.SendResult(
197
200
{
198
201
TriggerCharacters = new string [ ] { " " } // TODO: Other characters here?
199
202
} ,
200
- DocumentFormattingProvider = true
203
+ DocumentFormattingProvider = true ,
204
+ DocumentRangeFormattingProvider = true
201
205
}
202
206
} ) ;
203
207
}
@@ -1156,45 +1160,89 @@ protected async Task HandleDocumentFormattingRequest(
1156
1160
DocumentFormattingParams formattingParams ,
1157
1161
RequestContext < TextEdit [ ] > requestContext )
1158
1162
{
1159
- // TODO Get settings
1160
- // TODO Update settings to store code formatting settings
1161
- var scriptFile = editorSession . Workspace . GetFile ( formattingParams . TextDocument . Uri ) ;
1162
-
1163
- // TODO raise an error event incase format returns null;
1164
- var formattedScript = await editorSession . AnalysisService . Format ( scriptFile . Contents ) ??
1165
- scriptFile . Contents ;
1166
- var extent = scriptFile . ScriptAst . Extent ;
1167
-
1168
- // todo create an extension for this
1169
- var editRange = new Range
1163
+ var result = await Format ( formattingParams . TextDocument . Uri , null ) ;
1164
+ await requestContext . SendResult ( new TextEdit [ 1 ]
1170
1165
{
1171
- Start = new Position
1166
+ new TextEdit
1172
1167
{
1173
- Line = extent . StartLineNumber - 1 ,
1174
- Character = extent . StartColumnNumber - 1
1168
+ NewText = result . Item1 ,
1169
+ Range = result . Item2
1175
1170
} ,
1176
- End = new Position
1177
- {
1178
- Line = extent . EndLineNumber - 1 ,
1179
- Character = extent . EndColumnNumber - 1
1180
- }
1181
- } ;
1171
+ } ) ;
1172
+ }
1182
1173
1174
+ protected async Task HandleDocumentRangeFormattingRequest (
1175
+ DocumentRangeFormattingParams formattingParams ,
1176
+ RequestContext < TextEdit [ ] > requestContext )
1177
+ {
1178
+ var result = await Format ( formattingParams . TextDocument . Uri , formattingParams . Range ) ;
1183
1179
await requestContext . SendResult ( new TextEdit [ 1 ]
1184
1180
{
1185
1181
new TextEdit
1186
1182
{
1187
- Range = editRange ,
1188
- NewText = formattedScript
1183
+ NewText = result . Item1 ,
1184
+ Range = result . Item2
1189
1185
} ,
1190
-
1191
1186
} ) ;
1192
1187
}
1193
1188
1194
1189
#endregion
1195
1190
1196
1191
#region Event Handlers
1197
1192
1193
+ private async Task < Tuple < string , Range > > Format ( string documentUri , Range range )
1194
+ {
1195
+
1196
+ // TODO Get settings
1197
+ // TODO Update settings to store code formatting settings
1198
+ var scriptFile = editorSession . Workspace . GetFile ( documentUri ) ;
1199
+
1200
+ // TODO raise an error event incase format returns null;
1201
+ string formattedScript ;
1202
+ if ( range == null )
1203
+ {
1204
+ formattedScript = await editorSession . AnalysisService . Format ( scriptFile . Contents ) ;
1205
+ }
1206
+ else
1207
+ {
1208
+ formattedScript = await editorSession . AnalysisService . Format (
1209
+ scriptFile . Contents ,
1210
+ range . Start . Line + 1 ,
1211
+ range . Start . Character + 1 ,
1212
+ range . End . Line + 1 ,
1213
+ range . End . Character + 1 ) ;
1214
+ }
1215
+
1216
+
1217
+ formattedScript = formattedScript ?? scriptFile . Contents ;
1218
+ var extent = scriptFile . ScriptAst . Extent ;
1219
+
1220
+ // todo create an extension for this
1221
+ Range editRange ;
1222
+ if ( range != null )
1223
+ {
1224
+ editRange = new Range
1225
+ {
1226
+ Start = new Position
1227
+ {
1228
+ Line = extent . StartLineNumber - 1 ,
1229
+ Character = extent . StartColumnNumber - 1
1230
+ } ,
1231
+ End = new Position
1232
+ {
1233
+ Line = extent . EndLineNumber - 1 ,
1234
+ Character = extent . EndColumnNumber - 1
1235
+ }
1236
+ } ;
1237
+ }
1238
+ else
1239
+ {
1240
+ editRange = range ;
1241
+ }
1242
+
1243
+ return Tuple . Create ( formattedScript , editRange ) ;
1244
+ }
1245
+
1198
1246
private async void PowerShellContext_RunspaceChanged ( object sender , Session . RunspaceChangedEventArgs e )
1199
1247
{
1200
1248
await this . messageSender . SendEvent (
0 commit comments