Skip to content

Commit 39d69d4

Browse files
author
Kapil Borle
committed
Add range formatting capability to the server
1 parent d8bf331 commit 39d69d4

File tree

1 file changed

+72
-24
lines changed

1 file changed

+72
-24
lines changed

src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs

Lines changed: 72 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ public void Start()
104104
this.messageHandlers.SetRequestHandler(WorkspaceSymbolRequest.Type, this.HandleWorkspaceSymbolRequest);
105105
this.messageHandlers.SetRequestHandler(CodeActionRequest.Type, this.HandleCodeActionRequest);
106106
this.messageHandlers.SetRequestHandler(DocumentFormattingRequest.Type, this.HandleDocumentFormattingRequest);
107+
this.messageHandlers.SetRequestHandler(
108+
DocumentRangeFormattingRequest.Type,
109+
this.HandleDocumentRangeFormattingRequest);
107110

108111
this.messageHandlers.SetRequestHandler(ShowOnlineHelpRequest.Type, this.HandleShowOnlineHelpRequest);
109112
this.messageHandlers.SetRequestHandler(ExpandAliasRequest.Type, this.HandleExpandAliasRequest);
@@ -197,7 +200,8 @@ await requestContext.SendResult(
197200
{
198201
TriggerCharacters = new string[] { " " } // TODO: Other characters here?
199202
},
200-
DocumentFormattingProvider = true
203+
DocumentFormattingProvider = true,
204+
DocumentRangeFormattingProvider = true
201205
}
202206
});
203207
}
@@ -1156,45 +1160,89 @@ protected async Task HandleDocumentFormattingRequest(
11561160
DocumentFormattingParams formattingParams,
11571161
RequestContext<TextEdit[]> requestContext)
11581162
{
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]
11701165
{
1171-
Start = new Position
1166+
new TextEdit
11721167
{
1173-
Line = extent.StartLineNumber - 1,
1174-
Character = extent.StartColumnNumber - 1
1168+
NewText = result.Item1,
1169+
Range = result.Item2
11751170
},
1176-
End = new Position
1177-
{
1178-
Line = extent.EndLineNumber - 1,
1179-
Character = extent.EndColumnNumber - 1
1180-
}
1181-
};
1171+
});
1172+
}
11821173

1174+
protected async Task HandleDocumentRangeFormattingRequest(
1175+
DocumentRangeFormattingParams formattingParams,
1176+
RequestContext<TextEdit[]> requestContext)
1177+
{
1178+
var result = await Format(formattingParams.TextDocument.Uri, formattingParams.Range);
11831179
await requestContext.SendResult(new TextEdit[1]
11841180
{
11851181
new TextEdit
11861182
{
1187-
Range = editRange,
1188-
NewText = formattedScript
1183+
NewText = result.Item1,
1184+
Range = result.Item2
11891185
},
1190-
11911186
});
11921187
}
11931188

11941189
#endregion
11951190

11961191
#region Event Handlers
11971192

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+
11981246
private async void PowerShellContext_RunspaceChanged(object sender, Session.RunspaceChangedEventArgs e)
11991247
{
12001248
await this.messageSender.SendEvent(

0 commit comments

Comments
 (0)