Skip to content

Commit 3f498ce

Browse files
committed
👍 Add interface for option methods
1 parent 4481100 commit 3f498ce

File tree

2 files changed

+129
-4
lines changed

2 files changed

+129
-4
lines changed

denops_std/option/types.ts

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import type { Denops } from "https://deno.land/x/denops_core@v4.0.0/mod.ts";
2+
3+
/**
4+
* An option that can be retrieved and modified.
5+
* @template T The type of the option value.
6+
*/
7+
export interface Option<T> {
8+
/**
9+
* Gets the value of the option.
10+
* @returns A Promise that resolves to the value of the option.
11+
*/
12+
get(denops: Denops): Promise<T>;
13+
14+
/**
15+
* Sets the value of the option.
16+
* @param value The new value of the option.
17+
* @returns A Promise that resolves when the option has been successfully set.
18+
*/
19+
set(denops: Denops, value: T): Promise<void>;
20+
21+
/**
22+
* Resets the value of the option to its default value.
23+
* @returns A Promise that resolves when the option has been successfully reset.
24+
*/
25+
reset(denops: Denops): Promise<void>;
26+
}
27+
28+
/**
29+
* A global option that can be retrieved and modified.
30+
* @template T The type of the option value.
31+
*/
32+
export interface GlobalOption<T> extends Option<T> {
33+
/**
34+
* Gets the global value of the option.
35+
* @returns A Promise that resolves to the value of the option.
36+
*/
37+
getGlobal(denops: Denops): Promise<T>;
38+
39+
/**
40+
* Sets the global value of the option.
41+
* @param value The new value of the option.
42+
* @returns A Promise that resolves when the option has been successfully set.
43+
*/
44+
setGlobal(denops: Denops, value: T): Promise<void>;
45+
46+
/**
47+
* Resets the global value of the option to its default value.
48+
* @returns A Promise that resolves when the option has been successfully reset.
49+
*/
50+
resetGlobal(denops: Denops): Promise<void>;
51+
}
52+
53+
/**
54+
* A local option that can be retrieved and modified.
55+
* @template T The type of the option value.
56+
*/
57+
export interface LocalOption<T> extends Option<T> {
58+
/**
59+
* Gets the local value of the option.
60+
* @returns A Promise that resolves to the value of the option.
61+
*/
62+
getLocal(denops: Denops): Promise<T>;
63+
64+
/**
65+
* Sets the local value of the option.
66+
* @param value The new value of the option.
67+
* @returns A Promise that resolves when the option has been successfully set.
68+
*/
69+
setLocal(denops: Denops, value: T): Promise<void>;
70+
71+
/**
72+
* Resets the local value of the option to its default value.
73+
* @returns A Promise that resolves when the option has been successfully reset.
74+
*/
75+
resetLocal(denops: Denops): Promise<void>;
76+
77+
/**
78+
* Gets the value of the option for the specified buffer.
79+
* @param bufnr The buffer number.
80+
* @returns A Promise that resolves to the value of the option.
81+
*/
82+
getBuffer(denops: Denops, bufnr: number): Promise<T>;
83+
84+
/**
85+
* Sets the value of the option for the specified buffer.
86+
* @param bufnr The buffer number.
87+
* @param value The new value of the option.
88+
* @returns A Promise that resolves when the option has been successfully set.
89+
*/
90+
setBuffer(denops: Denops, bufnr: number, value: T): Promise<void>;
91+
92+
/**
93+
* Gets the value of the option for the specified window.
94+
* @param winnr The window number or `window-ID`.
95+
* @returns A Promise that resolves to the value of the option.
96+
*/
97+
getWindow(denops: Denops, winnr: number): Promise<T>;
98+
99+
/**
100+
* Sets the value of the option for the specified window.
101+
* @param winnr The window number or `window-ID`.
102+
* @param value The new value of the option.
103+
* @returns A Promise that resolves when the option has been successfully set.
104+
*/
105+
setWindow(denops: Denops, winnr: number, value: T): Promise<void>;
106+
}
107+
108+
/**
109+
* A global or local option that can be retrieved and modified.
110+
* @template T The type of the option value.
111+
*/
112+
export type GlobalOrLocalOption<T> = GlobalOption<T> & LocalOption<T>;

scripts/gen-option/format.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Option, OptionType } from "./types.ts";
1+
import type { Option, OptionScope, OptionType } from "./types.ts";
22

33
const denops = "https://deno.land/x/denops_core@v4.0.0/mod.ts";
44

@@ -30,11 +30,12 @@ export function formatDocs(docs: string): string[] {
3030
return ["/**", ...normalizedLines, " */"];
3131
}
3232

33-
function formatOption({ name, type, scope, docs }: Option): string[] {
34-
name = translate[name] ?? name;
33+
function formatOption(option: Option): string[] {
34+
const { type, scope, docs } = option;
35+
const name = translate[option.name] ?? option.name;
3536
const lines = [
3637
...formatDocs(docs),
37-
`export const ${name} = {`,
38+
`export const ${name}: ${getOptionTypeName(scope, type)} = {`,
3839
...formatOptionBody(name, type),
3940
...(scope.includes("global") ? formatGlobalOptionBody(name, type) : []),
4041
...(scope.includes("local") ? formatLocalOptionBody(name, type) : []),
@@ -46,6 +47,16 @@ function formatOption({ name, type, scope, docs }: Option): string[] {
4647
return lines;
4748
}
4849

50+
function getOptionTypeName(scope: OptionScope[], type: OptionType): string {
51+
if (scope.includes("global") && scope.includes("local")) {
52+
return `GlobalOrLocalOption<${type}>`;
53+
} else if (scope.includes("global")) {
54+
return `GlobalOption<${type}>`;
55+
} else {
56+
return `LocalOption<${type}>`;
57+
}
58+
}
59+
4960
function formatOptionBody(name: string, type: OptionType): string[] {
5061
const lines = [
5162
` async get(denops: Denops): Promise<${type}> {`,
@@ -124,11 +135,13 @@ function formatWindowOptionBody(name: string, type: OptionType): string[] {
124135
export function format(options: Option[], root: string): string[] {
125136
const fn = `${root}/../function/mod.ts`;
126137
const variable = `${root}/../variable/mod.ts`;
138+
const types = `${root}/types.ts`;
127139
const lines = [
128140
"// NOTE: This file is generated. Do NOT modify it manually.",
129141
`import type { Denops } from "${denops}";`,
130142
`import { getbufvar, setbufvar, getwinvar, setwinvar } from "${fn}";`,
131143
`import { globalOptions, localOptions, options } from "${variable}";`,
144+
`import type { GlobalOption, GlobalOrLocalOption, LocalOption } from "${types}";`,
132145
"",
133146
...options.map(formatOption),
134147
];

0 commit comments

Comments
 (0)