suggestAlternatives.js 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 { IContextKeyService, RawContextKey } from '../../../platform/contextkey/common/contextkey.js';
  15. let SuggestAlternatives = class SuggestAlternatives {
  16. constructor(_editor, contextKeyService) {
  17. this._editor = _editor;
  18. this._index = 0;
  19. this._ckOtherSuggestions = SuggestAlternatives.OtherSuggestions.bindTo(contextKeyService);
  20. }
  21. dispose() {
  22. this.reset();
  23. }
  24. reset() {
  25. var _a;
  26. this._ckOtherSuggestions.reset();
  27. (_a = this._listener) === null || _a === void 0 ? void 0 : _a.dispose();
  28. this._model = undefined;
  29. this._acceptNext = undefined;
  30. this._ignore = false;
  31. }
  32. set({ model, index }, acceptNext) {
  33. // no suggestions -> nothing to do
  34. if (model.items.length === 0) {
  35. this.reset();
  36. return;
  37. }
  38. // no alternative suggestions -> nothing to do
  39. let nextIndex = SuggestAlternatives._moveIndex(true, model, index);
  40. if (nextIndex === index) {
  41. this.reset();
  42. return;
  43. }
  44. this._acceptNext = acceptNext;
  45. this._model = model;
  46. this._index = index;
  47. this._listener = this._editor.onDidChangeCursorPosition(() => {
  48. if (!this._ignore) {
  49. this.reset();
  50. }
  51. });
  52. this._ckOtherSuggestions.set(true);
  53. }
  54. static _moveIndex(fwd, model, index) {
  55. let newIndex = index;
  56. while (true) {
  57. newIndex = (newIndex + model.items.length + (fwd ? +1 : -1)) % model.items.length;
  58. if (newIndex === index) {
  59. break;
  60. }
  61. if (!model.items[newIndex].completion.additionalTextEdits) {
  62. break;
  63. }
  64. }
  65. return newIndex;
  66. }
  67. next() {
  68. this._move(true);
  69. }
  70. prev() {
  71. this._move(false);
  72. }
  73. _move(fwd) {
  74. if (!this._model) {
  75. // nothing to reason about
  76. return;
  77. }
  78. try {
  79. this._ignore = true;
  80. this._index = SuggestAlternatives._moveIndex(fwd, this._model, this._index);
  81. this._acceptNext({ index: this._index, item: this._model.items[this._index], model: this._model });
  82. }
  83. finally {
  84. this._ignore = false;
  85. }
  86. }
  87. };
  88. SuggestAlternatives.OtherSuggestions = new RawContextKey('hasOtherSuggestions', false);
  89. SuggestAlternatives = __decorate([
  90. __param(1, IContextKeyService)
  91. ], SuggestAlternatives);
  92. export { SuggestAlternatives };