|
| 1 | +/** |
| 2 | + * A module to provide compatibility layer for popup window in Vim and Neovim. |
| 3 | + * |
| 4 | + * ```typescript |
| 5 | + * import type { Denops } from "https://deno.land/x/denops_std@$MODULE_VERSION/mod.ts"; |
| 6 | + * import * as buffer from "https://deno.land/x/denops_std@$MODULE_VERSION/buffer/mod.ts"; |
| 7 | + * import * as fn from "https://deno.land/x/denops_std@$MODULE_VERSION/function/mod.ts"; |
| 8 | + * import * as popup from "https://deno.land/x/denops_std@$MODULE_VERSION/popup/mod.ts"; |
| 9 | + * |
| 10 | + * export async function main(denops: Denops): Promise<void> { |
| 11 | + * // Create a new buffer |
| 12 | + * const bufnr = await fn.bufadd(denops, ""); |
| 13 | + * await fn.bufload(denops, bufnr); |
| 14 | + * |
| 15 | + * // Write some text to the buffer |
| 16 | + * await buffer.replace(denops, bufnr, ["Hello, world!"]); |
| 17 | + * |
| 18 | + * // Open a popup window showing the buffer |
| 19 | + * const popupWindow = await popup.open(denops, { |
| 20 | + * bufnr, |
| 21 | + * relative: "editor", |
| 22 | + * width: 20, |
| 23 | + * height: 20, |
| 24 | + * row: 1, |
| 25 | + * col: 1, |
| 26 | + * }); |
| 27 | + * |
| 28 | + * // Wiat 3 seconds |
| 29 | + * await new Promise((resolve) => setTimeout(resolve, 3000)); |
| 30 | + * |
| 31 | + * // Close the popup window |
| 32 | + * await popupWindow.close(); |
| 33 | + * } |
| 34 | + * ``` |
| 35 | + * |
| 36 | + * Or with `await using` statement: |
| 37 | + * |
| 38 | + * ```typescript |
| 39 | + * import type { Denops } from "https://deno.land/x/denops_std@$MODULE_VERSION/mod.ts"; |
| 40 | + * import * as buffer from "https://deno.land/x/denops_std@$MODULE_VERSION/buffer/mod.ts"; |
| 41 | + * import * as fn from "https://deno.land/x/denops_std@$MODULE_VERSION/function/mod.ts"; |
| 42 | + * import * as popup from "https://deno.land/x/denops_std@$MODULE_VERSION/popup/mod.ts"; |
| 43 | + * |
| 44 | + * export async function main(denops: Denops): Promise<void> { |
| 45 | + * // Create a new buffer |
| 46 | + * const bufnr = await fn.bufadd(denops, ""); |
| 47 | + * await fn.bufload(denops, bufnr); |
| 48 | + * |
| 49 | + * // Write some text to the buffer |
| 50 | + * await buffer.replace(denops, bufnr, ["Hello, world!"]); |
| 51 | + * |
| 52 | + * // Open a popup window showing the buffer |
| 53 | + * await using popupWindow = await popup.open(denops, { |
| 54 | + * bufnr, |
| 55 | + * relative: "editor", |
| 56 | + * width: 20, |
| 57 | + * height: 20, |
| 58 | + * row: 1, |
| 59 | + * col: 1, |
| 60 | + * }); |
| 61 | + * |
| 62 | + * // Wiat 3 seconds |
| 63 | + * await new Promise((resolve) => setTimeout(resolve, 3000)); |
| 64 | + * |
| 65 | + * // The popup window is automatically closed, due to `await using` statement |
| 66 | + * } |
| 67 | + * ``` |
| 68 | + * |
| 69 | + * Note that this module does NOT work with `batch.collect()`. |
| 70 | + * |
| 71 | + * @module |
| 72 | + */ |
| 73 | + |
| 74 | +import type { Denops } from "../mod.ts"; |
| 75 | +import * as fn from "../function/mod.ts"; |
| 76 | + |
| 77 | +import type { OpenOptions, PopupWindow } from "./types.ts"; |
| 78 | +import { |
| 79 | + closePopup as closePopupVim, |
| 80 | + openPopup as openPopupVim, |
| 81 | +} from "./vim.ts"; |
| 82 | +import { |
| 83 | + closePopup as closePopupNvim, |
| 84 | + openPopup as openPopupNvim, |
| 85 | +} from "./nvim.ts"; |
| 86 | + |
| 87 | +/** |
| 88 | + * Open a popup window showing the buffer in Vim/Neovim compatible way. |
| 89 | + * |
| 90 | + * ```typescript |
| 91 | + * import type { Denops } from "https://deno.land/x/denops_std@$MODULE_VERSION/mod.ts"; |
| 92 | + * import * as popup from "https://deno.land/x/denops_std@$MODULE_VERSION/popup/mod.ts"; |
| 93 | + * |
| 94 | + * export async function main(denops: Denops): Promise<void> { |
| 95 | + * // Open a popup window |
| 96 | + * const popupWindow = await popup.open(denops, { |
| 97 | + * relative: "editor", |
| 98 | + * width: 20, |
| 99 | + * height: 20, |
| 100 | + * row: 1, |
| 101 | + * col: 1, |
| 102 | + * }); |
| 103 | + * |
| 104 | + * // Do something with the popup window... |
| 105 | + * |
| 106 | + * // Close the popup window manually |
| 107 | + * await popupWindow.close(); |
| 108 | + * } |
| 109 | + * ``` |
| 110 | + * |
| 111 | + * Or with `await using` statement: |
| 112 | + * |
| 113 | + * ```typescript |
| 114 | + * import type { Denops } from "https://deno.land/x/denops_std@$MODULE_VERSION/mod.ts"; |
| 115 | + * import * as popup from "https://deno.land/x/denops_std@$MODULE_VERSION/popup/mod.ts"; |
| 116 | + * |
| 117 | + * export async function main(denops: Denops): Promise<void> { |
| 118 | + * // Open a popup window with `await using` statement |
| 119 | + * await using popupWindow = await popup.open(denops, { |
| 120 | + * relative: "editor", |
| 121 | + * width: 20, |
| 122 | + * height: 20, |
| 123 | + * row: 1, |
| 124 | + * col: 1, |
| 125 | + * }); |
| 126 | + * |
| 127 | + * // Do something with the popup window... |
| 128 | + * |
| 129 | + * // The popup window is automatically closed, due to `await using` statement |
| 130 | + * } |
| 131 | + * ``` |
| 132 | + * |
| 133 | + * Note that this function does NOT work in `batch.collect()`. |
| 134 | + */ |
| 135 | +export async function open( |
| 136 | + denops: Denops, |
| 137 | + options: OpenOptions, |
| 138 | +): Promise<PopupWindow> { |
| 139 | + if (options.title && !options.border) { |
| 140 | + // Vim allows `title` without `border`, but Neovim does not. |
| 141 | + // so we throw an error here to keep consistent behavior. |
| 142 | + throw new Error("title requires border to be set"); |
| 143 | + } |
| 144 | + const open = denops.meta.host === "vim" ? openPopupVim : openPopupNvim; |
| 145 | + const close = denops.meta.host === "vim" ? closePopupVim : closePopupNvim; |
| 146 | + const bufnr = options.bufnr ?? await fn.bufadd(denops, ""); |
| 147 | + const winid = await open(denops, bufnr, options); |
| 148 | + if (!options.noRedraw) { |
| 149 | + await denops.redraw(); |
| 150 | + } |
| 151 | + return { |
| 152 | + bufnr, |
| 153 | + winid, |
| 154 | + close: async () => { |
| 155 | + await close(denops, winid); |
| 156 | + if (!options.noRedraw) { |
| 157 | + await denops.redraw(); |
| 158 | + } |
| 159 | + }, |
| 160 | + [Symbol.asyncDispose]: async () => { |
| 161 | + try { |
| 162 | + await close(denops, winid); |
| 163 | + await denops.redraw(); |
| 164 | + } catch { |
| 165 | + // Fail silently |
| 166 | + } |
| 167 | + }, |
| 168 | + }; |
| 169 | +} |
| 170 | + |
| 171 | +export type { OpenOptions, PopupWindow } from "./types.ts"; |
0 commit comments