|
| 1 | +import { Denops } from "./deps.ts"; |
| 2 | + |
| 3 | +/** |
| 4 | + * Vim variable groups |
| 5 | + * |
| 6 | + * g - Global variables |
| 7 | + * b - Buffer local variables |
| 8 | + * w - Window local variables |
| 9 | + * t - Tab page local variables |
| 10 | + * v - Vim's variables |
| 11 | + * env - Environment variables |
| 12 | + * |
| 13 | + */ |
| 14 | +export type VariableGroups = "g" | "b" | "w" | "t" | "v" | "env"; |
| 15 | + |
| 16 | +export async function getVar<T = unknown>( |
| 17 | + denops: Denops, |
| 18 | + group: VariableGroups, |
| 19 | + prop: string, |
| 20 | +): Promise<T> { |
| 21 | + const name = group === "env" ? `\$${prop}` : `${group}:${prop}`; |
| 22 | + // deno-lint-ignore no-explicit-any |
| 23 | + return (await denops.eval(name)) as any; |
| 24 | +} |
| 25 | + |
| 26 | +export async function setVar<T = unknown>( |
| 27 | + denops: Denops, |
| 28 | + group: VariableGroups, |
| 29 | + prop: string, |
| 30 | + value: T, |
| 31 | +): Promise<void> { |
| 32 | + const name = group === "env" ? `\$${prop}` : `${group}:${prop}`; |
| 33 | + await denops.cmd(`let ${name} = value`, { |
| 34 | + value, |
| 35 | + }); |
| 36 | +} |
| 37 | + |
| 38 | +export async function removeVar( |
| 39 | + denops: Denops, |
| 40 | + group: VariableGroups, |
| 41 | + prop: string, |
| 42 | +): Promise<void> { |
| 43 | + const name = group === "env" ? `\$${prop}` : `${group}:${prop}`; |
| 44 | + await denops.cmd(`unlet ${name}`); |
| 45 | +} |
| 46 | + |
| 47 | +export class VariableHelper { |
| 48 | + #denops: Denops; |
| 49 | + #group: VariableGroups; |
| 50 | + |
| 51 | + constructor(denops: Denops, group: VariableGroups) { |
| 52 | + this.#denops = denops; |
| 53 | + this.#group = group; |
| 54 | + } |
| 55 | + |
| 56 | + async get<T = unknown>(prop: string): Promise<T> { |
| 57 | + return await getVar(this.#denops, this.#group, prop); |
| 58 | + } |
| 59 | + |
| 60 | + async set<T = unknown>(prop: string, value: T): Promise<void> { |
| 61 | + await setVar(this.#denops, this.#group, prop, value); |
| 62 | + } |
| 63 | + |
| 64 | + async remove(prop: string): Promise<void> { |
| 65 | + await removeVar(this.#denops, this.#group, prop); |
| 66 | + } |
| 67 | +} |
0 commit comments