5986bb462947988cd7b6b976b7d0dda16ab74f633efeddc82cba75c9851ae8b0084d27053f349c4a9a90e06dce1070291bdfd60b304070e801781aea95d07b 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  6. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  7. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  8. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  9. return c > 3 && r && Object.defineProperty(target, key, r), r;
  10. };
  11. var __param = (this && this.__param) || function (paramIndex, decorator) {
  12. return function (target, key) { decorator(target, key, paramIndex); }
  13. };
  14. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  15. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  16. return new (P || (P = Promise))(function (resolve, reject) {
  17. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  18. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  19. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  20. step((generator = generator.apply(thisArg, _arguments || [])).next());
  21. });
  22. };
  23. import { createCancelablePromise } from '../../../../../base/common/async.js';
  24. import { onUnexpectedError } from '../../../../../base/common/errors.js';
  25. import { KeyChord } from '../../../../../base/common/keyCodes.js';
  26. import { DisposableStore } from '../../../../../base/common/lifecycle.js';
  27. import { ICodeEditorService } from '../../../../browser/services/codeEditorService.js';
  28. import { Position } from '../../../../common/core/position.js';
  29. import { Range } from '../../../../common/core/range.js';
  30. import { getOuterEditor, PeekContext } from '../../../peekView/browser/peekView.js';
  31. import * as nls from '../../../../../nls.js';
  32. import { CommandsRegistry } from '../../../../../platform/commands/common/commands.js';
  33. import { IConfigurationService } from '../../../../../platform/configuration/common/configuration.js';
  34. import { ContextKeyExpr, IContextKeyService, RawContextKey } from '../../../../../platform/contextkey/common/contextkey.js';
  35. import { IInstantiationService } from '../../../../../platform/instantiation/common/instantiation.js';
  36. import { KeybindingsRegistry } from '../../../../../platform/keybinding/common/keybindingsRegistry.js';
  37. import { IListService, WorkbenchListFocusContextKey, WorkbenchTreeElementCanCollapse, WorkbenchTreeElementCanExpand } from '../../../../../platform/list/browser/listService.js';
  38. import { INotificationService } from '../../../../../platform/notification/common/notification.js';
  39. import { IStorageService } from '../../../../../platform/storage/common/storage.js';
  40. import { OneReference } from '../referencesModel.js';
  41. import { LayoutData, ReferenceWidget } from './referencesWidget.js';
  42. export const ctxReferenceSearchVisible = new RawContextKey('referenceSearchVisible', false, nls.localize('referenceSearchVisible', "Whether reference peek is visible, like 'Peek References' or 'Peek Definition'"));
  43. let ReferencesController = class ReferencesController {
  44. constructor(_defaultTreeKeyboardSupport, _editor, contextKeyService, _editorService, _notificationService, _instantiationService, _storageService, _configurationService) {
  45. this._defaultTreeKeyboardSupport = _defaultTreeKeyboardSupport;
  46. this._editor = _editor;
  47. this._editorService = _editorService;
  48. this._notificationService = _notificationService;
  49. this._instantiationService = _instantiationService;
  50. this._storageService = _storageService;
  51. this._configurationService = _configurationService;
  52. this._disposables = new DisposableStore();
  53. this._requestIdPool = 0;
  54. this._ignoreModelChangeEvent = false;
  55. this._referenceSearchVisible = ctxReferenceSearchVisible.bindTo(contextKeyService);
  56. }
  57. static get(editor) {
  58. return editor.getContribution(ReferencesController.ID);
  59. }
  60. dispose() {
  61. var _a, _b;
  62. this._referenceSearchVisible.reset();
  63. this._disposables.dispose();
  64. (_a = this._widget) === null || _a === void 0 ? void 0 : _a.dispose();
  65. (_b = this._model) === null || _b === void 0 ? void 0 : _b.dispose();
  66. this._widget = undefined;
  67. this._model = undefined;
  68. }
  69. toggleWidget(range, modelPromise, peekMode) {
  70. // close current widget and return early is position didn't change
  71. let widgetPosition;
  72. if (this._widget) {
  73. widgetPosition = this._widget.position;
  74. }
  75. this.closeWidget();
  76. if (!!widgetPosition && range.containsPosition(widgetPosition)) {
  77. return;
  78. }
  79. this._peekMode = peekMode;
  80. this._referenceSearchVisible.set(true);
  81. // close the widget on model/mode changes
  82. this._disposables.add(this._editor.onDidChangeModelLanguage(() => { this.closeWidget(); }));
  83. this._disposables.add(this._editor.onDidChangeModel(() => {
  84. if (!this._ignoreModelChangeEvent) {
  85. this.closeWidget();
  86. }
  87. }));
  88. const storageKey = 'peekViewLayout';
  89. const data = LayoutData.fromJSON(this._storageService.get(storageKey, 0 /* StorageScope.PROFILE */, '{}'));
  90. this._widget = this._instantiationService.createInstance(ReferenceWidget, this._editor, this._defaultTreeKeyboardSupport, data);
  91. this._widget.setTitle(nls.localize('labelLoading', "Loading..."));
  92. this._widget.show(range);
  93. this._disposables.add(this._widget.onDidClose(() => {
  94. modelPromise.cancel();
  95. if (this._widget) {
  96. this._storageService.store(storageKey, JSON.stringify(this._widget.layoutData), 0 /* StorageScope.PROFILE */, 1 /* StorageTarget.MACHINE */);
  97. this._widget = undefined;
  98. }
  99. this.closeWidget();
  100. }));
  101. this._disposables.add(this._widget.onDidSelectReference(event => {
  102. const { element, kind } = event;
  103. if (!element) {
  104. return;
  105. }
  106. switch (kind) {
  107. case 'open':
  108. if (event.source !== 'editor' || !this._configurationService.getValue('editor.stablePeek')) {
  109. // when stable peek is configured we don't close
  110. // the peek window on selecting the editor
  111. this.openReference(element, false, false);
  112. }
  113. break;
  114. case 'side':
  115. this.openReference(element, true, false);
  116. break;
  117. case 'goto':
  118. if (peekMode) {
  119. this._gotoReference(element);
  120. }
  121. else {
  122. this.openReference(element, false, true);
  123. }
  124. break;
  125. }
  126. }));
  127. const requestId = ++this._requestIdPool;
  128. modelPromise.then(model => {
  129. var _a;
  130. // still current request? widget still open?
  131. if (requestId !== this._requestIdPool || !this._widget) {
  132. model.dispose();
  133. return undefined;
  134. }
  135. (_a = this._model) === null || _a === void 0 ? void 0 : _a.dispose();
  136. this._model = model;
  137. // show widget
  138. return this._widget.setModel(this._model).then(() => {
  139. if (this._widget && this._model && this._editor.hasModel()) { // might have been closed
  140. // set title
  141. if (!this._model.isEmpty) {
  142. this._widget.setMetaTitle(nls.localize('metaTitle.N', "{0} ({1})", this._model.title, this._model.references.length));
  143. }
  144. else {
  145. this._widget.setMetaTitle('');
  146. }
  147. // set 'best' selection
  148. const uri = this._editor.getModel().uri;
  149. const pos = new Position(range.startLineNumber, range.startColumn);
  150. const selection = this._model.nearestReference(uri, pos);
  151. if (selection) {
  152. return this._widget.setSelection(selection).then(() => {
  153. if (this._widget && this._editor.getOption(79 /* EditorOption.peekWidgetDefaultFocus */) === 'editor') {
  154. this._widget.focusOnPreviewEditor();
  155. }
  156. });
  157. }
  158. }
  159. return undefined;
  160. });
  161. }, error => {
  162. this._notificationService.error(error);
  163. });
  164. }
  165. changeFocusBetweenPreviewAndReferences() {
  166. if (!this._widget) {
  167. // can be called while still resolving...
  168. return;
  169. }
  170. if (this._widget.isPreviewEditorFocused()) {
  171. this._widget.focusOnReferenceTree();
  172. }
  173. else {
  174. this._widget.focusOnPreviewEditor();
  175. }
  176. }
  177. goToNextOrPreviousReference(fwd) {
  178. return __awaiter(this, void 0, void 0, function* () {
  179. if (!this._editor.hasModel() || !this._model || !this._widget) {
  180. // can be called while still resolving...
  181. return;
  182. }
  183. const currentPosition = this._widget.position;
  184. if (!currentPosition) {
  185. return;
  186. }
  187. const source = this._model.nearestReference(this._editor.getModel().uri, currentPosition);
  188. if (!source) {
  189. return;
  190. }
  191. const target = this._model.nextOrPreviousReference(source, fwd);
  192. const editorFocus = this._editor.hasTextFocus();
  193. const previewEditorFocus = this._widget.isPreviewEditorFocused();
  194. yield this._widget.setSelection(target);
  195. yield this._gotoReference(target);
  196. if (editorFocus) {
  197. this._editor.focus();
  198. }
  199. else if (this._widget && previewEditorFocus) {
  200. this._widget.focusOnPreviewEditor();
  201. }
  202. });
  203. }
  204. revealReference(reference) {
  205. return __awaiter(this, void 0, void 0, function* () {
  206. if (!this._editor.hasModel() || !this._model || !this._widget) {
  207. // can be called while still resolving...
  208. return;
  209. }
  210. yield this._widget.revealReference(reference);
  211. });
  212. }
  213. closeWidget(focusEditor = true) {
  214. var _a, _b;
  215. (_a = this._widget) === null || _a === void 0 ? void 0 : _a.dispose();
  216. (_b = this._model) === null || _b === void 0 ? void 0 : _b.dispose();
  217. this._referenceSearchVisible.reset();
  218. this._disposables.clear();
  219. this._widget = undefined;
  220. this._model = undefined;
  221. if (focusEditor) {
  222. this._editor.focus();
  223. }
  224. this._requestIdPool += 1; // Cancel pending requests
  225. }
  226. _gotoReference(ref) {
  227. if (this._widget) {
  228. this._widget.hide();
  229. }
  230. this._ignoreModelChangeEvent = true;
  231. const range = Range.lift(ref.range).collapseToStart();
  232. return this._editorService.openCodeEditor({
  233. resource: ref.uri,
  234. options: { selection: range, selectionSource: "code.jump" /* TextEditorSelectionSource.JUMP */ }
  235. }, this._editor).then(openedEditor => {
  236. var _a;
  237. this._ignoreModelChangeEvent = false;
  238. if (!openedEditor || !this._widget) {
  239. // something went wrong...
  240. this.closeWidget();
  241. return;
  242. }
  243. if (this._editor === openedEditor) {
  244. //
  245. this._widget.show(range);
  246. this._widget.focusOnReferenceTree();
  247. }
  248. else {
  249. // we opened a different editor instance which means a different controller instance.
  250. // therefore we stop with this controller and continue with the other
  251. const other = ReferencesController.get(openedEditor);
  252. const model = this._model.clone();
  253. this.closeWidget();
  254. openedEditor.focus();
  255. other === null || other === void 0 ? void 0 : other.toggleWidget(range, createCancelablePromise(_ => Promise.resolve(model)), (_a = this._peekMode) !== null && _a !== void 0 ? _a : false);
  256. }
  257. }, (err) => {
  258. this._ignoreModelChangeEvent = false;
  259. onUnexpectedError(err);
  260. });
  261. }
  262. openReference(ref, sideBySide, pinned) {
  263. // clear stage
  264. if (!sideBySide) {
  265. this.closeWidget();
  266. }
  267. const { uri, range } = ref;
  268. this._editorService.openCodeEditor({
  269. resource: uri,
  270. options: { selection: range, selectionSource: "code.jump" /* TextEditorSelectionSource.JUMP */, pinned }
  271. }, this._editor, sideBySide);
  272. }
  273. };
  274. ReferencesController.ID = 'editor.contrib.referencesController';
  275. ReferencesController = __decorate([
  276. __param(2, IContextKeyService),
  277. __param(3, ICodeEditorService),
  278. __param(4, INotificationService),
  279. __param(5, IInstantiationService),
  280. __param(6, IStorageService),
  281. __param(7, IConfigurationService)
  282. ], ReferencesController);
  283. export { ReferencesController };
  284. function withController(accessor, fn) {
  285. const outerEditor = getOuterEditor(accessor);
  286. if (!outerEditor) {
  287. return;
  288. }
  289. const controller = ReferencesController.get(outerEditor);
  290. if (controller) {
  291. fn(controller);
  292. }
  293. }
  294. KeybindingsRegistry.registerCommandAndKeybindingRule({
  295. id: 'togglePeekWidgetFocus',
  296. weight: 100 /* KeybindingWeight.EditorContrib */,
  297. primary: KeyChord(2048 /* KeyMod.CtrlCmd */ | 41 /* KeyCode.KeyK */, 60 /* KeyCode.F2 */),
  298. when: ContextKeyExpr.or(ctxReferenceSearchVisible, PeekContext.inPeekEditor),
  299. handler(accessor) {
  300. withController(accessor, controller => {
  301. controller.changeFocusBetweenPreviewAndReferences();
  302. });
  303. }
  304. });
  305. KeybindingsRegistry.registerCommandAndKeybindingRule({
  306. id: 'goToNextReference',
  307. weight: 100 /* KeybindingWeight.EditorContrib */ - 10,
  308. primary: 62 /* KeyCode.F4 */,
  309. secondary: [70 /* KeyCode.F12 */],
  310. when: ContextKeyExpr.or(ctxReferenceSearchVisible, PeekContext.inPeekEditor),
  311. handler(accessor) {
  312. withController(accessor, controller => {
  313. controller.goToNextOrPreviousReference(true);
  314. });
  315. }
  316. });
  317. KeybindingsRegistry.registerCommandAndKeybindingRule({
  318. id: 'goToPreviousReference',
  319. weight: 100 /* KeybindingWeight.EditorContrib */ - 10,
  320. primary: 1024 /* KeyMod.Shift */ | 62 /* KeyCode.F4 */,
  321. secondary: [1024 /* KeyMod.Shift */ | 70 /* KeyCode.F12 */],
  322. when: ContextKeyExpr.or(ctxReferenceSearchVisible, PeekContext.inPeekEditor),
  323. handler(accessor) {
  324. withController(accessor, controller => {
  325. controller.goToNextOrPreviousReference(false);
  326. });
  327. }
  328. });
  329. // commands that aren't needed anymore because there is now ContextKeyExpr.OR
  330. CommandsRegistry.registerCommandAlias('goToNextReferenceFromEmbeddedEditor', 'goToNextReference');
  331. CommandsRegistry.registerCommandAlias('goToPreviousReferenceFromEmbeddedEditor', 'goToPreviousReference');
  332. // close
  333. CommandsRegistry.registerCommandAlias('closeReferenceSearchEditor', 'closeReferenceSearch');
  334. CommandsRegistry.registerCommand('closeReferenceSearch', accessor => withController(accessor, controller => controller.closeWidget()));
  335. KeybindingsRegistry.registerKeybindingRule({
  336. id: 'closeReferenceSearch',
  337. weight: 100 /* KeybindingWeight.EditorContrib */ - 101,
  338. primary: 9 /* KeyCode.Escape */,
  339. secondary: [1024 /* KeyMod.Shift */ | 9 /* KeyCode.Escape */],
  340. when: ContextKeyExpr.and(PeekContext.inPeekEditor, ContextKeyExpr.not('config.editor.stablePeek'))
  341. });
  342. KeybindingsRegistry.registerKeybindingRule({
  343. id: 'closeReferenceSearch',
  344. weight: 200 /* KeybindingWeight.WorkbenchContrib */ + 50,
  345. primary: 9 /* KeyCode.Escape */,
  346. secondary: [1024 /* KeyMod.Shift */ | 9 /* KeyCode.Escape */],
  347. when: ContextKeyExpr.and(ctxReferenceSearchVisible, ContextKeyExpr.not('config.editor.stablePeek'))
  348. });
  349. KeybindingsRegistry.registerCommandAndKeybindingRule({
  350. id: 'revealReference',
  351. weight: 200 /* KeybindingWeight.WorkbenchContrib */,
  352. primary: 3 /* KeyCode.Enter */,
  353. mac: {
  354. primary: 3 /* KeyCode.Enter */,
  355. secondary: [2048 /* KeyMod.CtrlCmd */ | 18 /* KeyCode.DownArrow */]
  356. },
  357. when: ContextKeyExpr.and(ctxReferenceSearchVisible, WorkbenchListFocusContextKey, WorkbenchTreeElementCanCollapse.negate(), WorkbenchTreeElementCanExpand.negate()),
  358. handler(accessor) {
  359. var _a;
  360. const listService = accessor.get(IListService);
  361. const focus = (_a = listService.lastFocusedList) === null || _a === void 0 ? void 0 : _a.getFocus();
  362. if (Array.isArray(focus) && focus[0] instanceof OneReference) {
  363. withController(accessor, controller => controller.revealReference(focus[0]));
  364. }
  365. }
  366. });
  367. KeybindingsRegistry.registerCommandAndKeybindingRule({
  368. id: 'openReferenceToSide',
  369. weight: 100 /* KeybindingWeight.EditorContrib */,
  370. primary: 2048 /* KeyMod.CtrlCmd */ | 3 /* KeyCode.Enter */,
  371. mac: {
  372. primary: 256 /* KeyMod.WinCtrl */ | 3 /* KeyCode.Enter */
  373. },
  374. when: ContextKeyExpr.and(ctxReferenceSearchVisible, WorkbenchListFocusContextKey, WorkbenchTreeElementCanCollapse.negate(), WorkbenchTreeElementCanExpand.negate()),
  375. handler(accessor) {
  376. var _a;
  377. const listService = accessor.get(IListService);
  378. const focus = (_a = listService.lastFocusedList) === null || _a === void 0 ? void 0 : _a.getFocus();
  379. if (Array.isArray(focus) && focus[0] instanceof OneReference) {
  380. withController(accessor, controller => controller.openReference(focus[0], true, true));
  381. }
  382. }
  383. });
  384. CommandsRegistry.registerCommand('openReference', (accessor) => {
  385. var _a;
  386. const listService = accessor.get(IListService);
  387. const focus = (_a = listService.lastFocusedList) === null || _a === void 0 ? void 0 : _a.getFocus();
  388. if (Array.isArray(focus) && focus[0] instanceof OneReference) {
  389. withController(accessor, controller => controller.openReference(focus[0], false, true));
  390. }
  391. });