snippetController2.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 { DisposableStore } from '../../../base/common/lifecycle.js';
  15. import { EditorCommand, registerEditorCommand, registerEditorContribution } from '../../browser/editorExtensions.js';
  16. import { Range } from '../../common/core/range.js';
  17. import { Selection } from '../../common/core/selection.js';
  18. import { EditorContextKeys } from '../../common/editorContextKeys.js';
  19. import { showSimpleSuggestions } from '../suggest/suggest.js';
  20. import { localize } from '../../../nls.js';
  21. import { ContextKeyExpr, IContextKeyService, RawContextKey } from '../../../platform/contextkey/common/contextkey.js';
  22. import { ILogService } from '../../../platform/log/common/log.js';
  23. import { SnippetSession } from './snippetSession.js';
  24. const _defaultOptions = {
  25. overwriteBefore: 0,
  26. overwriteAfter: 0,
  27. undoStopBefore: true,
  28. undoStopAfter: true,
  29. adjustWhitespace: true,
  30. clipboardText: undefined,
  31. overtypingCapturer: undefined
  32. };
  33. let SnippetController2 = class SnippetController2 {
  34. constructor(_editor, _logService, contextKeyService) {
  35. this._editor = _editor;
  36. this._logService = _logService;
  37. this._snippetListener = new DisposableStore();
  38. this._modelVersionId = -1;
  39. this._inSnippet = SnippetController2.InSnippetMode.bindTo(contextKeyService);
  40. this._hasNextTabstop = SnippetController2.HasNextTabstop.bindTo(contextKeyService);
  41. this._hasPrevTabstop = SnippetController2.HasPrevTabstop.bindTo(contextKeyService);
  42. }
  43. static get(editor) {
  44. return editor.getContribution(SnippetController2.ID);
  45. }
  46. dispose() {
  47. var _a;
  48. this._inSnippet.reset();
  49. this._hasPrevTabstop.reset();
  50. this._hasNextTabstop.reset();
  51. (_a = this._session) === null || _a === void 0 ? void 0 : _a.dispose();
  52. this._snippetListener.dispose();
  53. }
  54. insert(template, opts) {
  55. // this is here to find out more about the yet-not-understood
  56. // error that sometimes happens when we fail to inserted a nested
  57. // snippet
  58. try {
  59. this._doInsert(template, typeof opts === 'undefined' ? _defaultOptions : Object.assign(Object.assign({}, _defaultOptions), opts));
  60. }
  61. catch (e) {
  62. this.cancel();
  63. this._logService.error(e);
  64. this._logService.error('snippet_error');
  65. this._logService.error('insert_template=', template);
  66. this._logService.error('existing_template=', this._session ? this._session._logInfo() : '<no_session>');
  67. }
  68. }
  69. _doInsert(template, opts) {
  70. if (!this._editor.hasModel()) {
  71. return;
  72. }
  73. // don't listen while inserting the snippet
  74. // as that is the inflight state causing cancelation
  75. this._snippetListener.clear();
  76. if (opts.undoStopBefore) {
  77. this._editor.getModel().pushStackElement();
  78. }
  79. if (!this._session) {
  80. this._modelVersionId = this._editor.getModel().getAlternativeVersionId();
  81. this._session = new SnippetSession(this._editor, template, opts);
  82. this._session.insert();
  83. }
  84. else {
  85. this._session.merge(template, opts);
  86. }
  87. if (opts.undoStopAfter) {
  88. this._editor.getModel().pushStackElement();
  89. }
  90. this._updateState();
  91. this._snippetListener.add(this._editor.onDidChangeModelContent(e => e.isFlush && this.cancel()));
  92. this._snippetListener.add(this._editor.onDidChangeModel(() => this.cancel()));
  93. this._snippetListener.add(this._editor.onDidChangeCursorSelection(() => this._updateState()));
  94. }
  95. _updateState() {
  96. if (!this._session || !this._editor.hasModel()) {
  97. // canceled in the meanwhile
  98. return;
  99. }
  100. if (this._modelVersionId === this._editor.getModel().getAlternativeVersionId()) {
  101. // undo until the 'before' state happened
  102. // and makes use cancel snippet mode
  103. return this.cancel();
  104. }
  105. if (!this._session.hasPlaceholder) {
  106. // don't listen for selection changes and don't
  107. // update context keys when the snippet is plain text
  108. return this.cancel();
  109. }
  110. if (this._session.isAtLastPlaceholder || !this._session.isSelectionWithinPlaceholders()) {
  111. return this.cancel();
  112. }
  113. this._inSnippet.set(true);
  114. this._hasPrevTabstop.set(!this._session.isAtFirstPlaceholder);
  115. this._hasNextTabstop.set(!this._session.isAtLastPlaceholder);
  116. this._handleChoice();
  117. }
  118. _handleChoice() {
  119. if (!this._session || !this._editor.hasModel()) {
  120. this._currentChoice = undefined;
  121. return;
  122. }
  123. const { choice } = this._session;
  124. if (!choice) {
  125. this._currentChoice = undefined;
  126. return;
  127. }
  128. if (this._currentChoice !== choice) {
  129. this._currentChoice = choice;
  130. this._editor.setSelections(this._editor.getSelections()
  131. .map(s => Selection.fromPositions(s.getStartPosition())));
  132. const [first] = choice.options;
  133. showSimpleSuggestions(this._editor, choice.options.map((option, i) => {
  134. // let before = choice.options.slice(0, i);
  135. // let after = choice.options.slice(i);
  136. return {
  137. kind: 13 /* Value */,
  138. label: option.value,
  139. insertText: option.value,
  140. // insertText: `\${1|${after.concat(before).join(',')}|}$0`,
  141. // snippetType: 'textmate',
  142. sortText: 'a'.repeat(i + 1),
  143. range: Range.fromPositions(this._editor.getPosition(), this._editor.getPosition().delta(0, first.value.length))
  144. };
  145. }));
  146. }
  147. }
  148. finish() {
  149. while (this._inSnippet.get()) {
  150. this.next();
  151. }
  152. }
  153. cancel(resetSelection = false) {
  154. var _a;
  155. this._inSnippet.reset();
  156. this._hasPrevTabstop.reset();
  157. this._hasNextTabstop.reset();
  158. this._snippetListener.clear();
  159. (_a = this._session) === null || _a === void 0 ? void 0 : _a.dispose();
  160. this._session = undefined;
  161. this._modelVersionId = -1;
  162. if (resetSelection) {
  163. // reset selection to the primary cursor when being asked
  164. // for. this happens when explicitly cancelling snippet mode,
  165. // e.g. when pressing ESC
  166. this._editor.setSelections([this._editor.getSelection()]);
  167. }
  168. }
  169. prev() {
  170. if (this._session) {
  171. this._session.prev();
  172. }
  173. this._updateState();
  174. }
  175. next() {
  176. if (this._session) {
  177. this._session.next();
  178. }
  179. this._updateState();
  180. }
  181. isInSnippet() {
  182. return Boolean(this._inSnippet.get());
  183. }
  184. };
  185. SnippetController2.ID = 'snippetController2';
  186. SnippetController2.InSnippetMode = new RawContextKey('inSnippetMode', false, localize('inSnippetMode', "Whether the editor in current in snippet mode"));
  187. SnippetController2.HasNextTabstop = new RawContextKey('hasNextTabstop', false, localize('hasNextTabstop', "Whether there is a next tab stop when in snippet mode"));
  188. SnippetController2.HasPrevTabstop = new RawContextKey('hasPrevTabstop', false, localize('hasPrevTabstop', "Whether there is a previous tab stop when in snippet mode"));
  189. SnippetController2 = __decorate([
  190. __param(1, ILogService),
  191. __param(2, IContextKeyService)
  192. ], SnippetController2);
  193. export { SnippetController2 };
  194. registerEditorContribution(SnippetController2.ID, SnippetController2);
  195. const CommandCtor = EditorCommand.bindToContribution(SnippetController2.get);
  196. registerEditorCommand(new CommandCtor({
  197. id: 'jumpToNextSnippetPlaceholder',
  198. precondition: ContextKeyExpr.and(SnippetController2.InSnippetMode, SnippetController2.HasNextTabstop),
  199. handler: ctrl => ctrl.next(),
  200. kbOpts: {
  201. weight: 100 /* EditorContrib */ + 30,
  202. kbExpr: EditorContextKeys.editorTextFocus,
  203. primary: 2 /* Tab */
  204. }
  205. }));
  206. registerEditorCommand(new CommandCtor({
  207. id: 'jumpToPrevSnippetPlaceholder',
  208. precondition: ContextKeyExpr.and(SnippetController2.InSnippetMode, SnippetController2.HasPrevTabstop),
  209. handler: ctrl => ctrl.prev(),
  210. kbOpts: {
  211. weight: 100 /* EditorContrib */ + 30,
  212. kbExpr: EditorContextKeys.editorTextFocus,
  213. primary: 1024 /* Shift */ | 2 /* Tab */
  214. }
  215. }));
  216. registerEditorCommand(new CommandCtor({
  217. id: 'leaveSnippet',
  218. precondition: SnippetController2.InSnippetMode,
  219. handler: ctrl => ctrl.cancel(true),
  220. kbOpts: {
  221. weight: 100 /* EditorContrib */ + 30,
  222. kbExpr: EditorContextKeys.editorTextFocus,
  223. primary: 9 /* Escape */,
  224. secondary: [1024 /* Shift */ | 9 /* Escape */]
  225. }
  226. }));
  227. registerEditorCommand(new CommandCtor({
  228. id: 'acceptSnippet',
  229. precondition: SnippetController2.InSnippetMode,
  230. handler: ctrl => ctrl.finish(),
  231. // kbOpts: {
  232. // weight: KeybindingWeight.EditorContrib + 30,
  233. // kbExpr: EditorContextKeys.textFocus,
  234. // primary: KeyCode.Enter,
  235. // }
  236. }));