Skip to content
Merged
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
43 changes: 43 additions & 0 deletions popup/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,49 @@ export async function open(
};
}

/**
* Close a popup window by its window ID.
*
* This function closes a popup window in both Vim and Neovim using the
* appropriate platform-specific method. After closing, it automatically
* triggers a redraw to ensure the UI is updated.
*
* @param denops - The Denops instance
* @param winid - The window ID of the popup window to close
*
* @example
* ```typescript
* import type { Entrypoint } from "jsr:@denops/std";
* import * as popup from "jsr:@denops/std/popup";
*
* export const main: Entrypoint = async (denops) => {
* // Open a popup window
* const popupWindow = await popup.open(denops, {
* relative: "editor",
* width: 20,
* height: 20,
* row: 1,
* col: 1,
* });
*
* // Do something with the popup window...
*
* // Close the popup window using the standalone close function
* await popup.close(denops, popupWindow.winid);
* }
* ```
*
* Note that this function does NOT work in `batch.collect()`.
*/
export async function close(
denops: Denops,
winid: number,
): Promise<void> {
const close = denops.meta.host === "vim" ? closePopupVim : closePopupNvim;
await close(denops, winid);
await denops.redraw();
}

/**
* Config a popup window in Vim/Neovim compatible way.
*
Expand Down
23 changes: 23 additions & 0 deletions popup/mod_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,28 @@ test({
},
});
}

await t.step({
name: `close() closes a popup window by window ID`,
fn: async () => {
const popupWindow = await popup.open(denops, {
relative: "editor",
width: 30,
height: 30,
row: 10,
col: 10,
});
const { winid } = popupWindow;

// Verify popup is open
assertEquals(await fn.win_gettype(denops, winid), "popup");

// Close using standalone close() function
await popup.close(denops, winid);

// Verify popup is closed
assertEquals(await fn.win_gettype(denops, winid), "unknown");
},
});
},
});