8ede9eddb97173bccfd82b873577cb302fd7c0ad05b140a176317d559766a7ac141823c836d25350f9e17c53ab906cfb0752723197adf810614a8ae49cbf81 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. import { implies, expressionsAreEqualWithConstantSubstitution } from '../../contextkey/common/contextkey.js';
  6. export class KeybindingResolver {
  7. constructor(defaultKeybindings, overrides, log) {
  8. this._log = log;
  9. this._defaultKeybindings = defaultKeybindings;
  10. this._defaultBoundCommands = new Map();
  11. for (const defaultKeybinding of defaultKeybindings) {
  12. const command = defaultKeybinding.command;
  13. if (command && command.charAt(0) !== '-') {
  14. this._defaultBoundCommands.set(command, true);
  15. }
  16. }
  17. this._map = new Map();
  18. this._lookupMap = new Map();
  19. this._keybindings = KeybindingResolver.handleRemovals([].concat(defaultKeybindings).concat(overrides));
  20. for (let i = 0, len = this._keybindings.length; i < len; i++) {
  21. const k = this._keybindings[i];
  22. if (k.keypressParts.length === 0) {
  23. // unbound
  24. continue;
  25. }
  26. if (k.when && k.when.type === 0 /* ContextKeyExprType.False */) {
  27. // when condition is false
  28. continue;
  29. }
  30. // TODO@chords
  31. this._addKeyPress(k.keypressParts[0], k);
  32. }
  33. }
  34. static _isTargetedForRemoval(defaultKb, keypressFirstPart, keypressChordPart, when) {
  35. // TODO@chords
  36. if (keypressFirstPart && defaultKb.keypressParts[0] !== keypressFirstPart) {
  37. return false;
  38. }
  39. // TODO@chords
  40. if (keypressChordPart && defaultKb.keypressParts[1] !== keypressChordPart) {
  41. return false;
  42. }
  43. if (when) {
  44. if (!defaultKb.when) {
  45. return false;
  46. }
  47. if (!expressionsAreEqualWithConstantSubstitution(when, defaultKb.when)) {
  48. return false;
  49. }
  50. }
  51. return true;
  52. }
  53. /**
  54. * Looks for rules containing "-commandId" and removes them.
  55. */
  56. static handleRemovals(rules) {
  57. // Do a first pass and construct a hash-map for removals
  58. const removals = new Map();
  59. for (let i = 0, len = rules.length; i < len; i++) {
  60. const rule = rules[i];
  61. if (rule.command && rule.command.charAt(0) === '-') {
  62. const command = rule.command.substring(1);
  63. if (!removals.has(command)) {
  64. removals.set(command, [rule]);
  65. }
  66. else {
  67. removals.get(command).push(rule);
  68. }
  69. }
  70. }
  71. if (removals.size === 0) {
  72. // There are no removals
  73. return rules;
  74. }
  75. // Do a second pass and keep only non-removed keybindings
  76. const result = [];
  77. for (let i = 0, len = rules.length; i < len; i++) {
  78. const rule = rules[i];
  79. if (!rule.command || rule.command.length === 0) {
  80. result.push(rule);
  81. continue;
  82. }
  83. if (rule.command.charAt(0) === '-') {
  84. continue;
  85. }
  86. const commandRemovals = removals.get(rule.command);
  87. if (!commandRemovals || !rule.isDefault) {
  88. result.push(rule);
  89. continue;
  90. }
  91. let isRemoved = false;
  92. for (const commandRemoval of commandRemovals) {
  93. // TODO@chords
  94. const keypressFirstPart = commandRemoval.keypressParts[0];
  95. const keypressChordPart = commandRemoval.keypressParts[1];
  96. const when = commandRemoval.when;
  97. if (this._isTargetedForRemoval(rule, keypressFirstPart, keypressChordPart, when)) {
  98. isRemoved = true;
  99. break;
  100. }
  101. }
  102. if (!isRemoved) {
  103. result.push(rule);
  104. continue;
  105. }
  106. }
  107. return result;
  108. }
  109. _addKeyPress(keypress, item) {
  110. const conflicts = this._map.get(keypress);
  111. if (typeof conflicts === 'undefined') {
  112. // There is no conflict so far
  113. this._map.set(keypress, [item]);
  114. this._addToLookupMap(item);
  115. return;
  116. }
  117. for (let i = conflicts.length - 1; i >= 0; i--) {
  118. const conflict = conflicts[i];
  119. if (conflict.command === item.command) {
  120. continue;
  121. }
  122. const conflictIsChord = (conflict.keypressParts.length > 1);
  123. const itemIsChord = (item.keypressParts.length > 1);
  124. // TODO@chords
  125. if (conflictIsChord && itemIsChord && conflict.keypressParts[1] !== item.keypressParts[1]) {
  126. // The conflict only shares the chord start with this command
  127. continue;
  128. }
  129. if (KeybindingResolver.whenIsEntirelyIncluded(conflict.when, item.when)) {
  130. // `item` completely overwrites `conflict`
  131. // Remove conflict from the lookupMap
  132. this._removeFromLookupMap(conflict);
  133. }
  134. }
  135. conflicts.push(item);
  136. this._addToLookupMap(item);
  137. }
  138. _addToLookupMap(item) {
  139. if (!item.command) {
  140. return;
  141. }
  142. let arr = this._lookupMap.get(item.command);
  143. if (typeof arr === 'undefined') {
  144. arr = [item];
  145. this._lookupMap.set(item.command, arr);
  146. }
  147. else {
  148. arr.push(item);
  149. }
  150. }
  151. _removeFromLookupMap(item) {
  152. if (!item.command) {
  153. return;
  154. }
  155. const arr = this._lookupMap.get(item.command);
  156. if (typeof arr === 'undefined') {
  157. return;
  158. }
  159. for (let i = 0, len = arr.length; i < len; i++) {
  160. if (arr[i] === item) {
  161. arr.splice(i, 1);
  162. return;
  163. }
  164. }
  165. }
  166. /**
  167. * Returns true if it is provable `a` implies `b`.
  168. */
  169. static whenIsEntirelyIncluded(a, b) {
  170. if (!b || b.type === 1 /* ContextKeyExprType.True */) {
  171. return true;
  172. }
  173. if (!a || a.type === 1 /* ContextKeyExprType.True */) {
  174. return false;
  175. }
  176. return implies(a, b);
  177. }
  178. getKeybindings() {
  179. return this._keybindings;
  180. }
  181. lookupPrimaryKeybinding(commandId, context) {
  182. const items = this._lookupMap.get(commandId);
  183. if (typeof items === 'undefined' || items.length === 0) {
  184. return null;
  185. }
  186. if (items.length === 1) {
  187. return items[0];
  188. }
  189. for (let i = items.length - 1; i >= 0; i--) {
  190. const item = items[i];
  191. if (context.contextMatchesRules(item.when)) {
  192. return item;
  193. }
  194. }
  195. return items[items.length - 1];
  196. }
  197. resolve(context, currentChord, keypress) {
  198. this._log(`| Resolving ${keypress}${currentChord ? ` chorded from ${currentChord}` : ``}`);
  199. let lookupMap = null;
  200. if (currentChord !== null) {
  201. // Fetch all chord bindings for `currentChord`
  202. const candidates = this._map.get(currentChord);
  203. if (typeof candidates === 'undefined') {
  204. // No chords starting with `currentChord`
  205. this._log(`\\ No keybinding entries.`);
  206. return null;
  207. }
  208. lookupMap = [];
  209. for (let i = 0, len = candidates.length; i < len; i++) {
  210. const candidate = candidates[i];
  211. // TODO@chords
  212. if (candidate.keypressParts[1] === keypress) {
  213. lookupMap.push(candidate);
  214. }
  215. }
  216. }
  217. else {
  218. const candidates = this._map.get(keypress);
  219. if (typeof candidates === 'undefined') {
  220. // No bindings with `keypress`
  221. this._log(`\\ No keybinding entries.`);
  222. return null;
  223. }
  224. lookupMap = candidates;
  225. }
  226. const result = this._findCommand(context, lookupMap);
  227. if (!result) {
  228. this._log(`\\ From ${lookupMap.length} keybinding entries, no when clauses matched the context.`);
  229. return null;
  230. }
  231. // TODO@chords
  232. if (currentChord === null && result.keypressParts.length > 1 && result.keypressParts[1] !== null) {
  233. this._log(`\\ From ${lookupMap.length} keybinding entries, matched chord, when: ${printWhenExplanation(result.when)}, source: ${printSourceExplanation(result)}.`);
  234. return {
  235. enterChord: true,
  236. leaveChord: false,
  237. commandId: null,
  238. commandArgs: null,
  239. bubble: false
  240. };
  241. }
  242. this._log(`\\ From ${lookupMap.length} keybinding entries, matched ${result.command}, when: ${printWhenExplanation(result.when)}, source: ${printSourceExplanation(result)}.`);
  243. return {
  244. enterChord: false,
  245. leaveChord: result.keypressParts.length > 1,
  246. commandId: result.command,
  247. commandArgs: result.commandArgs,
  248. bubble: result.bubble
  249. };
  250. }
  251. _findCommand(context, matches) {
  252. for (let i = matches.length - 1; i >= 0; i--) {
  253. const k = matches[i];
  254. if (!KeybindingResolver._contextMatchesRules(context, k.when)) {
  255. continue;
  256. }
  257. return k;
  258. }
  259. return null;
  260. }
  261. static _contextMatchesRules(context, rules) {
  262. if (!rules) {
  263. return true;
  264. }
  265. return rules.evaluate(context);
  266. }
  267. }
  268. function printWhenExplanation(when) {
  269. if (!when) {
  270. return `no when condition`;
  271. }
  272. return `${when.serialize()}`;
  273. }
  274. function printSourceExplanation(kb) {
  275. return (kb.extensionId
  276. ? (kb.isBuiltinExtension ? `built-in extension ${kb.extensionId}` : `user extension ${kb.extensionId}`)
  277. : (kb.isDefault ? `built-in` : `user`));
  278. }