提交 551f9188 编写于 作者: A Alvin Tang

Added new setting editor.comments.includeEmptyLines for feature request raised...

Added new setting editor.comments.includeEmptyLines for feature request raised in #88480 with a new suite of tests based off original.
上级 b980d895
......@@ -1034,6 +1034,11 @@ export interface IEditorCommentsOptions {
* Defaults to true.
*/
insertSpace?: boolean;
/**
* Includes commenting empty lines when inserting line comments.
* Defaults to false.
*/
includeEmptyLines?: boolean;
}
export type EditorCommentsOptions = Readonly<Required<IEditorCommentsOptions>>;
......@@ -1043,6 +1048,7 @@ class EditorComments extends BaseEditorOption<EditorOption.comments, EditorComme
constructor() {
const defaults: EditorCommentsOptions = {
insertSpace: true,
includeEmptyLines: false,
};
super(
EditorOption.comments, 'comments', defaults,
......@@ -1052,6 +1058,11 @@ class EditorComments extends BaseEditorOption<EditorOption.comments, EditorComme
default: defaults.insertSpace,
description: nls.localize('comments.insertSpace', "Controls whether a space character is inserted when commenting.")
},
'editor.comments.includeEmptyLines': {
type: 'boolean',
default: defaults.includeEmptyLines,
description: nls.localize('comments.includeEmptyLines', 'Controls if empty lines should be included with toggle, add or remove actions for line comments.')
},
}
);
}
......@@ -1063,6 +1074,7 @@ class EditorComments extends BaseEditorOption<EditorOption.comments, EditorComme
const input = _input as IEditorCommentsOptions;
return {
insertSpace: EditorBooleanOption.boolean(input.insertSpace, this.defaultValue.insertSpace),
includeEmptyLines: EditorBooleanOption.boolean(input.includeEmptyLines, this.defaultValue.includeEmptyLines),
};
}
}
......
......@@ -36,7 +36,13 @@ abstract class CommentLineAction extends EditorAction {
const commentsOptions = editor.getOption(EditorOption.comments);
for (const selection of selections) {
commands.push(new LineCommentCommand(selection, modelOptions.tabSize, this._type, commentsOptions.insertSpace));
commands.push(new LineCommentCommand(
selection,
modelOptions.tabSize,
this._type,
commentsOptions.insertSpace,
commentsOptions.includeEmptyLines
));
}
editor.pushUndoStop();
......
......@@ -53,11 +53,18 @@ export class LineCommentCommand implements ICommand {
private readonly _tabSize: number;
private readonly _type: Type;
private readonly _insertSpace: boolean;
private readonly _includeEmptyLines: boolean;
private _selectionId: string | null;
private _deltaColumn: number;
private _moveEndPositionDown: boolean;
constructor(selection: Selection, tabSize: number, type: Type, insertSpace: boolean) {
constructor(
selection: Selection,
tabSize: number,
type: Type,
insertSpace: boolean,
includeEmptyLines: boolean,
) {
this._selection = selection;
this._tabSize = tabSize;
this._type = type;
......@@ -65,6 +72,7 @@ export class LineCommentCommand implements ICommand {
this._selectionId = null;
this._deltaColumn = 0;
this._moveEndPositionDown = false;
this._includeEmptyLines = includeEmptyLines;
}
/**
......@@ -100,7 +108,7 @@ export class LineCommentCommand implements ICommand {
* Analyze lines and decide which lines are relevant and what the toggle should do.
* Also, build up several offsets and lengths useful in the generation of editor operations.
*/
public static _analyzeLines(type: Type, insertSpace: boolean, model: ISimpleModel, lines: ILinePreflightData[], startLineNumber: number): IPreflightData {
public static _analyzeLines(type: Type, insertSpace: boolean, model: ISimpleModel, lines: ILinePreflightData[], startLineNumber: number, includeEmptyLines: boolean): IPreflightData {
let onlyWhitespaceLines = true;
let shouldRemoveComments: boolean;
......@@ -122,11 +130,11 @@ export class LineCommentCommand implements ICommand {
if (lineContentStartOffset === -1) {
// Empty or whitespace only line
if (type === Type.Toggle) {
lineData.ignore = true;
lineData.ignore = !includeEmptyLines;
} else if (type === Type.ForceAdd) {
lineData.ignore = true;
lineData.ignore = !includeEmptyLines;
} else {
lineData.ignore = true;
lineData.ignore = !includeEmptyLines;
}
lineData.commentStrOffset = lineContent.length;
continue;
......@@ -176,7 +184,7 @@ export class LineCommentCommand implements ICommand {
/**
* Analyze all lines and decide exactly what to do => not supported | insert line comments | remove line comments
*/
public static _gatherPreflightData(type: Type, insertSpace: boolean, model: ITextModel, startLineNumber: number, endLineNumber: number): IPreflightData {
public static _gatherPreflightData(type: Type, insertSpace: boolean, model: ITextModel, startLineNumber: number, endLineNumber: number, includeEmptyLines: boolean): IPreflightData {
const lines = LineCommentCommand._gatherPreflightCommentStrings(model, startLineNumber, endLineNumber);
if (lines === null) {
return {
......@@ -184,7 +192,7 @@ export class LineCommentCommand implements ICommand {
};
}
return LineCommentCommand._analyzeLines(type, insertSpace, model, lines, startLineNumber);
return LineCommentCommand._analyzeLines(type, insertSpace, model, lines, startLineNumber, includeEmptyLines);
}
/**
......@@ -326,7 +334,15 @@ export class LineCommentCommand implements ICommand {
s = s.setEndPosition(s.endLineNumber - 1, model.getLineMaxColumn(s.endLineNumber - 1));
}
const data = LineCommentCommand._gatherPreflightData(this._type, this._insertSpace, model, s.startLineNumber, s.endLineNumber);
const data = LineCommentCommand._gatherPreflightData(
this._type,
this._insertSpace,
model,
s.startLineNumber,
s.endLineNumber,
this._includeEmptyLines
);
if (data.supported) {
return this._executeLineComments(model, builder, data, s);
}
......
......@@ -19,13 +19,13 @@ suite('Editor Contrib - Line Comment Command', () => {
function testLineCommentCommand(lines: string[], selection: Selection, expectedLines: string[], expectedSelection: Selection): void {
let mode = new CommentMode({ lineComment: '!@#', blockComment: ['<!@#', '#@!>'] });
testCommand(lines, mode.getLanguageIdentifier(), selection, (sel) => new LineCommentCommand(sel, 4, Type.Toggle, true), expectedLines, expectedSelection);
testCommand(lines, mode.getLanguageIdentifier(), selection, (sel) => new LineCommentCommand(sel, 4, Type.Toggle, true, false), expectedLines, expectedSelection);
mode.dispose();
}
function testAddLineCommentCommand(lines: string[], selection: Selection, expectedLines: string[], expectedSelection: Selection): void {
let mode = new CommentMode({ lineComment: '!@#', blockComment: ['<!@#', '#@!>'] });
testCommand(lines, mode.getLanguageIdentifier(), selection, (sel) => new LineCommentCommand(sel, 4, Type.ForceAdd, true), expectedLines, expectedSelection);
testCommand(lines, mode.getLanguageIdentifier(), selection, (sel) => new LineCommentCommand(sel, 4, Type.ForceAdd, true, false), expectedLines, expectedSelection);
mode.dispose();
}
......@@ -47,7 +47,7 @@ suite('Editor Contrib - Line Comment Command', () => {
test('case insensitive', function () {
function testLineCommentCommand(lines: string[], selection: Selection, expectedLines: string[], expectedSelection: Selection): void {
let mode = new CommentMode({ lineComment: 'rem' });
testCommand(lines, mode.getLanguageIdentifier(), selection, (sel) => new LineCommentCommand(sel, 4, Type.Toggle, true), expectedLines, expectedSelection);
testCommand(lines, mode.getLanguageIdentifier(), selection, (sel) => new LineCommentCommand(sel, 4, Type.Toggle, true, false), expectedLines, expectedSelection);
mode.dispose();
}
......@@ -91,7 +91,7 @@ suite('Editor Contrib - Line Comment Command', () => {
' ',
' c',
'\t\td'
]), createBasicLinePreflightData(['//', 'rem', '!@#', '!@#']), 1);
]), createBasicLinePreflightData(['//', 'rem', '!@#', '!@#']), 1, false);
if (!r.supported) {
throw new Error(`unexpected`);
}
......@@ -122,7 +122,7 @@ suite('Editor Contrib - Line Comment Command', () => {
' rem ',
' !@# c',
'\t\t!@#d'
]), createBasicLinePreflightData(['//', 'rem', '!@#', '!@#']), 1);
]), createBasicLinePreflightData(['//', 'rem', '!@#', '!@#']), 1, false);
if (!r.supported) {
throw new Error(`unexpected`);
}
......@@ -631,7 +631,7 @@ suite('Editor Contrib - Line Comment Command', () => {
test('insertSpace false', () => {
function testLineCommentCommand(lines: string[], selection: Selection, expectedLines: string[], expectedSelection: Selection): void {
let mode = new CommentMode({ lineComment: '!@#' });
testCommand(lines, mode.getLanguageIdentifier(), selection, (sel) => new LineCommentCommand(sel, 4, Type.Toggle, false), expectedLines, expectedSelection);
testCommand(lines, mode.getLanguageIdentifier(), selection, (sel) => new LineCommentCommand(sel, 4, Type.Toggle, false, false), expectedLines, expectedSelection);
mode.dispose();
}
......@@ -650,7 +650,7 @@ suite('Editor Contrib - Line Comment Command', () => {
test('insertSpace false does not remove space', () => {
function testLineCommentCommand(lines: string[], selection: Selection, expectedLines: string[], expectedSelection: Selection): void {
let mode = new CommentMode({ lineComment: '!@#' });
testCommand(lines, mode.getLanguageIdentifier(), selection, (sel) => new LineCommentCommand(sel, 4, Type.Toggle, false), expectedLines, expectedSelection);
testCommand(lines, mode.getLanguageIdentifier(), selection, (sel) => new LineCommentCommand(sel, 4, Type.Toggle, false, false), expectedLines, expectedSelection);
mode.dispose();
}
......@@ -665,13 +665,104 @@ suite('Editor Contrib - Line Comment Command', () => {
new Selection(1, 1, 1, 1)
);
});
suite('includeEmptyLines true', () => {
function testLineCommentCommand(lines: string[], selection: Selection, expectedLines: string[], expectedSelection: Selection): void {
let mode = new CommentMode({ lineComment: '!@#', blockComment: ['<!@#', '#@!>'] });
testCommand(lines, mode.getLanguageIdentifier(), selection, (sel) => new LineCommentCommand(sel, 4, Type.Toggle, true, true), expectedLines, expectedSelection);
mode.dispose();
}
test('does not ignore whitespace lines', () => {
testLineCommentCommand(
[
'\tsome text',
'\t ',
'',
'\tsome more text'
],
new Selection(4, 2, 1, 1),
[
'!@# \tsome text',
'!@# \t ',
'!@# ',
'!@# \tsome more text'
],
new Selection(4, 6, 1, 5)
);
});
test('removes its own', function () {
testLineCommentCommand(
[
'\t!@# some text',
'\t ',
'\t\t!@# some more text'
],
new Selection(3, 2, 1, 1),
[
'\tsome text',
'\t ',
'\t\tsome more text'
],
new Selection(3, 2, 1, 1)
);
});
test('works in only whitespace', function () {
testLineCommentCommand(
[
'\t ',
'\t',
'\t\tsome more text'
],
new Selection(3, 1, 1, 1),
[
'\t!@# ',
'\t!@# ',
'\t\tsome more text'
],
new Selection(3, 1, 1, 1)
);
});
test('comments single line', function () {
testLineCommentCommand(
[
'some text',
'\tsome more text'
],
new Selection(1, 1, 1, 1),
[
'!@# some text',
'\tsome more text'
],
new Selection(1, 5, 1, 5)
);
});
test('detects indentation', function () {
testLineCommentCommand(
[
'\tsome text',
'\tsome more text'
],
new Selection(2, 2, 1, 1),
[
'\t!@# some text',
'\t!@# some more text'
],
new Selection(2, 2, 1, 1)
);
});
});
});
suite('Editor Contrib - Line Comment As Block Comment', () => {
function testLineCommentCommand(lines: string[], selection: Selection, expectedLines: string[], expectedSelection: Selection): void {
let mode = new CommentMode({ lineComment: '', blockComment: ['(', ')'] });
testCommand(lines, mode.getLanguageIdentifier(), selection, (sel) => new LineCommentCommand(sel, 4, Type.Toggle, true), expectedLines, expectedSelection);
testCommand(lines, mode.getLanguageIdentifier(), selection, (sel) => new LineCommentCommand(sel, 4, Type.Toggle, true, false), expectedLines, expectedSelection);
mode.dispose();
}
......@@ -782,7 +873,7 @@ suite('Editor Contrib - Line Comment As Block Comment', () => {
suite('Editor Contrib - Line Comment As Block Comment 2', () => {
function testLineCommentCommand(lines: string[], selection: Selection, expectedLines: string[], expectedSelection: Selection): void {
let mode = new CommentMode({ lineComment: null, blockComment: ['<!@#', '#@!>'] });
testCommand(lines, mode.getLanguageIdentifier(), selection, (sel) => new LineCommentCommand(sel, 4, Type.Toggle, true), expectedLines, expectedSelection);
testCommand(lines, mode.getLanguageIdentifier(), selection, (sel) => new LineCommentCommand(sel, 4, Type.Toggle, true, false), expectedLines, expectedSelection);
mode.dispose();
}
......@@ -1023,7 +1114,7 @@ suite('Editor Contrib - Line Comment in mixed modes', () => {
lines,
outerMode.getLanguageIdentifier(),
selection,
(sel) => new LineCommentCommand(sel, 4, Type.Toggle, true),
(sel) => new LineCommentCommand(sel, 4, Type.Toggle, true, false),
expectedLines,
expectedSelection,
true
......
......@@ -3189,6 +3189,11 @@ declare namespace monaco.editor {
* Defaults to true.
*/
insertSpace?: boolean;
/**
* Includes commenting empty lines when inserting line comments.
* Defaults to false.
*/
includeEmptyLines?: boolean;
}
export type EditorCommentsOptions = Readonly<Required<IEditorCommentsOptions>>;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册