0a4efa337403d528b9c3ffdfc2ba691d66e5ea16908b471bbf58668bd51fc19b1cb1844a0769b198441e5068b29d407910f2519ea8b9068ea5e2a5fa0cdf07 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 { alert } from '../../../../base/browser/ui/aria/aria.js';
  24. import { MarkdownString } from '../../../../base/common/htmlContent.js';
  25. import { KeyChord } from '../../../../base/common/keyCodes.js';
  26. import './anchorSelect.css';
  27. import { EditorAction, registerEditorAction, registerEditorContribution } from '../../../browser/editorExtensions.js';
  28. import { Selection } from '../../../common/core/selection.js';
  29. import { EditorContextKeys } from '../../../common/editorContextKeys.js';
  30. import { localize } from '../../../../nls.js';
  31. import { IContextKeyService, RawContextKey } from '../../../../platform/contextkey/common/contextkey.js';
  32. export const SelectionAnchorSet = new RawContextKey('selectionAnchorSet', false);
  33. let SelectionAnchorController = class SelectionAnchorController {
  34. constructor(editor, contextKeyService) {
  35. this.editor = editor;
  36. this.selectionAnchorSetContextKey = SelectionAnchorSet.bindTo(contextKeyService);
  37. this.modelChangeListener = editor.onDidChangeModel(() => this.selectionAnchorSetContextKey.reset());
  38. }
  39. static get(editor) {
  40. return editor.getContribution(SelectionAnchorController.ID);
  41. }
  42. setSelectionAnchor() {
  43. if (this.editor.hasModel()) {
  44. const position = this.editor.getPosition();
  45. this.editor.changeDecorations((accessor) => {
  46. if (this.decorationId) {
  47. accessor.removeDecoration(this.decorationId);
  48. }
  49. this.decorationId = accessor.addDecoration(Selection.fromPositions(position, position), {
  50. description: 'selection-anchor',
  51. stickiness: 1 /* TrackedRangeStickiness.NeverGrowsWhenTypingAtEdges */,
  52. hoverMessage: new MarkdownString().appendText(localize('selectionAnchor', "Selection Anchor")),
  53. className: 'selection-anchor'
  54. });
  55. });
  56. this.selectionAnchorSetContextKey.set(!!this.decorationId);
  57. alert(localize('anchorSet', "Anchor set at {0}:{1}", position.lineNumber, position.column));
  58. }
  59. }
  60. goToSelectionAnchor() {
  61. if (this.editor.hasModel() && this.decorationId) {
  62. const anchorPosition = this.editor.getModel().getDecorationRange(this.decorationId);
  63. if (anchorPosition) {
  64. this.editor.setPosition(anchorPosition.getStartPosition());
  65. }
  66. }
  67. }
  68. selectFromAnchorToCursor() {
  69. if (this.editor.hasModel() && this.decorationId) {
  70. const start = this.editor.getModel().getDecorationRange(this.decorationId);
  71. if (start) {
  72. const end = this.editor.getPosition();
  73. this.editor.setSelection(Selection.fromPositions(start.getStartPosition(), end));
  74. this.cancelSelectionAnchor();
  75. }
  76. }
  77. }
  78. cancelSelectionAnchor() {
  79. if (this.decorationId) {
  80. const decorationId = this.decorationId;
  81. this.editor.changeDecorations((accessor) => {
  82. accessor.removeDecoration(decorationId);
  83. this.decorationId = undefined;
  84. });
  85. this.selectionAnchorSetContextKey.set(false);
  86. }
  87. }
  88. dispose() {
  89. this.cancelSelectionAnchor();
  90. this.modelChangeListener.dispose();
  91. }
  92. };
  93. SelectionAnchorController.ID = 'editor.contrib.selectionAnchorController';
  94. SelectionAnchorController = __decorate([
  95. __param(1, IContextKeyService)
  96. ], SelectionAnchorController);
  97. class SetSelectionAnchor extends EditorAction {
  98. constructor() {
  99. super({
  100. id: 'editor.action.setSelectionAnchor',
  101. label: localize('setSelectionAnchor', "Set Selection Anchor"),
  102. alias: 'Set Selection Anchor',
  103. precondition: undefined,
  104. kbOpts: {
  105. kbExpr: EditorContextKeys.editorTextFocus,
  106. primary: KeyChord(2048 /* KeyMod.CtrlCmd */ | 41 /* KeyCode.KeyK */, 2048 /* KeyMod.CtrlCmd */ | 32 /* KeyCode.KeyB */),
  107. weight: 100 /* KeybindingWeight.EditorContrib */
  108. }
  109. });
  110. }
  111. run(_accessor, editor) {
  112. var _a;
  113. return __awaiter(this, void 0, void 0, function* () {
  114. (_a = SelectionAnchorController.get(editor)) === null || _a === void 0 ? void 0 : _a.setSelectionAnchor();
  115. });
  116. }
  117. }
  118. class GoToSelectionAnchor extends EditorAction {
  119. constructor() {
  120. super({
  121. id: 'editor.action.goToSelectionAnchor',
  122. label: localize('goToSelectionAnchor', "Go to Selection Anchor"),
  123. alias: 'Go to Selection Anchor',
  124. precondition: SelectionAnchorSet,
  125. });
  126. }
  127. run(_accessor, editor) {
  128. var _a;
  129. return __awaiter(this, void 0, void 0, function* () {
  130. (_a = SelectionAnchorController.get(editor)) === null || _a === void 0 ? void 0 : _a.goToSelectionAnchor();
  131. });
  132. }
  133. }
  134. class SelectFromAnchorToCursor extends EditorAction {
  135. constructor() {
  136. super({
  137. id: 'editor.action.selectFromAnchorToCursor',
  138. label: localize('selectFromAnchorToCursor', "Select from Anchor to Cursor"),
  139. alias: 'Select from Anchor to Cursor',
  140. precondition: SelectionAnchorSet,
  141. kbOpts: {
  142. kbExpr: EditorContextKeys.editorTextFocus,
  143. primary: KeyChord(2048 /* KeyMod.CtrlCmd */ | 41 /* KeyCode.KeyK */, 2048 /* KeyMod.CtrlCmd */ | 41 /* KeyCode.KeyK */),
  144. weight: 100 /* KeybindingWeight.EditorContrib */
  145. }
  146. });
  147. }
  148. run(_accessor, editor) {
  149. var _a;
  150. return __awaiter(this, void 0, void 0, function* () {
  151. (_a = SelectionAnchorController.get(editor)) === null || _a === void 0 ? void 0 : _a.selectFromAnchorToCursor();
  152. });
  153. }
  154. }
  155. class CancelSelectionAnchor extends EditorAction {
  156. constructor() {
  157. super({
  158. id: 'editor.action.cancelSelectionAnchor',
  159. label: localize('cancelSelectionAnchor', "Cancel Selection Anchor"),
  160. alias: 'Cancel Selection Anchor',
  161. precondition: SelectionAnchorSet,
  162. kbOpts: {
  163. kbExpr: EditorContextKeys.editorTextFocus,
  164. primary: 9 /* KeyCode.Escape */,
  165. weight: 100 /* KeybindingWeight.EditorContrib */
  166. }
  167. });
  168. }
  169. run(_accessor, editor) {
  170. var _a;
  171. return __awaiter(this, void 0, void 0, function* () {
  172. (_a = SelectionAnchorController.get(editor)) === null || _a === void 0 ? void 0 : _a.cancelSelectionAnchor();
  173. });
  174. }
  175. }
  176. registerEditorContribution(SelectionAnchorController.ID, SelectionAnchorController);
  177. registerEditorAction(SetSelectionAnchor);
  178. registerEditorAction(GoToSelectionAnchor);
  179. registerEditorAction(SelectFromAnchorToCursor);
  180. registerEditorAction(CancelSelectionAnchor);