Skip to content

Commit 631a9d8

Browse files
committed
Merge pull request #2489 from dbaeumer-feature/formatOptions
1 parent d7af0db commit 631a9d8

File tree

3 files changed

+87
-27
lines changed

3 files changed

+87
-27
lines changed

src/server/editorServices.ts

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/// <reference path="..\compiler\commandLineParser.ts" />
22
/// <reference path="..\services\services.ts" />
3+
/// <reference path="protocol.d.ts" />
4+
/// <reference path="session.ts" />
35
/// <reference path="node.d.ts" />
46

57
module ts.server {
@@ -16,6 +18,16 @@ module ts.server {
1618

1719
var lineCollectionCapacity = 4;
1820

21+
function mergeFormatOptions(formatCodeOptions: FormatCodeOptions, formatOptions: protocol.FormatOptions): void {
22+
var hasOwnProperty = Object.prototype.hasOwnProperty;
23+
Object.keys(formatOptions).forEach((key) => {
24+
var codeKey = key.charAt(0).toUpperCase() + key.substring(1);
25+
if (hasOwnProperty.call(formatCodeOptions, codeKey)) {
26+
formatCodeOptions[codeKey] = formatOptions[key];
27+
}
28+
});
29+
}
30+
1931
class ScriptInfo {
2032
svc: ScriptVersionCache;
2133
children: ScriptInfo[] = []; // files referenced by this file
@@ -27,12 +39,9 @@ module ts.server {
2739
this.svc = ScriptVersionCache.fromString(content);
2840
}
2941

30-
setFormatOptions(tabSize?: number, indentSize?: number) {
31-
if (tabSize) {
32-
this.formatCodeOptions.TabSize = tabSize;
33-
}
34-
if (indentSize) {
35-
this.formatCodeOptions.IndentSize = indentSize;
42+
setFormatOptions(formatOptions: protocol.FormatOptions): void {
43+
if (formatOptions) {
44+
mergeFormatOptions(this.formatCodeOptions, formatOptions);
3645
}
3746
}
3847

@@ -448,15 +457,19 @@ module ts.server {
448457
if (args.file) {
449458
var info = this.filenameToScriptInfo[args.file];
450459
if (info) {
451-
info.setFormatOptions(args.tabSize, args.indentSize);
452-
this.log("Host configuration update for file " + args.file + " tab size " + args.tabSize);
460+
info.setFormatOptions(args.formatOptions);
461+
this.log("Host configuration update for file " + args.file);
453462
}
454463
}
455464
else {
456-
this.hostConfiguration.formatCodeOptions.TabSize = args.tabSize;
457-
this.hostConfiguration.formatCodeOptions.IndentSize = args.indentSize;
458-
this.hostConfiguration.hostInfo = args.hostInfo;
459-
this.log("Host information " + args.hostInfo, "Info");
465+
if (args.hostInfo !== undefined) {
466+
this.hostConfiguration.hostInfo = args.hostInfo;
467+
this.log("Host information " + args.hostInfo, "Info");
468+
}
469+
if (args.formatOptions) {
470+
mergeFormatOptions(this.hostConfiguration.formatCodeOptions, args.formatOptions);
471+
this.log("Format host information updated", "Info");
472+
}
460473
}
461474
}
462475

src/server/protocol.d.ts

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -299,23 +299,77 @@ declare module ts.server.protocol {
299299
body?: RenameResponseBody;
300300
}
301301

302+
/**
303+
* Editor options
304+
*/
305+
export interface EditorOptions {
306+
307+
/** Number of spaces for each tab. Default value is 4. */
308+
tabSize?: number;
309+
310+
/** Number of spaces to indent during formatting. Default value is 4. */
311+
indentSize?: number;
312+
313+
/** The new line character to be used. Default value is the OS line delimiter. */
314+
newLineCharacter?: string;
315+
316+
/** Whether tabs should be converted to spaces. Default value is true. */
317+
convertTabsToSpaces?: boolean;
318+
}
319+
320+
/**
321+
* Format options
322+
*/
323+
export interface FormatOptions extends EditorOptions {
324+
325+
/** Defines space handling after a comma delimiter. Default value is true. */
326+
insertSpaceAfterCommaDelimiter?: boolean;
327+
328+
/** Defines space handling after a semicolon in a for statemen. Default value is true */
329+
insertSpaceAfterSemicolonInForStatements?: boolean;
330+
331+
/** Defines space handling after a binary operator. Default value is true. */
332+
insertSpaceBeforeAndAfterBinaryOperators?: boolean;
333+
334+
/** Defines space handling after keywords in control flow statement. Default value is true. */
335+
insertSpaceAfterKeywordsInControlFlowStatements?: boolean;
336+
337+
/** Defines space handling after function keyword for anonymous functions. Default value is false. */
338+
insertSpaceAfterFunctionKeywordForAnonymousFunctions?: boolean;
339+
340+
/** Defines space handling after opening and before closing non empty parenthesis. Default value is false. */
341+
insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis?: boolean;
342+
343+
/** Defines whether an open brace is put onto a new line for functions or not. Default value is false. */
344+
placeOpenBraceOnNewLineForFunctions?: boolean;
345+
346+
/** Defines whether an open brace is put onto a new line for control blocks or not. Default value is false. */
347+
placeOpenBraceOnNewLineForControlBlocks?: boolean;
348+
349+
/** Index operator */
350+
[key:string] : string | number | boolean;
351+
}
352+
302353
/**
303354
* Information found in a configure request.
304355
*/
305356
export interface ConfigureRequestArguments {
306-
/** Number of spaces for each tab */
307-
tabSize: number;
308-
/** Number of spaces to indent during formatting */
309-
indentSize: number;
357+
310358
/**
311359
* Information about the host, for example 'Emacs 24.4' or
312360
* 'Sublime Text version 3075'
313361
*/
314-
hostInfo: string;
362+
hostInfo?: string;
363+
315364
/**
316365
* If present, tab settings apply only to this file.
317366
*/
318367
file?: string;
368+
369+
/**
370+
* The format options to use during formatting and other code editing features.
371+
*/
372+
formatOptions?: FormatOptions;
319373
}
320374

321375
/**
@@ -337,10 +391,6 @@ declare module ts.server.protocol {
337391
* Information found in an "open" request.
338392
*/
339393
export interface OpenRequestArgs extends FileRequestArgs {
340-
/** Initial tab size of file. */
341-
tabSize?: number;
342-
/** Number of spaces to indent during formatting */
343-
indentSize?: number;
344394
}
345395

346396
/**

src/server/session.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -398,12 +398,9 @@ module ts.server {
398398
};
399399
}
400400

401-
openClientFile(fileName: string, tabSize?: number, indentSize?: number) {
401+
openClientFile(fileName: string) {
402402
var file = ts.normalizePath(fileName);
403-
var info = this.projectService.openClientFile(file);
404-
if (info) {
405-
info.setFormatOptions(tabSize, indentSize);
406-
}
403+
this.projectService.openClientFile(file);
407404
}
408405

409406
getQuickInfo(line: number, offset: number, fileName: string): protocol.QuickInfoResponseBody {
@@ -789,7 +786,7 @@ module ts.server {
789786
}
790787
case CommandNames.Open: {
791788
var openArgs = <protocol.OpenRequestArgs>request.arguments;
792-
this.openClientFile(openArgs.file,openArgs.tabSize, openArgs.indentSize);
789+
this.openClientFile(openArgs.file);
793790
responseRequired = false;
794791
break;
795792
}

0 commit comments

Comments
 (0)