-
Notifications
You must be signed in to change notification settings - Fork 39.5k
Expand file tree
/
Copy pathblockCommentCommand.ts
More file actions
316 lines (267 loc) · 11 KB
/
blockCommentCommand.ts
File metadata and controls
316 lines (267 loc) · 11 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { CharCode } from '../../../../base/common/charCode.js';
import { EditOperation, ISingleEditOperation } from '../../../common/core/editOperation.js';
import { Position } from '../../../common/core/position.js';
import { Range } from '../../../common/core/range.js';
import { Selection } from '../../../common/core/selection.js';
import { ICommand, ICursorStateComputerData, IEditOperationBuilder } from '../../../common/editorCommon.js';
import { ITextModel } from '../../../common/model.js';
import { ILanguageConfigurationService } from '../../../common/languages/languageConfigurationRegistry.js';
export class BlockCommentCommand implements ICommand {
private readonly _selection: Selection;
private readonly _insertSpace: boolean;
private _usedEndToken: string | null;
private _isRemove: boolean = false;
constructor(
selection: Selection,
insertSpace: boolean,
private readonly languageConfigurationService: ILanguageConfigurationService
) {
this._selection = selection;
this._insertSpace = insertSpace;
this._usedEndToken = null;
}
public static findEnclosingBlockCommentRange(model: ITextModel, position: Position, languageConfigurationService: ILanguageConfigurationService): Range | null {
const lineNumber = position.lineNumber;
const column = position.column;
// Get language config
const languageId = model.getLanguageIdAtPosition(lineNumber, column);
const config = languageConfigurationService.getLanguageConfiguration(languageId).comments;
if (!config || !config.blockCommentStartToken || !config.blockCommentEndToken) {
return null;
}
const startToken = config.blockCommentStartToken;
const endToken = config.blockCommentEndToken;
// Find the opening token by walking backward
let startLine = lineNumber;
let startCol = column;
let foundStart = false;
// First, search backward on the current line
const currentLineContent = model.getLineContent(startLine);
let idx = Math.min(startCol - 1, currentLineContent.length);
while (idx >= 0) {
if (BlockCommentCommand._haystackHasNeedleAtOffset(currentLineContent, startToken, idx)) {
startCol = idx + 1; // 1-based
foundStart = true;
break;
}
idx--;
}
if (!foundStart) {
for (let ln = startLine - 1; ln >= 1; ln--) {
const lineContent = model.getLineContent(ln);
idx = lineContent.lastIndexOf(startToken);
if (idx !== -1) {
startLine = ln;
startCol = idx + 1; // 1-based
foundStart = true;
break;
}
}
}
if (!foundStart) {
return null;
}
let endLine = startLine;
let endCol = startCol + startToken.length;
let foundEnd = false;
let currentLine = startLine;
let currentCol = endCol;
while (currentLine <= model.getLineCount()) {
const lineContent = model.getLineContent(currentLine);
if (currentLine === startLine) {
idx = lineContent.indexOf(endToken, currentCol - 1);
} else {
idx = lineContent.indexOf(endToken);
}
if (idx !== -1) {
endLine = currentLine;
endCol = idx + endToken.length + 1; // 1-based, exclusive end after end token
foundEnd = true;
break;
}
currentLine++;
currentCol = 1;
}
if (!foundEnd) {
return null;
}
const commentRange = new Range(startLine, startCol, endLine, endCol);
if (!commentRange.containsPosition(position)) {
return null;
}
// Return the full range including tokens
return commentRange;
}
public static _haystackHasNeedleAtOffset(haystack: string, needle: string, offset: number): boolean {
if (offset < 0) {
return false;
}
const needleLength = needle.length;
const haystackLength = haystack.length;
if (offset + needleLength > haystackLength) {
return false;
}
for (let i = 0; i < needleLength; i++) {
const codeA = haystack.charCodeAt(offset + i);
const codeB = needle.charCodeAt(i);
if (codeA === codeB) {
continue;
}
if (codeA >= CharCode.A && codeA <= CharCode.Z && codeA + 32 === codeB) {
// codeA is upper-case variant of codeB
continue;
}
if (codeB >= CharCode.A && codeB <= CharCode.Z && codeB + 32 === codeA) {
// codeB is upper-case variant of codeA
continue;
}
return false;
}
return true;
}
private _createOperationsForBlockComment(selection: Range, startToken: string, endToken: string, insertSpace: boolean, model: ITextModel, builder: IEditOperationBuilder): void {
const startLineNumber = selection.startLineNumber;
const startColumn = selection.startColumn;
const endLineNumber = selection.endLineNumber;
const endColumn = selection.endColumn;
const startLineText = model.getLineContent(startLineNumber);
const endLineText = model.getLineContent(endLineNumber);
let startTokenIndex = startLineText.lastIndexOf(startToken, startColumn - 1 + startToken.length);
let endTokenIndex = endLineText.indexOf(endToken, endColumn - 1 - endToken.length);
if (startTokenIndex !== -1 && endTokenIndex !== -1) {
if (startLineNumber === endLineNumber) {
const lineBetweenTokens = startLineText.substring(startTokenIndex + startToken.length, endTokenIndex);
if (lineBetweenTokens.indexOf(endToken) >= 0) {
// force to add a block comment
startTokenIndex = -1;
endTokenIndex = -1;
}
} else {
const startLineAfterStartToken = startLineText.substring(startTokenIndex + startToken.length);
const endLineBeforeEndToken = endLineText.substring(0, endTokenIndex);
if (startLineAfterStartToken.indexOf(endToken) >= 0 || endLineBeforeEndToken.indexOf(endToken) >= 0) {
// force to add a block comment
startTokenIndex = -1;
endTokenIndex = -1;
}
}
}
let ops: ISingleEditOperation[];
if (startTokenIndex !== -1 && endTokenIndex !== -1) {
// Consider spaces as part of the comment tokens
if (insertSpace && startTokenIndex + startToken.length < startLineText.length && startLineText.charCodeAt(startTokenIndex + startToken.length) === CharCode.Space) {
// Pretend the start token contains a trailing space
startToken = startToken + ' ';
}
if (insertSpace && endTokenIndex > 0 && endLineText.charCodeAt(endTokenIndex - 1) === CharCode.Space) {
// Pretend the end token contains a leading space
endToken = ' ' + endToken;
endTokenIndex -= 1;
}
ops = BlockCommentCommand._createRemoveBlockCommentOperations(
new Range(startLineNumber, startTokenIndex + startToken.length + 1, endLineNumber, endTokenIndex + 1), startToken, endToken
);
} else {
ops = BlockCommentCommand._createAddBlockCommentOperations(selection, startToken, endToken, this._insertSpace);
this._usedEndToken = ops.length === 1 ? endToken : null;
}
for (const op of ops) {
builder.addTrackedEditOperation(op.range, op.text);
}
}
public static _createRemoveBlockCommentOperations(r: Range, startToken: string, endToken: string): ISingleEditOperation[] {
const res: ISingleEditOperation[] = [];
if (!Range.isEmpty(r)) {
// Remove block comment start
res.push(EditOperation.delete(new Range(
r.startLineNumber, r.startColumn - startToken.length,
r.startLineNumber, r.startColumn
)));
// Remove block comment end
res.push(EditOperation.delete(new Range(
r.endLineNumber, r.endColumn,
r.endLineNumber, r.endColumn + endToken.length
)));
} else {
// Remove both continuously
res.push(EditOperation.delete(new Range(
r.startLineNumber, r.startColumn - startToken.length,
r.endLineNumber, r.endColumn + endToken.length
)));
}
return res;
}
public static _createAddBlockCommentOperations(r: Range, startToken: string, endToken: string, insertSpace: boolean): ISingleEditOperation[] {
const res: ISingleEditOperation[] = [];
if (!Range.isEmpty(r)) {
// Insert block comment start
res.push(EditOperation.insert(new Position(r.startLineNumber, r.startColumn), startToken + (insertSpace ? ' ' : '')));
// Insert block comment end
res.push(EditOperation.insert(new Position(r.endLineNumber, r.endColumn), (insertSpace ? ' ' : '') + endToken));
} else {
// Insert both continuously
res.push(EditOperation.replace(new Range(
r.startLineNumber, r.startColumn,
r.endLineNumber, r.endColumn
), startToken + ' ' + endToken));
}
return res;
}
public getEditOperations(model: ITextModel, builder: IEditOperationBuilder): void {
const startLineNumber = this._selection.startLineNumber;
const startColumn = this._selection.startColumn;
model.tokenization.tokenizeIfCheap(startLineNumber);
const languageId = model.getLanguageIdAtPosition(startLineNumber, startColumn);
const config = this.languageConfigurationService.getLanguageConfiguration(languageId).comments;
if (!config || !config.blockCommentStartToken || !config.blockCommentEndToken) {
// Mode does not support block comments
return;
}
if (this._selection.isEmpty()) {
const result = BlockCommentCommand.findEnclosingBlockCommentRange(model, this._selection.getPosition(), this.languageConfigurationService);
if (result) {
// Remove start token
const startTokenRange = new Range(result.startLineNumber, result.startColumn, result.startLineNumber, result.startColumn + config.blockCommentStartToken.length);
builder.addTrackedEditOperation(startTokenRange, '');
// Remove end token
const endTokenRange = new Range(result.endLineNumber, result.endColumn - config.blockCommentEndToken.length, result.endLineNumber, result.endColumn);
builder.addTrackedEditOperation(endTokenRange, '');
this._isRemove = true;
return;
}
// Else, no enclosing comment found, proceeding with normal logic
}
this._createOperationsForBlockComment(this._selection, config.blockCommentStartToken, config.blockCommentEndToken, this._insertSpace, model, builder);
}
public computeCursorState(model: ITextModel, helper: ICursorStateComputerData): Selection {
if (this._isRemove) {
const inverseEditOperations = helper.getInverseEditOperations();
const srcRange = inverseEditOperations[0].range;
return new Selection(srcRange.startLineNumber, srcRange.startColumn, srcRange.startLineNumber, srcRange.startColumn);
}
const inverseEditOperations = helper.getInverseEditOperations();
if (inverseEditOperations.length === 2) {
const startTokenEditOperation = inverseEditOperations[0];
const endTokenEditOperation = inverseEditOperations[1];
return new Selection(
startTokenEditOperation.range.endLineNumber,
startTokenEditOperation.range.endColumn,
endTokenEditOperation.range.startLineNumber,
endTokenEditOperation.range.startColumn
);
} else {
const srcRange = inverseEditOperations[0].range;
const deltaColumn = this._usedEndToken ? -this._usedEndToken.length - 1 : 0; // minus 1 space before endToken
return new Selection(
srcRange.endLineNumber,
srcRange.endColumn + deltaColumn,
srcRange.endLineNumber,
srcRange.endColumn + deltaColumn
);
}
}
}