Skip to content

fix(popup): support relative: cusror on vim #281

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

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
36 changes: 36 additions & 0 deletions popup/mod_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,41 @@ test({
},
});
}

await t.step({
name: `open() with relative cursor`,
fn: async () => {
await fn.append(denops, 0, [
"0123456789",
"0123456789",
"0123456789",
"0123456789",
"0123456789",
"0123456789",
"0123456789",
"0123456789",
"0123456789",
"0123456789",
]);
await fn.cursor(denops, [5, 5]);

await using popupWindow = await popup.open(denops, {
relative: "cursor",
width: 20,
height: 20,
row: 3,
col: 3,
anchor: "NW",
});

assertEquals(await fn.win_gettype(denops, popupWindow.winid), "popup");
assertEquals(await fn.winwidth(denops, popupWindow.winid), 20);
assertEquals(await fn.winheight(denops, popupWindow.winid), 20);

const info = (await fn.getwininfo(denops, popupWindow.winid))[0];
assertEquals(info.winrow, 7, "winrow should be 7");
assertEquals(info.wincol, 7, "wincol should be 7");
Comment on lines +140 to +141
Copy link
Author

Choose a reason for hiding this comment

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

The 7 comes from the nvim's test result.

},
});
},
});
22 changes: 20 additions & 2 deletions popup/vim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,30 @@ function toPopupCreateOptions(
};
}

function handleRelative(
relative: "editor" | "cursor",
offset: number,
): string | number {
switch (relative) {
case "editor":
return offset;
case "cursor":
return offset < 0 ? `cursor${offset}` : `cursor+${offset}`;
default:
unreachable(relative);
}
}

function toPopupSetOptionsOptions(
options: Partial<Omit<OpenOptions, "bufnr" | "noRedraw">>,
): vimFn.PopupSetOptionsOptions {
const v: vimFn.PopupCreateOptions = {
line: options.row,
col: options.col,
line: options.row
? handleRelative(options.relative ?? "editor", options.row)
: undefined,
col: options.col
? handleRelative(options.relative ?? "editor", options.col)
: undefined,
pos: options.anchor ? posFromAnchor(options.anchor) : undefined,
fixed: true, // To keep consistent with the behavior of Neovim's floating window
flip: false, // To keep consistent with the behavior of Neovim's floating window
Expand Down
Loading