44d59a750285dcde71749b429867b3422cfd9f1ea6046491d0c6265f3c2fd8cb0bbff051a44f30f0e55151ec9b580271588759839b053fb6ee152259ac7a96 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 { Emitter } from '../../../../base/common/event.js';
  6. import { Disposable } from '../../../../base/common/lifecycle.js';
  7. import { applyEdits } from './utils.js';
  8. export class GhostText {
  9. constructor(lineNumber, parts, additionalReservedLineCount = 0) {
  10. this.lineNumber = lineNumber;
  11. this.parts = parts;
  12. this.additionalReservedLineCount = additionalReservedLineCount;
  13. }
  14. renderForScreenReader(lineText) {
  15. if (this.parts.length === 0) {
  16. return '';
  17. }
  18. const lastPart = this.parts[this.parts.length - 1];
  19. const cappedLineText = lineText.substr(0, lastPart.column - 1);
  20. const text = applyEdits(cappedLineText, this.parts.map(p => ({
  21. range: { startLineNumber: 1, endLineNumber: 1, startColumn: p.column, endColumn: p.column },
  22. text: p.lines.join('\n')
  23. })));
  24. return text.substring(this.parts[0].column - 1);
  25. }
  26. isEmpty() {
  27. return this.parts.every(p => p.lines.length === 0);
  28. }
  29. }
  30. export class GhostTextPart {
  31. constructor(column, lines,
  32. /**
  33. * Indicates if this part is a preview of an inline suggestion when a suggestion is previewed.
  34. */
  35. preview) {
  36. this.column = column;
  37. this.lines = lines;
  38. this.preview = preview;
  39. }
  40. }
  41. export class GhostTextReplacement {
  42. constructor(lineNumber, columnStart, length, newLines, additionalReservedLineCount = 0) {
  43. this.lineNumber = lineNumber;
  44. this.columnStart = columnStart;
  45. this.length = length;
  46. this.newLines = newLines;
  47. this.additionalReservedLineCount = additionalReservedLineCount;
  48. this.parts = [
  49. new GhostTextPart(this.columnStart + this.length, this.newLines, false),
  50. ];
  51. }
  52. renderForScreenReader(_lineText) {
  53. return this.newLines.join('\n');
  54. }
  55. }
  56. export class BaseGhostTextWidgetModel extends Disposable {
  57. constructor(editor) {
  58. super();
  59. this.editor = editor;
  60. this._expanded = undefined;
  61. this.onDidChangeEmitter = new Emitter();
  62. this.onDidChange = this.onDidChangeEmitter.event;
  63. this._register(editor.onDidChangeConfiguration((e) => {
  64. if (e.hasChanged(108 /* EditorOption.suggest */) && this._expanded === undefined) {
  65. this.onDidChangeEmitter.fire();
  66. }
  67. }));
  68. }
  69. setExpanded(expanded) {
  70. this._expanded = true;
  71. this.onDidChangeEmitter.fire();
  72. }
  73. }