diff --git a/src/vs/editor/contrib/comment/common/lineCommentCommand.ts b/src/vs/editor/contrib/comment/common/lineCommentCommand.ts index 1352bec2320..8f0abccfd32 100644 --- a/src/vs/editor/contrib/comment/common/lineCommentCommand.ts +++ b/src/vs/editor/contrib/comment/common/lineCommentCommand.ts @@ -200,8 +200,16 @@ export class LineCommentCommand implements editorCommon.ICommand { ops = LineCommentCommand._createAddLineCommentsOperations(data.lines, s.startLineNumber); } + const cursorPosition = new Position(s.positionLineNumber, s.positionColumn); + for (var i = 0, len = ops.length; i < len; i++) { builder.addEditOperation(ops[i].range, ops[i].text); + if (ops[i].range.isEmpty() && ops[i].range.getStartPosition().equals(cursorPosition)) { + const lineContent = model.getLineContent(cursorPosition.lineNumber); + if (lineContent.length + 1 === cursorPosition.column) { + this._deltaColumn = ops[i].text.length; + } + } } this._selectionId = builder.trackSelection(s); diff --git a/src/vs/editor/contrib/comment/test/common/lineCommentCommand.test.ts b/src/vs/editor/contrib/comment/test/common/lineCommentCommand.test.ts index 443415d8820..57c946f76f9 100644 --- a/src/vs/editor/contrib/comment/test/common/lineCommentCommand.test.ts +++ b/src/vs/editor/contrib/comment/test/common/lineCommentCommand.test.ts @@ -518,6 +518,50 @@ suite('Editor Contrib - Line Comment Command', () => { ); }); + test('issue #35673: Comment hotkeys throws the cursor before the comment', () => { + testLineCommentCommand( + [ + 'first', + '', + '\tsecond line', + 'third line', + 'fourth line', + 'fifth' + ], + new Selection(2, 1, 2, 1), + [ + 'first', + '!@# ', + '\tsecond line', + 'third line', + 'fourth line', + 'fifth' + ], + new Selection(2, 5, 2, 5) + ); + + testLineCommentCommand( + [ + 'first', + '\t', + '\tsecond line', + 'third line', + 'fourth line', + 'fifth' + ], + new Selection(2, 2, 2, 2), + [ + 'first', + '\t!@# ', + '\tsecond line', + 'third line', + 'fourth line', + 'fifth' + ], + new Selection(2, 6, 2, 6) + ); + }); + test('issue #2837 "Add Line Comment" fault when blank lines involved', function () { testAddLineCommentCommand( [