提交 77b19e50 编写于 作者: S Sandeep Somavarapu

Implement line up and down for cursor move #9143

上级 e83b5b41
......@@ -1128,7 +1128,7 @@ export class Cursor extends EventEmitter {
}
private _cursorMove(ctx: IMultipleCursorOperationContext): boolean {
return this._invokeForAll(ctx, (cursorIndex: number, oneCursor: OneCursor, oneCtx: IOneCursorOperationContext) => OneCursorOp.move(oneCursor, !!ctx.eventData.inSelectionMode, ctx.eventData.to, ctx.eventSource, oneCtx));
return this._invokeForAll(ctx, (cursorIndex: number, oneCursor: OneCursor, oneCtx: IOneCursorOperationContext) => OneCursorOp.move(oneCursor, ctx.eventData, ctx.eventSource, oneCtx));
}
private _columnSelectToLineNumber: number = 0;
......@@ -1294,11 +1294,21 @@ export class Cursor extends EventEmitter {
}
private _moveDown(inSelectionMode:boolean, isPaged:boolean, ctx: IMultipleCursorOperationContext): boolean {
return this._invokeForAll(ctx, (cursorIndex: number, oneCursor: OneCursor, oneCtx: IOneCursorOperationContext) => OneCursorOp.moveDown(oneCursor, inSelectionMode, isPaged, ctx.eventData && ctx.eventData.pageSize || 0, oneCtx));
ctx.eventData= ctx.eventData || {};
ctx.eventData.to= editorCommon.CursorMoveViewPosition.LineDown;
ctx.eventData.inSelectionMode= inSelectionMode;
ctx.eventData.isPaged= isPaged;
return this._cursorMove(ctx);
}
private _moveUp(inSelectionMode:boolean, isPaged:boolean, ctx: IMultipleCursorOperationContext): boolean {
return this._invokeForAll(ctx, (cursorIndex: number, oneCursor: OneCursor, oneCtx: IOneCursorOperationContext) => OneCursorOp.moveUp(oneCursor, inSelectionMode, isPaged, ctx.eventData && ctx.eventData.pageSize || 0, oneCtx));
ctx.eventData= ctx.eventData || {};
ctx.eventData.to= editorCommon.CursorMoveViewPosition.LineUp;
ctx.eventData.inSelectionMode= inSelectionMode;
ctx.eventData.isPaged= isPaged;
return this._cursorMove(ctx);
}
private _moveToBeginningOfLine(inSelectionMode:boolean, ctx: IMultipleCursorOperationContext): boolean {
......
......@@ -6,7 +6,6 @@
import {onUnexpectedError, illegalArgument} from 'vs/base/common/errors';
import * as strings from 'vs/base/common/strings';
import * as types from 'vs/base/common/types';
import {ReplaceCommand, ReplaceCommandWithOffsetCursorState, ReplaceCommandWithoutChangingPosition} from 'vs/editor/common/commands/replaceCommand';
import {ShiftCommand} from 'vs/editor/common/commands/shiftCommand';
import {SurroundSelectionCommand} from 'vs/editor/common/commands/surroundSelectionCommand';
......@@ -631,19 +630,6 @@ export class OneCursorOp {
validatedViewPosition = cursor.convertModelPositionToViewPosition(validatedPosition.lineNumber, validatedPosition.column);
}
return this.move(cursor, inSelectionMode, validatedViewPosition, eventSource, ctx);
}
public static move(cursor: OneCursor, inSelectionMode: boolean, to: editorCommon.IPosition | string, eventSource: string, ctx: IOneCursorOperationContext): boolean {
if (!to) {
illegalArgument('to');
}
if (types.isString(to)) {
return this.moveToLogicalViewPosition(cursor, inSelectionMode, to, ctx);
}
let viewPosition: editorCommon.IPosition = <editorCommon.IPosition>to;
let reason = (eventSource === 'mouse' ? editorCommon.CursorChangeReason.Explicit : editorCommon.CursorChangeReason.NotSet);
if (eventSource === 'api') {
ctx.shouldRevealVerticalInCenter = true;
......@@ -651,15 +637,21 @@ export class OneCursorOp {
if (reason) {
ctx.cursorPositionChangeReason = reason;
}
cursor.moveViewPosition(inSelectionMode, viewPosition.lineNumber, viewPosition.column, 0, false);
cursor.moveViewPosition(inSelectionMode, validatedViewPosition.lineNumber, validatedViewPosition.column, 0, false);
return true;
}
private static moveToLogicalViewPosition(cursor: OneCursor, inSelectionMode: boolean, cursorMoveViewPosition: string, ctx: IOneCursorOperationContext): boolean {
public static move(cursor: OneCursor, moveParams: editorCommon.CursorMoveArguments, eventSource: string, ctx: IOneCursorOperationContext): boolean {
if (!moveParams.to) {
illegalArgument('to');
}
let inSelectionMode = !!moveParams.inSelectionMode;
let validatedViewPosition = cursor.getValidViewPosition();
let viewLineNumber = validatedViewPosition.lineNumber;
let noOfLines = moveParams.isPaged ? (moveParams.pageSize || cursor.getPageSize()) : moveParams.noOfLines;
let viewColumn;
switch (cursorMoveViewPosition) {
switch (moveParams.to) {
case editorCommon.CursorMoveViewPosition.LineStart:
viewColumn = cursor.getViewLineMinColumn(viewLineNumber);
break;
......@@ -675,6 +667,10 @@ export class OneCursorOp {
case editorCommon.CursorMoveViewPosition.LineLastNonWhitespaceCharacter:
viewColumn = cursor.getViewLineLastNonWhiteSpaceColumn(viewLineNumber);
break;
case editorCommon.CursorMoveViewPosition.LineUp:
return this.moveUp(cursor, inSelectionMode, noOfLines, ctx);
case editorCommon.CursorMoveViewPosition.LineDown:
return this.moveDown(cursor, inSelectionMode, noOfLines, ctx);
default:
return false;
}
......@@ -866,8 +862,8 @@ export class OneCursorOp {
return true;
}
public static moveDown(cursor:OneCursor, inSelectionMode: boolean, isPaged: boolean, usePageSize: number, ctx: IOneCursorOperationContext): boolean {
let linesCount = isPaged ? (usePageSize || cursor.getPageSize()) : 1;
public static moveDown(cursor:OneCursor, inSelectionMode: boolean, noOfLines: number, ctx: IOneCursorOperationContext): boolean {
let linesCount = noOfLines > 0 ? noOfLines : 1;
let viewLineNumber:number,
viewColumn:number;
......@@ -907,8 +903,8 @@ export class OneCursorOp {
return true;
}
public static moveUp(cursor:OneCursor, inSelectionMode: boolean, isPaged: boolean, usePageSize: number, ctx: IOneCursorOperationContext): boolean {
let linesCount = isPaged ? (usePageSize || cursor.getPageSize()) : 1;
public static moveUp(cursor:OneCursor, inSelectionMode: boolean, noOfLines: number, ctx: IOneCursorOperationContext): boolean {
let linesCount = noOfLines > 0 ? noOfLines : 1;
let viewLineNumber:number,
viewColumn:number;
......
......@@ -4175,14 +4175,58 @@ export var EventType = {
};
/**
* Logical positions in the view for cursor move command.
* Positions in the view for cursor move command.
*/
export const CursorMoveViewPosition = {
LineStart: 'lineStart',
LineFirstNonWhitespaceCharacter: 'lineFirstNonWhitespaceCharacter',
LineColumnCenter: 'lineColumnCenter',
LineEnd: 'lineEnd',
LineLastNonWhitespaceCharacter: 'lineLastNonWhitespaceCharacter'
LineLastNonWhitespaceCharacter: 'lineLastNonWhitespaceCharacter',
LineUp: 'lineUp',
LineDown: 'lineDown'
};
/**
* Arguments for Cursor move command
*/
export interface CursorMoveArguments {
to: string;
inSelectionMode?: boolean;
noOfLines?: number;
isPaged?: boolean;
pageSize?: number;
};
/**
* @internal
*/
let isCursorMoveArgs= function(arg): boolean {
if (!types.isObject(arg)) {
return false;
}
if (!types.isString(arg.to)) {
return false;
}
if (!types.isUndefined(arg.inSelectionMode) && !types.isBoolean(arg.inSelectionMode)) {
return false;
}
if (!types.isUndefined(arg.noOfLines) && !types.isNumber(arg.noOfLines)) {
return false;
}
if (!types.isUndefined(arg.isPaged) && !types.isBoolean(arg.isPaged)) {
return false;
}
if (!types.isUndefined(arg.pageSize) && !types.isNumber(arg.pageSize)) {
return false;
}
return true;
};
/**
......@@ -4195,7 +4239,7 @@ export var CommandDescription = {
{
name: nls.localize('editorCommand.cursorMove.arg.name', "Cursor move argument"),
description: nls.localize('editorCommand.cursorMove.arg.description', "Argument containing mandatory 'to' value and an optional 'inSelectionMode' value. Value of 'to' has to be a defined value in `CursorMoveViewPosition`."),
constraint: (arg) => types.isObject(arg) && types.isString(arg.to) && (types.isUndefined(arg.inSelectionMode) || types.isBoolean(arg.inSelectionMode))
constraint: isCursorMoveArgs
}
]
}
......
......@@ -56,8 +56,12 @@ function moveToLineLastNonWhiteSpaceCharacter(cursor: Cursor) {
move(cursor, {to: CursorMoveViewPosition.LineLastNonWhitespaceCharacter});
}
function moveToPosition(cursor: Cursor, lineNumber: number, column: number) {
move(cursor, {to: new Position(lineNumber, column)});
function moveUpByCursorMoveCommand(cursor: Cursor, noOfLines: number= 1, inSelectionMode?: boolean) {
move(cursor, {to: CursorMoveViewPosition.LineUp, noOfLines: noOfLines, inSelectionMode: inSelectionMode});
}
function moveDownByCursorMoveCommand(cursor: Cursor, noOfLines: number= 1, inSelectionMode?: boolean) {
move(cursor, {to: CursorMoveViewPosition.LineDown, noOfLines: noOfLines, inSelectionMode: inSelectionMode});
}
function moveTo(cursor: Cursor, lineNumber: number, column: number, inSelectionMode: boolean = false) {
......@@ -221,7 +225,7 @@ suite('Editor Controller - Cursor', () => {
cursorEqual(thisCursor, 1, 1);
});
// --------- move to first character of line
// --------- cursor move command
test('move to first character of line from middle', () => {
moveTo(thisCursor, 1, 8);
......@@ -319,11 +323,70 @@ suite('Editor Controller - Cursor', () => {
cursorEqual(thisCursor, 1, 11);
});
test('move to position', () => {
moveToPosition(thisCursor, 3, 10);
cursorEqual(thisCursor, 3, 10);
test('move up by cursor move command', () => {
moveTo(thisCursor, 3, 5);
cursorEqual(thisCursor, 3, 5);
moveUpByCursorMoveCommand(thisCursor, 2);
cursorEqual(thisCursor, 1, 5);
moveUpByCursorMoveCommand(thisCursor, 1);
cursorEqual(thisCursor, 1, 1);
});
test('move up with selection by cursor move command', () => {
moveTo(thisCursor, 3, 5);
cursorEqual(thisCursor, 3, 5);
moveUpByCursorMoveCommand(thisCursor, 1, true);
cursorEqual(thisCursor, 2, 2, 3, 5);
moveUp(thisCursor, 1, true);
cursorEqual(thisCursor, 1, 5, 3, 5);
});
test('move up and down with tabs by cursor move command', () => {
moveTo(thisCursor, 1, 5);
cursorEqual(thisCursor, 1, 5);
moveDownByCursorMoveCommand(thisCursor, 4);
cursorEqual(thisCursor, 5, 2);
moveUpByCursorMoveCommand(thisCursor, 1);
cursorEqual(thisCursor, 4, 1);
moveUpByCursorMoveCommand(thisCursor, 1);
cursorEqual(thisCursor, 3, 5);
moveUpByCursorMoveCommand(thisCursor, 1);
cursorEqual(thisCursor, 2, 2);
moveUpByCursorMoveCommand(thisCursor, 1);
cursorEqual(thisCursor, 1, 5);
});
test('move up and down with end of lines starting from a long one by cursor move command', () => {
moveToEndOfLine(thisCursor);
cursorEqual(thisCursor, 1, LINE1.length - 1);
moveToEndOfLine(thisCursor);
cursorEqual(thisCursor, 1, LINE1.length + 1);
moveDownByCursorMoveCommand(thisCursor, 2);
cursorEqual(thisCursor, 3, LINE3.length + 1);
moveDownByCursorMoveCommand(thisCursor, 1);
cursorEqual(thisCursor, 4, LINE4.length + 1);
moveDownByCursorMoveCommand(thisCursor, 1);
cursorEqual(thisCursor, 5, LINE5.length + 1);
moveUpByCursorMoveCommand(thisCursor, 4);
cursorEqual(thisCursor, 1, LINE1.length + 1);
});
// --- end of cursor move command tests
test('move', () => {
moveTo(thisCursor, 1, 2);
cursorEqual(thisCursor, 1, 2);
......
......@@ -3190,7 +3190,7 @@ declare module monaco.editor {
};
/**
* Logical positions in the view for cursor move command.
* Positions in the view for cursor move command.
*/
export const CursorMoveViewPosition: {
LineStart: string;
......@@ -3198,8 +3198,21 @@ declare module monaco.editor {
LineColumnCenter: string;
LineEnd: string;
LineLastNonWhitespaceCharacter: string;
LineUp: string;
LineDown: string;
};
/**
* Arguments for Cursor move command
*/
export interface CursorMoveArguments {
to: string;
inSelectionMode?: boolean;
noOfLines?: number;
isPaged?: boolean;
pageSize?: number;
}
/**
* Built-in commands.
*/
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册