Skip to content

Commit 7e5db3f

Browse files
committed
Fixes microsoft#35673: Comment hotkeys throws the cursor before the comment
1 parent 6e7a575 commit 7e5db3f

2 files changed

Lines changed: 52 additions & 0 deletions

File tree

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,16 @@ export class LineCommentCommand implements editorCommon.ICommand {
200200
ops = LineCommentCommand._createAddLineCommentsOperations(data.lines, s.startLineNumber);
201201
}
202202

203+
const cursorPosition = new Position(s.positionLineNumber, s.positionColumn);
204+
203205
for (var i = 0, len = ops.length; i < len; i++) {
204206
builder.addEditOperation(ops[i].range, ops[i].text);
207+
if (ops[i].range.isEmpty() && ops[i].range.getStartPosition().equals(cursorPosition)) {
208+
const lineContent = model.getLineContent(cursorPosition.lineNumber);
209+
if (lineContent.length + 1 === cursorPosition.column) {
210+
this._deltaColumn = ops[i].text.length;
211+
}
212+
}
205213
}
206214

207215
this._selectionId = builder.trackSelection(s);

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,50 @@ suite('Editor Contrib - Line Comment Command', () => {
518518
);
519519
});
520520

521+
test('issue #35673: Comment hotkeys throws the cursor before the comment', () => {
522+
testLineCommentCommand(
523+
[
524+
'first',
525+
'',
526+
'\tsecond line',
527+
'third line',
528+
'fourth line',
529+
'fifth'
530+
],
531+
new Selection(2, 1, 2, 1),
532+
[
533+
'first',
534+
'!@# ',
535+
'\tsecond line',
536+
'third line',
537+
'fourth line',
538+
'fifth'
539+
],
540+
new Selection(2, 5, 2, 5)
541+
);
542+
543+
testLineCommentCommand(
544+
[
545+
'first',
546+
'\t',
547+
'\tsecond line',
548+
'third line',
549+
'fourth line',
550+
'fifth'
551+
],
552+
new Selection(2, 2, 2, 2),
553+
[
554+
'first',
555+
'\t!@# ',
556+
'\tsecond line',
557+
'third line',
558+
'fourth line',
559+
'fifth'
560+
],
561+
new Selection(2, 6, 2, 6)
562+
);
563+
});
564+
521565
test('issue #2837 "Add Line Comment" fault when blank lines involved', function () {
522566
testAddLineCommentCommand(
523567
[

0 commit comments

Comments
 (0)