Skip to content

Commit 0ebfb5e

Browse files
author
Kapil Borle
committed
Send a range array to Format method
1 parent ebb6f03 commit 0ebfb5e

File tree

2 files changed

+14
-46
lines changed

2 files changed

+14
-46
lines changed

src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,33 +1202,19 @@ private async Task<Tuple<string, Range>> Format(
12021202
FormattingOptions options,
12031203
Range range)
12041204
{
1205-
1206-
// TODO Get settings
1207-
// TODO Update settings to store code formatting settings
12081205
var scriptFile = editorSession.Workspace.GetFile(documentUri);
12091206
var pssaSettings = currentSettings.CodeFormatting.GetPSSASettingsHashTable(
12101207
options.TabSize,
12111208
options.InsertSpaces);
12121209

1213-
// TODO raise an error event incase format returns null;
1210+
// TODO raise an error event in case format returns null;
12141211
string formattedScript;
12151212
Range editRange;
1216-
int sl, sc, el, ec;
1217-
if (range == null)
1218-
{
1219-
sl = -1;
1220-
sc = -1;
1221-
el = -1;
1222-
ec = -1;
1223-
}
1224-
else
1225-
{
1226-
sl = range.Start.Line + 1;
1227-
sc = range.Start.Character + 1;
1228-
el = range.End.Line + 1;
1229-
ec = range.End.Character + 1;
1230-
}
1231-
1213+
var rangeList = range == null ? null : new int[] {
1214+
range.Start.Line + 1,
1215+
range.Start.Character + 1,
1216+
range.End.Line + 1,
1217+
range.End.Character + 1};
12321218
var extent = scriptFile.ScriptAst.Extent;
12331219

12341220
// todo create an extension for converting range to script extent
@@ -1249,10 +1235,7 @@ private async Task<Tuple<string, Range>> Format(
12491235
formattedScript = await editorSession.AnalysisService.Format(
12501236
scriptFile.Contents,
12511237
pssaSettings,
1252-
sl,
1253-
sc,
1254-
el,
1255-
ec);
1238+
rangeList);
12561239
formattedScript = formattedScript ?? scriptFile.Contents;
12571240
return Tuple.Create(formattedScript, editRange);
12581241
}

src/PowerShellEditorServices/Analysis/AnalysisService.cs

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -254,40 +254,25 @@ public IEnumerable<string> GetPSScriptAnalyzerRules()
254254
public async Task<string> Format(
255255
string scriptDefinition,
256256
Hashtable settings,
257-
int startLineNumber = -1,
258-
int startColumnNumber = -1,
259-
int endLineNumber = -1,
260-
int endColumnNumber = -1)
257+
int[] rangeList)
261258
{
262259
// we cannot use Range type therefore this workaround of using -1 default value
263260
if (!hasScriptAnalyzerModule)
264261
{
265262
return null;
266263
}
267264

268-
Dictionary<string, object> argsDict;
269-
270-
// this is a workaround till range formatter is enable in script analyzer
271-
if (startLineNumber == -1)
272-
{
273-
argsDict = new Dictionary<string, object> {
265+
var argsDict = new Dictionary<string, object> {
274266
{"ScriptDefinition", scriptDefinition},
275267
{"Settings", settings}
276-
};
277-
}
278-
else
268+
};
269+
if (rangeList != null)
279270
{
280-
argsDict = new Dictionary<string, object> {
281-
{"ScriptDefinition", scriptDefinition},
282-
{"Settings", settings},
283-
{"StartLineNumber", startLineNumber},
284-
{"StartColumnNumber", startColumnNumber},
285-
{"EndLineNumber", endLineNumber},
286-
{"EndColumnNumber", endColumnNumber}
287-
};
271+
argsDict.Add("Range", rangeList);
288272
}
273+
289274
var result = await InvokePowerShellAsync("Invoke-Formatter", argsDict);
290-
return result?.Select(r => r.ImmediateBaseObject as string).FirstOrDefault();
275+
return result?.Select(r => r?.ImmediateBaseObject as string).FirstOrDefault();
291276
}
292277

293278
/// <summary>

0 commit comments

Comments
 (0)