"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
require("./textAreaHandler.css.js");
const nls = require("../../../nls.js");
const browser = require("../../../base/browser/browser.js");
const fastDomNode = require("../../../base/browser/fastDomNode.js");
const platform = require("../../../base/common/platform.js");
const strings = require("../../../base/common/strings.js");
const domFontInfo = require("../config/domFontInfo.js");
const textAreaInput = require("./textAreaInput.js");
const textAreaState = require("./textAreaState.js");
const viewPart = require("../view/viewPart.js");
const lineNumbers = require("../viewParts/lineNumbers/lineNumbers.js");
const margin = require("../viewParts/margin/margin.js");
const editorOptions = require("../../common/config/editorOptions.js");
const wordCharacterClassifier = require("../../common/core/wordCharacterClassifier.js");
const position = require("../../common/core/position.js");
const range = require("../../common/core/range.js");
const selection = require("../../common/core/selection.js");
const mouseCursor = require("../../../base/browser/ui/mouseCursor/mouseCursor.js");
const languages = require("../../common/languages.js");
const color = require("../../../base/common/color.js");
class VisibleTextAreaData {
  constructor(_context, modelLineNumber, distanceToModelLineStart, widthOfHiddenLineTextBefore, distanceToModelLineEnd) {
    this._context = _context;
    this.modelLineNumber = modelLineNumber;
    this.distanceToModelLineStart = distanceToModelLineStart;
    this.widthOfHiddenLineTextBefore = widthOfHiddenLineTextBefore;
    this.distanceToModelLineEnd = distanceToModelLineEnd;
    this._visibleTextAreaBrand = void 0;
    this.startPosition = null;
    this.endPosition = null;
    this.visibleTextareaStart = null;
    this.visibleTextareaEnd = null;
    this._previousPresentation = null;
  }
  prepareRender(visibleRangeProvider) {
    const startModelPosition = new position.Position(this.modelLineNumber, this.distanceToModelLineStart + 1);
    const endModelPosition = new position.Position(this.modelLineNumber, this._context.viewModel.model.getLineMaxColumn(this.modelLineNumber) - this.distanceToModelLineEnd);
    this.startPosition = this._context.viewModel.coordinatesConverter.convertModelPositionToViewPosition(startModelPosition);
    this.endPosition = this._context.viewModel.coordinatesConverter.convertModelPositionToViewPosition(endModelPosition);
    if (this.startPosition.lineNumber === this.endPosition.lineNumber) {
      this.visibleTextareaStart = visibleRangeProvider.visibleRangeForPosition(this.startPosition);
      this.visibleTextareaEnd = visibleRangeProvider.visibleRangeForPosition(this.endPosition);
    } else {
      this.visibleTextareaStart = null;
      this.visibleTextareaEnd = null;
    }
  }
  definePresentation(tokenPresentation) {
    if (!this._previousPresentation) {
      if (tokenPresentation) {
        this._previousPresentation = tokenPresentation;
      } else {
        this._previousPresentation = {
          foreground: 1,
          italic: false,
          bold: false,
          underline: false,
          strikethrough: false
        };
      }
    }
    return this._previousPresentation;
  }
}
const canUseZeroSizeTextarea = browser.isFirefox;
class TextAreaHandler extends viewPart.ViewPart {
  constructor(context, viewController, visibleRangeProvider) {
    super(context);
    this._primaryCursorPosition = new position.Position(1, 1);
    this._primaryCursorVisibleRange = null;
    this._viewController = viewController;
    this._visibleRangeProvider = visibleRangeProvider;
    this._scrollLeft = 0;
    this._scrollTop = 0;
    const options = this._context.configuration.options;
    const layoutInfo = options.get(
      131
      /* layoutInfo */
    );
    this._setAccessibilityOptions(options);
    this._contentLeft = layoutInfo.contentLeft;
    this._contentWidth = layoutInfo.contentWidth;
    this._contentHeight = layoutInfo.height;
    this._fontInfo = options.get(
      44
      /* fontInfo */
    );
    this._lineHeight = options.get(
      59
      /* lineHeight */
    );
    this._emptySelectionClipboard = options.get(
      32
      /* emptySelectionClipboard */
    );
    this._copyWithSyntaxHighlighting = options.get(
      21
      /* copyWithSyntaxHighlighting */
    );
    this._visibleTextArea = null;
    this._selections = [new selection.Selection(1, 1, 1, 1)];
    this._modelSelections = [new selection.Selection(1, 1, 1, 1)];
    this._lastRenderPosition = null;
    this.textArea = fastDomNode.createFastDomNode(document.createElement("textarea"));
    viewPart.PartFingerprints.write(
      this.textArea,
      6
      /* TextArea */
    );
    this.textArea.setClassName(`inputarea ${mouseCursor.MOUSE_CURSOR_TEXT_CSS_CLASS_NAME}`);
    this.textArea.setAttribute("wrap", "off");
    this.textArea.setAttribute("autocorrect", "off");
    this.textArea.setAttribute("autocapitalize", "off");
    this.textArea.setAttribute("autocomplete", "off");
    this.textArea.setAttribute("spellcheck", "false");
    this.textArea.setAttribute("aria-label", this._getAriaLabel(options));
    this.textArea.setAttribute("tabindex", String(options.get(
      112
      /* tabIndex */
    )));
    this.textArea.setAttribute("role", "textbox");
    this.textArea.setAttribute("aria-roledescription", nls.localize("editor", "editor"));
    this.textArea.setAttribute("aria-multiline", "true");
    this.textArea.setAttribute("aria-haspopup", "false");
    this.textArea.setAttribute("aria-autocomplete", "both");
    if (options.get(
      30
      /* domReadOnly */
    ) && options.get(
      81
      /* readOnly */
    )) {
      this.textArea.setAttribute("readonly", "true");
    }
    this.textAreaCover = fastDomNode.createFastDomNode(document.createElement("div"));
    this.textAreaCover.setPosition("absolute");
    const simpleModel = {
      getLineCount: () => {
        return this._context.viewModel.getLineCount();
      },
      getLineMaxColumn: (lineNumber) => {
        return this._context.viewModel.getLineMaxColumn(lineNumber);
      },
      getValueInRange: (range2, eol) => {
        return this._context.viewModel.getValueInRange(range2, eol);
      }
    };
    const textAreaInputHost = {
      getDataToCopy: () => {
        const rawTextToCopy = this._context.viewModel.getPlainTextToCopy(this._modelSelections, this._emptySelectionClipboard, platform.isWindows);
        const newLineCharacter = this._context.viewModel.model.getEOL();
        const isFromEmptySelection = this._emptySelectionClipboard && this._modelSelections.length === 1 && this._modelSelections[0].isEmpty();
        const multicursorText = Array.isArray(rawTextToCopy) ? rawTextToCopy : null;
        const text = Array.isArray(rawTextToCopy) ? rawTextToCopy.join(newLineCharacter) : rawTextToCopy;
        let html = void 0;
        let mode = null;
        if (textAreaInput.CopyOptions.forceCopyWithSyntaxHighlighting || this._copyWithSyntaxHighlighting && text.length < 65536) {
          const richText = this._context.viewModel.getRichTextToCopy(this._modelSelections, this._emptySelectionClipboard);
          if (richText) {
            html = richText.html;
            mode = richText.mode;
          }
        }
        return {
          isFromEmptySelection,
          multicursorText,
          text,
          html,
          mode
        };
      },
      getScreenReaderContent: (currentState) => {
        if (this._accessibilitySupport === 1) {
          if (platform.isMacintosh) {
            const selection2 = this._selections[0];
            if (selection2.isEmpty()) {
              const position2 = selection2.getStartPosition();
              let textBefore = this._getWordBeforePosition(position2);
              if (textBefore.length === 0) {
                textBefore = this._getCharacterBeforePosition(position2);
              }
              if (textBefore.length > 0) {
                return new textAreaState.TextAreaState(textBefore, textBefore.length, textBefore.length, position2, position2);
              }
            }
          }
          return textAreaState.TextAreaState.EMPTY;
        }
        if (browser.isAndroid) {
          const selection2 = this._selections[0];
          if (selection2.isEmpty()) {
            const position2 = selection2.getStartPosition();
            const [wordAtPosition, positionOffsetInWord] = this._getAndroidWordAtPosition(position2);
            if (wordAtPosition.length > 0) {
              return new textAreaState.TextAreaState(wordAtPosition, positionOffsetInWord, positionOffsetInWord, position2, position2);
            }
          }
          return textAreaState.TextAreaState.EMPTY;
        }
        return textAreaState.PagedScreenReaderStrategy.fromEditorSelection(
          currentState,
          simpleModel,
          this._selections[0],
          this._accessibilityPageSize,
          this._accessibilitySupport === 0
          /* Unknown */
        );
      },
      deduceModelPosition: (viewAnchorPosition, deltaOffset, lineFeedCnt) => {
        return this._context.viewModel.deduceModelPositionRelativeToViewPosition(viewAnchorPosition, deltaOffset, lineFeedCnt);
      }
    };
    const textAreaWrapper = this._register(new textAreaInput.TextAreaWrapper(this.textArea.domNode));
    this._textAreaInput = this._register(new textAreaInput.TextAreaInput(textAreaInputHost, textAreaWrapper, platform.OS, browser));
    this._register(this._textAreaInput.onKeyDown((e) => {
      this._viewController.emitKeyDown(e);
    }));
    this._register(this._textAreaInput.onKeyUp((e) => {
      this._viewController.emitKeyUp(e);
    }));
    this._register(this._textAreaInput.onPaste((e) => {
      let pasteOnNewLine = false;
      let multicursorText = null;
      let mode = null;
      if (e.metadata) {
        pasteOnNewLine = this._emptySelectionClipboard && !!e.metadata.isFromEmptySelection;
        multicursorText = typeof e.metadata.multicursorText !== "undefined" ? e.metadata.multicursorText : null;
        mode = e.metadata.mode;
      }
      this._viewController.paste(e.text, pasteOnNewLine, multicursorText, mode);
    }));
    this._register(this._textAreaInput.onCut(() => {
      this._viewController.cut();
    }));
    this._register(this._textAreaInput.onType((e) => {
      if (e.replacePrevCharCnt || e.replaceNextCharCnt || e.positionDelta) {
        this._viewController.compositionType(e.text, e.replacePrevCharCnt, e.replaceNextCharCnt, e.positionDelta);
      } else {
        this._viewController.type(e.text);
      }
    }));
    this._register(this._textAreaInput.onSelectionChangeRequest((modelSelection) => {
      this._viewController.setSelection(modelSelection);
    }));
    this._register(this._textAreaInput.onCompositionStart((e) => {
      const ta = this.textArea.domNode;
      const modelSelection = this._modelSelections[0];
      const { distanceToModelLineStart, widthOfHiddenTextBefore } = (() => {
        const textBeforeSelection = ta.value.substring(0, Math.min(ta.selectionStart, ta.selectionEnd));
        const lineFeedOffset1 = textBeforeSelection.lastIndexOf("\n");
        const lineTextBeforeSelection = textBeforeSelection.substring(lineFeedOffset1 + 1);
        const tabOffset1 = lineTextBeforeSelection.lastIndexOf("	");
        const desiredVisibleBeforeCharCount = lineTextBeforeSelection.length - tabOffset1 - 1;
        const startModelPosition = modelSelection.getStartPosition();
        const visibleBeforeCharCount = Math.min(startModelPosition.column - 1, desiredVisibleBeforeCharCount);
        const distanceToModelLineStart2 = startModelPosition.column - 1 - visibleBeforeCharCount;
        const hiddenLineTextBefore = lineTextBeforeSelection.substring(0, lineTextBeforeSelection.length - visibleBeforeCharCount);
        const widthOfHiddenTextBefore2 = measureText(hiddenLineTextBefore, this._fontInfo);
        return { distanceToModelLineStart: distanceToModelLineStart2, widthOfHiddenTextBefore: widthOfHiddenTextBefore2 };
      })();
      const { distanceToModelLineEnd } = (() => {
        const textAfterSelection = ta.value.substring(Math.max(ta.selectionStart, ta.selectionEnd));
        const lineFeedOffset2 = textAfterSelection.indexOf("\n");
        const lineTextAfterSelection = lineFeedOffset2 === -1 ? textAfterSelection : textAfterSelection.substring(0, lineFeedOffset2);
        const tabOffset2 = lineTextAfterSelection.indexOf("	");
        const desiredVisibleAfterCharCount = tabOffset2 === -1 ? lineTextAfterSelection.length : lineTextAfterSelection.length - tabOffset2 - 1;
        const endModelPosition = modelSelection.getEndPosition();
        const visibleAfterCharCount = Math.min(this._context.viewModel.model.getLineMaxColumn(endModelPosition.lineNumber) - endModelPosition.column, desiredVisibleAfterCharCount);
        const distanceToModelLineEnd2 = this._context.viewModel.model.getLineMaxColumn(endModelPosition.lineNumber) - endModelPosition.column - visibleAfterCharCount;
        return { distanceToModelLineEnd: distanceToModelLineEnd2 };
      })();
      this._context.viewModel.revealRange(
        "keyboard",
        true,
        range.Range.fromPositions(this._selections[0].getStartPosition()),
        0,
        1
        /* Immediate */
      );
      this._visibleTextArea = new VisibleTextAreaData(this._context, modelSelection.startLineNumber, distanceToModelLineStart, widthOfHiddenTextBefore, distanceToModelLineEnd);
      this._visibleTextArea.prepareRender(this._visibleRangeProvider);
      this._render();
      this.textArea.setClassName(`inputarea ${mouseCursor.MOUSE_CURSOR_TEXT_CSS_CLASS_NAME} ime-input`);
      this._viewController.compositionStart();
      this._context.viewModel.onCompositionStart();
    }));
    this._register(this._textAreaInput.onCompositionUpdate((e) => {
      if (!this._visibleTextArea) {
        return;
      }
      this._visibleTextArea.prepareRender(this._visibleRangeProvider);
      this._render();
    }));
    this._register(this._textAreaInput.onCompositionEnd(() => {
      this._visibleTextArea = null;
      this._render();
      this.textArea.setClassName(`inputarea ${mouseCursor.MOUSE_CURSOR_TEXT_CSS_CLASS_NAME}`);
      this._viewController.compositionEnd();
      this._context.viewModel.onCompositionEnd();
    }));
    this._register(this._textAreaInput.onFocus(() => {
      this._context.viewModel.setHasFocus(true);
    }));
    this._register(this._textAreaInput.onBlur(() => {
      this._context.viewModel.setHasFocus(false);
    }));
  }
  dispose() {
    super.dispose();
  }
  _getAndroidWordAtPosition(position2) {
    const ANDROID_WORD_SEPARATORS = '`~!@#$%^&*()-=+[{]}\\|;:",.<>/?';
    const lineContent = this._context.viewModel.getLineContent(position2.lineNumber);
    const wordSeparators = wordCharacterClassifier.getMapForWordSeparators(ANDROID_WORD_SEPARATORS);
    let goingLeft = true;
    let startColumn = position2.column;
    let goingRight = true;
    let endColumn = position2.column;
    let distance = 0;
    while (distance < 50 && (goingLeft || goingRight)) {
      if (goingLeft && startColumn <= 1) {
        goingLeft = false;
      }
      if (goingLeft) {
        const charCode = lineContent.charCodeAt(startColumn - 2);
        const charClass = wordSeparators.get(charCode);
        if (charClass !== 0) {
          goingLeft = false;
        } else {
          startColumn--;
        }
      }
      if (goingRight && endColumn > lineContent.length) {
        goingRight = false;
      }
      if (goingRight) {
        const charCode = lineContent.charCodeAt(endColumn - 1);
        const charClass = wordSeparators.get(charCode);
        if (charClass !== 0) {
          goingRight = false;
        } else {
          endColumn++;
        }
      }
      distance++;
    }
    return [lineContent.substring(startColumn - 1, endColumn - 1), position2.column - startColumn];
  }
  _getWordBeforePosition(position2) {
    const lineContent = this._context.viewModel.getLineContent(position2.lineNumber);
    const wordSeparators = wordCharacterClassifier.getMapForWordSeparators(this._context.configuration.options.get(
      117
      /* wordSeparators */
    ));
    let column = position2.column;
    let distance = 0;
    while (column > 1) {
      const charCode = lineContent.charCodeAt(column - 2);
      const charClass = wordSeparators.get(charCode);
      if (charClass !== 0 || distance > 50) {
        return lineContent.substring(column - 1, position2.column - 1);
      }
      distance++;
      column--;
    }
    return lineContent.substring(0, position2.column - 1);
  }
  _getCharacterBeforePosition(position2) {
    if (position2.column > 1) {
      const lineContent = this._context.viewModel.getLineContent(position2.lineNumber);
      const charBefore = lineContent.charAt(position2.column - 2);
      if (!strings.isHighSurrogate(charBefore.charCodeAt(0))) {
        return charBefore;
      }
    }
    return "";
  }
  _getAriaLabel(options) {
    const accessibilitySupport = options.get(
      2
      /* accessibilitySupport */
    );
    if (accessibilitySupport === 1) {
      return nls.localize("accessibilityOffAriaLabel", "The editor is not accessible at this time. Press {0} for options.", platform.isLinux ? "Shift+Alt+F1" : "Alt+F1");
    }
    return options.get(
      4
      /* ariaLabel */
    );
  }
  _setAccessibilityOptions(options) {
    this._accessibilitySupport = options.get(
      2
      /* accessibilitySupport */
    );
    const accessibilityPageSize = options.get(
      3
      /* accessibilityPageSize */
    );
    if (this._accessibilitySupport === 2 && accessibilityPageSize === editorOptions.EditorOptions.accessibilityPageSize.defaultValue) {
      this._accessibilityPageSize = 500;
    } else {
      this._accessibilityPageSize = accessibilityPageSize;
    }
  }
  // --- begin event handlers
  onConfigurationChanged(e) {
    const options = this._context.configuration.options;
    const layoutInfo = options.get(
      131
      /* layoutInfo */
    );
    this._setAccessibilityOptions(options);
    this._contentLeft = layoutInfo.contentLeft;
    this._contentWidth = layoutInfo.contentWidth;
    this._contentHeight = layoutInfo.height;
    this._fontInfo = options.get(
      44
      /* fontInfo */
    );
    this._lineHeight = options.get(
      59
      /* lineHeight */
    );
    this._emptySelectionClipboard = options.get(
      32
      /* emptySelectionClipboard */
    );
    this._copyWithSyntaxHighlighting = options.get(
      21
      /* copyWithSyntaxHighlighting */
    );
    this.textArea.setAttribute("aria-label", this._getAriaLabel(options));
    this.textArea.setAttribute("tabindex", String(options.get(
      112
      /* tabIndex */
    )));
    if (e.hasChanged(
      30
      /* domReadOnly */
    ) || e.hasChanged(
      81
      /* readOnly */
    )) {
      if (options.get(
        30
        /* domReadOnly */
      ) && options.get(
        81
        /* readOnly */
      )) {
        this.textArea.setAttribute("readonly", "true");
      } else {
        this.textArea.removeAttribute("readonly");
      }
    }
    if (e.hasChanged(
      2
      /* accessibilitySupport */
    )) {
      this._textAreaInput.writeScreenReaderContent("strategy changed");
    }
    return true;
  }
  onCursorStateChanged(e) {
    this._selections = e.selections.slice(0);
    this._modelSelections = e.modelSelections.slice(0);
    this._textAreaInput.writeScreenReaderContent("selection changed");
    return true;
  }
  onDecorationsChanged(e) {
    return true;
  }
  onFlushed(e) {
    return true;
  }
  onLinesChanged(e) {
    return true;
  }
  onLinesDeleted(e) {
    return true;
  }
  onLinesInserted(e) {
    return true;
  }
  onScrollChanged(e) {
    this._scrollLeft = e.scrollLeft;
    this._scrollTop = e.scrollTop;
    return true;
  }
  onZonesChanged(e) {
    return true;
  }
  // --- end event handlers
  // --- begin view API
  isFocused() {
    return this._textAreaInput.isFocused();
  }
  focusTextArea() {
    this._textAreaInput.focusTextArea();
  }
  getLastRenderData() {
    return this._lastRenderPosition;
  }
  setAriaOptions(options) {
    if (options.activeDescendant) {
      this.textArea.setAttribute("aria-haspopup", "true");
      this.textArea.setAttribute("aria-autocomplete", "list");
      this.textArea.setAttribute("aria-activedescendant", options.activeDescendant);
    } else {
      this.textArea.setAttribute("aria-haspopup", "false");
      this.textArea.setAttribute("aria-autocomplete", "both");
      this.textArea.removeAttribute("aria-activedescendant");
    }
    if (options.role) {
      this.textArea.setAttribute("role", options.role);
    }
  }
  prepareRender(ctx) {
    this._primaryCursorPosition = new position.Position(this._selections[0].positionLineNumber, this._selections[0].positionColumn);
    this._primaryCursorVisibleRange = ctx.visibleRangeForPosition(this._primaryCursorPosition);
    if (this._visibleTextArea) {
      this._visibleTextArea.prepareRender(ctx);
    }
  }
  render(ctx) {
    this._textAreaInput.writeScreenReaderContent("render");
    this._render();
  }
  _render() {
    if (this._visibleTextArea) {
      const visibleStart = this._visibleTextArea.visibleTextareaStart;
      const visibleEnd = this._visibleTextArea.visibleTextareaEnd;
      const startPosition = this._visibleTextArea.startPosition;
      const endPosition = this._visibleTextArea.endPosition;
      if (startPosition && endPosition && visibleStart && visibleEnd && visibleEnd.left >= this._scrollLeft && visibleStart.left <= this._scrollLeft + this._contentWidth) {
        const top2 = this._context.viewLayout.getVerticalOffsetForLineNumber(this._primaryCursorPosition.lineNumber) - this._scrollTop;
        const lineCount = this._newlinecount(this.textArea.domNode.value.substr(0, this.textArea.domNode.selectionStart));
        let scrollLeft = this._visibleTextArea.widthOfHiddenLineTextBefore;
        let left2 = this._contentLeft + visibleStart.left - this._scrollLeft;
        let width = visibleEnd.left - visibleStart.left + 1;
        if (left2 < this._contentLeft) {
          const delta = this._contentLeft - left2;
          left2 += delta;
          scrollLeft += delta;
          width -= delta;
        }
        if (width > this._contentWidth) {
          width = this._contentWidth;
        }
        const viewLineData = this._context.viewModel.getViewLineData(startPosition.lineNumber);
        const startTokenIndex = viewLineData.tokens.findTokenIndexAtOffset(startPosition.column - 1);
        const endTokenIndex = viewLineData.tokens.findTokenIndexAtOffset(endPosition.column - 1);
        const textareaSpansSingleToken = startTokenIndex === endTokenIndex;
        const presentation = this._visibleTextArea.definePresentation(textareaSpansSingleToken ? viewLineData.tokens.getPresentation(startTokenIndex) : null);
        this.textArea.domNode.scrollTop = lineCount * this._lineHeight;
        this.textArea.domNode.scrollLeft = scrollLeft;
        this._doRender({
          lastRenderPosition: null,
          top: top2,
          left: left2,
          width,
          height: this._lineHeight,
          useCover: false,
          color: (languages.TokenizationRegistry.getColorMap() || [])[presentation.foreground],
          italic: presentation.italic,
          bold: presentation.bold,
          underline: presentation.underline,
          strikethrough: presentation.strikethrough
        });
      }
      return;
    }
    if (!this._primaryCursorVisibleRange) {
      this._renderAtTopLeft();
      return;
    }
    const left = this._contentLeft + this._primaryCursorVisibleRange.left - this._scrollLeft;
    if (left < this._contentLeft || left > this._contentLeft + this._contentWidth) {
      this._renderAtTopLeft();
      return;
    }
    const top = this._context.viewLayout.getVerticalOffsetForLineNumber(this._selections[0].positionLineNumber) - this._scrollTop;
    if (top < 0 || top > this._contentHeight) {
      this._renderAtTopLeft();
      return;
    }
    if (platform.isMacintosh) {
      this._doRender({
        lastRenderPosition: this._primaryCursorPosition,
        top,
        left,
        width: canUseZeroSizeTextarea ? 0 : 1,
        height: this._lineHeight,
        useCover: false
      });
      this.textArea.domNode.scrollLeft = this._primaryCursorVisibleRange.left;
      const lineCount = this._newlinecount(this.textArea.domNode.value.substr(0, this.textArea.domNode.selectionStart));
      this.textArea.domNode.scrollTop = lineCount * this._lineHeight;
      return;
    }
    this._doRender({
      lastRenderPosition: this._primaryCursorPosition,
      top,
      left,
      width: canUseZeroSizeTextarea ? 0 : 1,
      height: canUseZeroSizeTextarea ? 0 : 1,
      useCover: false
    });
  }
  _newlinecount(text) {
    let result = 0;
    let startIndex = -1;
    do {
      startIndex = text.indexOf("\n", startIndex + 1);
      if (startIndex === -1) {
        break;
      }
      result++;
    } while (true);
    return result;
  }
  _renderAtTopLeft() {
    this._doRender({
      lastRenderPosition: null,
      top: 0,
      left: 0,
      width: canUseZeroSizeTextarea ? 0 : 1,
      height: canUseZeroSizeTextarea ? 0 : 1,
      useCover: true
    });
  }
  _doRender(renderData) {
    this._lastRenderPosition = renderData.lastRenderPosition;
    const ta = this.textArea;
    const tac = this.textAreaCover;
    domFontInfo.applyFontInfo(ta, this._fontInfo);
    ta.setTop(renderData.top);
    ta.setLeft(renderData.left);
    ta.setWidth(renderData.width);
    ta.setHeight(renderData.height);
    ta.setColor(renderData.color ? color.Color.Format.CSS.formatHex(renderData.color) : "");
    ta.setFontStyle(renderData.italic ? "italic" : "");
    if (renderData.bold) {
      ta.setFontWeight("bold");
    }
    ta.setTextDecoration(`${renderData.underline ? " underline" : ""}${renderData.strikethrough ? " line-through" : ""}`);
    tac.setTop(renderData.useCover ? renderData.top : 0);
    tac.setLeft(renderData.useCover ? renderData.left : 0);
    tac.setWidth(renderData.useCover ? renderData.width : 0);
    tac.setHeight(renderData.useCover ? renderData.height : 0);
    const options = this._context.configuration.options;
    if (options.get(
      50
      /* glyphMargin */
    )) {
      tac.setClassName("monaco-editor-background textAreaCover " + margin.Margin.OUTER_CLASS_NAME);
    } else {
      if (options.get(
        60
        /* lineNumbers */
      ).renderType !== 0) {
        tac.setClassName("monaco-editor-background textAreaCover " + lineNumbers.LineNumbersOverlay.CLASS_NAME);
      } else {
        tac.setClassName("monaco-editor-background textAreaCover");
      }
    }
  }
}
function measureText(text, fontInfo) {
  if (text.length === 0) {
    return 0;
  }
  const container = document.createElement("div");
  container.style.position = "absolute";
  container.style.top = "-50000px";
  container.style.width = "50000px";
  const regularDomNode = document.createElement("span");
  domFontInfo.applyFontInfo(regularDomNode, fontInfo);
  regularDomNode.style.whiteSpace = "pre";
  regularDomNode.append(text);
  container.appendChild(regularDomNode);
  document.body.appendChild(container);
  const res = regularDomNode.offsetWidth;
  document.body.removeChild(container);
  return res;
}
exports.TextAreaHandler = TextAreaHandler;
