diff --git a/src/vs/editor/common/languages/languageConfigurationRegistry.ts b/src/vs/editor/common/languages/languageConfigurationRegistry.ts index 0dffed073d4c2..09397ccdd98d4 100644 --- a/src/vs/editor/common/languages/languageConfigurationRegistry.ts +++ b/src/vs/editor/common/languages/languageConfigurationRegistry.ts @@ -457,10 +457,13 @@ export class ResolvedLanguageConfiguration { const comments: ICommentsConfiguration = {}; if (commentRule.lineComment) { + // Surrounding whitespace is not part of the comment marker; spacing on + // insertion is controlled by the `editor.comments.insertSpace` setting. + // Keeping it would break comment toggling, see #249958. if (typeof commentRule.lineComment === 'string') { - comments.lineCommentToken = commentRule.lineComment; + comments.lineCommentToken = commentRule.lineComment.trim(); } else { - comments.lineCommentToken = commentRule.lineComment.comment; + comments.lineCommentToken = commentRule.lineComment.comment.trim(); comments.lineCommentNoIndent = commentRule.lineComment.noIndent; } } diff --git a/src/vs/editor/contrib/comment/test/browser/lineCommentCommand.test.ts b/src/vs/editor/contrib/comment/test/browser/lineCommentCommand.test.ts index 8c4e0b6dce244..e733835de19da 100644 --- a/src/vs/editor/contrib/comment/test/browser/lineCommentCommand.test.ts +++ b/src/vs/editor/contrib/comment/test/browser/lineCommentCommand.test.ts @@ -101,6 +101,72 @@ suite('Editor Contrib - Line Comment Command', () => { ); }); + test('toggle line comment when the comment token has surrounding whitespace (#249958)', function () { + const testLineCommentCommand = createTestCommandHelper( + { lineComment: ' !@# ' }, + (accessor, sel) => new LineCommentCommand(accessor.get(ILanguageConfigurationService), sel, 4, Type.Toggle, true, true) + ); + + // Adds the trimmed comment token + testLineCommentCommand( + [ + 'some text' + ], + new Selection(1, 1, 1, 1), + [ + '!@# some text' + ], + new Selection(1, 5, 1, 5) + ); + + // Toggling again removes it instead of stacking another comment + testLineCommentCommand( + [ + '!@# some text' + ], + new Selection(1, 1, 1, 1), + [ + 'some text' + ], + new Selection(1, 1, 1, 1) + ); + }); + + test('toggle line comment when the object-form comment token has surrounding whitespace (#249958)', function () { + const testLineCommentCommand = createTestCommandHelper( + { lineComment: { comment: ' !@# ', noIndent: true } }, + (accessor, sel) => new LineCommentCommand(accessor.get(ILanguageConfigurationService), sel, 4, Type.Toggle, true, true) + ); + + // Adds the trimmed comment token at the first column (noIndent) + testLineCommentCommand( + [ + 'some text', + '\tsome more text' + ], + new Selection(2, 1, 2, 1), + [ + 'some text', + '!@# \tsome more text' + ], + new Selection(2, 5, 2, 5) + ); + + // Toggling again removes it instead of stacking another comment + testLineCommentCommand( + [ + 'some text', + '!@# \tsome more text' + ], + new Selection(2, 1, 2, 1), + [ + 'some text', + '\tsome more text' + ], + new Selection(2, 1, 2, 1) + ); + }); + function createSimpleModel(lines: string[]): ISimpleModel { return { getLineContent: (lineNumber: number) => {