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" ) ;
44
55const commandDictionaryJson = fs . readJSONSync (
66 path . join (
77 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" ) ;
1111
12- const letters = ' abcdefghijklmnopqrstuvwxyz' . split ( '' ) ;
12+ const letters = " abcdefghijklmnopqrstuvwxyz" . split ( "" ) ;
1313const 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 ( " " )
1616 . map ( ( word , index ) => [ letters [ index ] , word ] as const ) ;
1717
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 ( " " )
2020 . map ( ( word , index ) => [ `${ index } ` , word ] as const ) ;
2121
2222const letterDictionary = defaultAlphabet
@@ -32,48 +32,48 @@ const commandDictionary = commandDictionaryJson.sections.reduce(
3232 ...r ,
3333 [ id ] : variations [ 0 ] . spokenForm ,
3434 } ) ,
35- { }
35+ { } ,
3636 ) ,
3737 } ;
3838 } ,
3939 { } as Record <
40- typeof commandDictionaryJson [ ' sections' ] [ number ] [ 'id' ] ,
40+ ( typeof commandDictionaryJson ) [ " sections" ] [ number ] [ "id" ] ,
4141 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" ]
4444 >
45- >
45+ > ,
4646) ;
4747
4848type CommandDictionary = typeof commandDictionary ;
4949
5050interface Modifier {
51- type : ' position' | ' containingScope' ;
52- position : keyof CommandDictionary [ ' positions' ] ;
51+ type : " position" | " containingScope" ;
52+ position : keyof CommandDictionary [ " positions" ] ;
5353 scopeType : {
54- type : keyof CommandDictionary [ ' scopes' ] ;
54+ type : keyof CommandDictionary [ " scopes" ] ;
5555 } ;
5656}
5757type PrimitiveTarget = {
58- type : ' primitive' ;
58+ type : " primitive" ;
5959 modifiers ?: Modifier [ ] ;
60- position ?: keyof CommandDictionary [ ' positions' ] ;
60+ position ?: keyof CommandDictionary [ " positions" ] ;
6161 mark ?: {
62- type : ' decoratedSymbol' ;
62+ type : " decoratedSymbol" ;
6363 character : keyof typeof letterDictionary ;
6464 } ;
6565} ;
6666
6767type RangeTarget = {
68- type : ' range' ;
68+ type : " range" ;
6969 excludeAnchor ?: boolean ;
7070 excludeActive ?: boolean ;
7171 anchor : Target ;
7272 active : Target ;
7373} ;
7474
7575type ListTarget = {
76- type : ' list' ;
76+ type : " list" ;
7777 elements : Target [ ] ;
7878} ;
7979
@@ -82,7 +82,7 @@ type Command = { action: { name: string }; targets: Target[] };
8282
8383function interpolate (
8484 template : string ,
85- handleAction : ( counter : number ) => string
85+ handleAction : ( counter : number ) => string ,
8686) {
8787 let counter = - 1 ;
8888 return template . replace ( / < [ a - z 0 - 9 ] + > / gi, ( ) => handleAction ( ++ counter ) ) ;
@@ -97,22 +97,24 @@ class SpokenForm {
9797
9898 public build ( ) {
9999 return interpolate ( this . getTemplate ( ) , ( counter ) =>
100- this . parseTarget ( this . command . targets [ counter ] )
100+ this . parseTarget ( this . command . targets [ counter ] ) ,
101101 ) ;
102102 }
103103
104104 private getTemplate ( ) {
105- return commandDictionary [ ' actions' ] [ this . command . action . name ] ;
105+ return commandDictionary [ " actions" ] [ this . command . action . name ] ;
106106 }
107107
108108 private parseTarget ( target : Target ) : string {
109- if ( ! target ) throw new Error ( `Excess mark` ) ;
109+ if ( ! target ) {
110+ throw new Error ( `Excess mark` ) ;
111+ }
110112 switch ( target . type ) {
111- case ' primitive' :
113+ case " primitive" :
112114 return this . parsePrimitiveTarget ( target ) ;
113- case ' range' :
115+ case " range" :
114116 return this . parseRangeTarget ( target ) ;
115- case ' list' :
117+ case " list" :
116118 return this . parseListTarget ( target ) ;
117119 default : {
118120 // @ts -expect-error - if this is hit we need to add new cases and types
@@ -126,58 +128,73 @@ class SpokenForm {
126128 ?. filter ( ( v ) : v is Modifier => ! ! v )
127129 ?. map ( ( mod ) => {
128130 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 ] ;
133135 }
134136 throw new Error ( `Unknown modifier type ${ mod . type } ` ) ;
135137 } )
136- . join ( ' ' ) || '' ;
138+ . join ( " " ) || "" ;
137139 if ( target . position ) {
138- prefix = `${ commandDictionary [ ' positions' ] [ target . position ] } ${ prefix } ` ;
140+ prefix = `${ commandDictionary [ " positions" ] [ target . position ] } ${ prefix } ` ;
139141 }
140142 if ( target . mark ) {
141- if ( target . mark . type !== ' decoratedSymbol' )
143+ if ( target . mark . type !== " decoratedSymbol" ) {
142144 throw new Error ( `Unknown target type ${ target . mark . type } ` ) ;
145+ }
143146 return (
144- ( prefix ? prefix + ' ' : '' ) + letterDictionary [ target . mark . character ]
147+ ( prefix ? prefix + " " : "" ) + letterDictionary [ target . mark . character ]
145148 ) ;
146149 }
147- if ( ! prefix ) throw new Error ( `Unknown mark` ) ;
150+ if ( ! prefix ) {
151+ throw new Error ( `Unknown mark` ) ;
152+ }
148153 return prefix ;
149154 }
150155
151156 parseRangeTarget ( target : RangeTarget ) {
152157 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+ }
160167 return interpolate (
161- commandDictionary [ ' compoundTargets' ] [ compoundTargetKey ] ,
168+ commandDictionary [ " compoundTargets" ] [ compoundTargetKey ] ,
162169 ( 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+ } ,
167178 ) ;
168179 }
169180 parseListTarget ( target : ListTarget ) {
170181 return target . elements . reduce ( ( result , element ) => {
171- if ( ! result ) return this . parseTarget ( element ) ;
182+ if ( ! result ) {
183+ return this . parseTarget ( element ) ;
184+ }
172185 return interpolate (
173- commandDictionary [ ' compoundTargets' ] [ ' listConnective' ] ,
186+ commandDictionary [ " compoundTargets" ] [ " listConnective" ] ,
174187 ( 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+ }
177194 throw Error ( `Invalid List` ) ;
178- }
195+ } ,
179196 ) ;
180- } , '' ) ;
197+ } , "" ) ;
181198 }
182199}
183200
0 commit comments