Skip to content

Process code formatting preset options #521

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1228,7 +1228,7 @@ private async Task<Tuple<string, Range>> Format(
Range range)
{
var scriptFile = editorSession.Workspace.GetFile(documentUri);
var pssaSettings = currentSettings.CodeFormatting.GetPSSASettingsHashTable(
var pssaSettings = currentSettings.CodeFormatting.GetPSSASettingsHashtable(
options.TabSize,
options.InsertSpaces);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using System;
using System.Reflection;
using System.Collections;
using System.Linq;
using System.Collections.Generic;

namespace Microsoft.PowerShell.EditorServices.Protocol.Server
{
Expand Down Expand Up @@ -93,11 +95,33 @@ public void Update(
}
}

/// <summary>
/// code formatting presets
/// </summary>
public enum CodeFormattingPreset
{

/// <summary>
/// Use the formatting settings as-is.
/// </summary>
Custom,

/// <summary>
/// Configure the formatting settings to resemble the one true brace style variant of KR indent/brace style.
/// </summary>
OTBS,

/// <summary>
/// Configure the formatting settings to resemble the Allman indent/brace style.
/// </summary>
Allman
}

public class CodeFormattingSettings
{
/// <summary>
/// Default constructor.
/// </summary>
/// </summary>>
public CodeFormattingSettings()
{

Expand All @@ -120,6 +144,7 @@ public CodeFormattingSettings(CodeFormattingSettings codeFormattingSettings)
}
}

public CodeFormattingPreset Preset { get; set; }
public bool OpenBraceOnSameLine { get; set; }
public bool NewLineAfterOpenBrace { get; set; }
public bool NewLineAfterCloseBrace { get; set; }
Expand All @@ -130,7 +155,43 @@ public CodeFormattingSettings(CodeFormattingSettings codeFormattingSettings)
public bool IgnoreOneLineBlock { get; set; }
public bool AlignPropertyValuePairs { get; set; }

public Hashtable GetPSSASettingsHashTable(int tabSize, bool insertSpaces)

/// <summary>
/// Get the settings hashtable that will be consumed by PSScriptAnalyzer.
/// </summary>
/// <param name="tabSize">The tab size in the number spaces.</param>
/// <param name="insertSpaces">If true, insert spaces otherwise insert tabs for indentation.</param>
/// <returns></returns>
public Hashtable GetPSSASettingsHashtable(
int tabSize,
bool insertSpaces)
{
var settings = GetCustomPSSASettingsHashtable(tabSize, insertSpaces);
var ruleSettings = (Hashtable)(settings["Rules"]);
var closeBraceSettings = (Hashtable)ruleSettings["PSPlaceCloseBrace"];
var openBraceSettings = (Hashtable)ruleSettings["PSPlaceOpenBrace"];
switch(Preset)
{
case CodeFormattingPreset.Allman:
openBraceSettings["OnSameLine"] = false;
openBraceSettings["NewLineAfter"] = true;
closeBraceSettings["NewLineAfter"] = true;
break;

case CodeFormattingPreset.OTBS:
openBraceSettings["OnSameLine"] = true;
openBraceSettings["NewLineAfter"] = true;
closeBraceSettings["NewLineAfter"] = false;
break;

default:
break;
}

return settings;
}

private Hashtable GetCustomPSSASettingsHashtable(int tabSize, bool insertSpaces)
{
return new Hashtable
{
Expand Down