提交 bf6776b9 编写于 作者: R rebornix

fix #87730.

上级 293af207
......@@ -7,6 +7,7 @@ import * as nls from 'vs/nls';
import { KeyChord, KeyCode, KeyMod } from 'vs/base/common/keyCodes';
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
import { EditorAction, IActionOptions, ServicesAccessor, registerEditorAction } from 'vs/editor/browser/editorExtensions';
import { Range } from 'vs/editor/common/core/range';
import { ICommand } from 'vs/editor/common/editorCommon';
import { EditorContextKeys } from 'vs/editor/common/editorContextKeys';
import { BlockCommentCommand } from 'vs/editor/contrib/comment/blockCommentCommand';
......@@ -31,17 +32,38 @@ abstract class CommentLineAction extends EditorAction {
const model = editor.getModel();
const commands: ICommand[] = [];
const selections = editor.getSelections();
const modelOptions = model.getOptions();
const commentsOptions = editor.getOption(EditorOption.comments);
const selections = editor.getSelections().map((selection, index) => ({ selection, index, ignoreFirstLine: false }));
selections.sort((a, b) => Range.compareRangesUsingStarts(a.selection, b.selection));
// Remove selections that would result in copying the same line
let prev = selections[0];
for (let i = 1; i < selections.length; i++) {
const curr = selections[i];
if (prev.selection.endLineNumber === curr.selection.startLineNumber) {
// these two selections would copy the same line
if (prev.index < curr.index) {
// prev wins
curr.ignoreFirstLine = true;
} else {
// curr wins
prev.ignoreFirstLine = true;
prev = curr;
}
}
}
for (const selection of selections) {
commands.push(new LineCommentCommand(
selection,
selection.selection,
modelOptions.tabSize,
this._type,
commentsOptions.insertSpace,
commentsOptions.ignoreEmptyLines
commentsOptions.ignoreEmptyLines,
selection.ignoreFirstLine
));
}
......
......@@ -57,13 +57,15 @@ export class LineCommentCommand implements ICommand {
private _selectionId: string | null;
private _deltaColumn: number;
private _moveEndPositionDown: boolean;
private _ignoreFirstLine: boolean;
constructor(
selection: Selection,
tabSize: number,
type: Type,
insertSpace: boolean,
ignoreEmptyLines: boolean
ignoreEmptyLines: boolean,
ignoreFirstLine?: boolean
) {
this._selection = selection;
this._tabSize = tabSize;
......@@ -73,6 +75,7 @@ export class LineCommentCommand implements ICommand {
this._deltaColumn = 0;
this._moveEndPositionDown = false;
this._ignoreEmptyLines = ignoreEmptyLines;
this._ignoreFirstLine = ignoreFirstLine || false;
}
/**
......@@ -108,7 +111,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, ignoreEmptyLines: boolean): IPreflightData {
public static _analyzeLines(type: Type, insertSpace: boolean, model: ISimpleModel, lines: ILinePreflightData[], startLineNumber: number, ignoreEmptyLines: boolean, ignoreFirstLine: boolean): IPreflightData {
let onlyWhitespaceLines = true;
let shouldRemoveComments: boolean;
......@@ -124,6 +127,12 @@ export class LineCommentCommand implements ICommand {
const lineData = lines[i];
const lineNumber = startLineNumber + i;
if (lineNumber === startLineNumber && ignoreFirstLine) {
// first line ignored
lineData.ignore = true;
continue;
}
const lineContent = model.getLineContent(lineNumber);
const lineContentStartOffset = strings.firstNonWhitespaceIndex(lineContent);
......@@ -178,7 +187,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, ignoreEmptyLines: boolean): IPreflightData {
public static _gatherPreflightData(type: Type, insertSpace: boolean, model: ITextModel, startLineNumber: number, endLineNumber: number, ignoreEmptyLines: boolean, ignoreFirstLine: boolean): IPreflightData {
const lines = LineCommentCommand._gatherPreflightCommentStrings(model, startLineNumber, endLineNumber);
if (lines === null) {
return {
......@@ -186,7 +195,7 @@ export class LineCommentCommand implements ICommand {
};
}
return LineCommentCommand._analyzeLines(type, insertSpace, model, lines, startLineNumber, ignoreEmptyLines);
return LineCommentCommand._analyzeLines(type, insertSpace, model, lines, startLineNumber, ignoreEmptyLines, ignoreFirstLine);
}
/**
......@@ -323,6 +332,12 @@ export class LineCommentCommand implements ICommand {
let s = this._selection;
this._moveEndPositionDown = false;
if (s.startLineNumber === s.endLineNumber && this._ignoreFirstLine) {
builder.addEditOperation(new Range(s.startLineNumber, model.getLineMaxColumn(s.startLineNumber), s.startLineNumber + 1, 1), s.startLineNumber === model.getLineCount() ? '' : '\n');
this._selectionId = builder.trackSelection(s);
return;
}
if (s.startLineNumber < s.endLineNumber && s.endColumn === 1) {
this._moveEndPositionDown = true;
s = s.setEndPosition(s.endLineNumber - 1, model.getLineMaxColumn(s.endLineNumber - 1));
......@@ -334,7 +349,8 @@ export class LineCommentCommand implements ICommand {
model,
s.startLineNumber,
s.endLineNumber,
this._ignoreEmptyLines
this._ignoreEmptyLines,
this._ignoreFirstLine
);
if (data.supported) {
......
......@@ -91,7 +91,7 @@ suite('Editor Contrib - Line Comment Command', () => {
' ',
' c',
'\t\td'
]), createBasicLinePreflightData(['//', 'rem', '!@#', '!@#']), 1, true);
]), createBasicLinePreflightData(['//', 'rem', '!@#', '!@#']), 1, true, 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, true);
]), createBasicLinePreflightData(['//', 'rem', '!@#', '!@#']), 1, true, false);
if (!r.supported) {
throw new Error(`unexpected`);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册