suggestModel.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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 { TimeoutTimer } from '../../../base/common/async.js';
  24. import { CancellationTokenSource } from '../../../base/common/cancellation.js';
  25. import { onUnexpectedError } from '../../../base/common/errors.js';
  26. import { Emitter } from '../../../base/common/event.js';
  27. import { DisposableStore, dispose } from '../../../base/common/lifecycle.js';
  28. import { getLeadingWhitespace, isHighSurrogate, isLowSurrogate } from '../../../base/common/strings.js';
  29. import { Selection } from '../../common/core/selection.js';
  30. import { CompletionProviderRegistry } from '../../common/modes.js';
  31. import { IEditorWorkerService } from '../../common/services/editorWorkerService.js';
  32. import { SnippetController2 } from '../snippet/snippetController2.js';
  33. import { WordDistance } from './wordDistance.js';
  34. import { IClipboardService } from '../../../platform/clipboard/common/clipboardService.js';
  35. import { IConfigurationService } from '../../../platform/configuration/common/configuration.js';
  36. import { IContextKeyService } from '../../../platform/contextkey/common/contextkey.js';
  37. import { ILogService } from '../../../platform/log/common/log.js';
  38. import { ITelemetryService } from '../../../platform/telemetry/common/telemetry.js';
  39. import { CompletionModel } from './completionModel.js';
  40. import { CompletionOptions, getSnippetSuggestSupport, getSuggestionComparator, provideSuggestionItems } from './suggest.js';
  41. export class LineContext {
  42. constructor(model, position, auto, shy) {
  43. this.leadingLineContent = model.getLineContent(position.lineNumber).substr(0, position.column - 1);
  44. this.leadingWord = model.getWordUntilPosition(position);
  45. this.lineNumber = position.lineNumber;
  46. this.column = position.column;
  47. this.auto = auto;
  48. this.shy = shy;
  49. }
  50. static shouldAutoTrigger(editor) {
  51. if (!editor.hasModel()) {
  52. return false;
  53. }
  54. const model = editor.getModel();
  55. const pos = editor.getPosition();
  56. model.tokenizeIfCheap(pos.lineNumber);
  57. const word = model.getWordAtPosition(pos);
  58. if (!word) {
  59. return false;
  60. }
  61. if (word.endColumn !== pos.column) {
  62. return false;
  63. }
  64. if (!isNaN(Number(word.word))) {
  65. return false;
  66. }
  67. return true;
  68. }
  69. }
  70. function isSuggestPreviewEnabled(editor) {
  71. return editor.getOption(105 /* suggest */).preview;
  72. }
  73. function canShowQuickSuggest(editor, contextKeyService, configurationService) {
  74. if (!Boolean(contextKeyService.getContextKeyValue('inlineSuggestionVisible'))) {
  75. // Allow if there is no inline suggestion.
  76. return true;
  77. }
  78. const allowQuickSuggestions = configurationService.getValue('editor.inlineSuggest.allowQuickSuggestions');
  79. if (allowQuickSuggestions !== undefined) {
  80. // Use setting if available.
  81. return Boolean(allowQuickSuggestions);
  82. }
  83. // Don't allow if inline suggestions are visible and no suggest preview is configured.
  84. // TODO disabled for copilot
  85. return false && isSuggestPreviewEnabled(editor);
  86. }
  87. function canShowSuggestOnTriggerCharacters(editor, contextKeyService, configurationService) {
  88. if (!Boolean(contextKeyService.getContextKeyValue('inlineSuggestionVisible'))) {
  89. // Allow if there is no inline suggestion.
  90. return true;
  91. }
  92. const allowQuickSuggestions = configurationService.getValue('editor.inlineSuggest.allowSuggestOnTriggerCharacters');
  93. if (allowQuickSuggestions !== undefined) {
  94. // Use setting if available.
  95. return Boolean(allowQuickSuggestions);
  96. }
  97. // Don't allow if inline suggestions are visible and no suggest preview is configured.
  98. // TODO disabled for copilot
  99. return false && isSuggestPreviewEnabled(editor);
  100. }
  101. let SuggestModel = class SuggestModel {
  102. constructor(_editor, _editorWorkerService, _clipboardService, _telemetryService, _logService, _contextKeyService, _configurationService) {
  103. this._editor = _editor;
  104. this._editorWorkerService = _editorWorkerService;
  105. this._clipboardService = _clipboardService;
  106. this._telemetryService = _telemetryService;
  107. this._logService = _logService;
  108. this._contextKeyService = _contextKeyService;
  109. this._configurationService = _configurationService;
  110. this._toDispose = new DisposableStore();
  111. this._quickSuggestDelay = 10;
  112. this._triggerCharacterListener = new DisposableStore();
  113. this._triggerQuickSuggest = new TimeoutTimer();
  114. this._state = 0 /* Idle */;
  115. this._completionDisposables = new DisposableStore();
  116. this._onDidCancel = new Emitter();
  117. this._onDidTrigger = new Emitter();
  118. this._onDidSuggest = new Emitter();
  119. this.onDidCancel = this._onDidCancel.event;
  120. this.onDidTrigger = this._onDidTrigger.event;
  121. this.onDidSuggest = this._onDidSuggest.event;
  122. this._telemetryGate = 0;
  123. this._currentSelection = this._editor.getSelection() || new Selection(1, 1, 1, 1);
  124. // wire up various listeners
  125. this._toDispose.add(this._editor.onDidChangeModel(() => {
  126. this._updateTriggerCharacters();
  127. this.cancel();
  128. }));
  129. this._toDispose.add(this._editor.onDidChangeModelLanguage(() => {
  130. this._updateTriggerCharacters();
  131. this.cancel();
  132. }));
  133. this._toDispose.add(this._editor.onDidChangeConfiguration(() => {
  134. this._updateTriggerCharacters();
  135. this._updateQuickSuggest();
  136. }));
  137. this._toDispose.add(CompletionProviderRegistry.onDidChange(() => {
  138. this._updateTriggerCharacters();
  139. this._updateActiveSuggestSession();
  140. }));
  141. this._toDispose.add(this._editor.onDidChangeCursorSelection(e => {
  142. this._onCursorChange(e);
  143. }));
  144. let editorIsComposing = false;
  145. this._toDispose.add(this._editor.onDidCompositionStart(() => {
  146. editorIsComposing = true;
  147. }));
  148. this._toDispose.add(this._editor.onDidCompositionEnd(() => {
  149. editorIsComposing = false;
  150. this._onCompositionEnd();
  151. }));
  152. this._toDispose.add(this._editor.onDidChangeModelContent(() => {
  153. // only filter completions when the editor isn't composing a character
  154. // allow-any-unicode-next-line
  155. // e.g. ¨ + u makes ü but just ¨ cannot be used for filtering
  156. if (!editorIsComposing) {
  157. this._refilterCompletionItems();
  158. }
  159. }));
  160. this._updateTriggerCharacters();
  161. this._updateQuickSuggest();
  162. }
  163. dispose() {
  164. dispose(this._triggerCharacterListener);
  165. dispose([this._onDidCancel, this._onDidSuggest, this._onDidTrigger, this._triggerQuickSuggest]);
  166. this._toDispose.dispose();
  167. this._completionDisposables.dispose();
  168. this.cancel();
  169. }
  170. // --- handle configuration & precondition changes
  171. _updateQuickSuggest() {
  172. this._quickSuggestDelay = this._editor.getOption(79 /* quickSuggestionsDelay */);
  173. if (isNaN(this._quickSuggestDelay) || (!this._quickSuggestDelay && this._quickSuggestDelay !== 0) || this._quickSuggestDelay < 0) {
  174. this._quickSuggestDelay = 10;
  175. }
  176. }
  177. _updateTriggerCharacters() {
  178. this._triggerCharacterListener.clear();
  179. if (this._editor.getOption(80 /* readOnly */)
  180. || !this._editor.hasModel()
  181. || !this._editor.getOption(108 /* suggestOnTriggerCharacters */)) {
  182. return;
  183. }
  184. const supportsByTriggerCharacter = new Map();
  185. for (const support of CompletionProviderRegistry.all(this._editor.getModel())) {
  186. for (const ch of support.triggerCharacters || []) {
  187. let set = supportsByTriggerCharacter.get(ch);
  188. if (!set) {
  189. set = new Set();
  190. set.add(getSnippetSuggestSupport());
  191. supportsByTriggerCharacter.set(ch, set);
  192. }
  193. set.add(support);
  194. }
  195. }
  196. const checkTriggerCharacter = (text) => {
  197. if (!canShowSuggestOnTriggerCharacters(this._editor, this._contextKeyService, this._configurationService)) {
  198. return;
  199. }
  200. if (LineContext.shouldAutoTrigger(this._editor)) {
  201. // don't trigger by trigger characters when this is a case for quick suggest
  202. return;
  203. }
  204. if (!text) {
  205. // came here from the compositionEnd-event
  206. const position = this._editor.getPosition();
  207. const model = this._editor.getModel();
  208. text = model.getLineContent(position.lineNumber).substr(0, position.column - 1);
  209. }
  210. let lastChar = '';
  211. if (isLowSurrogate(text.charCodeAt(text.length - 1))) {
  212. if (isHighSurrogate(text.charCodeAt(text.length - 2))) {
  213. lastChar = text.substr(text.length - 2);
  214. }
  215. }
  216. else {
  217. lastChar = text.charAt(text.length - 1);
  218. }
  219. const supports = supportsByTriggerCharacter.get(lastChar);
  220. if (supports) {
  221. // keep existing items that where not computed by the
  222. // supports/providers that want to trigger now
  223. const existing = this._completionModel
  224. ? { items: this._completionModel.adopt(supports), clipboardText: this._completionModel.clipboardText }
  225. : undefined;
  226. this.trigger({ auto: true, shy: false, triggerCharacter: lastChar }, Boolean(this._completionModel), supports, existing);
  227. }
  228. };
  229. this._triggerCharacterListener.add(this._editor.onDidType(checkTriggerCharacter));
  230. this._triggerCharacterListener.add(this._editor.onDidCompositionEnd(checkTriggerCharacter));
  231. }
  232. // --- trigger/retrigger/cancel suggest
  233. get state() {
  234. return this._state;
  235. }
  236. cancel(retrigger = false) {
  237. var _a;
  238. if (this._state !== 0 /* Idle */) {
  239. this._triggerQuickSuggest.cancel();
  240. (_a = this._requestToken) === null || _a === void 0 ? void 0 : _a.cancel();
  241. this._requestToken = undefined;
  242. this._state = 0 /* Idle */;
  243. this._completionModel = undefined;
  244. this._context = undefined;
  245. this._onDidCancel.fire({ retrigger });
  246. }
  247. }
  248. clear() {
  249. this._completionDisposables.clear();
  250. }
  251. _updateActiveSuggestSession() {
  252. if (this._state !== 0 /* Idle */) {
  253. if (!this._editor.hasModel() || !CompletionProviderRegistry.has(this._editor.getModel())) {
  254. this.cancel();
  255. }
  256. else {
  257. this.trigger({ auto: this._state === 2 /* Auto */, shy: false }, true);
  258. }
  259. }
  260. }
  261. _onCursorChange(e) {
  262. if (!this._editor.hasModel()) {
  263. return;
  264. }
  265. const prevSelection = this._currentSelection;
  266. this._currentSelection = this._editor.getSelection();
  267. if (!e.selection.isEmpty()
  268. || (e.reason !== 0 /* NotSet */ && e.reason !== 3 /* Explicit */)
  269. || (e.source !== 'keyboard' && e.source !== 'deleteLeft')) {
  270. // Early exit if nothing needs to be done!
  271. // Leave some form of early exit check here if you wish to continue being a cursor position change listener ;)
  272. this.cancel();
  273. return;
  274. }
  275. if (this._state === 0 /* Idle */ && e.reason === 0 /* NotSet */) {
  276. if (prevSelection.containsRange(this._currentSelection) || prevSelection.getEndPosition().isBeforeOrEqual(this._currentSelection.getPosition())) {
  277. // cursor did move RIGHT due to typing -> trigger quick suggest
  278. this._doTriggerQuickSuggest();
  279. }
  280. }
  281. else if (this._state !== 0 /* Idle */ && e.reason === 3 /* Explicit */) {
  282. // suggest is active and something like cursor keys are used to move
  283. // the cursor. this means we can refilter at the new position
  284. this._refilterCompletionItems();
  285. }
  286. }
  287. _onCompositionEnd() {
  288. // trigger or refilter when composition ends
  289. if (this._state === 0 /* Idle */) {
  290. this._doTriggerQuickSuggest();
  291. }
  292. else {
  293. this._refilterCompletionItems();
  294. }
  295. }
  296. _doTriggerQuickSuggest() {
  297. if (this._editor.getOption(78 /* quickSuggestions */) === false) {
  298. // not enabled
  299. return;
  300. }
  301. if (this._editor.getOption(105 /* suggest */).snippetsPreventQuickSuggestions && SnippetController2.get(this._editor).isInSnippet()) {
  302. // no quick suggestion when in snippet mode
  303. return;
  304. }
  305. this.cancel();
  306. this._triggerQuickSuggest.cancelAndSet(() => {
  307. if (this._state !== 0 /* Idle */) {
  308. return;
  309. }
  310. if (!LineContext.shouldAutoTrigger(this._editor)) {
  311. return;
  312. }
  313. if (!this._editor.hasModel()) {
  314. return;
  315. }
  316. const model = this._editor.getModel();
  317. const pos = this._editor.getPosition();
  318. // validate enabled now
  319. const quickSuggestions = this._editor.getOption(78 /* quickSuggestions */);
  320. if (quickSuggestions === false) {
  321. return;
  322. }
  323. else if (quickSuggestions === true) {
  324. // all good
  325. }
  326. else {
  327. // Check the type of the token that triggered this
  328. model.tokenizeIfCheap(pos.lineNumber);
  329. const lineTokens = model.getLineTokens(pos.lineNumber);
  330. const tokenType = lineTokens.getStandardTokenType(lineTokens.findTokenIndexAtOffset(Math.max(pos.column - 1 - 1, 0)));
  331. const inValidScope = quickSuggestions.other && tokenType === 0 /* Other */
  332. || quickSuggestions.comments && tokenType === 1 /* Comment */
  333. || quickSuggestions.strings && tokenType === 2 /* String */;
  334. if (!inValidScope) {
  335. return;
  336. }
  337. }
  338. if (!canShowQuickSuggest(this._editor, this._contextKeyService, this._configurationService)) {
  339. // do not trigger quick suggestions if inline suggestions are shown
  340. return;
  341. }
  342. if (!CompletionProviderRegistry.has(model)) {
  343. return;
  344. }
  345. // we made it till here -> trigger now
  346. this.trigger({ auto: true, shy: false });
  347. }, this._quickSuggestDelay);
  348. }
  349. _refilterCompletionItems() {
  350. // Re-filter suggestions. This MUST run async because filtering/scoring
  351. // uses the model content AND the cursor position. The latter is NOT
  352. // updated when the document has changed (the event which drives this method)
  353. // and therefore a little pause (next mirco task) is needed. See:
  354. // https://stackoverflow.com/questions/25915634/difference-between-microtask-and-macrotask-within-an-event-loop-context#25933985
  355. Promise.resolve().then(() => {
  356. if (this._state === 0 /* Idle */) {
  357. return;
  358. }
  359. if (!this._editor.hasModel()) {
  360. return;
  361. }
  362. const model = this._editor.getModel();
  363. const position = this._editor.getPosition();
  364. const ctx = new LineContext(model, position, this._state === 2 /* Auto */, false);
  365. this._onNewContext(ctx);
  366. });
  367. }
  368. trigger(context, retrigger = false, onlyFrom, existing) {
  369. var _a;
  370. if (!this._editor.hasModel()) {
  371. return;
  372. }
  373. const model = this._editor.getModel();
  374. const auto = context.auto;
  375. const ctx = new LineContext(model, this._editor.getPosition(), auto, context.shy);
  376. // Cancel previous requests, change state & update UI
  377. this.cancel(retrigger);
  378. this._state = auto ? 2 /* Auto */ : 1 /* Manual */;
  379. this._onDidTrigger.fire({ auto, shy: context.shy, position: this._editor.getPosition() });
  380. // Capture context when request was sent
  381. this._context = ctx;
  382. // Build context for request
  383. let suggestCtx = { triggerKind: (_a = context.triggerKind) !== null && _a !== void 0 ? _a : 0 /* Invoke */ };
  384. if (context.triggerCharacter) {
  385. suggestCtx = {
  386. triggerKind: 1 /* TriggerCharacter */,
  387. triggerCharacter: context.triggerCharacter
  388. };
  389. }
  390. this._requestToken = new CancellationTokenSource();
  391. // kind filter and snippet sort rules
  392. const snippetSuggestions = this._editor.getOption(100 /* snippetSuggestions */);
  393. let snippetSortOrder = 1 /* Inline */;
  394. switch (snippetSuggestions) {
  395. case 'top':
  396. snippetSortOrder = 0 /* Top */;
  397. break;
  398. // ↓ that's the default anyways...
  399. // case 'inline':
  400. // snippetSortOrder = SnippetSortOrder.Inline;
  401. // break;
  402. case 'bottom':
  403. snippetSortOrder = 2 /* Bottom */;
  404. break;
  405. }
  406. const { itemKind: itemKindFilter, showDeprecated } = SuggestModel._createSuggestFilter(this._editor);
  407. const wordDistance = WordDistance.create(this._editorWorkerService, this._editor);
  408. const completions = provideSuggestionItems(model, this._editor.getPosition(), new CompletionOptions(snippetSortOrder, itemKindFilter, onlyFrom, showDeprecated), suggestCtx, this._requestToken.token);
  409. Promise.all([completions, wordDistance]).then(([completions, wordDistance]) => __awaiter(this, void 0, void 0, function* () {
  410. var _b;
  411. (_b = this._requestToken) === null || _b === void 0 ? void 0 : _b.dispose();
  412. if (!this._editor.hasModel()) {
  413. return;
  414. }
  415. let clipboardText = existing === null || existing === void 0 ? void 0 : existing.clipboardText;
  416. if (!clipboardText && completions.needsClipboard) {
  417. clipboardText = yield this._clipboardService.readText();
  418. }
  419. if (this._state === 0 /* Idle */) {
  420. return;
  421. }
  422. const model = this._editor.getModel();
  423. let items = completions.items;
  424. if (existing) {
  425. const cmpFn = getSuggestionComparator(snippetSortOrder);
  426. items = items.concat(existing.items).sort(cmpFn);
  427. }
  428. const ctx = new LineContext(model, this._editor.getPosition(), auto, context.shy);
  429. this._completionModel = new CompletionModel(items, this._context.column, {
  430. leadingLineContent: ctx.leadingLineContent,
  431. characterCountDelta: ctx.column - this._context.column
  432. }, wordDistance, this._editor.getOption(105 /* suggest */), this._editor.getOption(100 /* snippetSuggestions */), clipboardText);
  433. // store containers so that they can be disposed later
  434. this._completionDisposables.add(completions.disposable);
  435. this._onNewContext(ctx);
  436. // finally report telemetry about durations
  437. this._reportDurationsTelemetry(completions.durations);
  438. })).catch(onUnexpectedError);
  439. }
  440. _reportDurationsTelemetry(durations) {
  441. if (this._telemetryGate++ % 230 !== 0) {
  442. return;
  443. }
  444. setTimeout(() => {
  445. this._telemetryService.publicLog2('suggest.durations.json', { data: JSON.stringify(durations) });
  446. this._logService.debug('suggest.durations.json', durations);
  447. });
  448. }
  449. static _createSuggestFilter(editor) {
  450. // kind filter and snippet sort rules
  451. const result = new Set();
  452. // snippet setting
  453. const snippetSuggestions = editor.getOption(100 /* snippetSuggestions */);
  454. if (snippetSuggestions === 'none') {
  455. result.add(27 /* Snippet */);
  456. }
  457. // type setting
  458. const suggestOptions = editor.getOption(105 /* suggest */);
  459. if (!suggestOptions.showMethods) {
  460. result.add(0 /* Method */);
  461. }
  462. if (!suggestOptions.showFunctions) {
  463. result.add(1 /* Function */);
  464. }
  465. if (!suggestOptions.showConstructors) {
  466. result.add(2 /* Constructor */);
  467. }
  468. if (!suggestOptions.showFields) {
  469. result.add(3 /* Field */);
  470. }
  471. if (!suggestOptions.showVariables) {
  472. result.add(4 /* Variable */);
  473. }
  474. if (!suggestOptions.showClasses) {
  475. result.add(5 /* Class */);
  476. }
  477. if (!suggestOptions.showStructs) {
  478. result.add(6 /* Struct */);
  479. }
  480. if (!suggestOptions.showInterfaces) {
  481. result.add(7 /* Interface */);
  482. }
  483. if (!suggestOptions.showModules) {
  484. result.add(8 /* Module */);
  485. }
  486. if (!suggestOptions.showProperties) {
  487. result.add(9 /* Property */);
  488. }
  489. if (!suggestOptions.showEvents) {
  490. result.add(10 /* Event */);
  491. }
  492. if (!suggestOptions.showOperators) {
  493. result.add(11 /* Operator */);
  494. }
  495. if (!suggestOptions.showUnits) {
  496. result.add(12 /* Unit */);
  497. }
  498. if (!suggestOptions.showValues) {
  499. result.add(13 /* Value */);
  500. }
  501. if (!suggestOptions.showConstants) {
  502. result.add(14 /* Constant */);
  503. }
  504. if (!suggestOptions.showEnums) {
  505. result.add(15 /* Enum */);
  506. }
  507. if (!suggestOptions.showEnumMembers) {
  508. result.add(16 /* EnumMember */);
  509. }
  510. if (!suggestOptions.showKeywords) {
  511. result.add(17 /* Keyword */);
  512. }
  513. if (!suggestOptions.showWords) {
  514. result.add(18 /* Text */);
  515. }
  516. if (!suggestOptions.showColors) {
  517. result.add(19 /* Color */);
  518. }
  519. if (!suggestOptions.showFiles) {
  520. result.add(20 /* File */);
  521. }
  522. if (!suggestOptions.showReferences) {
  523. result.add(21 /* Reference */);
  524. }
  525. if (!suggestOptions.showColors) {
  526. result.add(22 /* Customcolor */);
  527. }
  528. if (!suggestOptions.showFolders) {
  529. result.add(23 /* Folder */);
  530. }
  531. if (!suggestOptions.showTypeParameters) {
  532. result.add(24 /* TypeParameter */);
  533. }
  534. if (!suggestOptions.showSnippets) {
  535. result.add(27 /* Snippet */);
  536. }
  537. if (!suggestOptions.showUsers) {
  538. result.add(25 /* User */);
  539. }
  540. if (!suggestOptions.showIssues) {
  541. result.add(26 /* Issue */);
  542. }
  543. return { itemKind: result, showDeprecated: suggestOptions.showDeprecated };
  544. }
  545. _onNewContext(ctx) {
  546. if (!this._context) {
  547. // happens when 24x7 IntelliSense is enabled and still in its delay
  548. return;
  549. }
  550. if (ctx.lineNumber !== this._context.lineNumber) {
  551. // e.g. happens when pressing Enter while IntelliSense is computed
  552. this.cancel();
  553. return;
  554. }
  555. if (getLeadingWhitespace(ctx.leadingLineContent) !== getLeadingWhitespace(this._context.leadingLineContent)) {
  556. // cancel IntelliSense when line start changes
  557. // happens when the current word gets outdented
  558. this.cancel();
  559. return;
  560. }
  561. if (ctx.column < this._context.column) {
  562. // typed -> moved cursor LEFT -> retrigger if still on a word
  563. if (ctx.leadingWord.word) {
  564. this.trigger({ auto: this._context.auto, shy: false }, true);
  565. }
  566. else {
  567. this.cancel();
  568. }
  569. return;
  570. }
  571. if (!this._completionModel) {
  572. // happens when IntelliSense is not yet computed
  573. return;
  574. }
  575. if (ctx.leadingWord.word.length !== 0 && ctx.leadingWord.startColumn > this._context.leadingWord.startColumn) {
  576. // started a new word while IntelliSense shows -> retrigger
  577. // Select those providers have not contributed to this completion model and re-trigger completions for
  578. // them. Also adopt the existing items and merge them into the new completion model
  579. const inactiveProvider = new Set(CompletionProviderRegistry.all(this._editor.getModel()));
  580. for (let provider of this._completionModel.allProvider) {
  581. inactiveProvider.delete(provider);
  582. }
  583. const items = this._completionModel.adopt(new Set());
  584. this.trigger({ auto: this._context.auto, shy: false }, true, inactiveProvider, { items, clipboardText: this._completionModel.clipboardText });
  585. return;
  586. }
  587. if (ctx.column > this._context.column && this._completionModel.incomplete.size > 0 && ctx.leadingWord.word.length !== 0) {
  588. // typed -> moved cursor RIGHT & incomple model & still on a word -> retrigger
  589. const { incomplete } = this._completionModel;
  590. const items = this._completionModel.adopt(incomplete);
  591. this.trigger({ auto: this._state === 2 /* Auto */, shy: false, triggerKind: 2 /* TriggerForIncompleteCompletions */ }, true, incomplete, { items, clipboardText: this._completionModel.clipboardText });
  592. }
  593. else {
  594. // typed -> moved cursor RIGHT -> update UI
  595. let oldLineContext = this._completionModel.lineContext;
  596. let isFrozen = false;
  597. this._completionModel.lineContext = {
  598. leadingLineContent: ctx.leadingLineContent,
  599. characterCountDelta: ctx.column - this._context.column
  600. };
  601. if (this._completionModel.items.length === 0) {
  602. if (LineContext.shouldAutoTrigger(this._editor) && this._context.leadingWord.endColumn < ctx.leadingWord.startColumn) {
  603. // retrigger when heading into a new word
  604. this.trigger({ auto: this._context.auto, shy: false }, true);
  605. return;
  606. }
  607. if (!this._context.auto) {
  608. // freeze when IntelliSense was manually requested
  609. this._completionModel.lineContext = oldLineContext;
  610. isFrozen = this._completionModel.items.length > 0;
  611. if (isFrozen && ctx.leadingWord.word.length === 0) {
  612. // there were results before but now there aren't
  613. // and also we are not on a word anymore -> cancel
  614. this.cancel();
  615. return;
  616. }
  617. }
  618. else {
  619. // nothing left
  620. this.cancel();
  621. return;
  622. }
  623. }
  624. this._onDidSuggest.fire({
  625. completionModel: this._completionModel,
  626. auto: this._context.auto,
  627. shy: this._context.shy,
  628. isFrozen,
  629. });
  630. }
  631. }
  632. };
  633. SuggestModel = __decorate([
  634. __param(1, IEditorWorkerService),
  635. __param(2, IClipboardService),
  636. __param(3, ITelemetryService),
  637. __param(4, ILogService),
  638. __param(5, IContextKeyService),
  639. __param(6, IConfigurationService)
  640. ], SuggestModel);
  641. export { SuggestModel };