Skip to content

Commit 6ebc8ab

Browse files
committed
introduce EditorSettings/FormatCodeSettings interfaces
1 parent 04d617d commit 6ebc8ab

8 files changed

Lines changed: 194 additions & 144 deletions

File tree

src/harness/fourslash.ts

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ namespace FourSlash {
202202
// Whether or not we should format on keystrokes
203203
public enableFormatting = true;
204204

205-
public formatCodeOptions: ts.FormatCodeOptions;
205+
public formatCodeSettings: ts.FormatCodeSettings;
206206

207207
private inputFiles: ts.Map<string> = {}; // Map between inputFile's fileName and its content for easily looking up when resolving references
208208

@@ -309,22 +309,22 @@ namespace FourSlash {
309309
Harness.Compiler.getDefaultLibrarySourceFile().text, /*isRootFile*/ false);
310310
}
311311

312-
this.formatCodeOptions = {
313-
IndentSize: 4,
314-
TabSize: 4,
315-
NewLineCharacter: Harness.IO.newLine(),
316-
ConvertTabsToSpaces: true,
317-
IndentStyle: ts.IndentStyle.Smart,
318-
InsertSpaceAfterCommaDelimiter: true,
319-
InsertSpaceAfterSemicolonInForStatements: true,
320-
InsertSpaceBeforeAndAfterBinaryOperators: true,
321-
InsertSpaceAfterKeywordsInControlFlowStatements: true,
322-
InsertSpaceAfterFunctionKeywordForAnonymousFunctions: false,
323-
InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: false,
324-
InsertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets: false,
325-
InsertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces: false,
326-
PlaceOpenBraceOnNewLineForFunctions: false,
327-
PlaceOpenBraceOnNewLineForControlBlocks: false,
312+
this.formatCodeSettings = {
313+
indentSize: 4,
314+
tabSize: 4,
315+
newLineCharacter: Harness.IO.newLine(),
316+
convertTabsToSpaces: true,
317+
indentStyle: ts.IndentStyle.Smart,
318+
insertSpaceAfterCommaDelimiter: true,
319+
insertSpaceAfterSemicolonInForStatements: true,
320+
insertSpaceBeforeAndAfterBinaryOperators: true,
321+
insertSpaceAfterKeywordsInControlFlowStatements: true,
322+
insertSpaceAfterFunctionKeywordForAnonymousFunctions: false,
323+
insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: false,
324+
insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets: false,
325+
insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces: false,
326+
placeOpenBraceOnNewLineForFunctions: false,
327+
placeOpenBraceOnNewLineForControlBlocks: false,
328328
};
329329

330330
// Open the first file by default
@@ -1278,7 +1278,7 @@ namespace FourSlash {
12781278

12791279
// Handle post-keystroke formatting
12801280
if (this.enableFormatting) {
1281-
const edits = this.languageService.getFormattingEditsAfterKeystroke(this.activeFile.fileName, offset, ch, this.formatCodeOptions);
1281+
const edits = this.languageService.getFormattingEditsAfterKeystroke(this.activeFile.fileName, offset, ch, this.formatCodeSettings);
12821282
if (edits.length) {
12831283
offset += this.applyEdits(this.activeFile.fileName, edits, /*isFormattingEdit*/ true);
12841284
// this.checkPostEditInvariants();
@@ -1316,7 +1316,7 @@ namespace FourSlash {
13161316

13171317
// Handle post-keystroke formatting
13181318
if (this.enableFormatting) {
1319-
const edits = this.languageService.getFormattingEditsAfterKeystroke(this.activeFile.fileName, offset, ch, this.formatCodeOptions);
1319+
const edits = this.languageService.getFormattingEditsAfterKeystroke(this.activeFile.fileName, offset, ch, this.formatCodeSettings);
13201320
if (edits.length) {
13211321
offset += this.applyEdits(this.activeFile.fileName, edits, /*isFormattingEdit*/ true);
13221322
}
@@ -1369,7 +1369,7 @@ namespace FourSlash {
13691369

13701370
// Handle post-keystroke formatting
13711371
if (this.enableFormatting) {
1372-
const edits = this.languageService.getFormattingEditsAfterKeystroke(this.activeFile.fileName, offset, ch, this.formatCodeOptions);
1372+
const edits = this.languageService.getFormattingEditsAfterKeystroke(this.activeFile.fileName, offset, ch, this.formatCodeSettings);
13731373
if (edits.length) {
13741374
offset += this.applyEdits(this.activeFile.fileName, edits, /*isFormattingEdit*/ true);
13751375
// this.checkPostEditInvariants();
@@ -1395,7 +1395,7 @@ namespace FourSlash {
13951395

13961396
// Handle formatting
13971397
if (this.enableFormatting) {
1398-
const edits = this.languageService.getFormattingEditsForRange(this.activeFile.fileName, start, offset, this.formatCodeOptions);
1398+
const edits = this.languageService.getFormattingEditsForRange(this.activeFile.fileName, start, offset, this.formatCodeSettings);
13991399
if (edits.length) {
14001400
offset += this.applyEdits(this.activeFile.fileName, edits, /*isFormattingEdit*/ true);
14011401
this.checkPostEditInvariants();
@@ -1469,30 +1469,30 @@ namespace FourSlash {
14691469
return runningOffset;
14701470
}
14711471

1472-
public copyFormatOptions(): ts.FormatCodeOptions {
1473-
return ts.clone(this.formatCodeOptions);
1472+
public copyFormatOptions(): ts.FormatCodeSettings {
1473+
return ts.clone(this.formatCodeSettings);
14741474
}
14751475

1476-
public setFormatOptions(formatCodeOptions: ts.FormatCodeOptions): ts.FormatCodeOptions {
1477-
const oldFormatCodeOptions = this.formatCodeOptions;
1478-
this.formatCodeOptions = formatCodeOptions;
1476+
public setFormatOptions(formatCodeOptions: ts.FormatCodeOptions | ts.FormatCodeSettings): ts.FormatCodeSettings {
1477+
const oldFormatCodeOptions = this.formatCodeSettings;
1478+
this.formatCodeSettings = ts.toEditorSettings(formatCodeOptions);
14791479
return oldFormatCodeOptions;
14801480
}
14811481

14821482
public formatDocument() {
1483-
const edits = this.languageService.getFormattingEditsForDocument(this.activeFile.fileName, this.formatCodeOptions);
1483+
const edits = this.languageService.getFormattingEditsForDocument(this.activeFile.fileName, this.formatCodeSettings);
14841484
this.currentCaretPosition += this.applyEdits(this.activeFile.fileName, edits, /*isFormattingEdit*/ true);
14851485
this.fixCaretPosition();
14861486
}
14871487

14881488
public formatSelection(start: number, end: number) {
1489-
const edits = this.languageService.getFormattingEditsForRange(this.activeFile.fileName, start, end, this.formatCodeOptions);
1489+
const edits = this.languageService.getFormattingEditsForRange(this.activeFile.fileName, start, end, this.formatCodeSettings);
14901490
this.currentCaretPosition += this.applyEdits(this.activeFile.fileName, edits, /*isFormattingEdit*/ true);
14911491
this.fixCaretPosition();
14921492
}
14931493

14941494
public formatOnType(pos: number, key: string) {
1495-
const edits = this.languageService.getFormattingEditsAfterKeystroke(this.activeFile.fileName, pos, key, this.formatCodeOptions);
1495+
const edits = this.languageService.getFormattingEditsAfterKeystroke(this.activeFile.fileName, pos, key, this.formatCodeSettings);
14961496
this.currentCaretPosition += this.applyEdits(this.activeFile.fileName, edits, /*isFormattingEdit*/ true);
14971497
this.fixCaretPosition();
14981498
}
@@ -1621,8 +1621,8 @@ namespace FourSlash {
16211621

16221622
private getIndentation(fileName: string, position: number, indentStyle: ts.IndentStyle): number {
16231623

1624-
const formatOptions = ts.clone(this.formatCodeOptions);
1625-
formatOptions.IndentStyle = indentStyle;
1624+
const formatOptions = ts.clone(this.formatCodeSettings);
1625+
formatOptions.indentStyle = indentStyle;
16261626

16271627
return this.languageService.getIndentationAtPosition(fileName, position, formatOptions);
16281628
}
@@ -3226,7 +3226,7 @@ namespace FourSlashInterface {
32263226
this.state.formatDocument();
32273227
}
32283228

3229-
public copyFormatOptions(): ts.FormatCodeOptions {
3229+
public copyFormatOptions(): ts.FormatCodeSettings {
32303230
return this.state.copyFormatOptions();
32313231
}
32323232

@@ -3246,7 +3246,7 @@ namespace FourSlashInterface {
32463246
public setOption(name: string, value: string): void;
32473247
public setOption(name: string, value: boolean): void;
32483248
public setOption(name: string, value: any): void {
3249-
this.state.formatCodeOptions[name] = value;
3249+
(<any>this.state.formatCodeSettings)[name] = value;
32503250
}
32513251
}
32523252

src/server/editorServices.ts

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,53 +16,51 @@ namespace ts.server {
1616
msg(s: string, type?: string): void;
1717
}
1818

19-
function getDefaultFormatCodeOptions(host: ServerHost): ts.FormatCodeOptions {
20-
return ts.clone(<ts.FormatCodeOptions>{
21-
IndentSize: 4,
22-
TabSize: 4,
23-
NewLineCharacter: host.newLine || "\n",
24-
ConvertTabsToSpaces: true,
25-
IndentStyle: ts.IndentStyle.Smart,
26-
InsertSpaceAfterCommaDelimiter: true,
27-
InsertSpaceAfterSemicolonInForStatements: true,
28-
InsertSpaceBeforeAndAfterBinaryOperators: true,
29-
InsertSpaceAfterKeywordsInControlFlowStatements: true,
30-
InsertSpaceAfterFunctionKeywordForAnonymousFunctions: false,
31-
InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: false,
32-
InsertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets: false,
33-
InsertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces: false,
34-
PlaceOpenBraceOnNewLineForFunctions: false,
35-
PlaceOpenBraceOnNewLineForControlBlocks: false,
19+
function getDefaultFormatCodeSettings(host: ServerHost): ts.FormatCodeSettings {
20+
return ts.clone(<ts.FormatCodeSettings>{
21+
indentSize: 4,
22+
tabSize: 4,
23+
newLineCharacter: host.newLine || "\n",
24+
convertTabsToSpaces: true,
25+
indentStyle: ts.IndentStyle.Smart,
26+
insertSpaceAfterCommaDelimiter: true,
27+
insertSpaceAfterSemicolonInForStatements: true,
28+
insertSpaceBeforeAndAfterBinaryOperators: true,
29+
insertSpaceAfterKeywordsInControlFlowStatements: true,
30+
insertSpaceAfterFunctionKeywordForAnonymousFunctions: false,
31+
insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: false,
32+
insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets: false,
33+
insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces: false,
34+
placeOpenBraceOnNewLineForFunctions: false,
35+
placeOpenBraceOnNewLineForControlBlocks: false,
3636
});
3737
}
3838

39-
function mergeFormatOptions(formatCodeOptions: FormatCodeOptions, formatOptions: protocol.FormatOptions): void {
40-
const hasOwnProperty = Object.prototype.hasOwnProperty;
41-
Object.keys(formatOptions).forEach((key) => {
42-
const codeKey = key.charAt(0).toUpperCase() + key.substring(1);
43-
if (hasOwnProperty.call(formatCodeOptions, codeKey)) {
44-
formatCodeOptions[codeKey] = formatOptions[key];
39+
function mergeMaps(target: Map<any>, source: Map<any>): void {
40+
for (const key in source) {
41+
if (hasProperty(source, key)) {
42+
target[key] = source[key];
4543
}
46-
});
44+
}
4745
}
4846

4947
export class ScriptInfo {
5048
svc: ScriptVersionCache;
5149
defaultProject: Project; // project to use by default for file
5250
fileWatcher: FileWatcher;
53-
formatCodeOptions: ts.FormatCodeOptions;
51+
formatCodeSettings: ts.FormatCodeSettings;
5452
path: Path;
5553
scriptKind: ScriptKind;
5654

5755
constructor(private host: ServerHost, public fileName: string, public content: string, public isOpen = false) {
5856
this.path = toPath(fileName, host.getCurrentDirectory(), createGetCanonicalFileName(host.useCaseSensitiveFileNames));
5957
this.svc = ScriptVersionCache.fromString(host, content);
60-
this.formatCodeOptions = getDefaultFormatCodeOptions(this.host);
58+
this.formatCodeSettings = getDefaultFormatCodeSettings(this.host);
6159
}
6260

63-
setFormatOptions(formatOptions: protocol.FormatOptions): void {
64-
if (formatOptions) {
65-
mergeFormatOptions(this.formatCodeOptions, formatOptions);
61+
setFormatOptions(formatSettings: protocol.FormatOptions): void {
62+
if (formatSettings) {
63+
mergeMaps(this.formatCodeSettings, formatSettings);
6664
}
6765
}
6866

@@ -603,7 +601,7 @@ namespace ts.server {
603601
}
604602

605603
export interface HostConfiguration {
606-
formatCodeOptions: ts.FormatCodeOptions;
604+
formatCodeOptions: ts.FormatCodeSettings;
607605
hostInfo: string;
608606
}
609607

@@ -665,7 +663,7 @@ namespace ts.server {
665663

666664
private setDefaultHostConfiguration() {
667665
this.hostConfiguration = {
668-
formatCodeOptions: getDefaultFormatCodeOptions(this.host),
666+
formatCodeOptions: getDefaultFormatCodeSettings(this.host),
669667
hostInfo: "Unknown host"
670668
};
671669
}
@@ -693,7 +691,7 @@ namespace ts.server {
693691
if (file) {
694692
const info = this.filenameToScriptInfo[file];
695693
if (info) {
696-
return info.formatCodeOptions;
694+
return info.formatCodeSettings;
697695
}
698696
}
699697
return this.hostConfiguration.formatCodeOptions;
@@ -1286,7 +1284,7 @@ namespace ts.server {
12861284
if (content !== undefined) {
12871285
info = new ScriptInfo(this.host, fileName, content, openedByClient);
12881286
info.scriptKind = scriptKind;
1289-
info.setFormatOptions(this.getFormatCodeOptions());
1287+
info.setFormatOptions(toEditorSettings(this.getFormatCodeOptions()));
12901288
this.filenameToScriptInfo[fileName] = info;
12911289
if (!info.isOpen) {
12921290
info.fileWatcher = this.host.watchFile(fileName, _ => { this.onSourceFileChanged(fileName); });
@@ -1322,7 +1320,7 @@ namespace ts.server {
13221320
this.log("Host information " + args.hostInfo, "Info");
13231321
}
13241322
if (args.formatOptions) {
1325-
mergeFormatOptions(this.hostConfiguration.formatCodeOptions, args.formatOptions);
1323+
mergeMaps(this.hostConfiguration.formatCodeOptions, args.formatOptions);
13261324
this.log("Format host information updated", "Info");
13271325
}
13281326
}

src/server/protocol.d.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -544,9 +544,6 @@ declare namespace ts.server.protocol {
544544

545545
/** Defines whether an open brace is put onto a new line for control blocks or not. Default value is false. */
546546
placeOpenBraceOnNewLineForControlBlocks?: boolean;
547-
548-
/** Index operator */
549-
[key: string]: string | number | boolean;
550547
}
551548

552549
/**

src/server/session.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -778,10 +778,10 @@ namespace ts.server {
778778
if (lineText.search("\\S") < 0) {
779779
// TODO: get these options from host
780780
const editorOptions: ts.EditorOptions = {
781-
IndentSize: formatOptions.IndentSize,
782-
TabSize: formatOptions.TabSize,
783-
NewLineCharacter: formatOptions.NewLineCharacter,
784-
ConvertTabsToSpaces: formatOptions.ConvertTabsToSpaces,
781+
IndentSize: formatOptions.indentSize,
782+
TabSize: formatOptions.tabSize,
783+
NewLineCharacter: formatOptions.newLineCharacter,
784+
ConvertTabsToSpaces: formatOptions.convertTabsToSpaces,
785785
IndentStyle: ts.IndentStyle.Smart,
786786
};
787787
const preferredIndent = project.languageService.getIndentationAtPosition(file, position, editorOptions);

0 commit comments

Comments
 (0)