Skip to content

Commit e248d08

Browse files
author
Andy
authored
Combine repeatString helper functions (#21235)
1 parent f96dc84 commit e248d08

File tree

3 files changed

+16
-25
lines changed

3 files changed

+16
-25
lines changed

src/harness/fourslash.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,8 +1535,8 @@ Actual: ${stringify(fullActual)}`);
15351535
const addSpanInfoString = () => {
15361536
if (previousSpanInfo) {
15371537
resultString += currentLine;
1538-
let thisLineMarker = repeatString(startColumn, " ") + repeatString(length, "~");
1539-
thisLineMarker += repeatString(this.alignmentForExtraInfo - thisLineMarker.length - prefixString.length + 1, " ");
1538+
let thisLineMarker = ts.repeatString(" ", startColumn) + ts.repeatString("~", length);
1539+
thisLineMarker += ts.repeatString(" ", this.alignmentForExtraInfo - thisLineMarker.length - prefixString.length + 1);
15401540
resultString += thisLineMarker;
15411541
resultString += "=> Pos: (" + (pos - length) + " to " + (pos - 1) + ") ";
15421542
resultString += " " + previousSpanInfo;
@@ -1551,7 +1551,7 @@ Actual: ${stringify(fullActual)}`);
15511551
if (resultString.length) {
15521552
resultString += "\n--------------------------------";
15531553
}
1554-
currentLine = "\n" + nextLine.toString() + repeatString(3 - nextLine.toString().length, " ") + ">" + this.activeFile.content.substring(pos, fileLineMap[nextLine]) + "\n ";
1554+
currentLine = "\n" + nextLine.toString() + ts.repeatString(" ", 3 - nextLine.toString().length) + ">" + this.activeFile.content.substring(pos, fileLineMap[nextLine]) + "\n ";
15551555
startColumn = 0;
15561556
length = 0;
15571557
}
@@ -2787,7 +2787,7 @@ Actual: ${stringify(fullActual)}`);
27872787
const items = this.languageService.getNavigationBarItems(this.activeFile.fileName);
27882788
Harness.IO.log(`Navigation bar (${items.length} items)`);
27892789
for (const item of items) {
2790-
Harness.IO.log(`${repeatString(item.indent, " ")}name: ${item.text}, kind: ${item.kind}, childItems: ${item.childItems.map(child => child.text)}`);
2790+
Harness.IO.log(`${ts.repeatString(" ", item.indent)}name: ${item.text}, kind: ${item.kind}, childItems: ${item.childItems.map(child => child.text)}`);
27912791
}
27922792
}
27932793

@@ -3689,14 +3689,6 @@ ${code}
36893689
};
36903690
}
36913691

3692-
function repeatString(count: number, char: string) {
3693-
let result = "";
3694-
for (let i = 0; i < count; i++) {
3695-
result += char;
3696-
}
3697-
return result;
3698-
}
3699-
37003692
function stringify(data: any, replacer?: (key: string, value: any) => any): string {
37013693
return JSON.stringify(data, replacer, 2);
37023694
}

src/services/formatting/formatting.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,13 +1277,13 @@ namespace ts.formatting {
12771277
}
12781278

12791279
if (internedTabsIndentation[tabs] === undefined) {
1280-
internedTabsIndentation[tabs] = tabString = repeat("\t", tabs);
1280+
internedTabsIndentation[tabs] = tabString = repeatString("\t", tabs);
12811281
}
12821282
else {
12831283
tabString = internedTabsIndentation[tabs];
12841284
}
12851285

1286-
return spaces ? tabString + repeat(" ", spaces) : tabString;
1286+
return spaces ? tabString + repeatString(" ", spaces) : tabString;
12871287
}
12881288
else {
12891289
let spacesString: string;
@@ -1294,23 +1294,14 @@ namespace ts.formatting {
12941294
}
12951295

12961296
if (internedSpacesIndentation[quotient] === undefined) {
1297-
spacesString = repeat(" ", options.indentSize * quotient);
1297+
spacesString = repeatString(" ", options.indentSize * quotient);
12981298
internedSpacesIndentation[quotient] = spacesString;
12991299
}
13001300
else {
13011301
spacesString = internedSpacesIndentation[quotient];
13021302
}
13031303

1304-
return remainder ? spacesString + repeat(" ", remainder) : spacesString;
1305-
}
1306-
1307-
function repeat(value: string, count: number): string {
1308-
let s = "";
1309-
for (let i = 0; i < count; i++) {
1310-
s += value;
1311-
}
1312-
1313-
return s;
1304+
return remainder ? spacesString + repeatString(" ", remainder) : spacesString;
13141305
}
13151306
}
13161307
}

src/services/utilities.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,6 +1110,14 @@ namespace ts {
11101110
export function getSnapshotText(snap: IScriptSnapshot): string {
11111111
return snap.getText(0, snap.getLength());
11121112
}
1113+
1114+
export function repeatString(str: string, count: number): string {
1115+
let result = "";
1116+
for (let i = 0; i < count; i++) {
1117+
result += str;
1118+
}
1119+
return result;
1120+
}
11131121
}
11141122

11151123
// Display-part writer helpers

0 commit comments

Comments
 (0)