symbolNavigation.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. import { Emitter } from '../../../base/common/event.js';
  15. import { combinedDisposable, DisposableStore, dispose } from '../../../base/common/lifecycle.js';
  16. import { isEqual } from '../../../base/common/resources.js';
  17. import { EditorCommand, registerEditorCommand } from '../../browser/editorExtensions.js';
  18. import { ICodeEditorService } from '../../browser/services/codeEditorService.js';
  19. import { Range } from '../../common/core/range.js';
  20. import { localize } from '../../../nls.js';
  21. import { IContextKeyService, RawContextKey } from '../../../platform/contextkey/common/contextkey.js';
  22. import { registerSingleton } from '../../../platform/instantiation/common/extensions.js';
  23. import { createDecorator } from '../../../platform/instantiation/common/instantiation.js';
  24. import { IKeybindingService } from '../../../platform/keybinding/common/keybinding.js';
  25. import { KeybindingsRegistry } from '../../../platform/keybinding/common/keybindingsRegistry.js';
  26. import { INotificationService } from '../../../platform/notification/common/notification.js';
  27. export const ctxHasSymbols = new RawContextKey('hasSymbols', false, localize('hasSymbols', "Whether there are symbol locations that can be navigated via keyboard-only."));
  28. export const ISymbolNavigationService = createDecorator('ISymbolNavigationService');
  29. let SymbolNavigationService = class SymbolNavigationService {
  30. constructor(contextKeyService, _editorService, _notificationService, _keybindingService) {
  31. this._editorService = _editorService;
  32. this._notificationService = _notificationService;
  33. this._keybindingService = _keybindingService;
  34. this._currentModel = undefined;
  35. this._currentIdx = -1;
  36. this._ignoreEditorChange = false;
  37. this._ctxHasSymbols = ctxHasSymbols.bindTo(contextKeyService);
  38. }
  39. reset() {
  40. var _a, _b;
  41. this._ctxHasSymbols.reset();
  42. (_a = this._currentState) === null || _a === void 0 ? void 0 : _a.dispose();
  43. (_b = this._currentMessage) === null || _b === void 0 ? void 0 : _b.dispose();
  44. this._currentModel = undefined;
  45. this._currentIdx = -1;
  46. }
  47. put(anchor) {
  48. const refModel = anchor.parent.parent;
  49. if (refModel.references.length <= 1) {
  50. this.reset();
  51. return;
  52. }
  53. this._currentModel = refModel;
  54. this._currentIdx = refModel.references.indexOf(anchor);
  55. this._ctxHasSymbols.set(true);
  56. this._showMessage();
  57. const editorState = new EditorState(this._editorService);
  58. const listener = editorState.onDidChange(_ => {
  59. if (this._ignoreEditorChange) {
  60. return;
  61. }
  62. const editor = this._editorService.getActiveCodeEditor();
  63. if (!editor) {
  64. return;
  65. }
  66. const model = editor.getModel();
  67. const position = editor.getPosition();
  68. if (!model || !position) {
  69. return;
  70. }
  71. let seenUri = false;
  72. let seenPosition = false;
  73. for (const reference of refModel.references) {
  74. if (isEqual(reference.uri, model.uri)) {
  75. seenUri = true;
  76. seenPosition = seenPosition || Range.containsPosition(reference.range, position);
  77. }
  78. else if (seenUri) {
  79. break;
  80. }
  81. }
  82. if (!seenUri || !seenPosition) {
  83. this.reset();
  84. }
  85. });
  86. this._currentState = combinedDisposable(editorState, listener);
  87. }
  88. revealNext(source) {
  89. if (!this._currentModel) {
  90. return Promise.resolve();
  91. }
  92. // get next result and advance
  93. this._currentIdx += 1;
  94. this._currentIdx %= this._currentModel.references.length;
  95. const reference = this._currentModel.references[this._currentIdx];
  96. // status
  97. this._showMessage();
  98. // open editor, ignore events while that happens
  99. this._ignoreEditorChange = true;
  100. return this._editorService.openCodeEditor({
  101. resource: reference.uri,
  102. options: {
  103. selection: Range.collapseToStart(reference.range),
  104. selectionRevealType: 3 /* NearTopIfOutsideViewport */
  105. }
  106. }, source).finally(() => {
  107. this._ignoreEditorChange = false;
  108. });
  109. }
  110. _showMessage() {
  111. var _a;
  112. (_a = this._currentMessage) === null || _a === void 0 ? void 0 : _a.dispose();
  113. const kb = this._keybindingService.lookupKeybinding('editor.gotoNextSymbolFromResult');
  114. const message = kb
  115. ? localize('location.kb', "Symbol {0} of {1}, {2} for next", this._currentIdx + 1, this._currentModel.references.length, kb.getLabel())
  116. : localize('location', "Symbol {0} of {1}", this._currentIdx + 1, this._currentModel.references.length);
  117. this._currentMessage = this._notificationService.status(message);
  118. }
  119. };
  120. SymbolNavigationService = __decorate([
  121. __param(0, IContextKeyService),
  122. __param(1, ICodeEditorService),
  123. __param(2, INotificationService),
  124. __param(3, IKeybindingService)
  125. ], SymbolNavigationService);
  126. registerSingleton(ISymbolNavigationService, SymbolNavigationService, true);
  127. registerEditorCommand(new class extends EditorCommand {
  128. constructor() {
  129. super({
  130. id: 'editor.gotoNextSymbolFromResult',
  131. precondition: ctxHasSymbols,
  132. kbOpts: {
  133. weight: 100 /* EditorContrib */,
  134. primary: 70 /* F12 */
  135. }
  136. });
  137. }
  138. runEditorCommand(accessor, editor) {
  139. return accessor.get(ISymbolNavigationService).revealNext(editor);
  140. }
  141. });
  142. KeybindingsRegistry.registerCommandAndKeybindingRule({
  143. id: 'editor.gotoNextSymbolFromResult.cancel',
  144. weight: 100 /* EditorContrib */,
  145. when: ctxHasSymbols,
  146. primary: 9 /* Escape */,
  147. handler(accessor) {
  148. accessor.get(ISymbolNavigationService).reset();
  149. }
  150. });
  151. //
  152. let EditorState = class EditorState {
  153. constructor(editorService) {
  154. this._listener = new Map();
  155. this._disposables = new DisposableStore();
  156. this._onDidChange = new Emitter();
  157. this.onDidChange = this._onDidChange.event;
  158. this._disposables.add(editorService.onCodeEditorRemove(this._onDidRemoveEditor, this));
  159. this._disposables.add(editorService.onCodeEditorAdd(this._onDidAddEditor, this));
  160. editorService.listCodeEditors().forEach(this._onDidAddEditor, this);
  161. }
  162. dispose() {
  163. this._disposables.dispose();
  164. this._onDidChange.dispose();
  165. dispose(this._listener.values());
  166. }
  167. _onDidAddEditor(editor) {
  168. this._listener.set(editor, combinedDisposable(editor.onDidChangeCursorPosition(_ => this._onDidChange.fire({ editor })), editor.onDidChangeModelContent(_ => this._onDidChange.fire({ editor }))));
  169. }
  170. _onDidRemoveEditor(editor) {
  171. var _a;
  172. (_a = this._listener.get(editor)) === null || _a === void 0 ? void 0 : _a.dispose();
  173. this._listener.delete(editor);
  174. }
  175. };
  176. EditorState = __decorate([
  177. __param(0, ICodeEditorService)
  178. ], EditorState);