forked from ultraworkers/claw-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresolver.ts
More file actions
244 lines (219 loc) · 6.92 KB
/
Copy pathresolver.ts
File metadata and controls
244 lines (219 loc) · 6.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import type { Key } from '../ink.js'
import { getKeyName, matchesBinding } from './match.js'
import { chordToString } from './parser.js'
import type {
KeybindingContextName,
ParsedBinding,
ParsedKeystroke,
} from './types.js'
export type ResolveResult =
| { type: 'match'; action: string }
| { type: 'none' }
| { type: 'unbound' }
export type ChordResolveResult =
| { type: 'match'; action: string }
| { type: 'none' }
| { type: 'unbound' }
| { type: 'chord_started'; pending: ParsedKeystroke[] }
| { type: 'chord_cancelled' }
/**
* Resolve a key input to an action.
* Pure function - no state, no side effects, just matching logic.
*
* @param input - The character input from Ink
* @param key - The Key object from Ink with modifier flags
* @param activeContexts - Array of currently active contexts (e.g., ['Chat', 'Global'])
* @param bindings - All parsed bindings to search through
* @returns The resolution result
*/
export function resolveKey(
input: string,
key: Key,
activeContexts: KeybindingContextName[],
bindings: ParsedBinding[],
): ResolveResult {
// Find matching bindings (last one wins for user overrides)
let match: ParsedBinding | undefined
const ctxSet = new Set(activeContexts)
for (const binding of bindings) {
// Phase 1: Only single-keystroke bindings
if (binding.chord.length !== 1) continue
if (!ctxSet.has(binding.context)) continue
if (matchesBinding(input, key, binding)) {
match = binding
}
}
if (!match) {
return { type: 'none' }
}
if (match.action === null) {
return { type: 'unbound' }
}
return { type: 'match', action: match.action }
}
/**
* Get display text for an action from bindings (e.g., "ctrl+t" for "app:toggleTodos").
* Searches in reverse order so user overrides take precedence.
*/
export function getBindingDisplayText(
action: string,
context: KeybindingContextName,
bindings: ParsedBinding[],
): string | undefined {
// Find the last binding for this action in this context
const binding = bindings.findLast(
b => b.action === action && b.context === context,
)
return binding ? chordToString(binding.chord) : undefined
}
/**
* Build a ParsedKeystroke from Ink's input/key.
*/
function buildKeystroke(input: string, key: Key): ParsedKeystroke | null {
const keyName = getKeyName(input, key)
if (!keyName) return null
// QUIRK: Ink sets key.meta=true when escape is pressed (see input-event.ts).
// This is legacy terminal behavior - we should NOT record this as a modifier
// for the escape key itself, otherwise chord matching will fail.
const effectiveMeta = key.escape ? false : key.meta
return {
key: keyName,
ctrl: key.ctrl,
alt: effectiveMeta,
shift: key.shift,
meta: effectiveMeta,
super: key.super,
}
}
/**
* Compare two ParsedKeystrokes for equality. Collapses alt/meta into
* one logical modifier — legacy terminals can't distinguish them (see
* match.ts modifiersMatch), so "alt+k" and "meta+k" are the same key.
* Super (cmd/win) is distinct — only arrives via kitty keyboard protocol.
*/
export function keystrokesEqual(
a: ParsedKeystroke,
b: ParsedKeystroke,
): boolean {
return (
a.key === b.key &&
a.ctrl === b.ctrl &&
a.shift === b.shift &&
(a.alt || a.meta) === (b.alt || b.meta) &&
a.super === b.super
)
}
/**
* Check if a chord prefix matches the beginning of a binding's chord.
*/
function chordPrefixMatches(
prefix: ParsedKeystroke[],
binding: ParsedBinding,
): boolean {
if (prefix.length >= binding.chord.length) return false
for (let i = 0; i < prefix.length; i++) {
const prefixKey = prefix[i]
const bindingKey = binding.chord[i]
if (!prefixKey || !bindingKey) return false
if (!keystrokesEqual(prefixKey, bindingKey)) return false
}
return true
}
/**
* Check if a full chord matches a binding's chord.
*/
function chordExactlyMatches(
chord: ParsedKeystroke[],
binding: ParsedBinding,
): boolean {
if (chord.length !== binding.chord.length) return false
for (let i = 0; i < chord.length; i++) {
const chordKey = chord[i]
const bindingKey = binding.chord[i]
if (!chordKey || !bindingKey) return false
if (!keystrokesEqual(chordKey, bindingKey)) return false
}
return true
}
/**
* Resolve a key with chord state support.
*
* This function handles multi-keystroke chord bindings like "ctrl+k ctrl+s".
*
* @param input - The character input from Ink
* @param key - The Key object from Ink with modifier flags
* @param activeContexts - Array of currently active contexts
* @param bindings - All parsed bindings
* @param pending - Current chord state (null if not in a chord)
* @returns Resolution result with chord state
*/
export function resolveKeyWithChordState(
input: string,
key: Key,
activeContexts: KeybindingContextName[],
bindings: ParsedBinding[],
pending: ParsedKeystroke[] | null,
): ChordResolveResult {
// Cancel chord on escape
if (key.escape && pending !== null) {
return { type: 'chord_cancelled' }
}
// Build current keystroke
const currentKeystroke = buildKeystroke(input, key)
if (!currentKeystroke) {
if (pending !== null) {
return { type: 'chord_cancelled' }
}
return { type: 'none' }
}
// Build the full chord sequence to test
const testChord = pending
? [...pending, currentKeystroke]
: [currentKeystroke]
// Filter bindings by active contexts (Set lookup: O(n) instead of O(n·m))
const ctxSet = new Set(activeContexts)
const contextBindings = bindings.filter(b => ctxSet.has(b.context))
// Check if this could be a prefix for longer chords. Group by chord
// string so a later null-override shadows the default it unbinds —
// otherwise null-unbinding `ctrl+x ctrl+k` still makes `ctrl+x` enter
// chord-wait and the single-key binding on the prefix never fires.
const chordWinners = new Map<string, string | null>()
for (const binding of contextBindings) {
if (
binding.chord.length > testChord.length &&
chordPrefixMatches(testChord, binding)
) {
chordWinners.set(chordToString(binding.chord), binding.action)
}
}
let hasLongerChords = false
for (const action of chordWinners.values()) {
if (action !== null) {
hasLongerChords = true
break
}
}
// If this keystroke could start a longer chord, prefer that
// (even if there's an exact single-key match)
if (hasLongerChords) {
return { type: 'chord_started', pending: testChord }
}
// Check for exact matches (last one wins)
let exactMatch: ParsedBinding | undefined
for (const binding of contextBindings) {
if (chordExactlyMatches(testChord, binding)) {
exactMatch = binding
}
}
if (exactMatch) {
if (exactMatch.action === null) {
return { type: 'unbound' }
}
return { type: 'match', action: exactMatch.action }
}
// No match and no potential longer chords
if (pending !== null) {
return { type: 'chord_cancelled' }
}
return { type: 'none' }
}