Skip to content

Commit 668c277

Browse files
committed
Honor insertSpace option also in the block comment command
1 parent 9aec9a3 commit 668c277

4 files changed

Lines changed: 70 additions & 24 deletions

File tree

src/vs/editor/contrib/comment/blockCommentCommand.ts

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ import { LanguageConfigurationRegistry } from 'vs/editor/common/modes/languageCo
1515
export class BlockCommentCommand implements ICommand {
1616

1717
private readonly _selection: Selection;
18+
private readonly _insertSpace: boolean;
1819
private _usedEndToken: string | null;
1920

20-
constructor(selection: Selection) {
21+
constructor(selection: Selection, insertSpace: boolean) {
2122
this._selection = selection;
23+
this._insertSpace = insertSpace;
2224
this._usedEndToken = null;
2325
}
2426

@@ -53,7 +55,7 @@ export class BlockCommentCommand implements ICommand {
5355
return true;
5456
}
5557

56-
private _createOperationsForBlockComment(selection: Range, startToken: string, endToken: string, model: ITextModel, builder: IEditOperationBuilder): void {
58+
private _createOperationsForBlockComment(selection: Range, startToken: string, endToken: string, insertSpace: boolean, model: ITextModel, builder: IEditOperationBuilder): void {
5759
const startLineNumber = selection.startLineNumber;
5860
const startColumn = selection.startColumn;
5961
const endLineNumber = selection.endLineNumber;
@@ -91,25 +93,21 @@ export class BlockCommentCommand implements ICommand {
9193

9294
if (startTokenIndex !== -1 && endTokenIndex !== -1) {
9395
// Consider spaces as part of the comment tokens
94-
if (startTokenIndex + startToken.length < startLineText.length) {
95-
if (startLineText.charCodeAt(startTokenIndex + startToken.length) === CharCode.Space) {
96-
// Pretend the start token contains a trailing space
97-
startToken = startToken + ' ';
98-
}
96+
if (insertSpace && startTokenIndex + startToken.length < startLineText.length && startLineText.charCodeAt(startTokenIndex + startToken.length) === CharCode.Space) {
97+
// Pretend the start token contains a trailing space
98+
startToken = startToken + ' ';
9999
}
100100

101-
if (endTokenIndex > 0) {
102-
if (endLineText.charCodeAt(endTokenIndex - 1) === CharCode.Space) {
103-
// Pretend the end token contains a leading space
104-
endToken = ' ' + endToken;
105-
endTokenIndex -= 1;
106-
}
101+
if (insertSpace && endTokenIndex > 0 && endLineText.charCodeAt(endTokenIndex - 1) === CharCode.Space) {
102+
// Pretend the end token contains a leading space
103+
endToken = ' ' + endToken;
104+
endTokenIndex -= 1;
107105
}
108106
ops = BlockCommentCommand._createRemoveBlockCommentOperations(
109107
new Range(startLineNumber, startTokenIndex + startToken.length + 1, endLineNumber, endTokenIndex + 1), startToken, endToken
110108
);
111109
} else {
112-
ops = BlockCommentCommand._createAddBlockCommentOperations(selection, startToken, endToken);
110+
ops = BlockCommentCommand._createAddBlockCommentOperations(selection, startToken, endToken, this._insertSpace);
113111
this._usedEndToken = ops.length === 1 ? endToken : null;
114112
}
115113

@@ -144,15 +142,15 @@ export class BlockCommentCommand implements ICommand {
144142
return res;
145143
}
146144

147-
public static _createAddBlockCommentOperations(r: Range, startToken: string, endToken: string): IIdentifiedSingleEditOperation[] {
145+
public static _createAddBlockCommentOperations(r: Range, startToken: string, endToken: string, insertSpace: boolean): IIdentifiedSingleEditOperation[] {
148146
let res: IIdentifiedSingleEditOperation[] = [];
149147

150148
if (!Range.isEmpty(r)) {
151149
// Insert block comment start
152-
res.push(EditOperation.insert(new Position(r.startLineNumber, r.startColumn), startToken + ' '));
150+
res.push(EditOperation.insert(new Position(r.startLineNumber, r.startColumn), startToken + (insertSpace ? ' ' : '')));
153151

154152
// Insert block comment end
155-
res.push(EditOperation.insert(new Position(r.endLineNumber, r.endColumn), ' ' + endToken));
153+
res.push(EditOperation.insert(new Position(r.endLineNumber, r.endColumn), (insertSpace ? ' ' : '') + endToken));
156154
} else {
157155
// Insert both continuously
158156
res.push(EditOperation.replace(new Range(
@@ -176,7 +174,7 @@ export class BlockCommentCommand implements ICommand {
176174
return;
177175
}
178176

179-
this._createOperationsForBlockComment(this._selection, config.blockCommentStartToken, config.blockCommentEndToken, model, builder);
177+
this._createOperationsForBlockComment(this._selection, config.blockCommentStartToken, config.blockCommentEndToken, this._insertSpace, model, builder);
180178
}
181179

182180
public computeCursorState(model: ITextModel, helper: ICursorStateComputerData): Selection {

src/vs/editor/contrib/comment/comment.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,11 @@ class BlockCommentAction extends EditorAction {
128128
return;
129129
}
130130

131-
let commands: ICommand[] = [];
132-
let selections = editor.getSelections();
131+
const commentsOptions = editor.getOption(EditorOption.comments);
132+
const commands: ICommand[] = [];
133+
const selections = editor.getSelections();
133134
for (const selection of selections) {
134-
commands.push(new BlockCommentCommand(selection));
135+
commands.push(new BlockCommentCommand(selection, commentsOptions.insertSpace));
135136
}
136137

137138
editor.pushUndoStop();

src/vs/editor/contrib/comment/lineCommentCommand.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,11 +291,17 @@ export class LineCommentCommand implements ICommand {
291291
firstNonWhitespaceIndex = lineContent.length;
292292
}
293293
ops = BlockCommentCommand._createAddBlockCommentOperations(
294-
new Range(s.startLineNumber, firstNonWhitespaceIndex + 1, s.startLineNumber, lineContent.length + 1), startToken, endToken
294+
new Range(s.startLineNumber, firstNonWhitespaceIndex + 1, s.startLineNumber, lineContent.length + 1),
295+
startToken,
296+
endToken,
297+
this._insertSpace
295298
);
296299
} else {
297300
ops = BlockCommentCommand._createAddBlockCommentOperations(
298-
new Range(s.startLineNumber, model.getLineFirstNonWhitespaceColumn(s.startLineNumber), s.endLineNumber, model.getLineMaxColumn(s.endLineNumber)), startToken, endToken
301+
new Range(s.startLineNumber, model.getLineFirstNonWhitespaceColumn(s.startLineNumber), s.endLineNumber, model.getLineMaxColumn(s.endLineNumber)),
302+
startToken,
303+
endToken,
304+
this._insertSpace
299305
);
300306
}
301307

src/vs/editor/contrib/comment/test/blockCommentCommand.test.ts

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { CommentMode } from 'vs/editor/test/common/commentMode';
99

1010
function testBlockCommentCommand(lines: string[], selection: Selection, expectedLines: string[], expectedSelection: Selection): void {
1111
let mode = new CommentMode({ lineComment: '!@#', blockComment: ['<0', '0>'] });
12-
testCommand(lines, mode.getLanguageIdentifier(), selection, (sel) => new BlockCommentCommand(sel), expectedLines, expectedSelection);
12+
testCommand(lines, mode.getLanguageIdentifier(), selection, (sel) => new BlockCommentCommand(sel, true), expectedLines, expectedSelection);
1313
mode.dispose();
1414
}
1515

@@ -468,4 +468,45 @@ suite('Editor Contrib - Block Comment Command', () => {
468468
new Selection(1, 1, 1, 1)
469469
);
470470
});
471+
472+
test('', () => {
473+
});
474+
475+
test('insertSpace false', () => {
476+
function testLineCommentCommand(lines: string[], selection: Selection, expectedLines: string[], expectedSelection: Selection): void {
477+
let mode = new CommentMode({ lineComment: '!@#', blockComment: ['<0', '0>'] });
478+
testCommand(lines, mode.getLanguageIdentifier(), selection, (sel) => new BlockCommentCommand(sel, false), expectedLines, expectedSelection);
479+
mode.dispose();
480+
}
481+
482+
testLineCommentCommand(
483+
[
484+
'some text'
485+
],
486+
new Selection(1, 1, 1, 5),
487+
[
488+
'<0some0> text'
489+
],
490+
new Selection(1, 3, 1, 7)
491+
);
492+
});
493+
494+
test('insertSpace false does not remove space', () => {
495+
function testLineCommentCommand(lines: string[], selection: Selection, expectedLines: string[], expectedSelection: Selection): void {
496+
let mode = new CommentMode({ lineComment: '!@#', blockComment: ['<0', '0>'] });
497+
testCommand(lines, mode.getLanguageIdentifier(), selection, (sel) => new BlockCommentCommand(sel, false), expectedLines, expectedSelection);
498+
mode.dispose();
499+
}
500+
501+
testLineCommentCommand(
502+
[
503+
'<0 some 0> text'
504+
],
505+
new Selection(1, 4, 1, 8),
506+
[
507+
' some text'
508+
],
509+
new Selection(1, 1, 1, 7)
510+
);
511+
});
471512
});

0 commit comments

Comments
 (0)