forked from microsoft/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyntaxDedenter.ts
More file actions
193 lines (159 loc) · 9.13 KB
/
Copy pathsyntaxDedenter.ts
File metadata and controls
193 lines (159 loc) · 9.13 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
///<reference path='references.ts' />
module TypeScript {
export class SyntaxDedenter extends SyntaxRewriter {
private lastTriviaWasNewLine: boolean;
constructor(dedentFirstToken: boolean,
private dedentationAmount: number,
private minimumIndent: number,
private options: FormattingOptions) {
super();
this.lastTriviaWasNewLine = dedentFirstToken;
}
private abort(): void {
this.lastTriviaWasNewLine = false;
this.dedentationAmount = 0;
}
private isAborted(): boolean {
return this.dedentationAmount === 0;
}
public visitToken(token: ISyntaxToken): ISyntaxToken {
if (token.width() === 0) {
return token;
}
var result = token;
if (this.lastTriviaWasNewLine) {
// have to add our indentation to every line that this token hits.
result = token.withLeadingTrivia(this.dedentTriviaList(token.leadingTrivia()));
}
if (this.isAborted()) {
// If we've decided to stop dedenting. Then just return immediately.
return token;
}
this.lastTriviaWasNewLine = token.hasTrailingNewLine();
return result;
}
private dedentTriviaList(triviaList: ISyntaxTriviaList): ISyntaxTriviaList {
var result: ISyntaxTrivia[] = [];
var dedentNextWhitespace = true;
// Keep walking through all our trivia (as long as we haven't decided to stop dedenting).
// Adjust the indentation on any whitespace trivia at the start of a line, or any multi-line
// trivia that span multiple lines.
for (var i = 0, n = triviaList.count(); i < n && !this.isAborted(); i++) {
var trivia = triviaList.syntaxTriviaAt(i);
var dedentThisTrivia = dedentNextWhitespace;
dedentNextWhitespace = false;
if (dedentThisTrivia) {
if (trivia.kind() === SyntaxKind.WhitespaceTrivia) {
// We pass in if there was a following newline after this whitespace. If there
// is, then it's fine if we dedent this newline all the way to 0. Otherwise,
// if the whitespace is followed by something, then we need to determine how
// much of the whitespace we can remove. If we can't remove all that we want,
// we'll need to adjust the dedentAmount. And, if we can't remove at all, then
// we need to stop dedenting entirely.
var hasFollowingNewLine = (i < triviaList.count() - 1) &&
triviaList.syntaxTriviaAt(i + 1).kind() === SyntaxKind.NewLineTrivia;
result.push(this.dedentWhitespace(trivia, hasFollowingNewLine));
continue;
}
else if (trivia.kind() !== SyntaxKind.NewLineTrivia) {
// We wanted to dedent, but the trivia we're on isn't whitespace and wasn't a
// newline. That means that we have something like a comment at the beginning
// of the line that we can't dedent. And, if we can't dedent it, then we
// shouldn't dedent this token or any more tokens.
this.abort();
break;
}
}
if (trivia.kind() === SyntaxKind.MultiLineCommentTrivia) {
// This trivia may span multiple lines. If it does, we need to dedent each
// successive line of it until it terminates.
result.push(this.dedentMultiLineComment(trivia));
continue;
}
// All other trivia we just append to the list.
result.push(trivia);
if (trivia.kind() === SyntaxKind.NewLineTrivia) {
// We hit a newline processing the trivia. We need to add the indentation to the
// next line as well.
dedentNextWhitespace = true;
}
}
if (dedentNextWhitespace) {
// We hit a new line as the last trivia (or there was no trivia). We want to dedent
// the next trivia, but we can't (because the token starts at the start of the line).
// If we can't dedent this, then we shouldn't dedent anymore.
this.abort();
}
if (this.isAborted()) {
return triviaList;
}
return Syntax.triviaList(result);
}
private dedentSegment(segment: string, hasFollowingNewLineTrivia: boolean): string {
// Find the position of the first non whitespace character in the segment.
var firstNonWhitespacePosition = Indentation.firstNonWhitespacePosition(segment);
if (firstNonWhitespacePosition === segment.length) {
if (hasFollowingNewLineTrivia) {
// It was entirely whitespace trivia, with a newline after it. Just trim this down
// to an empty string.
return "";
}
}
else if (CharacterInfo.isLineTerminator(segment.charCodeAt(firstNonWhitespacePosition))) {
// It was entirely whitespace, with a newline after it. Just trim this down to
// the newline
return segment.substring(firstNonWhitespacePosition);
}
// It was whitespace without a newline following it. We need to try to dedent this a bit.
// Convert that position to a column.
var firstNonWhitespaceColumn = Indentation.columnForPositionInString(segment, firstNonWhitespacePosition, this.options);
// Find the new column we want the nonwhitespace text to start at. Ideally it would be
// whatever column it was minus the dedentation amount. However, we won't go below a
// specified minimum indent (hence, max(initial - dedentAmount, minIndent). *But* if
// the initial column was less than that minimum indent, then we'll keep it at that column.
// (hence min(initial, desired)).
var newFirstNonWhitespaceColumn =
MathPrototype.min(firstNonWhitespaceColumn,
MathPrototype.max(firstNonWhitespaceColumn - this.dedentationAmount, this.minimumIndent));
if (newFirstNonWhitespaceColumn === firstNonWhitespaceColumn) {
// We aren't able to detent this token. Abort what we're doing
this.abort();
return segment;
}
// Update the dedentation amount for all subsequent tokens we run into.
this.dedentationAmount = firstNonWhitespaceColumn - newFirstNonWhitespaceColumn;
Debug.assert(this.dedentationAmount >= 0);
// Compute an indentation string for that.
var indentationString = Indentation.indentationString(newFirstNonWhitespaceColumn, this.options);
// Join the new indentation and the original string without its indentation.
return indentationString + segment.substring(firstNonWhitespacePosition);
}
private dedentWhitespace(trivia: ISyntaxTrivia, hasFollowingNewLineTrivia: boolean): ISyntaxTrivia {
var newIndentation = this.dedentSegment(trivia.fullText(), hasFollowingNewLineTrivia);
return Syntax.whitespace(newIndentation);
}
private dedentMultiLineComment(trivia: ISyntaxTrivia): ISyntaxTrivia {
var segments = Syntax.splitMultiLineCommentTriviaIntoMultipleLines(trivia);
if (segments.length === 1) {
// If there was only one segment, then this wasn't multiline.
return trivia;
}
for (var i = 1; i < segments.length; i++) {
var segment = segments[i];
segments[i] = this.dedentSegment(segment, /*hasFollowingNewLineTrivia*/ false);
}
var result = segments.join("");
// Create a new trivia token out of the indented lines.
return Syntax.multiLineComment(result);
}
public static dedentNode<T extends ISyntaxNode>(node: T, dedentFirstToken: boolean, dedentAmount: number, minimumIndent: number, options: FormattingOptions): T {
var dedenter = new SyntaxDedenter(dedentFirstToken, dedentAmount, minimumIndent, options);
var result = node.accept(dedenter);
if (dedenter.isAborted()) {
// We failed to dedent a token in this node. Return the original node as is.
return node;
}
return result;
}
}
}