Skip to content

Commit 5db4d79

Browse files
Kapil Borledaviwil
Kapil Borle
authored andcommitted
Add method to process formatting presets
1 parent 6a30ba0 commit 5db4d79

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1228,7 +1228,7 @@ private async Task<Tuple<string, Range>> Format(
12281228
Range range)
12291229
{
12301230
var scriptFile = editorSession.Workspace.GetFile(documentUri);
1231-
var pssaSettings = currentSettings.CodeFormatting.GetPSSASettingsHashTable(
1231+
var pssaSettings = currentSettings.CodeFormatting.GetPSSASettingsHashtable(
12321232
options.TabSize,
12331233
options.InsertSpaces);
12341234

src/PowerShellEditorServices.Protocol/Server/LanguageServerSettings.cs

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
using System;
99
using System.Reflection;
1010
using System.Collections;
11+
using System.Linq;
12+
using System.Collections.Generic;
1113

1214
namespace Microsoft.PowerShell.EditorServices.Protocol.Server
1315
{
@@ -93,18 +95,33 @@ public void Update(
9395
}
9496
}
9597

98+
/// <summary>
99+
/// code formatting presets
100+
/// </summary>
96101
public enum CodeFormattingPreset
97102
{
103+
104+
/// <summary>
105+
/// Use the formatting settings as-is.
106+
/// </summary>
98107
Custom,
108+
109+
/// <summary>
110+
/// Configure the formatting settings to resemble the one true brace style variant of KR indent/brace style.
111+
/// </summary>
99112
OTBS,
113+
114+
/// <summary>
115+
/// Configure the formatting settings to resemble the Allman indent/brace style.
116+
/// </summary>
100117
Allman
101118
}
102119

103120
public class CodeFormattingSettings
104121
{
105122
/// <summary>
106123
/// Default constructor.
107-
/// </summary>
124+
/// </summary>>
108125
public CodeFormattingSettings()
109126
{
110127

@@ -138,7 +155,43 @@ public CodeFormattingSettings(CodeFormattingSettings codeFormattingSettings)
138155
public bool IgnoreOneLineBlock { get; set; }
139156
public bool AlignPropertyValuePairs { get; set; }
140157

141-
public Hashtable GetPSSASettingsHashTable(int tabSize, bool insertSpaces)
158+
159+
/// <summary>
160+
/// Get the settings hashtable that will be consumed by PSScriptAnalyzer.
161+
/// </summary>
162+
/// <param name="tabSize">The tab size in the number spaces.</param>
163+
/// <param name="insertSpaces">If true, insert spaces otherwise insert tabs for indentation.</param>
164+
/// <returns></returns>
165+
public Hashtable GetPSSASettingsHashtable(
166+
int tabSize,
167+
bool insertSpaces)
168+
{
169+
var settings = GetCustomPSSASettingsHashtable(tabSize, insertSpaces);
170+
var ruleSettings = (Hashtable)(settings["Rules"]);
171+
var closeBraceSettings = (Hashtable)ruleSettings["PSPlaceCloseBrace"];
172+
var openBraceSettings = (Hashtable)ruleSettings["PSPlaceOpenBrace"];
173+
switch(Preset)
174+
{
175+
case CodeFormattingPreset.Allman:
176+
openBraceSettings["OnSameLine"] = false;
177+
openBraceSettings["NewLineAfter"] = true;
178+
closeBraceSettings["NewLineAfter"] = true;
179+
break;
180+
181+
case CodeFormattingPreset.OTBS:
182+
openBraceSettings["OnSameLine"] = true;
183+
openBraceSettings["NewLineAfter"] = true;
184+
closeBraceSettings["NewLineAfter"] = false;
185+
break;
186+
187+
default:
188+
break;
189+
}
190+
191+
return settings;
192+
}
193+
194+
private Hashtable GetCustomPSSASettingsHashtable(int tabSize, bool insertSpaces)
142195
{
143196
return new Hashtable
144197
{

0 commit comments

Comments
 (0)