| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302 |
- /*---------------------------------------------------------------------------------------------
- * Copyright (c) Microsoft Corporation. All rights reserved.
- * Licensed under the MIT License. See License.txt in the project root for license information.
- *--------------------------------------------------------------------------------------------*/
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
- 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;
- return c > 3 && r && Object.defineProperty(target, key, r), r;
- };
- var __param = (this && this.__param) || function (paramIndex, decorator) {
- return function (target, key) { decorator(target, key, paramIndex); }
- };
- import { DisposableStore } from '../../../../base/common/lifecycle.js';
- import { assertType } from '../../../../base/common/types.js';
- import { EditorCommand, registerEditorCommand, registerEditorContribution } from '../../../browser/editorExtensions.js';
- import { Position } from '../../../common/core/position.js';
- import { Selection } from '../../../common/core/selection.js';
- import { EditorContextKeys } from '../../../common/editorContextKeys.js';
- import { ILanguageConfigurationService } from '../../../common/languages/languageConfigurationRegistry.js';
- import { ILanguageFeaturesService } from '../../../common/services/languageFeatures.js';
- import { showSimpleSuggestions } from '../../suggest/browser/suggest.js';
- import { localize } from '../../../../nls.js';
- import { ContextKeyExpr, IContextKeyService, RawContextKey } from '../../../../platform/contextkey/common/contextkey.js';
- import { ILogService } from '../../../../platform/log/common/log.js';
- import { SnippetSession } from './snippetSession.js';
- const _defaultOptions = {
- overwriteBefore: 0,
- overwriteAfter: 0,
- undoStopBefore: true,
- undoStopAfter: true,
- adjustWhitespace: true,
- clipboardText: undefined,
- overtypingCapturer: undefined
- };
- let SnippetController2 = class SnippetController2 {
- constructor(_editor, _logService, _languageFeaturesService, contextKeyService, _languageConfigurationService) {
- this._editor = _editor;
- this._logService = _logService;
- this._languageFeaturesService = _languageFeaturesService;
- this._languageConfigurationService = _languageConfigurationService;
- this._snippetListener = new DisposableStore();
- this._modelVersionId = -1;
- this._inSnippet = SnippetController2.InSnippetMode.bindTo(contextKeyService);
- this._hasNextTabstop = SnippetController2.HasNextTabstop.bindTo(contextKeyService);
- this._hasPrevTabstop = SnippetController2.HasPrevTabstop.bindTo(contextKeyService);
- }
- static get(editor) {
- return editor.getContribution(SnippetController2.ID);
- }
- dispose() {
- var _a;
- this._inSnippet.reset();
- this._hasPrevTabstop.reset();
- this._hasNextTabstop.reset();
- (_a = this._session) === null || _a === void 0 ? void 0 : _a.dispose();
- this._snippetListener.dispose();
- }
- apply(edits, opts) {
- try {
- this._doInsert(edits, typeof opts === 'undefined' ? _defaultOptions : Object.assign(Object.assign({}, _defaultOptions), opts));
- }
- catch (e) {
- this.cancel();
- this._logService.error(e);
- this._logService.error('snippet_error');
- this._logService.error('insert_edits=', edits);
- this._logService.error('existing_template=', this._session ? this._session._logInfo() : '<no_session>');
- }
- }
- insert(template, opts) {
- // this is here to find out more about the yet-not-understood
- // error that sometimes happens when we fail to inserted a nested
- // snippet
- try {
- this._doInsert(template, typeof opts === 'undefined' ? _defaultOptions : Object.assign(Object.assign({}, _defaultOptions), opts));
- }
- catch (e) {
- this.cancel();
- this._logService.error(e);
- this._logService.error('snippet_error');
- this._logService.error('insert_template=', template);
- this._logService.error('existing_template=', this._session ? this._session._logInfo() : '<no_session>');
- }
- }
- _doInsert(template, opts) {
- var _a;
- if (!this._editor.hasModel()) {
- return;
- }
- // don't listen while inserting the snippet
- // as that is the inflight state causing cancelation
- this._snippetListener.clear();
- if (opts.undoStopBefore) {
- this._editor.getModel().pushStackElement();
- }
- // don't merge
- if (this._session && typeof template !== 'string') {
- this.cancel();
- }
- if (!this._session) {
- this._modelVersionId = this._editor.getModel().getAlternativeVersionId();
- this._session = new SnippetSession(this._editor, template, opts, this._languageConfigurationService);
- this._session.insert();
- }
- else {
- assertType(typeof template === 'string');
- this._session.merge(template, opts);
- }
- if (opts.undoStopAfter) {
- this._editor.getModel().pushStackElement();
- }
- // regster completion item provider when there is any choice element
- if ((_a = this._session) === null || _a === void 0 ? void 0 : _a.hasChoice) {
- this._choiceCompletionItemProvider = {
- provideCompletionItems: (model, position) => {
- if (!this._session || model !== this._editor.getModel() || !Position.equals(this._editor.getPosition(), position)) {
- return undefined;
- }
- const { activeChoice } = this._session;
- if (!activeChoice || activeChoice.choice.options.length === 0) {
- return undefined;
- }
- const word = model.getValueInRange(activeChoice.range);
- const isAnyOfOptions = Boolean(activeChoice.choice.options.find(o => o.value === word));
- const suggestions = [];
- for (let i = 0; i < activeChoice.choice.options.length; i++) {
- const option = activeChoice.choice.options[i];
- suggestions.push({
- kind: 13 /* CompletionItemKind.Value */,
- label: option.value,
- insertText: option.value,
- sortText: 'a'.repeat(i + 1),
- range: activeChoice.range,
- filterText: isAnyOfOptions ? `${word}_${option.value}` : undefined,
- command: { id: 'jumpToNextSnippetPlaceholder', title: localize('next', 'Go to next placeholder...') }
- });
- }
- return { suggestions };
- }
- };
- const registration = this._languageFeaturesService.completionProvider.register({
- language: this._editor.getModel().getLanguageId(),
- pattern: this._editor.getModel().uri.fsPath,
- scheme: this._editor.getModel().uri.scheme
- }, this._choiceCompletionItemProvider);
- this._snippetListener.add(registration);
- }
- this._updateState();
- this._snippetListener.add(this._editor.onDidChangeModelContent(e => e.isFlush && this.cancel()));
- this._snippetListener.add(this._editor.onDidChangeModel(() => this.cancel()));
- this._snippetListener.add(this._editor.onDidChangeCursorSelection(() => this._updateState()));
- }
- _updateState() {
- if (!this._session || !this._editor.hasModel()) {
- // canceled in the meanwhile
- return;
- }
- if (this._modelVersionId === this._editor.getModel().getAlternativeVersionId()) {
- // undo until the 'before' state happened
- // and makes use cancel snippet mode
- return this.cancel();
- }
- if (!this._session.hasPlaceholder) {
- // don't listen for selection changes and don't
- // update context keys when the snippet is plain text
- return this.cancel();
- }
- if (this._session.isAtLastPlaceholder || !this._session.isSelectionWithinPlaceholders()) {
- this._editor.getModel().pushStackElement();
- return this.cancel();
- }
- this._inSnippet.set(true);
- this._hasPrevTabstop.set(!this._session.isAtFirstPlaceholder);
- this._hasNextTabstop.set(!this._session.isAtLastPlaceholder);
- this._handleChoice();
- }
- _handleChoice() {
- if (!this._session || !this._editor.hasModel()) {
- this._currentChoice = undefined;
- return;
- }
- const { activeChoice } = this._session;
- if (!activeChoice || !this._choiceCompletionItemProvider) {
- this._currentChoice = undefined;
- return;
- }
- if (this._currentChoice !== activeChoice.choice) {
- this._currentChoice = activeChoice.choice;
- // trigger suggest with the special choice completion provider
- queueMicrotask(() => {
- showSimpleSuggestions(this._editor, this._choiceCompletionItemProvider);
- });
- }
- }
- finish() {
- while (this._inSnippet.get()) {
- this.next();
- }
- }
- cancel(resetSelection = false) {
- var _a;
- this._inSnippet.reset();
- this._hasPrevTabstop.reset();
- this._hasNextTabstop.reset();
- this._snippetListener.clear();
- this._currentChoice = undefined;
- (_a = this._session) === null || _a === void 0 ? void 0 : _a.dispose();
- this._session = undefined;
- this._modelVersionId = -1;
- if (resetSelection) {
- // reset selection to the primary cursor when being asked
- // for. this happens when explicitly cancelling snippet mode,
- // e.g. when pressing ESC
- this._editor.setSelections([this._editor.getSelection()]);
- }
- }
- prev() {
- if (this._session) {
- this._session.prev();
- }
- this._updateState();
- }
- next() {
- if (this._session) {
- this._session.next();
- }
- this._updateState();
- }
- isInSnippet() {
- return Boolean(this._inSnippet.get());
- }
- };
- SnippetController2.ID = 'snippetController2';
- SnippetController2.InSnippetMode = new RawContextKey('inSnippetMode', false, localize('inSnippetMode', "Whether the editor in current in snippet mode"));
- SnippetController2.HasNextTabstop = new RawContextKey('hasNextTabstop', false, localize('hasNextTabstop', "Whether there is a next tab stop when in snippet mode"));
- SnippetController2.HasPrevTabstop = new RawContextKey('hasPrevTabstop', false, localize('hasPrevTabstop', "Whether there is a previous tab stop when in snippet mode"));
- SnippetController2 = __decorate([
- __param(1, ILogService),
- __param(2, ILanguageFeaturesService),
- __param(3, IContextKeyService),
- __param(4, ILanguageConfigurationService)
- ], SnippetController2);
- export { SnippetController2 };
- registerEditorContribution(SnippetController2.ID, SnippetController2);
- const CommandCtor = EditorCommand.bindToContribution(SnippetController2.get);
- registerEditorCommand(new CommandCtor({
- id: 'jumpToNextSnippetPlaceholder',
- precondition: ContextKeyExpr.and(SnippetController2.InSnippetMode, SnippetController2.HasNextTabstop),
- handler: ctrl => ctrl.next(),
- kbOpts: {
- weight: 100 /* KeybindingWeight.EditorContrib */ + 30,
- kbExpr: EditorContextKeys.editorTextFocus,
- primary: 2 /* KeyCode.Tab */
- }
- }));
- registerEditorCommand(new CommandCtor({
- id: 'jumpToPrevSnippetPlaceholder',
- precondition: ContextKeyExpr.and(SnippetController2.InSnippetMode, SnippetController2.HasPrevTabstop),
- handler: ctrl => ctrl.prev(),
- kbOpts: {
- weight: 100 /* KeybindingWeight.EditorContrib */ + 30,
- kbExpr: EditorContextKeys.editorTextFocus,
- primary: 1024 /* KeyMod.Shift */ | 2 /* KeyCode.Tab */
- }
- }));
- registerEditorCommand(new CommandCtor({
- id: 'leaveSnippet',
- precondition: SnippetController2.InSnippetMode,
- handler: ctrl => ctrl.cancel(true),
- kbOpts: {
- weight: 100 /* KeybindingWeight.EditorContrib */ + 30,
- kbExpr: EditorContextKeys.editorTextFocus,
- primary: 9 /* KeyCode.Escape */,
- secondary: [1024 /* KeyMod.Shift */ | 9 /* KeyCode.Escape */]
- }
- }));
- registerEditorCommand(new CommandCtor({
- id: 'acceptSnippet',
- precondition: SnippetController2.InSnippetMode,
- handler: ctrl => ctrl.finish(),
- // kbOpts: {
- // weight: KeybindingWeight.EditorContrib + 30,
- // kbExpr: EditorContextKeys.textFocus,
- // primary: KeyCode.Enter,
- // }
- }));
- // ---
- export function performSnippetEdit(editor, snippet, selections) {
- const controller = SnippetController2.get(editor);
- if (!controller) {
- return false;
- }
- editor.focus();
- controller.apply(selections.map(selection => {
- return {
- range: Selection.liftSelection(selection),
- template: snippet
- };
- }));
- return controller.isInSnippet();
- }
|