Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Editor Scroll #681

Merged
merged 7 commits into from
Sep 15, 2016
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 105 additions & 53 deletions src/actions/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -522,13 +522,39 @@ class CommandCtrlW extends BaseCommand {
}
}

@RegisterAction
class CommandCtrlE extends BaseCommand {
abstract class CommandEditorScroll extends BaseCommand {
modes = [ModeName.Normal];
keys = ["<C-e>"];
canBePrefixedWithCount = true;
keys: string[];
to: string;
by: string;

public async exec(position: Position, vimState: VimState): Promise<VimState> {
await vscode.commands.executeCommand("scrollLineDown");
vimState.postponedCodeViewChanges.push({
name: "editorScroll",
args: {
to: this.to,
by: this.by,
value: 1,
revealCursor: true
}
});

return vimState;
}

public async execCount(position: Position, vimState: VimState): Promise<VimState> {
let timesToRepeat = this.canBePrefixedWithCount ? vimState.recordedState.count || 1 : 1;

vimState.postponedCodeViewChanges.push({
name: "editorScroll",
args: {
to: this.to,
by: this.by,
value: timesToRepeat,
revealCursor: true
}
});

return vimState;
}
Expand Down Expand Up @@ -609,16 +635,49 @@ class CommandDeleteIndentInCurrentLine extends BaseCommand {
}
}

class CommandCtrlE extends CommandEditorScroll {
keys = ["<C-e>"];
to = "down";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we create enums for to and by values?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, that's definitely necessary.

by = "line";
}

@RegisterAction
class CommandCtrlY extends BaseCommand {
modes = [ModeName.Normal];
class CommandCtrlY extends CommandEditorScroll {
keys = ["<C-y>"];
to = "up";
by = "line";
}

public async exec(position: Position, vimState: VimState): Promise<VimState> {
await vscode.commands.executeCommand("scrollLineUp");
@RegisterAction
class CommandMoveFullPageUp extends CommandEditorScroll {
modes = [ModeName.Normal, ModeName.Visual, ModeName.VisualLine, ModeName.VisualBlock];
keys = ["<C-b>"];
to = "up";
by = "page";
}

return vimState;
}
@RegisterAction
class CommandMoveFullPageDown extends CommandEditorScroll {
modes = [ModeName.Normal, ModeName.Visual, ModeName.VisualLine, ModeName.VisualBlock];
keys = ["<C-f>"];
to = "down";
by = "page";
}

@RegisterAction
class CommandMoveHalfPageDown extends CommandEditorScroll {
modes = [ModeName.Normal, ModeName.Visual, ModeName.VisualLine, ModeName.VisualBlock];
keys = ["<C-d>"];
to = "down";
by = "halfPage";
}

@RegisterAction
class CommandMoveHalfPageUp extends CommandEditorScroll {
modes = [ModeName.Normal, ModeName.Visual, ModeName.VisualLine, ModeName.VisualBlock];
keys = ["<C-u>"];
to = "up";
by = "halfPage";
}

@RegisterAction
Expand Down Expand Up @@ -1680,6 +1739,42 @@ class CommandCenterScroll extends BaseCommand {
}
}

@RegisterAction
class CommandTopScroll extends BaseCommand {
modes = [ModeName.Normal];
keys = ["z", "t"];

public async exec(position: Position, vimState: VimState): Promise<VimState> {
vimState.postponedCodeViewChanges.push({
name: "revealLine",
args: {
lineNumber: position.line,
at: 'top'
}
});

return vimState;
}
}

@RegisterAction
class CommandBottomScroll extends BaseCommand {
modes = [ModeName.Normal];
keys = ["z", "b"];

public async exec(position: Position, vimState: VimState): Promise<VimState> {
vimState.postponedCodeViewChanges.push({
name: "revealLine",
args: {
lineNumber: position.line,
at: 'bottom'
}
});

return vimState;
}
}

@RegisterAction
class CommandGoToOtherEndOfHighlightedText extends BaseCommand {
modes = [ModeName.Visual, ModeName.VisualLine];
Expand Down Expand Up @@ -1725,49 +1820,6 @@ class CommandRedo extends BaseCommand {
}
}

@RegisterAction
class CommandMoveFullPageDown extends BaseCommand {
modes = [ModeName.Normal, ModeName.Visual, ModeName.VisualLine, ModeName.VisualBlock];
keys = ["<C-f>"];

public async exec(position: Position, vimState: VimState): Promise<VimState> {
await vscode.commands.executeCommand("cursorPageDown");
return vimState;
}
}

@RegisterAction
class CommandMoveFullPageUp extends BaseCommand {
modes = [ModeName.Normal, ModeName.Visual, ModeName.VisualLine, ModeName.VisualBlock];
keys = ["<C-b>"];

public async exec(position: Position, vimState: VimState): Promise<VimState> {
await vscode.commands.executeCommand("cursorPageUp");
return vimState;
}
}

@RegisterAction
class CommandMoveHalfPageDown extends BaseMovement {
keys = ["<C-d>"];

public async execAction(position: Position, vimState: VimState): Promise<Position> {
return new Position(
Math.min(TextEditor.getLineCount() - 1, position.line + Configuration.getInstance().scroll),
position.character
);
}
}

@RegisterAction
class CommandMoveHalfPageUp extends BaseMovement {
keys = ["<C-u>"];

public async execAction(position: Position, vimState: VimState): Promise<Position> {
return new Position(Math.max(0, position.line - Configuration.getInstance().scroll), position.character);
}
}

@RegisterAction
class CommandDeleteToLineEnd extends BaseCommand {
modes = [ModeName.Normal];
Expand Down
21 changes: 21 additions & 0 deletions src/mode/modeHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ export enum VimSpecialCommands {
Dot
}

export class ViewChange {
public name: string;
Copy link
Member

@jpoon jpoon Sep 14, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: maybe rename this to command? As that'll be inline with how it's used in executeCommand.

public args: any;
}

/**
* The VimState class holds permanent state that carries over from action
* to action.
Expand Down Expand Up @@ -68,6 +73,13 @@ export class VimState {

public focusChanged = false;

/**
* Every time we invoke a VS Code command which might trigger Code's view update,
* we should postpone its view updating phase to avoid conflicting with our internal view updating mechanism.
* This array is used to cache every VS Code view updating event and they will be triggered once we run the inhouse `viewUpdate`.
*/
public postponedCodeViewChanges: ViewChange[] = [];

/**
* Used to prevent non-recursive remappings from looping.
*/
Expand Down Expand Up @@ -1088,6 +1100,15 @@ export class ModeHandler implements vscode.Disposable {

vscode.window.activeTextEditor.setDecorations(this._caretDecoration, rangesToDraw);

if (this.vimState.postponedCodeViewChanges.length > 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this outside if necessary?

for (let i = 0; i < this.vimState.postponedCodeViewChanges.length; i++) {
let viewChange = this.vimState.postponedCodeViewChanges[i];
await vscode.commands.executeCommand(viewChange.name, viewChange.args);
}

this.vimState.postponedCodeViewChanges = [];
}

if (this.currentMode.name === ModeName.SearchInProgressMode) {
this.setupStatusBarItem(`Searching for: ${ this.vimState.searchState!.searchString }`);
} else {
Expand Down