mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-07 14:34:35 -06:00
Merge pull request #2402 from Microsoft/dropInternedStrings
drop interned indentation prefixes if format options has changed
This commit is contained in:
commit
17f3e1462d
@ -1008,10 +1008,20 @@ module ts.formatting {
|
||||
return SyntaxKind.Unknown;
|
||||
}
|
||||
|
||||
let internedTabsIndentation: string[];
|
||||
let internedSpacesIndentation: string[];
|
||||
var internedSizes: { tabSize: number; indentSize: number };
|
||||
var internedTabsIndentation: string[];
|
||||
var internedSpacesIndentation: string[];
|
||||
|
||||
export function getIndentationString(indentation: number, options: FormatCodeOptions): string {
|
||||
// reset interned strings if FormatCodeOptions were changed
|
||||
let resetInternedStrings =
|
||||
!internedSizes || (internedSizes.tabSize !== options.TabSize || internedSizes.indentSize !== options.IndentSize);
|
||||
|
||||
if (resetInternedStrings) {
|
||||
internedSizes = { tabSize: options.TabSize, indentSize: options.IndentSize };
|
||||
internedTabsIndentation = internedSpacesIndentation = undefined;
|
||||
}
|
||||
|
||||
if (!options.ConvertTabsToSpaces) {
|
||||
let tabs = Math.floor(indentation / options.TabSize);
|
||||
let spaces = indentation - tabs * options.TabSize;
|
||||
|
||||
@ -1,54 +0,0 @@
|
||||
module ts.formatting {
|
||||
|
||||
var internedTabsIndentation: string[];
|
||||
var internedSpacesIndentation: string[];
|
||||
|
||||
export function getIndentationString(indentation: number, options: FormatCodeOptions): string {
|
||||
if (!options.ConvertTabsToSpaces) {
|
||||
var tabs = Math.floor(indentation / options.TabSize);
|
||||
var spaces = indentation - tabs * options.TabSize;
|
||||
|
||||
var tabString: string;
|
||||
if (!internedTabsIndentation) {
|
||||
internedTabsIndentation = [];
|
||||
}
|
||||
|
||||
if (internedTabsIndentation[tabs] === undefined) {
|
||||
internedTabsIndentation[tabs] = tabString = repeat('\t', tabs);
|
||||
}
|
||||
else {
|
||||
tabString = internedTabsIndentation[tabs];
|
||||
}
|
||||
|
||||
return spaces ? tabString + repeat(" ", spaces) : tabString;
|
||||
}
|
||||
else {
|
||||
var spacesString: string;
|
||||
var quotient = Math.floor(indentation / options.IndentSize);
|
||||
var remainder = indentation % options.IndentSize;
|
||||
if (!internedSpacesIndentation) {
|
||||
internedSpacesIndentation = [];
|
||||
}
|
||||
|
||||
if (internedSpacesIndentation[quotient] === undefined) {
|
||||
spacesString = repeat(" ", options.IndentSize * quotient);
|
||||
internedSpacesIndentation[quotient] = spacesString;
|
||||
}
|
||||
else {
|
||||
spacesString = internedSpacesIndentation[quotient];
|
||||
}
|
||||
|
||||
|
||||
return remainder ? spacesString + repeat(" ", remainder) : spacesString;
|
||||
}
|
||||
|
||||
function repeat(value: string, count: number): string {
|
||||
var s = "";
|
||||
for (var i = 0; i < count; ++i) {
|
||||
s += value;
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
}
|
||||
}
|
||||
24
tests/cases/fourslash/formattingChangeSettings.ts
Normal file
24
tests/cases/fourslash/formattingChangeSettings.ts
Normal file
@ -0,0 +1,24 @@
|
||||
/// <reference path='fourslash.ts'/>
|
||||
|
||||
////module M {
|
||||
/////*1*/var x=1;
|
||||
////}
|
||||
|
||||
var originalOptions = format.copyFormatOptions();
|
||||
|
||||
format.document();
|
||||
|
||||
goTo.marker("1");
|
||||
verify.currentLineContentIs(" var x = 1;");
|
||||
|
||||
var copy = format.copyFormatOptions();
|
||||
copy.TabSize = 2;
|
||||
copy.IndentSize = 2;
|
||||
|
||||
format.setFormatOptions(copy);
|
||||
format.document();
|
||||
|
||||
goTo.marker("1");
|
||||
verify.currentLineContentIs(" var x = 1;");
|
||||
|
||||
format.setFormatOptions(originalOptions);
|
||||
Loading…
x
Reference in New Issue
Block a user