Merge pull request #2402 from Microsoft/dropInternedStrings

drop interned indentation prefixes if format options has changed
This commit is contained in:
Vladimir Matveev 2015-03-17 17:17:53 -07:00
commit 17f3e1462d
3 changed files with 36 additions and 56 deletions

View File

@ -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;

View File

@ -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;
}
}
}

View 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);