11import {
22 KeybindingRule ,
3- KeybindingsConfig ,
43 KeybindingsConfigError ,
54 KeybindingShortcut ,
65 KeybindingWhenNode ,
@@ -170,11 +169,11 @@ function encodeWhenAst(node: KeybindingWhenNode): string {
170169}
171170
172171const RawKeybindingsEntries = fromLenientJson ( Schema . Array ( Schema . Unknown ) ) ;
173- const KeybindingsConfigPrettyJson = fromJsonStringPretty ( KeybindingsConfig ) ;
172+ const RawKeybindingsEntriesPrettyJson = fromJsonStringPretty ( Schema . Array ( Schema . Unknown ) ) ;
174173const decodeKeybindingRuleExit = Schema . decodeUnknownExit ( KeybindingRule ) ;
175174const decodeResolvedKeybindingFromConfigExit = Schema . decodeExit ( ResolvedKeybindingFromConfig ) ;
176175const decodeRawKeybindingsEntriesExit = Schema . decodeUnknownExit ( RawKeybindingsEntries ) ;
177- const encodeKeybindingsConfigPrettyJson = Schema . encodeEffect ( KeybindingsConfigPrettyJson ) ;
176+ const encodeRawKeybindingsEntriesPrettyJson = Schema . encodeEffect ( RawKeybindingsEntriesPrettyJson ) ;
178177
179178export interface KeybindingsConfigState {
180179 readonly keybindings : ResolvedKeybindingsConfig ;
@@ -234,11 +233,11 @@ export class Keybindings extends Context.Service<
234233 */
235234 readonly upsertKeybindingRule : (
236235 input : ServerUpsertKeybindingInput ,
237- ) => Effect . Effect < ResolvedKeybindingsConfig , KeybindingsConfigError > ;
236+ ) => Effect . Effect < KeybindingsConfigState , KeybindingsConfigError > ;
238237
239238 readonly removeKeybindingRule : (
240239 input : ServerRemoveKeybindingInput ,
241- ) => Effect . Effect < ResolvedKeybindingsConfig , KeybindingsConfigError > ;
240+ ) => Effect . Effect < KeybindingsConfigState , KeybindingsConfigError > ;
242241 }
243242> ( ) ( "t3/keybindings" ) { }
244243
@@ -278,8 +277,10 @@ const make = Effect.gen(function* () {
278277 ) ,
279278 ) ;
280279
280+ // Entries this build cannot decode, such as a typo or a command from a newer
281+ // build, are written back unchanged so editing another shortcut keeps them.
281282 const loadWritableCustomKeybindingsConfig = Effect . fn ( function * ( ) : Effect . fn . Return <
282- readonly KeybindingRule [ ] ,
283+ ReadonlyArray < { readonly value : unknown ; readonly rule : KeybindingRule | null } > ,
283284 KeybindingsConfigError
284285 > {
285286 if ( ! ( yield * readConfigExists ) ) {
@@ -298,29 +299,16 @@ const make = Effect.gen(function* () {
298299 ) ,
299300 ) ;
300301
301- return yield * Effect . forEach ( rawConfig , ( entry ) =>
302- Effect . gen ( function * ( ) {
303- const decodedRule = decodeKeybindingRuleExit ( entry ) ;
304- if ( decodedRule . _tag === "Failure" ) {
305- yield * Effect . logWarning ( "ignoring invalid keybinding entry" , {
306- path : keybindingsConfigPath ,
307- entry,
308- error : Cause . pretty ( decodedRule . cause ) ,
309- } ) ;
310- return null ;
311- }
312- const resolved = decodeResolvedKeybindingFromConfigExit ( decodedRule . value ) ;
313- if ( resolved . _tag === "Failure" ) {
314- yield * Effect . logWarning ( "ignoring invalid keybinding entry" , {
315- path : keybindingsConfigPath ,
316- entry,
317- error : Cause . pretty ( resolved . cause ) ,
318- } ) ;
319- return null ;
320- }
321- return decodedRule . value ;
322- } ) ,
323- ) . pipe ( Effect . map ( Array . filter ( Predicate . isNotNull ) ) ) ;
302+ return rawConfig . map ( ( entry ) => {
303+ const decodedRule = decodeKeybindingRuleExit ( entry ) ;
304+ if (
305+ decodedRule . _tag === "Success" &&
306+ decodeResolvedKeybindingFromConfigExit ( decodedRule . value ) . _tag === "Success"
307+ ) {
308+ return { value : decodedRule . value , rule : decodedRule . value } ;
309+ }
310+ return { value : entry , rule : null } ;
311+ } ) ;
324312 } ) ;
325313
326314 const loadRuntimeCustomKeybindingsConfig = Effect . fn ( function * ( ) : Effect . fn . Return <
@@ -403,8 +391,8 @@ const make = Effect.gen(function* () {
403391 ) ,
404392 ) ;
405393
406- const writeConfigAtomically = ( rules : readonly KeybindingRule [ ] ) => {
407- return encodeKeybindingsConfigPrettyJson ( rules ) . pipe (
394+ const writeConfigAtomically = ( entries : ReadonlyArray < unknown > ) => {
395+ return encodeRawKeybindingsEntriesPrettyJson ( entries ) . pipe (
408396 Effect . map ( ( encoded ) => `${ encoded } \n` ) ,
409397 Effect . flatMap ( ( encoded ) =>
410398 writeFileStringAtomically ( {
@@ -444,13 +432,14 @@ const make = Effect.gen(function* () {
444432
445433 const loadConfigStateFromCacheOrDisk = Cache . get ( resolvedConfigCache , resolvedConfigCacheKey ) ;
446434
447- const revalidateAndEmit = upsertSemaphore . withPermits ( 1 ) (
448- Effect . gen ( function * ( ) {
449- yield * Cache . invalidate ( resolvedConfigCache , resolvedConfigCacheKey ) ;
450- const configState = yield * loadConfigStateFromCacheOrDisk ;
451- yield * emitChange ( configState ) ;
452- } ) ,
453- ) ;
435+ const reloadAndEmit = Effect . gen ( function * ( ) {
436+ yield * Cache . invalidate ( resolvedConfigCache , resolvedConfigCacheKey ) ;
437+ const configState = yield * loadConfigStateFromCacheOrDisk ;
438+ yield * emitChange ( configState ) ;
439+ return configState ;
440+ } ) ;
441+
442+ const revalidateAndEmit = upsertSemaphore . withPermits ( 1 ) ( reloadAndEmit ) ;
454443
455444 const syncDefaultKeybindingsOnStartup = upsertSemaphore . withPermits ( 1 ) (
456445 Effect . gen ( function * ( ) {
@@ -648,15 +637,16 @@ const make = Effect.gen(function* () {
648637 const rule = keybindingRuleFromInput ( input ) ;
649638 const replaceTarget = replaceTargetFromUpsertInput ( input ) ;
650639 const nextConfig = [
651- ...customConfig . filter ( ( entry ) => {
640+ ...customConfig . filter ( ( { rule : entry } ) => {
641+ if ( ! entry ) return true ;
652642 if ( replaceTarget ) {
653643 return (
654644 ! isSameKeybindingRule ( entry , replaceTarget ) && ! isSameKeybindingRule ( entry , rule )
655645 ) ;
656646 }
657647 return ! isSameKeybindingRule ( entry , rule ) ;
658648 } ) ,
659- rule ,
649+ { value : rule , rule } ,
660650 ] ;
661651 const cappedConfig =
662652 nextConfig . length > MAX_KEYBINDINGS_COUNT
@@ -668,40 +658,20 @@ const make = Effect.gen(function* () {
668658 maxEntries : MAX_KEYBINDINGS_COUNT ,
669659 } ) ;
670660 }
671- yield * writeConfigAtomically ( cappedConfig ) ;
672- const nextResolved = mergeWithDefaultKeybindings (
673- compileResolvedKeybindingsConfig ( cappedConfig ) ,
674- ) ;
675- yield * Cache . set ( resolvedConfigCache , resolvedConfigCacheKey , {
676- keybindings : nextResolved ,
677- issues : [ ] ,
678- } ) ;
679- yield * emitChange ( {
680- keybindings : nextResolved ,
681- issues : [ ] ,
682- } ) ;
683- return nextResolved ;
661+ yield * writeConfigAtomically ( cappedConfig . map ( ( { value } ) => value ) ) ;
662+ return yield * reloadAndEmit ;
684663 } ) ,
685664 ) ,
686665 removeKeybindingRule : ( input ) =>
687666 upsertSemaphore . withPermits ( 1 ) (
688667 Effect . gen ( function * ( ) {
689668 const customConfig = yield * loadWritableCustomKeybindingsConfig ( ) ;
690669 const target = keybindingRuleFromInput ( input ) ;
691- const nextConfig = customConfig . filter ( ( entry ) => ! isSameKeybindingRule ( entry , target ) ) ;
692- yield * writeConfigAtomically ( nextConfig ) ;
693- const nextResolved = mergeWithDefaultKeybindings (
694- compileResolvedKeybindingsConfig ( nextConfig ) ,
670+ const nextConfig = customConfig . filter (
671+ ( { rule } ) => ! rule || ! isSameKeybindingRule ( rule , target ) ,
695672 ) ;
696- yield * Cache . set ( resolvedConfigCache , resolvedConfigCacheKey , {
697- keybindings : nextResolved ,
698- issues : [ ] ,
699- } ) ;
700- yield * emitChange ( {
701- keybindings : nextResolved ,
702- issues : [ ] ,
703- } ) ;
704- return nextResolved ;
673+ yield * writeConfigAtomically ( nextConfig . map ( ( { value } ) => value ) ) ;
674+ return yield * reloadAndEmit ;
705675 } ) ,
706676 ) ,
707677 } satisfies Keybindings [ "Service" ] ;
0 commit comments