@@ -24,6 +24,22 @@ function defaultValue(type: OptionType): string {
24
24
}
25
25
}
26
26
27
+ function coerceValue ( expr : string , type : OptionType ) : string {
28
+ switch ( type ) {
29
+ case "string" :
30
+ return `(${ expr } ) as string` ;
31
+ case "number" :
32
+ return `(${ expr } ) as number` ;
33
+ case "boolean" :
34
+ // Vim returns (0 | 1) so coerce to boolean.
35
+ return `Boolean(${ expr } )` ;
36
+ default : {
37
+ const unknownType : never = type ;
38
+ throw new Error ( `Unknown type ${ unknownType } ` ) ;
39
+ }
40
+ }
41
+ }
42
+
27
43
export function formatDocs ( docs : string ) : string [ ] {
28
44
const lines = docs . replaceAll ( / \* \/ / g, "* /" ) . split ( "\n" ) ;
29
45
const normalizedLines = lines . map ( ( v ) => ` * ${ v } ` . trimEnd ( ) ) ;
@@ -49,7 +65,8 @@ function formatOption({ name, type, scope, docs }: Option): string[] {
49
65
function formatOptionBody ( name : string , type : OptionType ) : string [ ] {
50
66
const lines = [
51
67
` async get(denops: Denops): Promise<${ type } > {` ,
52
- ` return await options.get(denops, "${ name } ") ?? ${ defaultValue ( type ) } ;` ,
68
+ ` const result = await options.get(denops, "${ name } ");` ,
69
+ ` return ${ coerceValue ( `result ?? ${ defaultValue ( type ) } ` , type ) } ;` ,
53
70
` },` ,
54
71
` set(denops: Denops, value: ${ type } ): Promise<void> {` ,
55
72
` return options.set(denops, "${ name } ", value);` ,
@@ -64,9 +81,8 @@ function formatOptionBody(name: string, type: OptionType): string[] {
64
81
function formatGlobalOptionBody ( name : string , type : OptionType ) : string [ ] {
65
82
const lines = [
66
83
` async getGlobal(denops: Denops): Promise<${ type } > {` ,
67
- ` return await globalOptions.get(denops, "${ name } ") ?? ${
68
- defaultValue ( type )
69
- } ;`,
84
+ ` const result = await globalOptions.get(denops, "${ name } ");` ,
85
+ ` return ${ coerceValue ( `result ?? ${ defaultValue ( type ) } ` , type ) } ;` ,
70
86
` },` ,
71
87
` setGlobal(denops: Denops, value: ${ type } ): Promise<void> {` ,
72
88
` return globalOptions.set(denops, "${ name } ", value);` ,
@@ -81,9 +97,8 @@ function formatGlobalOptionBody(name: string, type: OptionType): string[] {
81
97
function formatLocalOptionBody ( name : string , type : OptionType ) : string [ ] {
82
98
const lines = [
83
99
` async getLocal(denops: Denops): Promise<${ type } > {` ,
84
- ` return await localOptions.get(denops, "${ name } ") ?? ${
85
- defaultValue ( type )
86
- } ;`,
100
+ ` const result = await localOptions.get(denops, "${ name } ");` ,
101
+ ` return ${ coerceValue ( `result ?? ${ defaultValue ( type ) } ` , type ) } ;` ,
87
102
` },` ,
88
103
` setLocal(denops: Denops, value: ${ type } ): Promise<void> {` ,
89
104
` return localOptions.set(denops, "${ name } ", value);` ,
@@ -99,7 +114,7 @@ function formatBufferOptionBody(name: string, type: OptionType): string[] {
99
114
const lines = [
100
115
` async getBuffer(denops: Denops, bufnr: number): Promise<${ type } > {` ,
101
116
` const result = await getbufvar(denops, bufnr, "&${ name } ");` ,
102
- ` return ( result as ${ type } ) ?? ${ defaultValue ( type ) } ;` ,
117
+ ` return ${ coerceValue ( ` result ?? ${ defaultValue ( type ) } ` , type ) } ;` ,
103
118
` },` ,
104
119
` setBuffer(denops: Denops, bufnr: number, value: ${ type } ): Promise<void> {` ,
105
120
` return setbufvar(denops, bufnr, "&${ name } ", value);` ,
@@ -112,7 +127,7 @@ function formatWindowOptionBody(name: string, type: OptionType): string[] {
112
127
const lines = [
113
128
` async getWindow(denops: Denops, winnr: number): Promise<${ type } > {` ,
114
129
` const result = await getwinvar(denops, winnr, "&${ name } ");` ,
115
- ` return ( result as ${ type } ) ?? ${ defaultValue ( type ) } ;` ,
130
+ ` return ${ coerceValue ( ` result ?? ${ defaultValue ( type ) } ` , type ) } ;` ,
116
131
` },` ,
117
132
` setWindow(denops: Denops, winnr: number, value: ${ type } ): Promise<void> {` ,
118
133
` return setwinvar(denops, winnr, "&${ name } ", value);` ,
0 commit comments