keybindings.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. import { type KeyId, matchesKey } from "./keys.ts";
  2. /**
  3. * Global keybinding registry.
  4. * Downstream packages can add keybindings via declaration merging.
  5. */
  6. export interface Keybindings {
  7. // Editor navigation and editing
  8. "tui.editor.cursorUp": true;
  9. "tui.editor.cursorDown": true;
  10. "tui.editor.cursorLeft": true;
  11. "tui.editor.cursorRight": true;
  12. "tui.editor.cursorWordLeft": true;
  13. "tui.editor.cursorWordRight": true;
  14. "tui.editor.cursorLineStart": true;
  15. "tui.editor.cursorLineEnd": true;
  16. "tui.editor.jumpForward": true;
  17. "tui.editor.jumpBackward": true;
  18. "tui.editor.pageUp": true;
  19. "tui.editor.pageDown": true;
  20. "tui.editor.deleteCharBackward": true;
  21. "tui.editor.deleteCharForward": true;
  22. "tui.editor.deleteWordBackward": true;
  23. "tui.editor.deleteWordForward": true;
  24. "tui.editor.deleteToLineStart": true;
  25. "tui.editor.deleteToLineEnd": true;
  26. "tui.editor.yank": true;
  27. "tui.editor.yankPop": true;
  28. "tui.editor.undo": true;
  29. // Generic input actions
  30. "tui.input.newLine": true;
  31. "tui.input.submit": true;
  32. "tui.input.tab": true;
  33. "tui.input.copy": true;
  34. // Generic selection actions
  35. "tui.select.up": true;
  36. "tui.select.down": true;
  37. "tui.select.pageUp": true;
  38. "tui.select.pageDown": true;
  39. "tui.select.confirm": true;
  40. "tui.select.cancel": true;
  41. }
  42. export type Keybinding = keyof Keybindings;
  43. export interface KeybindingDefinition {
  44. defaultKeys: KeyId | KeyId[];
  45. description?: string;
  46. }
  47. export type KeybindingDefinitions = Record<string, KeybindingDefinition>;
  48. export type KeybindingsConfig = Record<string, KeyId | KeyId[] | undefined>;
  49. export const TUI_KEYBINDINGS = {
  50. "tui.editor.cursorUp": { defaultKeys: "up", description: "Move cursor up" },
  51. "tui.editor.cursorDown": { defaultKeys: "down", description: "Move cursor down" },
  52. "tui.editor.cursorLeft": {
  53. defaultKeys: ["left", "ctrl+b"],
  54. description: "Move cursor left",
  55. },
  56. "tui.editor.cursorRight": {
  57. defaultKeys: ["right", "ctrl+f"],
  58. description: "Move cursor right",
  59. },
  60. "tui.editor.cursorWordLeft": {
  61. defaultKeys: ["alt+left", "ctrl+left", "alt+b"],
  62. description: "Move cursor word left",
  63. },
  64. "tui.editor.cursorWordRight": {
  65. defaultKeys: ["alt+right", "ctrl+right", "alt+f"],
  66. description: "Move cursor word right",
  67. },
  68. "tui.editor.cursorLineStart": {
  69. defaultKeys: ["home", "ctrl+a"],
  70. description: "Move to line start",
  71. },
  72. "tui.editor.cursorLineEnd": {
  73. defaultKeys: ["end", "ctrl+e"],
  74. description: "Move to line end",
  75. },
  76. "tui.editor.jumpForward": {
  77. defaultKeys: "ctrl+]",
  78. description: "Jump forward to character",
  79. },
  80. "tui.editor.jumpBackward": {
  81. defaultKeys: "ctrl+alt+]",
  82. description: "Jump backward to character",
  83. },
  84. "tui.editor.pageUp": { defaultKeys: "pageUp", description: "Page up" },
  85. "tui.editor.pageDown": { defaultKeys: "pageDown", description: "Page down" },
  86. "tui.editor.deleteCharBackward": {
  87. defaultKeys: "backspace",
  88. description: "Delete character backward",
  89. },
  90. "tui.editor.deleteCharForward": {
  91. defaultKeys: ["delete", "ctrl+d"],
  92. description: "Delete character forward",
  93. },
  94. "tui.editor.deleteWordBackward": {
  95. defaultKeys: ["ctrl+w", "alt+backspace"],
  96. description: "Delete word backward",
  97. },
  98. "tui.editor.deleteWordForward": {
  99. defaultKeys: ["alt+d", "alt+delete"],
  100. description: "Delete word forward",
  101. },
  102. "tui.editor.deleteToLineStart": {
  103. defaultKeys: "ctrl+u",
  104. description: "Delete to line start",
  105. },
  106. "tui.editor.deleteToLineEnd": {
  107. defaultKeys: "ctrl+k",
  108. description: "Delete to line end",
  109. },
  110. "tui.editor.yank": { defaultKeys: "ctrl+y", description: "Yank" },
  111. "tui.editor.yankPop": { defaultKeys: "alt+y", description: "Yank pop" },
  112. "tui.editor.undo": { defaultKeys: "ctrl+-", description: "Undo" },
  113. "tui.input.newLine": { defaultKeys: "shift+enter", description: "Insert newline" },
  114. "tui.input.submit": { defaultKeys: "enter", description: "Submit input" },
  115. "tui.input.tab": { defaultKeys: "tab", description: "Tab / autocomplete" },
  116. "tui.input.copy": { defaultKeys: "ctrl+c", description: "Copy selection" },
  117. "tui.select.up": { defaultKeys: "up", description: "Move selection up" },
  118. "tui.select.down": { defaultKeys: "down", description: "Move selection down" },
  119. "tui.select.pageUp": { defaultKeys: "pageUp", description: "Selection page up" },
  120. "tui.select.pageDown": {
  121. defaultKeys: "pageDown",
  122. description: "Selection page down",
  123. },
  124. "tui.select.confirm": { defaultKeys: "enter", description: "Confirm selection" },
  125. "tui.select.cancel": {
  126. defaultKeys: ["escape", "ctrl+c"],
  127. description: "Cancel selection",
  128. },
  129. } as const satisfies KeybindingDefinitions;
  130. export interface KeybindingConflict {
  131. key: KeyId;
  132. keybindings: string[];
  133. }
  134. function normalizeKeys(keys: KeyId | KeyId[] | undefined): KeyId[] {
  135. if (keys === undefined) return [];
  136. const keyList = Array.isArray(keys) ? keys : [keys];
  137. const seen = new Set<KeyId>();
  138. const result: KeyId[] = [];
  139. for (const key of keyList) {
  140. if (!seen.has(key)) {
  141. seen.add(key);
  142. result.push(key);
  143. }
  144. }
  145. return result;
  146. }
  147. export class KeybindingsManager {
  148. private definitions: KeybindingDefinitions;
  149. private userBindings: KeybindingsConfig;
  150. private keysById = new Map<Keybinding, KeyId[]>();
  151. private conflicts: KeybindingConflict[] = [];
  152. constructor(definitions: KeybindingDefinitions, userBindings: KeybindingsConfig = {}) {
  153. this.definitions = definitions;
  154. this.userBindings = userBindings;
  155. this.rebuild();
  156. }
  157. private rebuild(): void {
  158. this.keysById.clear();
  159. this.conflicts = [];
  160. const userClaims = new Map<KeyId, Set<Keybinding>>();
  161. for (const [keybinding, keys] of Object.entries(this.userBindings)) {
  162. if (!(keybinding in this.definitions)) continue;
  163. for (const key of normalizeKeys(keys)) {
  164. const claimants = userClaims.get(key) ?? new Set<Keybinding>();
  165. claimants.add(keybinding as Keybinding);
  166. userClaims.set(key, claimants);
  167. }
  168. }
  169. for (const [key, keybindings] of userClaims) {
  170. if (keybindings.size > 1) {
  171. this.conflicts.push({ key, keybindings: [...keybindings] });
  172. }
  173. }
  174. for (const [id, definition] of Object.entries(this.definitions)) {
  175. const userKeys = this.userBindings[id];
  176. const keys = userKeys === undefined ? normalizeKeys(definition.defaultKeys) : normalizeKeys(userKeys);
  177. this.keysById.set(id as Keybinding, keys);
  178. }
  179. }
  180. matches(data: string, keybinding: Keybinding): boolean {
  181. const keys = this.keysById.get(keybinding) ?? [];
  182. for (const key of keys) {
  183. if (matchesKey(data, key)) return true;
  184. }
  185. return false;
  186. }
  187. getKeys(keybinding: Keybinding): KeyId[] {
  188. return [...(this.keysById.get(keybinding) ?? [])];
  189. }
  190. getDefinition(keybinding: Keybinding): KeybindingDefinition {
  191. return this.definitions[keybinding];
  192. }
  193. getConflicts(): KeybindingConflict[] {
  194. return this.conflicts.map((conflict) => ({ ...conflict, keybindings: [...conflict.keybindings] }));
  195. }
  196. setUserBindings(userBindings: KeybindingsConfig): void {
  197. this.userBindings = userBindings;
  198. this.rebuild();
  199. }
  200. getUserBindings(): KeybindingsConfig {
  201. return { ...this.userBindings };
  202. }
  203. getResolvedBindings(): KeybindingsConfig {
  204. const resolved: KeybindingsConfig = {};
  205. for (const id of Object.keys(this.definitions)) {
  206. const keys = this.keysById.get(id as Keybinding) ?? [];
  207. resolved[id] = keys.length === 1 ? keys[0]! : [...keys];
  208. }
  209. return resolved;
  210. }
  211. }
  212. let globalKeybindings: KeybindingsManager | null = null;
  213. export function setKeybindings(keybindings: KeybindingsManager): void {
  214. globalKeybindings = keybindings;
  215. }
  216. export function getKeybindings(): KeybindingsManager {
  217. if (!globalKeybindings) {
  218. globalKeybindings = new KeybindingsManager(TUI_KEYBINDINGS);
  219. }
  220. return globalKeybindings;
  221. }