forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtextBuilder.ts
More file actions
47 lines (39 loc) · 1.2 KB
/
textBuilder.ts
File metadata and controls
47 lines (39 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { isWhiteSpace } from './characters';
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
export class TextBuilder {
private segments: string[] = [];
public getText(): string {
if (this.isLastWhiteSpace()) {
this.segments.pop();
}
return this.segments.join('');
}
public softAppendSpace(count: number = 1): void {
if (this.segments.length === 0) {
return;
}
if (this.isLastWhiteSpace()) {
count = count - 1;
}
for (let i = 0; i < count; i += 1) {
this.segments.push(' ');
}
}
public append(text: string): void {
this.segments.push(text);
}
private isLastWhiteSpace(): boolean {
return this.segments.length > 0 && this.isWhitespace(this.segments[this.segments.length - 1]);
}
private isWhitespace(s: string): boolean {
for (let i = 0; i < s.length; i += 1) {
if (!isWhiteSpace(s.charCodeAt(i))) {
return false;
}
}
return true;
}
}