Skip to content

Commit

Permalink
fix(scrolling): fix c-u/c-y/c-f/c-b by deletion (#885)
Browse files Browse the repository at this point in the history
  • Loading branch information
theol0403 committed Oct 13, 2022
1 parent 0372e30 commit 468b071
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 177 deletions.
53 changes: 12 additions & 41 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,6 @@
"default": false,
"markdownDescription": "Use neovim installed in WSL. If you enable this setting, specify the path to the neovim executable installed in WSL `neovimExecutablePaths.linux` setting"
},
"vscode-neovim.revealCursorScrollLine": {
"type": "boolean",
"default": false,
"markdownDescription": "If 'true' reveals the cursor when scrolling by line and if it is outside view port"
},
"vscode-neovim.logPath": {
"type": "string",
"default": "",
Expand Down Expand Up @@ -296,41 +291,13 @@
"command": "vscode-neovim.send-cmdline",
"title": "Neovim: Send key in cmdline"
},
{
"command": "vscode-neovim.ctrl-b",
"title": "Neovim: ctrl-b"
},
{
"command": "vscode-neovim.ctrl-d",
"title": "Neovim: ctrl-d"
},
{
"command": "vscode-neovim.ctrl-e",
"title": "Neovim: ctrl-e"
},
{
"command": "vscode-neovim.ctrl-f",
"title": "Neovim: ctrl-f"
},
{
"command": "vscode-neovim.ctrl-u",
"title": "Neovim: ctrl-u"
},
{
"command": "vscode-neovim.ctrl-y",
"title": "Neovim: ctrl-y"
},
{
"command": "vscode-neovim.shift-m",
"title": "Neovim: shift-m"
},
{
"command": "vscode-neovim.shift-l",
"title": "Neovim: shift-l"
},
{
"command": "vscode-neovim:shift-h",
"title": "Neovim: shift-h"
}
],
"keybindings": [
Expand Down Expand Up @@ -705,24 +672,27 @@
"args": "<C-a>"
},
{
"command": "vscode-neovim.ctrl-b",
"command": "vscode-neovim.send",
"key": "ctrl+b",
"when": "editorTextFocus && neovim.init && neovim.mode != insert && neovim.ctrlKeysNormal"
"when": "editorTextFocus && neovim.init && neovim.mode != insert && neovim.ctrlKeysNormal",
"args": "<C-b>"
},
{
"command": "vscode-neovim.ctrl-d",
"command": "vscode-neovim.send",
"key": "ctrl+d",
"when": "editorTextFocus && neovim.init && neovim.mode != insert && neovim.ctrlKeysNormal"
"when": "editorTextFocus && neovim.init && neovim.mode != insert && neovim.ctrlKeysNormal",
"args": "<C-d>"
},
{
"command": "vscode-neovim.ctrl-e",
"key": "ctrl+e",
"when": "editorTextFocus && neovim.init && neovim.mode != insert && neovim.ctrlKeysNormal"
},
{
"command": "vscode-neovim.ctrl-f",
"command": "vscode-neovim.send",
"key": "ctrl+f",
"when": "editorTextFocus && neovim.init && neovim.mode != insert && neovim.ctrlKeysNormal"
"when": "editorTextFocus && neovim.init && neovim.mode != insert && neovim.ctrlKeysNormal",
"args": "<C-f>"
},
{
"command": "vscode-neovim.send",
Expand All @@ -743,9 +713,10 @@
"args": "<C-r>"
},
{
"command": "vscode-neovim.ctrl-u",
"command": "vscode-neovim.send",
"key": "ctrl+u",
"when": "editorTextFocus && neovim.init && neovim.mode != insert && neovim.ctrlKeysNormal"
"when": "editorTextFocus && neovim.init && neovim.mode != insert && neovim.ctrlKeysNormal",
"args": "<C-u>"
},
{
"command": "vscode-neovim.send",
Expand Down
73 changes: 2 additions & 71 deletions src/commands_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,9 @@ import { NeovimClient } from "neovim";
import { NeovimExtensionRequestProcessable } from "./neovim_events_processable";

export class CommandsController implements Disposable, NeovimExtensionRequestProcessable {
private client: NeovimClient;

private disposables: Disposable[] = [];

private revealCursorScrollLine: boolean;

public constructor(client: NeovimClient, revealCursorScrollLine: boolean) {
this.client = client;
this.revealCursorScrollLine = revealCursorScrollLine;
this.disposables.push(
vscode.commands.registerCommand("vscode-neovim.ctrl-f", () => this.scrollPage("page", "down")),
);
this.disposables.push(
vscode.commands.registerCommand("vscode-neovim.ctrl-b", () => this.scrollPage("page", "up")),
);
this.disposables.push(
vscode.commands.registerCommand("vscode-neovim.ctrl-d", () => this.scrollPage("halfPage", "down")),
);
this.disposables.push(
vscode.commands.registerCommand("vscode-neovim.ctrl-u", () => this.scrollPage("halfPage", "up")),
);
public constructor(private client: NeovimClient) {
this.disposables.push(vscode.commands.registerCommand("vscode-neovim.ctrl-e", () => this.scrollLine("down")));
this.disposables.push(vscode.commands.registerCommand("vscode-neovim.ctrl-y", () => this.scrollLine("up")));
}
Expand All @@ -43,63 +25,12 @@ export class CommandsController implements Disposable, NeovimExtensionRequestPro
this.revealLine(at, !!updateCursor);
break;
}
case "move-cursor": {
const [to] = args as ["top" | "middle" | "bottom"];
this.goToLine(to);
break;
}
case "scroll": {
const [by, to] = args as ["page" | "halfPage", "up" | "down"];
this.scrollPage(by, to);
break;
}
case "scroll-line": {
const [to] = args as ["up" | "down"];
this.scrollLine(to);
break;
}
case "insert-line": {
const [type] = args as ["before" | "after"];
await this.client.command("startinsert");
await vscode.commands.executeCommand(
type === "before" ? "editor.action.insertLineBefore" : "editor.action.insertLineAfter",
);
break;
}
}
}

/// SCROLL COMMANDS ///
private scrollPage = (by: "page" | "halfPage", to: "up" | "down"): void => {
vscode.commands.executeCommand("editorScroll", { to, by, revealCursor: true });
};

private scrollLine = (to: "up" | "down"): void => {
vscode.commands.executeCommand("editorScroll", { to, by: "line", revealCursor: this.revealCursorScrollLine });
};

private goToLine = (to: "top" | "middle" | "bottom"): void => {
const e = vscode.window.activeTextEditor;
if (!e) {
return;
}
const topVisible = e.visibleRanges[0].start.line;
const bottomVisible = e.visibleRanges[0].end.line;
const lineNum =
to === "top"
? topVisible
: to === "bottom"
? bottomVisible
: Math.floor(topVisible + (bottomVisible - topVisible) / 2);
const line = e.document.lineAt(lineNum);
e.selections = [
new vscode.Selection(
lineNum,
line.firstNonWhitespaceCharacterIndex,
lineNum,
line.firstNonWhitespaceCharacterIndex,
),
];
vscode.commands.executeCommand("editorScroll", { to, by: "line", revealCursor: true });
};

// zz, zt, zb and others
Expand Down
23 changes: 3 additions & 20 deletions src/document_change_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,6 @@ export class DocumentChangeManager implements Disposable, NeovimExtensionRequest
* Dot repeat workaround
*/
private dotRepeatChange: DotRepeatChange | undefined;
/**
* A hint for dot-repeat indicating of how the insert mode was started
*/
private dotRepeatStartModeInsertHint?: "o" | "O";
/**
* True when we're currently applying edits, so incoming changes will go into pending events queue
*/
Expand Down Expand Up @@ -119,12 +115,8 @@ export class DocumentChangeManager implements Disposable, NeovimExtensionRequest
return (this.textDocumentChangePromise.get(doc)?.length || 0) > 0;
}

public async handleExtensionRequest(name: string, args: unknown[]): Promise<void> {
if (name === "insert-line") {
const [type] = args as ["before" | "after"];
this.dotRepeatStartModeInsertHint = type === "before" ? "O" : "o";
this.logger.debug(`${LOG_PREFIX}: Setting start insert mode hint - ${this.dotRepeatStartModeInsertHint}`);
}
public async handleExtensionRequest(): Promise<void> {
// skip
}

public async syncDotRepeatWithNeovim(): Promise<void> {
Expand Down Expand Up @@ -168,13 +160,6 @@ export class DocumentChangeManager implements Disposable, NeovimExtensionRequest
);
}
let editStr = "";
if (dotRepeatChange.startMode) {
editStr += `<Esc>${dotRepeatChange.startMode}`;
// remove EOL from first change
if (dotRepeatChange.text.startsWith(dotRepeatChange.eol)) {
dotRepeatChange.text = dotRepeatChange.text.slice(dotRepeatChange.eol.length);
}
}
if (dotRepeatChange.rangeLength) {
editStr += [...new Array(dotRepeatChange.rangeLength).keys()].map(() => "<BS>").join("");
}
Expand Down Expand Up @@ -483,19 +468,17 @@ export class DocumentChangeManager implements Disposable, NeovimExtensionRequest
}

const eol = doc.eol === EndOfLine.LF ? "\n" : "\r\n";
const startModeHint = this.dotRepeatStartModeInsertHint;
const activeEditor = window.activeTextEditor;

// Store dot repeat
if (activeEditor && activeEditor.document === doc && this.main.modeManager.isInsertMode) {
this.dotRepeatStartModeInsertHint = undefined;
const cursor = activeEditor.selection.active;
for (const change of contentChanges) {
if (isCursorChange(change, cursor, eol)) {
if (this.dotRepeatChange && isChangeSubsequentToChange(change, this.dotRepeatChange)) {
this.dotRepeatChange = accumulateDotRepeatChange(change, this.dotRepeatChange);
} else {
this.dotRepeatChange = normalizeDotRepeatChange(change, eol, startModeHint);
this.dotRepeatChange = normalizeDotRepeatChange(change, eol);
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
const useCtrlKeysNormalMode = settings.get("useCtrlKeysForNormalMode", true);
const useCtrlKeysInsertMode = settings.get("useCtrlKeysForInsertMode", true);
const useWsl = isWindows && settings.get("useWSL", false);
const revealCursorScrollLine = settings.get("revealCursorScrollLine", false);
const neovimWidth = settings.get("neovimWidth", 1000);
const neovimViewportHeightExtend = settings.get("neovimViewportHeightExtend", 1);
const customInit = getNeovimInitPath() ?? "";
Expand Down Expand Up @@ -46,7 +45,6 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
useWsl: ext.extensionKind === vscode.ExtensionKind.Workspace ? false : useWsl,
neovimViewportWidth: neovimWidth,
neovimViewportHeightExtend: neovimViewportHeightExtend,
revealCursorScrollLine: revealCursorScrollLine,
logConf: {
logPath,
outputToConsole,
Expand Down
3 changes: 1 addition & 2 deletions src/main_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ export interface ControllerSettings {
clean: boolean;
neovimViewportWidth: number;
neovimViewportHeightExtend: number;
revealCursorScrollLine: boolean;
logConf: {
level: "none" | "error" | "warn" | "debug";
logPath: string;
Expand Down Expand Up @@ -174,7 +173,7 @@ export class MainController implements vscode.Disposable {
const channel = await this.client.channelId;
await this.client.setVar("vscode_channel", channel);

this.commandsController = new CommandsController(this.client, this.settings.revealCursorScrollLine);
this.commandsController = new CommandsController(this.client);
this.disposables.push(this.commandsController);

this.modeManager = new ModeManager(this.logger);
Expand Down
2 changes: 1 addition & 1 deletion src/test/suite/vscode-integartion-specific.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ describe("VSCode integration specific stuff", () => {
);
const editor = await vscode.window.showTextDocument(doc, vscode.ViewColumn.One);
await wait(1000);
await vscode.commands.executeCommand("vscode-neovim.ctrl-f");
await sendVSCodeKeys("<C-f>", 0);
await wait(1500);

let visibleRange = editor.visibleRanges[0];
Expand Down
11 changes: 1 addition & 10 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ export interface DotRepeatChange {
* Change text
*/
text: string;
/**
* Set if it was the first change and started either through o or O
*/
startMode?: "o" | "O";
/**
* Text eol
*/
Expand Down Expand Up @@ -351,16 +347,11 @@ export function getNeovimInitPath(): string | undefined {
return getSystemSpecificSetting("neovimInitVimPaths", legacySettingInfo);
}

export function normalizeDotRepeatChange(
change: TextDocumentContentChangeEvent,
eol: string,
startMode?: "o" | "O",
): DotRepeatChange {
export function normalizeDotRepeatChange(change: TextDocumentContentChangeEvent, eol: string): DotRepeatChange {
return {
rangeLength: change.rangeLength,
rangeOffset: change.rangeOffset,
text: change.text,
startMode,
eol,
};
}
Expand Down
30 changes: 0 additions & 30 deletions vim/vscode-scrolling.vim
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,3 @@ nnoremap z- <Cmd>call <SID>reveal('bottom', 1)<CR>
xnoremap z- <Cmd>call <SID>reveal('bottom', 1)<CR>
nnoremap zb <Cmd>call <SID>reveal('bottom', 0)<CR>
xnoremap zb <Cmd>call <SID>reveal('bottom', 0)<CR>

function s:moveCursor(to)
" Native VSCode commands don't register jumplist. Fix by registering jumplist in Vim e.g. for subsequent use of <C-o>
normal! m'
call VSCodeExtensionNotify('move-cursor', a:to)
endfunction

nnoremap H <Cmd>call <SID>moveCursor('top')<CR>
xnoremap H <Cmd>call <SID>moveCursor('top')<CR>
nnoremap M <Cmd>call <SID>moveCursor('middle')<CR>
xnoremap M <Cmd>call <SID>moveCursor('middle')<CR>
nnoremap L <Cmd>call <SID>moveCursor('bottom')<CR>
xnoremap L <Cmd>call <SID>moveCursor('bottom')<CR>
" Disabled due to scroll problems (the ext binds them directly)
" nnoremap <silent> <expr> <C-d> VSCodeExtensionCall('scroll', 'halfPage', 'down')
" xnoremap <silent> <expr> <C-d> VSCodeExtensionCall('scroll', 'halfPage', 'down')
" nnoremap <silent> <expr> <C-u> VSCodeExtensionCall('scroll', 'halfPage', 'up')
" xnoremap <silent> <expr> <C-u> VSCodeExtensionCall('scroll', 'halfPage', 'up')

" nnoremap <silent> <expr> <C-f> VSCodeExtensionCall('scroll', 'page', 'down')
" xnoremap <silent> <expr> <C-f> VSCodeExtensionCall('scroll', 'page', 'down')
" nnoremap <silent> <expr> <C-b> VSCodeExtensionCall('scroll', 'page', 'up')
" xnoremap <silent> <expr> <C-b> VSCodeExtensionCall('scroll', 'page', 'up')

" nnoremap <silent> <expr> <C-e> VSCodeExtensionNotify('scroll-line', 'down')
" xnoremap <silent> <expr> <C-e> VSCodeExtensionNotify('scroll-line', 'down')
" nnoremap <silent> <expr> <C-y> VSCodeExtensionNotify('scroll-line', 'up')
" xnoremap <silent> <expr> <C-y> VSCodeExtensionNotify('scroll-line', 'up')

0 comments on commit 468b071

Please sign in to comment.