diff --git a/popup/mod_test.ts b/popup/mod_test.ts index 01081b6..16115cd 100644 --- a/popup/mod_test.ts +++ b/popup/mod_test.ts @@ -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"); + }, + }); }, }); diff --git a/popup/vim.ts b/popup/vim.ts index f9db6ec..605f95d 100644 --- a/popup/vim.ts +++ b/popup/vim.ts @@ -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>, ): 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