ghostTextController.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 { Disposable, MutableDisposable, toDisposable } from '../../../base/common/lifecycle.js';
  24. import { firstNonWhitespaceIndex } from '../../../base/common/strings.js';
  25. import { EditorAction, EditorCommand, registerEditorAction, registerEditorCommand, registerEditorContribution } from '../../browser/editorExtensions.js';
  26. import { CursorColumns } from '../../common/controller/cursorColumns.js';
  27. import { EditorContextKeys } from '../../common/editorContextKeys.js';
  28. import { inlineSuggestCommitId } from './consts.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. import { KeybindingsRegistry } from '../../../platform/keybinding/common/keybindingsRegistry.js';
  35. let GhostTextController = class GhostTextController extends Disposable {
  36. constructor(editor, instantiationService) {
  37. super();
  38. this.editor = editor;
  39. this.instantiationService = instantiationService;
  40. this.triggeredExplicitly = false;
  41. this.activeController = this._register(new MutableDisposable());
  42. this._register(this.editor.onDidChangeModel(() => {
  43. this.updateModelController();
  44. }));
  45. this._register(this.editor.onDidChangeConfiguration((e) => {
  46. if (e.hasChanged(105 /* suggest */)) {
  47. this.updateModelController();
  48. }
  49. if (e.hasChanged(54 /* 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 neccessary. It will recreate the activeController.
  63. updateModelController() {
  64. const suggestOptions = this.editor.getOption(105 /* suggest */);
  65. const inlineSuggestOptions = this.editor.getOption(54 /* 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. }
  73. shouldShowHoverAt(hoverRange) {
  74. var _a;
  75. return ((_a = this.activeModel) === null || _a === void 0 ? void 0 : _a.shouldShowHoverAt(hoverRange)) || false;
  76. }
  77. shouldShowHoverAtViewZone(viewZoneId) {
  78. var _a, _b;
  79. return ((_b = (_a = this.activeController.value) === null || _a === void 0 ? void 0 : _a.widget) === null || _b === void 0 ? void 0 : _b.shouldShowHoverAtViewZone(viewZoneId)) || false;
  80. }
  81. trigger() {
  82. var _a;
  83. this.triggeredExplicitly = true;
  84. if (!this.activeController.value) {
  85. this.updateModelController();
  86. }
  87. (_a = this.activeModel) === null || _a === void 0 ? void 0 : _a.triggerInlineCompletion();
  88. }
  89. commit() {
  90. var _a;
  91. (_a = this.activeModel) === null || _a === void 0 ? void 0 : _a.commitInlineCompletion();
  92. }
  93. hide() {
  94. var _a;
  95. (_a = this.activeModel) === null || _a === void 0 ? void 0 : _a.hideInlineCompletion();
  96. }
  97. showNextInlineCompletion() {
  98. var _a;
  99. (_a = this.activeModel) === null || _a === void 0 ? void 0 : _a.showNextInlineCompletion();
  100. }
  101. showPreviousInlineCompletion() {
  102. var _a;
  103. (_a = this.activeModel) === null || _a === void 0 ? void 0 : _a.showPreviousInlineCompletion();
  104. }
  105. hasMultipleInlineCompletions() {
  106. var _a;
  107. return __awaiter(this, void 0, void 0, function* () {
  108. const result = yield ((_a = this.activeModel) === null || _a === void 0 ? void 0 : _a.hasMultipleInlineCompletions());
  109. return result !== undefined ? result : false;
  110. });
  111. }
  112. };
  113. GhostTextController.inlineSuggestionVisible = new RawContextKey('inlineSuggestionVisible', false, nls.localize('inlineSuggestionVisible', "Whether an inline suggestion is visible"));
  114. GhostTextController.inlineSuggestionHasIndentation = new RawContextKey('inlineSuggestionHasIndentation', false, nls.localize('inlineSuggestionHasIndentation', "Whether the inline suggestion starts with whitespace"));
  115. 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"));
  116. GhostTextController.ID = 'editor.contrib.ghostTextController';
  117. GhostTextController = __decorate([
  118. __param(1, IInstantiationService)
  119. ], GhostTextController);
  120. export { GhostTextController };
  121. class GhostTextContextKeys {
  122. constructor(contextKeyService) {
  123. this.contextKeyService = contextKeyService;
  124. this.inlineCompletionVisible = GhostTextController.inlineSuggestionVisible.bindTo(this.contextKeyService);
  125. this.inlineCompletionSuggestsIndentation = GhostTextController.inlineSuggestionHasIndentation.bindTo(this.contextKeyService);
  126. this.inlineCompletionSuggestsIndentationLessThanTabSize = GhostTextController.inlineSuggestionHasIndentationLessThanTabSize.bindTo(this.contextKeyService);
  127. }
  128. }
  129. /**
  130. * The controller for a text editor with an initialized text model.
  131. * Must be disposed as soon as the model detaches from the editor.
  132. */
  133. let ActiveGhostTextController = class ActiveGhostTextController extends Disposable {
  134. constructor(editor, instantiationService, contextKeyService) {
  135. super();
  136. this.editor = editor;
  137. this.instantiationService = instantiationService;
  138. this.contextKeyService = contextKeyService;
  139. this.contextKeys = new GhostTextContextKeys(this.contextKeyService);
  140. this.model = this._register(this.instantiationService.createInstance(GhostTextModel, this.editor));
  141. this.widget = this._register(this.instantiationService.createInstance(GhostTextWidget, this.editor, this.model));
  142. this._register(toDisposable(() => {
  143. this.contextKeys.inlineCompletionVisible.set(false);
  144. this.contextKeys.inlineCompletionSuggestsIndentation.set(false);
  145. this.contextKeys.inlineCompletionSuggestsIndentationLessThanTabSize.set(true);
  146. }));
  147. this._register(this.model.onDidChange(() => {
  148. this.updateContextKeys();
  149. }));
  150. this.updateContextKeys();
  151. }
  152. updateContextKeys() {
  153. var _a;
  154. this.contextKeys.inlineCompletionVisible.set(((_a = this.model.activeInlineCompletionsModel) === null || _a === void 0 ? void 0 : _a.ghostText) !== undefined);
  155. let startsWithIndentation = false;
  156. let startsWithIndentationLessThanTabSize = true;
  157. const ghostText = this.model.inlineCompletionsModel.ghostText;
  158. if (!!this.model.activeInlineCompletionsModel && ghostText && ghostText.parts.length > 0) {
  159. const { column, lines } = ghostText.parts[0];
  160. const firstLine = lines[0];
  161. const indentationEndColumn = this.editor.getModel().getLineIndentColumn(ghostText.lineNumber);
  162. const inIndentation = column <= indentationEndColumn;
  163. if (inIndentation) {
  164. let firstNonWsIdx = firstNonWhitespaceIndex(firstLine);
  165. if (firstNonWsIdx === -1) {
  166. firstNonWsIdx = firstLine.length - 1;
  167. }
  168. startsWithIndentation = firstNonWsIdx > 0;
  169. const tabSize = this.editor.getModel().getOptions().tabSize;
  170. const visibleColumnIndentation = CursorColumns.visibleColumnFromColumn(firstLine, firstNonWsIdx + 1, tabSize);
  171. startsWithIndentationLessThanTabSize = visibleColumnIndentation < tabSize;
  172. }
  173. }
  174. this.contextKeys.inlineCompletionSuggestsIndentation.set(startsWithIndentation);
  175. this.contextKeys.inlineCompletionSuggestsIndentationLessThanTabSize.set(startsWithIndentationLessThanTabSize);
  176. }
  177. };
  178. ActiveGhostTextController = __decorate([
  179. __param(1, IInstantiationService),
  180. __param(2, IContextKeyService)
  181. ], ActiveGhostTextController);
  182. export { ActiveGhostTextController };
  183. const GhostTextCommand = EditorCommand.bindToContribution(GhostTextController.get);
  184. export const commitInlineSuggestionAction = new GhostTextCommand({
  185. id: inlineSuggestCommitId,
  186. precondition: GhostTextController.inlineSuggestionVisible,
  187. handler(x) {
  188. x.commit();
  189. x.editor.focus();
  190. }
  191. });
  192. registerEditorCommand(commitInlineSuggestionAction);
  193. KeybindingsRegistry.registerKeybindingRule({
  194. primary: 2 /* Tab */,
  195. weight: 200,
  196. id: commitInlineSuggestionAction.id,
  197. when: ContextKeyExpr.and(commitInlineSuggestionAction.precondition, EditorContextKeys.tabMovesFocus.toNegated(), GhostTextController.inlineSuggestionHasIndentationLessThanTabSize),
  198. });
  199. registerEditorCommand(new GhostTextCommand({
  200. id: 'editor.action.inlineSuggest.hide',
  201. precondition: GhostTextController.inlineSuggestionVisible,
  202. kbOpts: {
  203. weight: 100,
  204. primary: 9 /* Escape */,
  205. },
  206. handler(x) {
  207. x.hide();
  208. }
  209. }));
  210. export class ShowNextInlineSuggestionAction extends EditorAction {
  211. constructor() {
  212. super({
  213. id: ShowNextInlineSuggestionAction.ID,
  214. label: nls.localize('action.inlineSuggest.showNext', "Show Next Inline Suggestion"),
  215. alias: 'Show Next Inline Suggestion',
  216. precondition: ContextKeyExpr.and(EditorContextKeys.writable, GhostTextController.inlineSuggestionVisible),
  217. kbOpts: {
  218. weight: 100,
  219. primary: 512 /* Alt */ | 89 /* BracketRight */,
  220. },
  221. });
  222. }
  223. run(accessor, editor) {
  224. return __awaiter(this, void 0, void 0, function* () {
  225. const controller = GhostTextController.get(editor);
  226. if (controller) {
  227. controller.showNextInlineCompletion();
  228. editor.focus();
  229. }
  230. });
  231. }
  232. }
  233. ShowNextInlineSuggestionAction.ID = 'editor.action.inlineSuggest.showNext';
  234. export class ShowPreviousInlineSuggestionAction extends EditorAction {
  235. constructor() {
  236. super({
  237. id: ShowPreviousInlineSuggestionAction.ID,
  238. label: nls.localize('action.inlineSuggest.showPrevious', "Show Previous Inline Suggestion"),
  239. alias: 'Show Previous Inline Suggestion',
  240. precondition: ContextKeyExpr.and(EditorContextKeys.writable, GhostTextController.inlineSuggestionVisible),
  241. kbOpts: {
  242. weight: 100,
  243. primary: 512 /* Alt */ | 87 /* BracketLeft */,
  244. },
  245. });
  246. }
  247. run(accessor, editor) {
  248. return __awaiter(this, void 0, void 0, function* () {
  249. const controller = GhostTextController.get(editor);
  250. if (controller) {
  251. controller.showPreviousInlineCompletion();
  252. editor.focus();
  253. }
  254. });
  255. }
  256. }
  257. ShowPreviousInlineSuggestionAction.ID = 'editor.action.inlineSuggest.showPrevious';
  258. export class TriggerInlineSuggestionAction extends EditorAction {
  259. constructor() {
  260. super({
  261. id: 'editor.action.inlineSuggest.trigger',
  262. label: nls.localize('action.inlineSuggest.trigger', "Trigger Inline Suggestion"),
  263. alias: 'Trigger Inline Suggestion',
  264. precondition: EditorContextKeys.writable
  265. });
  266. }
  267. run(accessor, editor) {
  268. return __awaiter(this, void 0, void 0, function* () {
  269. const controller = GhostTextController.get(editor);
  270. if (controller) {
  271. controller.trigger();
  272. }
  273. });
  274. }
  275. }
  276. registerEditorContribution(GhostTextController.ID, GhostTextController);
  277. registerEditorAction(TriggerInlineSuggestionAction);
  278. registerEditorAction(ShowNextInlineSuggestionAction);
  279. registerEditorAction(ShowPreviousInlineSuggestionAction);