e0c23633bdbdc5ccc8938bc768e6319858b5057ae627bba723595a793322159c29933523999ecad933e90e4e6d1b82ecbd037400aa62688bec0f0f83af8ab3 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 { Emitter } from '../../../../base/common/event.js';
  24. import { Disposable, MutableDisposable, toDisposable } from '../../../../base/common/lifecycle.js';
  25. import { firstNonWhitespaceIndex } from '../../../../base/common/strings.js';
  26. import { EditorAction } from '../../../browser/editorExtensions.js';
  27. import { CursorColumns } from '../../../common/core/cursorColumns.js';
  28. import { EditorContextKeys } from '../../../common/editorContextKeys.js';
  29. import { GhostTextModel } from './ghostTextModel.js';
  30. import { GhostTextWidget } from './ghostTextWidget.js';
  31. import * as nls from '../../../../nls.js';
  32. import { ContextKeyExpr, IContextKeyService, RawContextKey } from '../../../../platform/contextkey/common/contextkey.js';
  33. import { IInstantiationService } from '../../../../platform/instantiation/common/instantiation.js';
  34. let GhostTextController = class GhostTextController extends Disposable {
  35. constructor(editor, instantiationService) {
  36. super();
  37. this.editor = editor;
  38. this.instantiationService = instantiationService;
  39. this.triggeredExplicitly = false;
  40. this.activeController = this._register(new MutableDisposable());
  41. this.activeModelDidChangeEmitter = this._register(new Emitter());
  42. this._register(this.editor.onDidChangeModel(() => {
  43. this.updateModelController();
  44. }));
  45. this._register(this.editor.onDidChangeConfiguration((e) => {
  46. if (e.hasChanged(108 /* EditorOption.suggest */)) {
  47. this.updateModelController();
  48. }
  49. if (e.hasChanged(57 /* EditorOption.inlineSuggest */)) {
  50. this.updateModelController();
  51. }
  52. }));
  53. this.updateModelController();
  54. }
  55. static get(editor) {
  56. return editor.getContribution(GhostTextController.ID);
  57. }
  58. get activeModel() {
  59. var _a;
  60. return (_a = this.activeController.value) === null || _a === void 0 ? void 0 : _a.model;
  61. }
  62. // Don't call this method when not necessary. It will recreate the activeController.
  63. updateModelController() {
  64. const suggestOptions = this.editor.getOption(108 /* EditorOption.suggest */);
  65. const inlineSuggestOptions = this.editor.getOption(57 /* EditorOption.inlineSuggest */);
  66. this.activeController.value = undefined;
  67. // ActiveGhostTextController is only created if one of those settings is set or if the inline completions are triggered explicitly.
  68. this.activeController.value =
  69. this.editor.hasModel() && (suggestOptions.preview || inlineSuggestOptions.enabled || this.triggeredExplicitly)
  70. ? this.instantiationService.createInstance(ActiveGhostTextController, this.editor)
  71. : undefined;
  72. this.activeModelDidChangeEmitter.fire();
  73. }
  74. shouldShowHoverAt(hoverRange) {
  75. var _a;
  76. return ((_a = this.activeModel) === null || _a === void 0 ? void 0 : _a.shouldShowHoverAt(hoverRange)) || false;
  77. }
  78. shouldShowHoverAtViewZone(viewZoneId) {
  79. var _a, _b;
  80. return ((_b = (_a = this.activeController.value) === null || _a === void 0 ? void 0 : _a.widget) === null || _b === void 0 ? void 0 : _b.shouldShowHoverAtViewZone(viewZoneId)) || false;
  81. }
  82. trigger() {
  83. var _a;
  84. this.triggeredExplicitly = true;
  85. if (!this.activeController.value) {
  86. this.updateModelController();
  87. }
  88. (_a = this.activeModel) === null || _a === void 0 ? void 0 : _a.triggerInlineCompletion();
  89. }
  90. commit() {
  91. var _a;
  92. (_a = this.activeModel) === null || _a === void 0 ? void 0 : _a.commitInlineCompletion();
  93. }
  94. hide() {
  95. var _a;
  96. (_a = this.activeModel) === null || _a === void 0 ? void 0 : _a.hideInlineCompletion();
  97. }
  98. showNextInlineCompletion() {
  99. var _a;
  100. (_a = this.activeModel) === null || _a === void 0 ? void 0 : _a.showNextInlineCompletion();
  101. }
  102. showPreviousInlineCompletion() {
  103. var _a;
  104. (_a = this.activeModel) === null || _a === void 0 ? void 0 : _a.showPreviousInlineCompletion();
  105. }
  106. hasMultipleInlineCompletions() {
  107. var _a;
  108. return __awaiter(this, void 0, void 0, function* () {
  109. const result = yield ((_a = this.activeModel) === null || _a === void 0 ? void 0 : _a.hasMultipleInlineCompletions());
  110. return result !== undefined ? result : false;
  111. });
  112. }
  113. };
  114. GhostTextController.inlineSuggestionVisible = new RawContextKey('inlineSuggestionVisible', false, nls.localize('inlineSuggestionVisible', "Whether an inline suggestion is visible"));
  115. GhostTextController.inlineSuggestionHasIndentation = new RawContextKey('inlineSuggestionHasIndentation', false, nls.localize('inlineSuggestionHasIndentation', "Whether the inline suggestion starts with whitespace"));
  116. GhostTextController.inlineSuggestionHasIndentationLessThanTabSize = new RawContextKey('inlineSuggestionHasIndentationLessThanTabSize', true, nls.localize('inlineSuggestionHasIndentationLessThanTabSize', "Whether the inline suggestion starts with whitespace that is less than what would be inserted by tab"));
  117. GhostTextController.ID = 'editor.contrib.ghostTextController';
  118. GhostTextController = __decorate([
  119. __param(1, IInstantiationService)
  120. ], GhostTextController);
  121. export { GhostTextController };
  122. class GhostTextContextKeys {
  123. constructor(contextKeyService) {
  124. this.contextKeyService = contextKeyService;
  125. this.inlineCompletionVisible = GhostTextController.inlineSuggestionVisible.bindTo(this.contextKeyService);
  126. this.inlineCompletionSuggestsIndentation = GhostTextController.inlineSuggestionHasIndentation.bindTo(this.contextKeyService);
  127. this.inlineCompletionSuggestsIndentationLessThanTabSize = GhostTextController.inlineSuggestionHasIndentationLessThanTabSize.bindTo(this.contextKeyService);
  128. }
  129. }
  130. /**
  131. * The controller for a text editor with an initialized text model.
  132. * Must be disposed as soon as the model detaches from the editor.
  133. */
  134. let ActiveGhostTextController = class ActiveGhostTextController extends Disposable {
  135. constructor(editor, instantiationService, contextKeyService) {
  136. super();
  137. this.editor = editor;
  138. this.instantiationService = instantiationService;
  139. this.contextKeyService = contextKeyService;
  140. this.contextKeys = new GhostTextContextKeys(this.contextKeyService);
  141. this.model = this._register(this.instantiationService.createInstance(GhostTextModel, this.editor));
  142. this.widget = this._register(this.instantiationService.createInstance(GhostTextWidget, this.editor, this.model));
  143. this._register(toDisposable(() => {
  144. this.contextKeys.inlineCompletionVisible.set(false);
  145. this.contextKeys.inlineCompletionSuggestsIndentation.set(false);
  146. this.contextKeys.inlineCompletionSuggestsIndentationLessThanTabSize.set(true);
  147. }));
  148. this._register(this.model.onDidChange(() => {
  149. this.updateContextKeys();
  150. }));
  151. this.updateContextKeys();
  152. }
  153. updateContextKeys() {
  154. var _a;
  155. this.contextKeys.inlineCompletionVisible.set(((_a = this.model.activeInlineCompletionsModel) === null || _a === void 0 ? void 0 : _a.ghostText) !== undefined);
  156. let startsWithIndentation = false;
  157. let startsWithIndentationLessThanTabSize = true;
  158. const ghostText = this.model.inlineCompletionsModel.ghostText;
  159. if (!!this.model.activeInlineCompletionsModel && ghostText && ghostText.parts.length > 0) {
  160. const { column, lines } = ghostText.parts[0];
  161. const firstLine = lines[0];
  162. const indentationEndColumn = this.editor.getModel().getLineIndentColumn(ghostText.lineNumber);
  163. const inIndentation = column <= indentationEndColumn;
  164. if (inIndentation) {
  165. let firstNonWsIdx = firstNonWhitespaceIndex(firstLine);
  166. if (firstNonWsIdx === -1) {
  167. firstNonWsIdx = firstLine.length - 1;
  168. }
  169. startsWithIndentation = firstNonWsIdx > 0;
  170. const tabSize = this.editor.getModel().getOptions().tabSize;
  171. const visibleColumnIndentation = CursorColumns.visibleColumnFromColumn(firstLine, firstNonWsIdx + 1, tabSize);
  172. startsWithIndentationLessThanTabSize = visibleColumnIndentation < tabSize;
  173. }
  174. }
  175. this.contextKeys.inlineCompletionSuggestsIndentation.set(startsWithIndentation);
  176. this.contextKeys.inlineCompletionSuggestsIndentationLessThanTabSize.set(startsWithIndentationLessThanTabSize);
  177. }
  178. };
  179. ActiveGhostTextController = __decorate([
  180. __param(1, IInstantiationService),
  181. __param(2, IContextKeyService)
  182. ], ActiveGhostTextController);
  183. export { ActiveGhostTextController };
  184. export class ShowNextInlineSuggestionAction extends EditorAction {
  185. constructor() {
  186. super({
  187. id: ShowNextInlineSuggestionAction.ID,
  188. label: nls.localize('action.inlineSuggest.showNext', "Show Next Inline Suggestion"),
  189. alias: 'Show Next Inline Suggestion',
  190. precondition: ContextKeyExpr.and(EditorContextKeys.writable, GhostTextController.inlineSuggestionVisible),
  191. kbOpts: {
  192. weight: 100,
  193. primary: 512 /* KeyMod.Alt */ | 89 /* KeyCode.BracketRight */,
  194. },
  195. });
  196. }
  197. run(accessor, editor) {
  198. return __awaiter(this, void 0, void 0, function* () {
  199. const controller = GhostTextController.get(editor);
  200. if (controller) {
  201. controller.showNextInlineCompletion();
  202. editor.focus();
  203. }
  204. });
  205. }
  206. }
  207. ShowNextInlineSuggestionAction.ID = 'editor.action.inlineSuggest.showNext';
  208. export class ShowPreviousInlineSuggestionAction extends EditorAction {
  209. constructor() {
  210. super({
  211. id: ShowPreviousInlineSuggestionAction.ID,
  212. label: nls.localize('action.inlineSuggest.showPrevious', "Show Previous Inline Suggestion"),
  213. alias: 'Show Previous Inline Suggestion',
  214. precondition: ContextKeyExpr.and(EditorContextKeys.writable, GhostTextController.inlineSuggestionVisible),
  215. kbOpts: {
  216. weight: 100,
  217. primary: 512 /* KeyMod.Alt */ | 87 /* KeyCode.BracketLeft */,
  218. },
  219. });
  220. }
  221. run(accessor, editor) {
  222. return __awaiter(this, void 0, void 0, function* () {
  223. const controller = GhostTextController.get(editor);
  224. if (controller) {
  225. controller.showPreviousInlineCompletion();
  226. editor.focus();
  227. }
  228. });
  229. }
  230. }
  231. ShowPreviousInlineSuggestionAction.ID = 'editor.action.inlineSuggest.showPrevious';
  232. export class TriggerInlineSuggestionAction extends EditorAction {
  233. constructor() {
  234. super({
  235. id: 'editor.action.inlineSuggest.trigger',
  236. label: nls.localize('action.inlineSuggest.trigger', "Trigger Inline Suggestion"),
  237. alias: 'Trigger Inline Suggestion',
  238. precondition: EditorContextKeys.writable
  239. });
  240. }
  241. run(accessor, editor) {
  242. return __awaiter(this, void 0, void 0, function* () {
  243. const controller = GhostTextController.get(editor);
  244. if (controller) {
  245. controller.trigger();
  246. }
  247. });
  248. }
  249. }