Skip to content

Commit fb63ff7

Browse files
author
Kapil Borle
committed
Provide settings for formatting
1 parent 466248e commit fb63ff7

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,7 +1159,11 @@ protected async Task HandleDocumentFormattingRequest(
11591159
DocumentFormattingParams formattingParams,
11601160
RequestContext<TextEdit[]> requestContext)
11611161
{
1162-
var result = await Format(formattingParams.TextDocument.Uri, null);
1162+
var result = await Format(
1163+
formattingParams.TextDocument.Uri,
1164+
formattingParams.options,
1165+
null);
1166+
11631167
await requestContext.SendResult(new TextEdit[1]
11641168
{
11651169
new TextEdit
@@ -1174,7 +1178,11 @@ protected async Task HandleDocumentRangeFormattingRequest(
11741178
DocumentRangeFormattingParams formattingParams,
11751179
RequestContext<TextEdit[]> requestContext)
11761180
{
1177-
var result = await Format(formattingParams.TextDocument.Uri, formattingParams.Range);
1181+
var result = await Format(
1182+
formattingParams.TextDocument.Uri,
1183+
formattingParams.options,
1184+
formattingParams.Range);
1185+
11781186
await requestContext.SendResult(new TextEdit[1]
11791187
{
11801188
new TextEdit
@@ -1191,20 +1199,23 @@ await requestContext.SendResult(new TextEdit[1]
11911199

11921200
private async Task<Tuple<string, Range>> Format(
11931201
string documentUri,
1202+
FormattingOptions options,
11941203
Range range)
11951204
{
11961205

11971206
// TODO Get settings
11981207
// TODO Update settings to store code formatting settings
11991208
var scriptFile = editorSession.Workspace.GetFile(documentUri);
1209+
var pssaSettings = currentSettings.CodeFormatting.GetPSSASettingsHashTable(
1210+
options.TabSize,
1211+
options.InsertSpaces);
12001212

12011213
// TODO raise an error event incase format returns null;
12021214
string formattedScript;
12031215
Range editRange;
12041216
int sl, sc, el, ec;
12051217
if (range == null)
12061218
{
1207-
formattedScript = await editorSession.AnalysisService.Format(scriptFile.Contents);
12081219
sl = -1;
12091220
sc = -1;
12101221
el = -1;
@@ -1235,7 +1246,13 @@ private async Task<Tuple<string, Range>> Format(
12351246
}
12361247
};
12371248

1238-
formattedScript = await editorSession.AnalysisService.Format(scriptFile.Contents, sl, sc, el, ec);
1249+
formattedScript = await editorSession.AnalysisService.Format(
1250+
scriptFile.Contents,
1251+
pssaSettings,
1252+
sl,
1253+
sc,
1254+
el,
1255+
ec);
12391256
formattedScript = formattedScript ?? scriptFile.Contents;
12401257
return Tuple.Create(formattedScript, editRange);
12411258
}

src/PowerShellEditorServices/Analysis/AnalysisService.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ public IEnumerable<string> GetPSScriptAnalyzerRules()
253253
/// <returns>The formatted script text.</returns>
254254
public async Task<string> Format(
255255
string scriptDefinition,
256+
Hashtable settings,
256257
int startLineNumber = -1,
257258
int startColumnNumber = -1,
258259
int endLineNumber = -1,
@@ -270,13 +271,15 @@ public async Task<string> Format(
270271
if (startLineNumber == -1)
271272
{
272273
argsDict = new Dictionary<string, object> {
273-
{"ScriptDefinition", scriptDefinition}
274+
{"ScriptDefinition", scriptDefinition},
275+
{"Settings", settings}
274276
};
275277
}
276278
else
277279
{
278280
argsDict = new Dictionary<string, object> {
279281
{"ScriptDefinition", scriptDefinition},
282+
{"Settings", settings},
280283
{"StartLineNumber", startLineNumber},
281284
{"StartColumnNumber", startColumnNumber},
282285
{"EndLineNumber", endLineNumber},

0 commit comments

Comments
 (0)