提交 026df274 编写于 作者: P Peng Lyu

lines operations strict null check.

#60565.
上级 914c6d96
......@@ -337,6 +337,7 @@
"./vs/editor/contrib/linesOperations/deleteLinesCommand.ts",
"./vs/editor/contrib/linesOperations/moveLinesCommand.ts",
"./vs/editor/contrib/linesOperations/sortLinesCommand.ts",
"./vs/editor/contrib/linesOperations/linesOperations.ts",
"./vs/editor/contrib/links/getLinks.ts",
"./vs/editor/contrib/links/links.ts",
"./vs/editor/contrib/markdown/markdownRenderer.ts",
......
......@@ -21,7 +21,11 @@ import { IElectricAction } from 'vs/editor/common/modes/supports/electricCharact
export class TypeOperations {
public static indent(config: CursorConfiguration, model: ICursorSimpleModel, selections: Selection[]): ICommand[] {
public static indent(config: CursorConfiguration, model: ICursorSimpleModel | null, selections: Selection[] | null): ICommand[] {
if (model === null || selections === null) {
return [];
}
let commands: ICommand[] = [];
for (let i = 0, len = selections.length; i < len; i++) {
commands[i] = new ShiftCommand(selections[i], {
......@@ -885,7 +889,11 @@ export class TypeOperations {
});
}
public static lineInsertBefore(config: CursorConfiguration, model: ITextModel, selections: Selection[]): ICommand[] {
public static lineInsertBefore(config: CursorConfiguration, model: ITextModel | null, selections: Selection[] | null): ICommand[] {
if (model === null || selections === null) {
return [];
}
let commands: ICommand[] = [];
for (let i = 0, len = selections.length; i < len; i++) {
let lineNumber = selections[i].positionLineNumber;
......@@ -902,7 +910,11 @@ export class TypeOperations {
return commands;
}
public static lineInsertAfter(config: CursorConfiguration, model: ITextModel, selections: Selection[]): ICommand[] {
public static lineInsertAfter(config: CursorConfiguration, model: ITextModel | null, selections: Selection[] | null): ICommand[] {
if (model === null || selections === null) {
return [];
}
let commands: ICommand[] = [];
for (let i = 0, len = selections.length; i < len; i++) {
const lineNumber = selections[i].positionLineNumber;
......
......@@ -249,7 +249,12 @@ export class TrimTrailingWhitespaceAction extends EditorAction {
cursors = (editor.getSelections() || []).map(s => new Position(s.positionLineNumber, s.positionColumn));
}
let command = new TrimTrailingWhitespaceCommand(editor.getSelection(), cursors);
let selection = editor.getSelection();
if (selection === null) {
return;
}
let command = new TrimTrailingWhitespaceCommand(selection, cursors);
editor.pushUndoStop();
editor.executeCommands(this.id, [command]);
......@@ -297,7 +302,11 @@ class DeleteLinesAction extends EditorAction {
private _getLinesToRemove(editor: ICodeEditor): IDeleteLinesOperation[] {
// Construct delete operations
let operations: IDeleteLinesOperation[] = editor.getSelections().map((s) => {
let selections = editor.getSelections();
if (selections === null) {
return [];
}
let operations: IDeleteLinesOperation[] = selections.map((s) => {
let endLineNumber = s.endLineNumber;
if (s.startLineNumber < s.endLineNumber && s.endColumn === 1) {
......@@ -378,7 +387,7 @@ class OutdentLinesAction extends EditorAction {
}
public run(_accessor: ServicesAccessor, editor: ICodeEditor): void {
CoreEditingCommands.Outdent.runEditorCommand(null, editor, null);
CoreEditingCommands.Outdent.runEditorCommand(_accessor, editor, null);
}
}
......@@ -435,6 +444,10 @@ export class InsertLineAfterAction extends EditorAction {
export abstract class AbstractDeleteAllToBoundaryAction extends EditorAction {
public run(_accessor: ServicesAccessor, editor: ICodeEditor): void {
const primaryCursor = editor.getSelection();
if (primaryCursor === null) {
return;
}
let rangesToDelete = this._getRangesToDelete(editor);
// merge overlapping selections
let effectiveRanges: Range[] = [];
......@@ -488,7 +501,7 @@ export class DeleteAllLeftAction extends AbstractDeleteAllToBoundaryAction {
}
_getEndCursorState(primaryCursor: Range, rangesToDelete: Range[]): Selection[] {
let endPrimaryCursor: Selection;
let endPrimaryCursor: Selection | null = null;
let endCursorState: Selection[] = [];
let deletedLines = 0;
......@@ -518,15 +531,24 @@ export class DeleteAllLeftAction extends AbstractDeleteAllToBoundaryAction {
}
_getRangesToDelete(editor: ICodeEditor): Range[] {
let rangesToDelete: Range[] = editor.getSelections();
let selections = editor.getSelections();
if (selections === null) {
return [];
}
let rangesToDelete: Range[] = selections;
let model = editor.getModel();
if (model === null) {
return [];
}
rangesToDelete.sort(Range.compareRangesUsingStarts);
rangesToDelete = rangesToDelete.map(selection => {
if (selection.isEmpty()) {
if (selection.startColumn === 1) {
let deleteFromLine = Math.max(1, selection.startLineNumber - 1);
let deleteFromColumn = selection.startLineNumber === 1 ? 1 : model.getLineContent(deleteFromLine).length + 1;
let deleteFromColumn = selection.startLineNumber === 1 ? 1 : model!.getLineContent(deleteFromLine).length + 1;
return new Range(deleteFromLine, deleteFromColumn, selection.startLineNumber, 1);
} else {
return new Range(selection.startLineNumber, 1, selection.startLineNumber, selection.startColumn);
......@@ -557,7 +579,7 @@ export class DeleteAllRightAction extends AbstractDeleteAllToBoundaryAction {
}
_getEndCursorState(primaryCursor: Range, rangesToDelete: Range[]): Selection[] {
let endPrimaryCursor: Selection;
let endPrimaryCursor: Selection | null = null;
let endCursorState: Selection[] = [];
for (let i = 0, len = rangesToDelete.length, offset = 0; i < len; i++) {
let range = rangesToDelete[i];
......@@ -579,10 +601,19 @@ export class DeleteAllRightAction extends AbstractDeleteAllToBoundaryAction {
_getRangesToDelete(editor: ICodeEditor): Range[] {
let model = editor.getModel();
if (model === null) {
return [];
}
let rangesToDelete: Range[] = editor.getSelections().map((sel) => {
let selections = editor.getSelections();
if (selections === null) {
return [];
}
let rangesToDelete: Range[] = selections.map((sel) => {
if (sel.isEmpty()) {
const maxColumn = model.getLineMaxColumn(sel.startLineNumber);
const maxColumn = model!.getLineMaxColumn(sel.startLineNumber);
if (sel.startColumn === maxColumn) {
return new Range(sel.startLineNumber, sel.startColumn, sel.startLineNumber + 1, 1);
......@@ -616,7 +647,14 @@ export class JoinLinesAction extends EditorAction {
public run(_accessor: ServicesAccessor, editor: ICodeEditor): void {
let selections = editor.getSelections();
if (selections === null) {
return;
}
let primaryCursor = editor.getSelection();
if (primaryCursor === null) {
return;
}
selections.sort(Range.compareRangesUsingStarts);
let reducedSelections: Selection[] = [];
......@@ -624,7 +662,7 @@ export class JoinLinesAction extends EditorAction {
let lastSelection = selections.reduce((previousValue, currentValue) => {
if (previousValue.isEmpty()) {
if (previousValue.endLineNumber === currentValue.startLineNumber) {
if (primaryCursor.equalsSelection(previousValue)) {
if (primaryCursor!.equalsSelection(previousValue)) {
primaryCursor = currentValue;
}
return currentValue;
......@@ -649,6 +687,10 @@ export class JoinLinesAction extends EditorAction {
reducedSelections.push(lastSelection);
let model = editor.getModel();
if (model === null) {
return;
}
let edits: IIdentifiedSingleEditOperation[] = [];
let endCursorState: Selection[] = [];
let endPrimaryCursor = primaryCursor;
......@@ -759,7 +801,15 @@ export class TransposeAction extends EditorAction {
public run(_accessor: ServicesAccessor, editor: ICodeEditor): void {
let selections = editor.getSelections();
if (selections === null) {
return;
}
let model = editor.getModel();
if (model === null) {
return;
}
let commands: ICommand[] = [];
for (let i = 0, len = selections.length; i < len; i++) {
......@@ -800,7 +850,15 @@ export class TransposeAction extends EditorAction {
export abstract class AbstractCaseAction extends EditorAction {
public run(_accessor: ServicesAccessor, editor: ICodeEditor): void {
let selections = editor.getSelections();
if (selections === null) {
return;
}
let model = editor.getModel();
if (model === null) {
return;
}
let commands: ICommand[] = [];
for (let i = 0, len = selections.length; i < len; i++) {
......
......@@ -33,7 +33,11 @@ export class SortLinesCommand implements editorCommon.ICommand {
return helper.getTrackedSelection(this.selectionId);
}
public static canRun(model: ITextModel, selection: Selection, descending: boolean): boolean {
public static canRun(model: ITextModel | null, selection: Selection, descending: boolean): boolean {
if (model === null) {
return false;
}
let data = getSortData(model, selection, descending);
if (!data) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册