86fd0653314377166c82c1f19d6fd06de4e815505a971ebaa61e28463bf3c1a1f86f303d025d8b76afc9d253a691c29831ebf806ba0d836f5f4bcc97665374 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  6. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  7. return new (P || (P = Promise))(function (resolve, reject) {
  8. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  9. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  10. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  11. step((generator = generator.apply(thisArg, _arguments || [])).next());
  12. });
  13. };
  14. import * as dom from '../../../../base/browser/dom.js';
  15. import { Action, Separator } from '../../../../base/common/actions.js';
  16. import { CancellationToken } from '../../../../base/common/cancellation.js';
  17. import { EditorExtensionsRegistry } from '../../../browser/editorExtensions.js';
  18. import { Range } from '../../../common/core/range.js';
  19. import { ITextModelService } from '../../../common/services/resolverService.js';
  20. import { DefinitionAction, SymbolNavigationAction, SymbolNavigationAnchor } from '../../gotoSymbol/browser/goToCommands.js';
  21. import { PeekContext } from '../../peekView/browser/peekView.js';
  22. import { isIMenuItem, MenuId, MenuRegistry } from '../../../../platform/actions/common/actions.js';
  23. import { ICommandService } from '../../../../platform/commands/common/commands.js';
  24. import { IContextKeyService } from '../../../../platform/contextkey/common/contextkey.js';
  25. import { IContextMenuService } from '../../../../platform/contextview/browser/contextView.js';
  26. import { IInstantiationService } from '../../../../platform/instantiation/common/instantiation.js';
  27. import { INotificationService, Severity } from '../../../../platform/notification/common/notification.js';
  28. export function showGoToContextMenu(accessor, editor, anchor, part) {
  29. var _a;
  30. return __awaiter(this, void 0, void 0, function* () {
  31. const resolverService = accessor.get(ITextModelService);
  32. const contextMenuService = accessor.get(IContextMenuService);
  33. const commandService = accessor.get(ICommandService);
  34. const instaService = accessor.get(IInstantiationService);
  35. const notificationService = accessor.get(INotificationService);
  36. yield part.item.resolve(CancellationToken.None);
  37. if (!part.part.location) {
  38. return;
  39. }
  40. const location = part.part.location;
  41. const menuActions = [];
  42. // from all registered (not active) context menu actions select those
  43. // that are a symbol navigation action
  44. const filter = new Set(MenuRegistry.getMenuItems(MenuId.EditorContext)
  45. .map(item => isIMenuItem(item) ? item.command.id : ''));
  46. for (const delegate of EditorExtensionsRegistry.getEditorActions()) {
  47. if (delegate instanceof SymbolNavigationAction && filter.has(delegate.id)) {
  48. menuActions.push(new Action(delegate.id, delegate.label, undefined, true, () => __awaiter(this, void 0, void 0, function* () {
  49. const ref = yield resolverService.createModelReference(location.uri);
  50. try {
  51. yield instaService.invokeFunction(delegate.run.bind(delegate), editor, new SymbolNavigationAnchor(ref.object.textEditorModel, Range.getStartPosition(location.range)));
  52. }
  53. finally {
  54. ref.dispose();
  55. }
  56. })));
  57. }
  58. }
  59. if (part.part.command) {
  60. const { command } = part.part;
  61. menuActions.push(new Separator());
  62. menuActions.push(new Action(command.id, command.title, undefined, true, () => __awaiter(this, void 0, void 0, function* () {
  63. var _b;
  64. try {
  65. yield commandService.executeCommand(command.id, ...((_b = command.arguments) !== null && _b !== void 0 ? _b : []));
  66. }
  67. catch (err) {
  68. notificationService.notify({
  69. severity: Severity.Error,
  70. source: part.item.provider.displayName,
  71. message: err
  72. });
  73. }
  74. })));
  75. }
  76. // show context menu
  77. const useShadowDOM = editor.getOption(117 /* EditorOption.useShadowDOM */);
  78. contextMenuService.showContextMenu({
  79. domForShadowRoot: useShadowDOM ? (_a = editor.getDomNode()) !== null && _a !== void 0 ? _a : undefined : undefined,
  80. getAnchor: () => {
  81. const box = dom.getDomNodePagePosition(anchor);
  82. return { x: box.left, y: box.top + box.height + 8 };
  83. },
  84. getActions: () => menuActions,
  85. onHide: () => {
  86. editor.focus();
  87. },
  88. autoSelectFirstItem: true,
  89. });
  90. });
  91. }
  92. export function goToDefinitionWithLocation(accessor, event, editor, location) {
  93. return __awaiter(this, void 0, void 0, function* () {
  94. const resolverService = accessor.get(ITextModelService);
  95. const ref = yield resolverService.createModelReference(location.uri);
  96. yield editor.invokeWithinContext((accessor) => __awaiter(this, void 0, void 0, function* () {
  97. const openToSide = event.hasSideBySideModifier;
  98. const contextKeyService = accessor.get(IContextKeyService);
  99. const isInPeek = PeekContext.inPeekEditor.getValue(contextKeyService);
  100. const canPeek = !openToSide && editor.getOption(80 /* EditorOption.definitionLinkOpensInPeek */) && !isInPeek;
  101. const action = new DefinitionAction({ openToSide, openInPeek: canPeek, muteMessage: true }, { alias: '', label: '', id: '', precondition: undefined });
  102. return action.run(accessor, editor, { model: ref.object.textEditorModel, position: Range.getStartPosition(location.range) });
  103. }));
  104. ref.dispose();
  105. });
  106. }