f400cc968909d669776a2e50da041f66fb3edf7aac276c1d5c946c203f5547e9e3aa77a14aaa67029a6641a0288a4490530c6322b9c8ebf982f8ac0ce0fad6 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. import { DisposableStore } from '../../../../base/common/lifecycle.js';
  6. export class OvertypingCapturer {
  7. constructor(editor, suggestModel) {
  8. this._disposables = new DisposableStore();
  9. this._lastOvertyped = [];
  10. this._empty = true;
  11. this._disposables.add(editor.onWillType(() => {
  12. if (!this._empty) {
  13. return;
  14. }
  15. if (!editor.hasModel()) {
  16. return;
  17. }
  18. const selections = editor.getSelections();
  19. const selectionsLength = selections.length;
  20. // Check if it will overtype any selections
  21. let willOvertype = false;
  22. for (let i = 0; i < selectionsLength; i++) {
  23. if (!selections[i].isEmpty()) {
  24. willOvertype = true;
  25. break;
  26. }
  27. }
  28. if (!willOvertype) {
  29. return;
  30. }
  31. this._lastOvertyped = [];
  32. const model = editor.getModel();
  33. for (let i = 0; i < selectionsLength; i++) {
  34. const selection = selections[i];
  35. // Check for overtyping capturer restrictions
  36. if (model.getValueLengthInRange(selection) > OvertypingCapturer._maxSelectionLength) {
  37. return;
  38. }
  39. this._lastOvertyped[i] = { value: model.getValueInRange(selection), multiline: selection.startLineNumber !== selection.endLineNumber };
  40. }
  41. this._empty = false;
  42. }));
  43. this._disposables.add(suggestModel.onDidCancel(e => {
  44. if (!this._empty && !e.retrigger) {
  45. this._empty = true;
  46. }
  47. }));
  48. }
  49. getLastOvertypedInfo(idx) {
  50. if (!this._empty && idx >= 0 && idx < this._lastOvertyped.length) {
  51. return this._lastOvertyped[idx];
  52. }
  53. return undefined;
  54. }
  55. dispose() {
  56. this._disposables.dispose();
  57. }
  58. }
  59. OvertypingCapturer._maxSelectionLength = 51200;