keybindingResolver.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 (let i = 0, len = defaultKeybindings.length; i < len; i++) {
  12. const command = defaultKeybindings[i].command;
  13. if (command) {
  14. this._defaultBoundCommands.set(command, true);
  15. }
  16. }
  17. this._map = new Map();
  18. this._lookupMap = new Map();
  19. this._keybindings = KeybindingResolver.combine(defaultKeybindings, overrides);
  20. for (let i = 0, len = this._keybindings.length; i < len; i++) {
  21. let k = this._keybindings[i];
  22. if (k.keypressParts.length === 0) {
  23. // unbound
  24. continue;
  25. }
  26. if (k.when && k.when.type === 0 /* 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, command, when) {
  35. if (defaultKb.command !== command) {
  36. return false;
  37. }
  38. // TODO@chords
  39. if (keypressFirstPart && defaultKb.keypressParts[0] !== keypressFirstPart) {
  40. return false;
  41. }
  42. // TODO@chords
  43. if (keypressChordPart && defaultKb.keypressParts[1] !== keypressChordPart) {
  44. return false;
  45. }
  46. if (when) {
  47. if (!defaultKb.when) {
  48. return false;
  49. }
  50. if (!expressionsAreEqualWithConstantSubstitution(when, defaultKb.when)) {
  51. return false;
  52. }
  53. }
  54. return true;
  55. }
  56. /**
  57. * Looks for rules containing -command in `overrides` and removes them directly from `defaults`.
  58. */
  59. static combine(defaults, rawOverrides) {
  60. defaults = defaults.slice(0);
  61. let overrides = [];
  62. for (const override of rawOverrides) {
  63. if (!override.command || override.command.length === 0 || override.command.charAt(0) !== '-') {
  64. overrides.push(override);
  65. continue;
  66. }
  67. const command = override.command.substr(1);
  68. // TODO@chords
  69. const keypressFirstPart = override.keypressParts[0];
  70. const keypressChordPart = override.keypressParts[1];
  71. const when = override.when;
  72. for (let j = defaults.length - 1; j >= 0; j--) {
  73. if (this._isTargetedForRemoval(defaults[j], keypressFirstPart, keypressChordPart, command, when)) {
  74. defaults.splice(j, 1);
  75. }
  76. }
  77. }
  78. return defaults.concat(overrides);
  79. }
  80. _addKeyPress(keypress, item) {
  81. const conflicts = this._map.get(keypress);
  82. if (typeof conflicts === 'undefined') {
  83. // There is no conflict so far
  84. this._map.set(keypress, [item]);
  85. this._addToLookupMap(item);
  86. return;
  87. }
  88. for (let i = conflicts.length - 1; i >= 0; i--) {
  89. let conflict = conflicts[i];
  90. if (conflict.command === item.command) {
  91. continue;
  92. }
  93. const conflictIsChord = (conflict.keypressParts.length > 1);
  94. const itemIsChord = (item.keypressParts.length > 1);
  95. // TODO@chords
  96. if (conflictIsChord && itemIsChord && conflict.keypressParts[1] !== item.keypressParts[1]) {
  97. // The conflict only shares the chord start with this command
  98. continue;
  99. }
  100. if (KeybindingResolver.whenIsEntirelyIncluded(conflict.when, item.when)) {
  101. // `item` completely overwrites `conflict`
  102. // Remove conflict from the lookupMap
  103. this._removeFromLookupMap(conflict);
  104. }
  105. }
  106. conflicts.push(item);
  107. this._addToLookupMap(item);
  108. }
  109. _addToLookupMap(item) {
  110. if (!item.command) {
  111. return;
  112. }
  113. let arr = this._lookupMap.get(item.command);
  114. if (typeof arr === 'undefined') {
  115. arr = [item];
  116. this._lookupMap.set(item.command, arr);
  117. }
  118. else {
  119. arr.push(item);
  120. }
  121. }
  122. _removeFromLookupMap(item) {
  123. if (!item.command) {
  124. return;
  125. }
  126. let arr = this._lookupMap.get(item.command);
  127. if (typeof arr === 'undefined') {
  128. return;
  129. }
  130. for (let i = 0, len = arr.length; i < len; i++) {
  131. if (arr[i] === item) {
  132. arr.splice(i, 1);
  133. return;
  134. }
  135. }
  136. }
  137. /**
  138. * Returns true if it is provable `a` implies `b`.
  139. */
  140. static whenIsEntirelyIncluded(a, b) {
  141. if (!b || b.type === 1 /* True */) {
  142. return true;
  143. }
  144. if (!a || a.type === 1 /* True */) {
  145. return false;
  146. }
  147. return implies(a, b);
  148. }
  149. getKeybindings() {
  150. return this._keybindings;
  151. }
  152. lookupPrimaryKeybinding(commandId, context) {
  153. const items = this._lookupMap.get(commandId);
  154. if (typeof items === 'undefined' || items.length === 0) {
  155. return null;
  156. }
  157. if (items.length === 1) {
  158. return items[0];
  159. }
  160. for (let i = items.length - 1; i >= 0; i--) {
  161. const item = items[i];
  162. if (context.contextMatchesRules(item.when)) {
  163. return item;
  164. }
  165. }
  166. return items[items.length - 1];
  167. }
  168. resolve(context, currentChord, keypress) {
  169. this._log(`| Resolving ${keypress}${currentChord ? ` chorded from ${currentChord}` : ``}`);
  170. let lookupMap = null;
  171. if (currentChord !== null) {
  172. // Fetch all chord bindings for `currentChord`
  173. const candidates = this._map.get(currentChord);
  174. if (typeof candidates === 'undefined') {
  175. // No chords starting with `currentChord`
  176. this._log(`\\ No keybinding entries.`);
  177. return null;
  178. }
  179. lookupMap = [];
  180. for (let i = 0, len = candidates.length; i < len; i++) {
  181. let candidate = candidates[i];
  182. // TODO@chords
  183. if (candidate.keypressParts[1] === keypress) {
  184. lookupMap.push(candidate);
  185. }
  186. }
  187. }
  188. else {
  189. const candidates = this._map.get(keypress);
  190. if (typeof candidates === 'undefined') {
  191. // No bindings with `keypress`
  192. this._log(`\\ No keybinding entries.`);
  193. return null;
  194. }
  195. lookupMap = candidates;
  196. }
  197. let result = this._findCommand(context, lookupMap);
  198. if (!result) {
  199. this._log(`\\ From ${lookupMap.length} keybinding entries, no when clauses matched the context.`);
  200. return null;
  201. }
  202. // TODO@chords
  203. if (currentChord === null && result.keypressParts.length > 1 && result.keypressParts[1] !== null) {
  204. this._log(`\\ From ${lookupMap.length} keybinding entries, matched chord, when: ${printWhenExplanation(result.when)}, source: ${printSourceExplanation(result)}.`);
  205. return {
  206. enterChord: true,
  207. leaveChord: false,
  208. commandId: null,
  209. commandArgs: null,
  210. bubble: false
  211. };
  212. }
  213. this._log(`\\ From ${lookupMap.length} keybinding entries, matched ${result.command}, when: ${printWhenExplanation(result.when)}, source: ${printSourceExplanation(result)}.`);
  214. return {
  215. enterChord: false,
  216. leaveChord: result.keypressParts.length > 1,
  217. commandId: result.command,
  218. commandArgs: result.commandArgs,
  219. bubble: result.bubble
  220. };
  221. }
  222. _findCommand(context, matches) {
  223. for (let i = matches.length - 1; i >= 0; i--) {
  224. let k = matches[i];
  225. if (!KeybindingResolver.contextMatchesRules(context, k.when)) {
  226. continue;
  227. }
  228. return k;
  229. }
  230. return null;
  231. }
  232. static contextMatchesRules(context, rules) {
  233. if (!rules) {
  234. return true;
  235. }
  236. return rules.evaluate(context);
  237. }
  238. }
  239. function printWhenExplanation(when) {
  240. if (!when) {
  241. return `no when condition`;
  242. }
  243. return `${when.serialize()}`;
  244. }
  245. function printSourceExplanation(kb) {
  246. return (kb.extensionId
  247. ? (kb.isBuiltinExtension ? `built-in extension ${kb.extensionId}` : `user extension ${kb.extensionId}`)
  248. : (kb.isDefault ? `built-in` : `user`));
  249. }