Implement editor scroll command - #9609

This commit is contained in:
Sandeep Somavarapu 2016-08-04 12:23:56 +02:00
parent 55ff6262bd
commit 1331596fa3
5 changed files with 139 additions and 8 deletions

View file

@ -440,6 +440,8 @@ export function createMonacoEditorAPI(): typeof monaco.editor {
EditorType: editorCommon.EditorType,
CursorMoveByUnit: editorCommon.CursorMoveByUnit,
CursorMovePosition: editorCommon.CursorMovePosition,
EditorScrollDirection: editorCommon.EditorScrollDirection,
EditorScrollByUnit: editorCommon.EditorScrollByUnit,
Handler: editorCommon.Handler,
// consts

View file

@ -231,6 +231,8 @@ registerCoreCommand(H.ExpandLineSelection, {
primary: KeyMod.CtrlCmd | KeyCode.KEY_I
});
registerCoreCommand(H.EditorScroll, { primary: null }, null, null, D.EditorScroll);
registerCoreCommand(H.ScrollLineUp, {
primary: KeyMod.CtrlCmd | KeyCode.UpArrow,
mac: { primary: KeyMod.WinCtrl | KeyCode.PageUp}

View file

@ -1003,6 +1003,8 @@ export class Cursor extends EventEmitter {
this._handlers[H.Outdent] = (ctx) => this._outdent(ctx);
this._handlers[H.Paste] = (ctx) => this._paste(ctx);
this._handlers[H.EditorScroll] = (ctx) => this._editorScroll(ctx);
this._handlers[H.ScrollLineUp] = (ctx) => this._scrollUp(false, ctx);
this._handlers[H.ScrollLineDown] = (ctx) => this._scrollDown(false, ctx);
this._handlers[H.ScrollPageUp] = (ctx) => this._scrollUp(true, ctx);
@ -1451,14 +1453,29 @@ export class Cursor extends EventEmitter {
}
}
private _scrollUp(isPaged: boolean, ctx: IMultipleCursorOperationContext): boolean {
ctx.requestScrollDeltaLines = isPaged ? -this.cursors.getAll()[0].getPageSize() : -1;
private _editorScroll(ctx: IMultipleCursorOperationContext): boolean {
let editorScrollArg: editorCommon.EditorScrollArguments = ctx.eventData;
let value = editorScrollArg.value || 1;
switch (editorScrollArg.to) {
case editorCommon.EditorScrollDirection.Up:
value = -(value);
case editorCommon.EditorScrollDirection.Down:
ctx.requestScrollDeltaLines = value * (editorCommon.EditorScrollByUnit.Page === editorScrollArg.by ? this.cursors.getAll()[0].getPageSize() : 1);
break;
}
return true;
}
private _scrollUp(isPaged: boolean, ctx: IMultipleCursorOperationContext): boolean {
ctx.eventData = <editorCommon.EditorScrollArguments>{ to: editorCommon.EditorScrollDirection.Up, value: 1 };
ctx.eventData.by = isPaged ? editorCommon.EditorScrollByUnit.Page : editorCommon.EditorScrollByUnit.Line;
return this._editorScroll(ctx);
}
private _scrollDown(isPaged: boolean, ctx: IMultipleCursorOperationContext): boolean {
ctx.requestScrollDeltaLines = isPaged ? this.cursors.getAll()[0].getPageSize() : 1;
return true;
ctx.eventData = <editorCommon.EditorScrollArguments>{ to: editorCommon.EditorScrollDirection.Down, value: 1 };
ctx.eventData.by = isPaged ? editorCommon.EditorScrollByUnit.Page : editorCommon.EditorScrollByUnit.Line;
return this._editorScroll(ctx);
}
private _distributePasteToCursors(ctx: IMultipleCursorOperationContext): string[] {

View file

@ -4,7 +4,6 @@
*--------------------------------------------------------------------------------------------*/
'use strict';
import * as nls from 'vs/nls';
import {IAction} from 'vs/base/common/actions';
import {IEventEmitter, BulkListenerCallback} from 'vs/base/common/eventEmitter';
import {MarkedString} from 'vs/base/common/htmlContent';
@ -4264,19 +4263,102 @@ let isCursorMoveArgs= function(arg): boolean {
return true;
};
/**
* Directions in the view for editor scroll command.
*/
export const EditorScrollDirection = {
Up: 'up',
Down: 'down',
};
/**
* Units for editor scroll 'by' argument
*/
export const EditorScrollByUnit = {
Line: 'line',
Page: 'page'
};
/**
* Arguments for editor scroll command
*/
export interface EditorScrollArguments {
to: string;
by?: string;
value?: number;
};
/**
* @internal
*/
let isEditorScrollArgs= function(arg): boolean {
if (!types.isObject(arg)) {
return false;
}
let scrollArg: EditorScrollArguments = arg;
if (!types.isString(scrollArg.to)) {
return false;
}
if (!types.isUndefined(scrollArg.by) && !types.isString(scrollArg.by)) {
return false;
}
if (!types.isUndefined(scrollArg.value) && !types.isNumber(scrollArg.value)) {
return false;
}
return true;
};
/**
* @internal
*/
export var CommandDescription = {
CursorMove: <ICommandHandlerDescription>{
description: nls.localize('editorCommand.cursorMove.description', "Move cursor to a logical position in the view"),
description: 'Move cursor to a logical position in the view',
args: [
{
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`."),
name: 'Cursor move argument object',
description: `Property-value pairs that can be passed through this argument:
'to': A mandatory logical position value providing where to move the cursor.
\`\`\`
'left', 'right', 'up', 'down',
'wrappedLineStart', 'wrappedLineFirstNonWhitespaceCharacter', 'wrappedLineColumnCenter', 'wrappedLineEnd' ,'wrappedLineLastNonWhitespaceCharacter',
'viewPortTop', 'viewPortCenter', 'viewPortBottom'
\`\`\`
'by': Unit to move. Default is computed based on 'to' value.
\`\`\`
'line', 'wrappedLine', 'character', 'halfLine'
\`\`\`
'value': Number of units to move. Default is '1'.
'select': If 'true' makes the selection. Default is 'false'.
`,
constraint: isCursorMoveArgs
}
]
},
EditorScroll: <ICommandHandlerDescription>{
description: 'Scroll editor in the given direction',
args: [
{
name: 'Editor scroll argument object',
description: `Property-value pairs that can be passed through this argument:
'to': A mandatory direction value.
\`\`\`
'up', 'down'
\`\`\`
'by': Unit to move. Default is computed based on 'to' value.
\`\`\`
'line', 'page'
\`\`\`
'value': Number of units to move. Default is '1'.
`,
constraint: isEditorScrollArgs
}
]
}
};
@ -4398,6 +4480,8 @@ export var Handler = {
SelectAll: 'selectAll',
EditorScroll: 'editorScroll',
ScrollLineUp: 'scrollLineUp',
ScrollLineDown: 'scrollLineDown',

26
src/vs/monaco.d.ts vendored
View file

@ -3242,6 +3242,31 @@ declare module monaco.editor {
value?: number;
}
/**
* Directions in the view for editor scroll command.
*/
export const EditorScrollDirection: {
Up: string;
Down: string;
};
/**
* Units for editor scroll 'by' argument
*/
export const EditorScrollByUnit: {
Line: string;
Page: string;
};
/**
* Arguments for editor scroll command
*/
export interface EditorScrollArguments {
to: string;
by?: string;
value?: number;
}
/**
* Built-in commands.
*/
@ -3331,6 +3356,7 @@ declare module monaco.editor {
LineInsertAfter: string;
LineBreakInsert: string;
SelectAll: string;
EditorScroll: string;
ScrollLineUp: string;
ScrollLineDown: string;
ScrollPageUp: string;