Very Popular
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266import type { Denops } from "https://deno.land/x/denops_core@v4.0.0/mod.ts";import { Getter, Remover, Setter } from "./types.ts";
type VariableGroup = "g" | "b" | "w" | "t" | "v";
async function getVar<T = unknown>( denops: Denops, group: VariableGroup, prop: string, defaultValue?: T,): Promise<T | null> { const name = `${group}:${prop}`; const result = await denops.eval(`exists(n) ? ${name} : v`, { n: name, v: defaultValue ?? null, }); // deno-lint-ignore no-explicit-any return result as any;}
async function setVar<T = unknown>( denops: Denops, group: VariableGroup, prop: string, value: T,): Promise<void> { const name = `${group}:${prop}`; await denops.cmd(`let ${name} = v`, { v: value, });}
async function removeVar( denops: Denops, group: VariableGroup, prop: string,): Promise<void> { const name = `${group}:${prop}`; await denops.cmd(`unlet ${name}`);}
/** * Global variables (`globals` or `g`) * * ```typescript * import { Denops } from "../mod.ts"; * import { globals } from "../variable/mod.ts"; * * export async function main(denops: Denops): Promise<void> { * // Set global variable * await globals.set(denops, "hello", "world"); * * // Get global variable * console.log(await globals.get(denops, "hello")); * * // Remove global variable * await globals.remove(denops, "hello"); * } * ``` */export const globals: Getter & Setter & Remover = { /** * Get variable */ get<T = unknown>( denops: Denops, prop: string, defaultValue?: T, ): Promise<T | null> { return getVar(denops, "g", prop, defaultValue); },
/** * Set variable */ set<T = unknown>(denops: Denops, prop: string, value: T): Promise<void> { return setVar(denops, "g", prop, value); },
/** * Remove variable */ remove(denops: Denops, prop: string): Promise<void> { return removeVar(denops, "g", prop); },};export const g = globals;
/** * Buffer local variables (`buffers` or `b`) * * ```typescript * import { Denops } from "../mod.ts"; * import { buffers } from "../variable/mod.ts"; * * export async function main(denops: Denops): Promise<void> { * // Set buffer variable * await buffers.set(denops, "hello", "world"); * * // Get buffer variable * console.log(await buffers.get(denops, "hello")); * * // Remove buffer variable * await buffers.remove(denops, "hello"); * } * ``` */export const buffers: Getter & Setter & Remover = { /** * Get variable */ get<T = unknown>( denops: Denops, prop: string, defaultValue?: T, ): Promise<T | null> { return getVar(denops, "b", prop, defaultValue); },
/** * Set variable */ set<T = unknown>(denops: Denops, prop: string, value: T): Promise<void> { return setVar(denops, "b", prop, value); },
/** * Remove variable */ remove(denops: Denops, prop: string): Promise<void> { return removeVar(denops, "b", prop); },};export const b = buffers;
/** * Window local variables (`windows` or `w`) * * ```typescript * import { Denops } from "../mod.ts"; * import { windows } from "../variable/mod.ts"; * * export async function main(denops: Denops): Promise<void> { * // Set window variable * await windows.set(denops, "hello", "world"); * * // Get window variable * console.log(await windows.get(denops, "hello")); * * // Remove window variable * await windows.remove(denops, "hello"); * } * ``` */export const windows: Getter & Setter & Remover = { /** * Get variable */ get<T = unknown>( denops: Denops, prop: string, defaultValue?: T, ): Promise<T | null> { return getVar(denops, "w", prop, defaultValue); },
/** * Set variable */ set<T = unknown>(denops: Denops, prop: string, value: T): Promise<void> { return setVar(denops, "w", prop, value); },
/** * Remove variable */ remove(denops: Denops, prop: string): Promise<void> { return removeVar(denops, "w", prop); },};export const w = windows;
/** * Tabpage local variables (`tabpages` or `t`) * * ```typescript * import { Denops } from "../mod.ts"; * import { tabpages } from "../variable/mod.ts"; * * export async function main(denops: Denops): Promise<void> { * // Set tabpage variable * await tabpages.set(denops, "hello", "world"); * * // Get tabpage variable * console.log(await tabpages.get(denops, "hello")); * * // Remove tabpage variable * await tabpages.remove(denops, "hello"); * } * ``` */export const tabpages: Getter & Setter & Remover = { /** * Get variable */ get<T = unknown>( denops: Denops, prop: string, defaultValue?: T, ): Promise<T | null> { return getVar(denops, "t", prop, defaultValue); },
/** * Set variable */ set<T = unknown>(denops: Denops, prop: string, value: T): Promise<void> { return setVar(denops, "t", prop, value); },
/** * Remove variable */ remove(denops: Denops, prop: string): Promise<void> { return removeVar(denops, "t", prop); },};export const t = tabpages;
/** * Vim variables (`vim` or `v`) * * ```typescript * import { Denops } from "../mod.ts"; * import { vim } from "../variable/mod.ts"; * * export async function main(denops: Denops): Promise<void> { * // Set vim variable * await vim.set(denops, "version", "world"); * * // Get vim variable * console.log(await vim.get(denops, "version")); * } * ``` */export const vim: Getter & Setter = { /** * Get variable */ get<T = unknown>( denops: Denops, prop: string, defaultValue?: T, ): Promise<T | null> { return getVar(denops, "v", prop, defaultValue); },
/** * Set variable */ set<T = unknown>(denops: Denops, prop: string, value: T): Promise<void> { return setVar(denops, "v", prop, value); },};export const v = vim;