1
- import path from ' path' ;
2
- import fs from ' fs-extra' ;
3
- const cursorlessRoot = path . resolve ( ' ../../../src' ) ;
1
+ import path from " path" ;
2
+ import fs from " fs-extra" ;
3
+ const cursorlessRoot = path . resolve ( " ../../../src" ) ;
4
4
5
5
const commandDictionaryJson = fs . readJSONSync (
6
6
path . join (
7
7
cursorlessRoot ,
8
- ' ../cursorless-nx/libs/cheatsheet/src/lib/data/sampleSpokenFormInfos/defaults.json'
9
- )
10
- ) as typeof import ( ' ../../../cheatsheet/src/lib/data/sampleSpokenFormInfos/defaults.json' ) ;
8
+ " ../cursorless-nx/libs/cheatsheet/src/lib/data/sampleSpokenFormInfos/defaults.json" ,
9
+ ) ,
10
+ ) as typeof import ( " ../../../cheatsheet/src/lib/data/sampleSpokenFormInfos/defaults.json" ) ;
11
11
12
- const letters = ' abcdefghijklmnopqrstuvwxyz' . split ( '' ) ;
12
+ const letters = " abcdefghijklmnopqrstuvwxyz" . split ( "" ) ;
13
13
const defaultAlphabet =
14
- ' air bat cap drum each fine gust harp sit jury crunch look made near odd pit quench red sun trap urge vest whale plex yank zip'
15
- . split ( ' ' )
14
+ " air bat cap drum each fine gust harp sit jury crunch look made near odd pit quench red sun trap urge vest whale plex yank zip"
15
+ . split ( " " )
16
16
. map ( ( word , index ) => [ letters [ index ] , word ] as const ) ;
17
17
18
- const defaultDigits = ' zero one two three four five six seven eight nine'
19
- . split ( ' ' )
18
+ const defaultDigits = " zero one two three four five six seven eight nine"
19
+ . split ( " " )
20
20
. map ( ( word , index ) => [ `${ index } ` , word ] as const ) ;
21
21
22
22
const letterDictionary = defaultAlphabet
@@ -32,48 +32,48 @@ const commandDictionary = commandDictionaryJson.sections.reduce(
32
32
...r ,
33
33
[ id ] : variations [ 0 ] . spokenForm ,
34
34
} ) ,
35
- { }
35
+ { } ,
36
36
) ,
37
37
} ;
38
38
} ,
39
39
{ } as Record <
40
- typeof commandDictionaryJson [ ' sections' ] [ number ] [ 'id' ] ,
40
+ ( typeof commandDictionaryJson ) [ " sections" ] [ number ] [ "id" ] ,
41
41
Record <
42
- typeof commandDictionaryJson [ ' sections' ] [ number ] [ ' items' ] [ number ] [ 'id' ] ,
43
- typeof commandDictionaryJson [ ' sections' ] [ number ] [ ' items' ] [ number ] [ ' variations' ] [ 0 ] [ ' spokenForm' ]
42
+ ( typeof commandDictionaryJson ) [ " sections" ] [ number ] [ " items" ] [ number ] [ "id" ] ,
43
+ ( typeof commandDictionaryJson ) [ " sections" ] [ number ] [ " items" ] [ number ] [ " variations" ] [ 0 ] [ " spokenForm" ]
44
44
>
45
- >
45
+ > ,
46
46
) ;
47
47
48
48
type CommandDictionary = typeof commandDictionary ;
49
49
50
50
interface Modifier {
51
- type : ' position' | ' containingScope' ;
52
- position : keyof CommandDictionary [ ' positions' ] ;
51
+ type : " position" | " containingScope" ;
52
+ position : keyof CommandDictionary [ " positions" ] ;
53
53
scopeType : {
54
- type : keyof CommandDictionary [ ' scopes' ] ;
54
+ type : keyof CommandDictionary [ " scopes" ] ;
55
55
} ;
56
56
}
57
57
type PrimitiveTarget = {
58
- type : ' primitive' ;
58
+ type : " primitive" ;
59
59
modifiers ?: Modifier [ ] ;
60
- position ?: keyof CommandDictionary [ ' positions' ] ;
60
+ position ?: keyof CommandDictionary [ " positions" ] ;
61
61
mark ?: {
62
- type : ' decoratedSymbol' ;
62
+ type : " decoratedSymbol" ;
63
63
character : keyof typeof letterDictionary ;
64
64
} ;
65
65
} ;
66
66
67
67
type RangeTarget = {
68
- type : ' range' ;
68
+ type : " range" ;
69
69
excludeAnchor ?: boolean ;
70
70
excludeActive ?: boolean ;
71
71
anchor : Target ;
72
72
active : Target ;
73
73
} ;
74
74
75
75
type ListTarget = {
76
- type : ' list' ;
76
+ type : " list" ;
77
77
elements : Target [ ] ;
78
78
} ;
79
79
@@ -82,7 +82,7 @@ type Command = { action: { name: string }; targets: Target[] };
82
82
83
83
function interpolate (
84
84
template : string ,
85
- handleAction : ( counter : number ) => string
85
+ handleAction : ( counter : number ) => string ,
86
86
) {
87
87
let counter = - 1 ;
88
88
return template . replace ( / < [ a - z 0 - 9 ] + > / gi, ( ) => handleAction ( ++ counter ) ) ;
@@ -97,22 +97,24 @@ class SpokenForm {
97
97
98
98
public build ( ) {
99
99
return interpolate ( this . getTemplate ( ) , ( counter ) =>
100
- this . parseTarget ( this . command . targets [ counter ] )
100
+ this . parseTarget ( this . command . targets [ counter ] ) ,
101
101
) ;
102
102
}
103
103
104
104
private getTemplate ( ) {
105
- return commandDictionary [ ' actions' ] [ this . command . action . name ] ;
105
+ return commandDictionary [ " actions" ] [ this . command . action . name ] ;
106
106
}
107
107
108
108
private parseTarget ( target : Target ) : string {
109
- if ( ! target ) throw new Error ( `Excess mark` ) ;
109
+ if ( ! target ) {
110
+ throw new Error ( `Excess mark` ) ;
111
+ }
110
112
switch ( target . type ) {
111
- case ' primitive' :
113
+ case " primitive" :
112
114
return this . parsePrimitiveTarget ( target ) ;
113
- case ' range' :
115
+ case " range" :
114
116
return this . parseRangeTarget ( target ) ;
115
- case ' list' :
117
+ case " list" :
116
118
return this . parseListTarget ( target ) ;
117
119
default : {
118
120
// @ts -expect-error - if this is hit we need to add new cases and types
@@ -126,58 +128,73 @@ class SpokenForm {
126
128
?. filter ( ( v ) : v is Modifier => ! ! v )
127
129
?. map ( ( mod ) => {
128
130
switch ( mod . type ) {
129
- case ' position' :
130
- return commandDictionary [ ' positions' ] [ mod . position ] ;
131
- case ' containingScope' :
132
- return commandDictionary [ ' scopes' ] [ mod . scopeType . type ] ;
131
+ case " position" :
132
+ return commandDictionary [ " positions" ] [ mod . position ] ;
133
+ case " containingScope" :
134
+ return commandDictionary [ " scopes" ] [ mod . scopeType . type ] ;
133
135
}
134
136
throw new Error ( `Unknown modifier type ${ mod . type } ` ) ;
135
137
} )
136
- . join ( ' ' ) || '' ;
138
+ . join ( " " ) || "" ;
137
139
if ( target . position ) {
138
- prefix = `${ commandDictionary [ ' positions' ] [ target . position ] } ${ prefix } ` ;
140
+ prefix = `${ commandDictionary [ " positions" ] [ target . position ] } ${ prefix } ` ;
139
141
}
140
142
if ( target . mark ) {
141
- if ( target . mark . type !== ' decoratedSymbol' )
143
+ if ( target . mark . type !== " decoratedSymbol" ) {
142
144
throw new Error ( `Unknown target type ${ target . mark . type } ` ) ;
145
+ }
143
146
return (
144
- ( prefix ? prefix + ' ' : '' ) + letterDictionary [ target . mark . character ]
147
+ ( prefix ? prefix + " " : "" ) + letterDictionary [ target . mark . character ]
145
148
) ;
146
149
}
147
- if ( ! prefix ) throw new Error ( `Unknown mark` ) ;
150
+ if ( ! prefix ) {
151
+ throw new Error ( `Unknown mark` ) ;
152
+ }
148
153
return prefix ;
149
154
}
150
155
151
156
parseRangeTarget ( target : RangeTarget ) {
152
157
let compoundTargetKey ;
153
- if ( target . excludeAnchor && target . excludeActive )
154
- compoundTargetKey = 'rangeExclusive' ;
155
- else if ( ! target . excludeAnchor && ! target . excludeActive )
156
- compoundTargetKey = 'rangeInclusive' ;
157
- else if ( ! target . excludeAnchor && target . excludeActive )
158
- compoundTargetKey = 'rangeExcludingEnd' ;
159
- else throw new Error ( `Bad inclusion range` ) ;
158
+ if ( target . excludeAnchor && target . excludeActive ) {
159
+ compoundTargetKey = "rangeExclusive" ;
160
+ } else if ( ! target . excludeAnchor && ! target . excludeActive ) {
161
+ compoundTargetKey = "rangeInclusive" ;
162
+ } else if ( ! target . excludeAnchor && target . excludeActive ) {
163
+ compoundTargetKey = "rangeExcludingEnd" ;
164
+ } else {
165
+ throw new Error ( `Bad inclusion range` ) ;
166
+ }
160
167
return interpolate (
161
- commandDictionary [ ' compoundTargets' ] [ compoundTargetKey ] ,
168
+ commandDictionary [ " compoundTargets" ] [ compoundTargetKey ] ,
162
169
( index ) => {
163
- if ( index === 0 ) return this . parseTarget ( target . anchor ) ;
164
- if ( index === 1 ) return this . parseTarget ( target . active ) ;
165
- return '' ;
166
- }
170
+ if ( index === 0 ) {
171
+ return this . parseTarget ( target . anchor ) ;
172
+ }
173
+ if ( index === 1 ) {
174
+ return this . parseTarget ( target . active ) ;
175
+ }
176
+ return "" ;
177
+ } ,
167
178
) ;
168
179
}
169
180
parseListTarget ( target : ListTarget ) {
170
181
return target . elements . reduce ( ( result , element ) => {
171
- if ( ! result ) return this . parseTarget ( element ) ;
182
+ if ( ! result ) {
183
+ return this . parseTarget ( element ) ;
184
+ }
172
185
return interpolate (
173
- commandDictionary [ ' compoundTargets' ] [ ' listConnective' ] ,
186
+ commandDictionary [ " compoundTargets" ] [ " listConnective" ] ,
174
187
( index ) => {
175
- if ( index === 0 ) return result ;
176
- if ( index === 1 ) return this . parseTarget ( element ) ;
188
+ if ( index === 0 ) {
189
+ return result ;
190
+ }
191
+ if ( index === 1 ) {
192
+ return this . parseTarget ( element ) ;
193
+ }
177
194
throw Error ( `Invalid List` ) ;
178
- }
195
+ } ,
179
196
) ;
180
- } , '' ) ;
197
+ } , "" ) ;
181
198
}
182
199
}
183
200
0 commit comments