Skip to content

Commit f3f42e3

Browse files
author
Kapil Borle
committed
Pass code formatting options to AnalysisService
1 parent b62e1c5 commit f3f42e3

File tree

3 files changed

+43
-11
lines changed

3 files changed

+43
-11
lines changed

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,11 @@
270270
"type": "boolean",
271271
"default": false,
272272
"description": "Launches the language service with the /waitForDebugger flag to force it to wait for a .NET debugger to attach before proceeding."
273+
},
274+
"powershell.codeformatting.openBraceOnSameLine":{
275+
"type":"boolean",
276+
"default": true,
277+
"description": "Places open brace on the same line as its associated statement."
273278
}
274279
}
275280
}

src/features/DocumentFormatter.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { languages, TextDocument, TextEdit, FormattingOptions, CancellationToken
33
import { LanguageClient, RequestType, NotificationType } from 'vscode-languageclient';
44
import Window = vscode.window;
55
import { IFeature } from '../feature';
6+
import * as Settings from '../settings';
7+
import * as Utils from '../utils';
68

79
export namespace ScriptFileMarkersRequest {
810
export const type: RequestType<any, any, void> = { get method(): string { return "powerShell/getScriptFileMarkers"; } };
@@ -12,6 +14,7 @@ export namespace ScriptFileMarkersRequest {
1214
interface ScriptFileMarkersRequestParams {
1315
filePath: string;
1416
rules: string[];
17+
settings: string;
1518
}
1619

1720
interface ScriptFileMarkersRequestResultParams {
@@ -71,11 +74,13 @@ class PSDocumentFormattingEditProvider implements vscode.DocumentFormattingEditP
7174
options: FormattingOptions,
7275
index: number): Thenable<TextEdit[]> | TextEdit[] {
7376
if (this.languageClient !== null && index < this.ruleOrder.length) {
77+
let rule = this.ruleOrder[index];
7478
return this.languageClient.sendRequest(
7579
ScriptFileMarkersRequest.type,
7680
{
7781
filePath: document.fileName,
78-
rules: [this.ruleOrder[index]]
82+
rules: [rule],
83+
settings: this.getSettings(rule)
7984
})
8085
.then((result: ScriptFileMarkersRequestResultParams) => {
8186

@@ -130,6 +135,18 @@ class PSDocumentFormattingEditProvider implements vscode.DocumentFormattingEditP
130135
setLanguageClient(languageClient: LanguageClient): void {
131136
this.languageClient = languageClient;
132137
}
138+
139+
getSettings(rule: string): string {
140+
let settings: Settings.ISettings = Settings.load(Utils.PowerShellLanguageId);
141+
return `@{
142+
IncludeRules = @('${rule}')
143+
Rules = @{
144+
PSPlaceOpenBrace = @{
145+
OnSameLine = \$${settings.codeformatting.openBraceOnSameLine}
146+
}
147+
}
148+
}`;
149+
}
133150
}
134151

135152
export class DocumentFormatterFeature implements IFeature {

src/settings.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
import vscode = require('vscode');
88

9+
export interface ICodeFormattingSettings {
10+
openBraceOnSameLine: boolean;
11+
}
12+
913
export interface IScriptAnalysisSettings {
1014
enable?: boolean
1115
settingsPath: string
@@ -19,31 +23,37 @@ export interface IDeveloperSettings {
1923
}
2024

2125
export interface ISettings {
22-
useX86Host?: boolean,
23-
enableProfileLoading?: boolean,
24-
scriptAnalysis?: IScriptAnalysisSettings,
25-
developer?: IDeveloperSettings,
26+
useX86Host?: boolean;
27+
enableProfileLoading?: boolean;
28+
scriptAnalysis?: IScriptAnalysisSettings;
29+
developer?: IDeveloperSettings;
30+
codeformatting?: ICodeFormattingSettings;
2631
}
2732

2833
export function load(myPluginId: string): ISettings {
29-
let configuration = vscode.workspace.getConfiguration(myPluginId);
34+
let configuration: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration(myPluginId);
3035

31-
let defaultScriptAnalysisSettings = {
36+
let defaultScriptAnalysisSettings: IScriptAnalysisSettings = {
3237
enable: true,
3338
settingsPath: ""
3439
};
3540

36-
let defaultDeveloperSettings = {
41+
let defaultDeveloperSettings: IDeveloperSettings = {
3742
powerShellExePath: undefined,
3843
bundledModulesPath: "../modules/",
3944
editorServicesLogLevel: "Normal",
4045
editorServicesWaitForDebugger: false
41-
}
46+
};
47+
48+
let defaultCodeFormattingSettings: ICodeFormattingSettings = {
49+
openBraceOnSameLine: true
50+
};
4251

4352
return {
4453
useX86Host: configuration.get<boolean>("useX86Host", false),
4554
enableProfileLoading: configuration.get<boolean>("enableProfileLoading", false),
4655
scriptAnalysis: configuration.get<IScriptAnalysisSettings>("scriptAnalysis", defaultScriptAnalysisSettings),
47-
developer: configuration.get<IDeveloperSettings>("developer", defaultDeveloperSettings)
48-
}
56+
developer: configuration.get<IDeveloperSettings>("developer", defaultDeveloperSettings),
57+
codeformatting: configuration.get<ICodeFormattingSettings>("codeformatting", defaultCodeFormattingSettings)
58+
};
4959
}

0 commit comments

Comments
 (0)