"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const aria = require("../../../../base/browser/ui/aria/aria.js");
const arrays = require("../../../../base/common/arrays.js");
const async = require("../../../../base/common/async.js");
const cancellation = require("../../../../base/common/cancellation.js");
const errors = require("../../../../base/common/errors.js");
const lifecycle = require("../../../../base/common/lifecycle.js");
const editorExtensions = require("../../../browser/editorExtensions.js");
const range = require("../../../common/core/range.js");
const editorContextKeys = require("../../../common/editorContextKeys.js");
const model = require("../../../common/model.js");
const textModel = require("../../../common/model/textModel.js");
const languages = require("../../../common/languages.js");
const nls = require("../../../../nls.js");
const contextkey = require("../../../../platform/contextkey/common/contextkey.js");
const colorRegistry = require("../../../../platform/theme/common/colorRegistry.js");
const themeService = require("../../../../platform/theme/common/themeService.js");
const languageFeatures = require("../../../common/services/languageFeatures.js");
var __decorate = globalThis && globalThis.__decorate || function(decorators, target, key, desc) {
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
    r = Reflect.decorate(decorators, target, key, desc);
  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;
  return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __param = globalThis && globalThis.__param || function(paramIndex, decorator) {
  return function(target, key) {
    decorator(target, key, paramIndex);
  };
};
const editorWordHighlight = colorRegistry.registerColor("editor.wordHighlightBackground", { dark: "#575757B8", light: "#57575740", hc: null }, nls.localize("wordHighlight", "Background color of a symbol during read-access, like reading a variable. The color must not be opaque so as not to hide underlying decorations."), true);
const editorWordHighlightStrong = colorRegistry.registerColor("editor.wordHighlightStrongBackground", { dark: "#004972B8", light: "#0e639c40", hc: null }, nls.localize("wordHighlightStrong", "Background color of a symbol during write-access, like writing to a variable. The color must not be opaque so as not to hide underlying decorations."), true);
const editorWordHighlightBorder = colorRegistry.registerColor("editor.wordHighlightBorder", { light: null, dark: null, hc: colorRegistry.activeContrastBorder }, nls.localize("wordHighlightBorder", "Border color of a symbol during read-access, like reading a variable."));
const editorWordHighlightStrongBorder = colorRegistry.registerColor("editor.wordHighlightStrongBorder", { light: null, dark: null, hc: colorRegistry.activeContrastBorder }, nls.localize("wordHighlightStrongBorder", "Border color of a symbol during write-access, like writing to a variable."));
const overviewRulerWordHighlightForeground = colorRegistry.registerColor("editorOverviewRuler.wordHighlightForeground", { dark: "#A0A0A0CC", light: "#A0A0A0CC", hc: "#A0A0A0CC" }, nls.localize("overviewRulerWordHighlightForeground", "Overview ruler marker color for symbol highlights. The color must not be opaque so as not to hide underlying decorations."), true);
const overviewRulerWordHighlightStrongForeground = colorRegistry.registerColor("editorOverviewRuler.wordHighlightStrongForeground", { dark: "#C0A0C0CC", light: "#C0A0C0CC", hc: "#C0A0C0CC" }, nls.localize("overviewRulerWordHighlightStrongForeground", "Overview ruler marker color for write-access symbol highlights. The color must not be opaque so as not to hide underlying decorations."), true);
const ctxHasWordHighlights = new contextkey.RawContextKey("hasWordHighlights", false);
function getOccurrencesAtPosition(registry, model2, position, token) {
  const orderedByScore = registry.ordered(model2);
  return async.first(orderedByScore.map((provider) => () => {
    return Promise.resolve(provider.provideDocumentHighlights(model2, position, token)).then(void 0, errors.onUnexpectedExternalError);
  }), arrays.isNonEmptyArray);
}
class OccurenceAtPositionRequest {
  constructor(_model, _selection, _wordSeparators) {
    this._model = _model;
    this._selection = _selection;
    this._wordSeparators = _wordSeparators;
    this._wordRange = this._getCurrentWordRange(_model, _selection);
    this._result = null;
  }
  get result() {
    if (!this._result) {
      this._result = async.createCancelablePromise((token) => this._compute(this._model, this._selection, this._wordSeparators, token));
    }
    return this._result;
  }
  _getCurrentWordRange(model2, selection) {
    const word = model2.getWordAtPosition(selection.getPosition());
    if (word) {
      return new range.Range(selection.startLineNumber, word.startColumn, selection.startLineNumber, word.endColumn);
    }
    return null;
  }
  isValid(model2, selection, decorationIds) {
    const lineNumber = selection.startLineNumber;
    const startColumn = selection.startColumn;
    const endColumn = selection.endColumn;
    const currentWordRange = this._getCurrentWordRange(model2, selection);
    let requestIsValid = Boolean(this._wordRange && this._wordRange.equalsRange(currentWordRange));
    for (let i = 0, len = decorationIds.length; !requestIsValid && i < len; i++) {
      let range2 = model2.getDecorationRange(decorationIds[i]);
      if (range2 && range2.startLineNumber === lineNumber) {
        if (range2.startColumn <= startColumn && range2.endColumn >= endColumn) {
          requestIsValid = true;
        }
      }
    }
    return requestIsValid;
  }
  cancel() {
    this.result.cancel();
  }
}
class SemanticOccurenceAtPositionRequest extends OccurenceAtPositionRequest {
  constructor(model2, selection, wordSeparators, providers) {
    super(model2, selection, wordSeparators);
    this._providers = providers;
  }
  _compute(model2, selection, wordSeparators, token) {
    return getOccurrencesAtPosition(this._providers, model2, selection.getPosition(), token).then((value) => value || []);
  }
}
class TextualOccurenceAtPositionRequest extends OccurenceAtPositionRequest {
  constructor(model2, selection, wordSeparators) {
    super(model2, selection, wordSeparators);
    this._selectionIsEmpty = selection.isEmpty();
  }
  _compute(model2, selection, wordSeparators, token) {
    return async.timeout(250, token).then(() => {
      if (!selection.isEmpty()) {
        return [];
      }
      const word = model2.getWordAtPosition(selection.getPosition());
      if (!word || word.word.length > 1e3) {
        return [];
      }
      const matches = model2.findMatches(word.word, true, false, true, wordSeparators, false);
      return matches.map((m) => {
        return {
          range: m.range,
          kind: languages.DocumentHighlightKind.Text
        };
      });
    });
  }
  isValid(model2, selection, decorationIds) {
    const currentSelectionIsEmpty = selection.isEmpty();
    if (this._selectionIsEmpty !== currentSelectionIsEmpty) {
      return false;
    }
    return super.isValid(model2, selection, decorationIds);
  }
}
function computeOccurencesAtPosition(registry, model2, selection, wordSeparators) {
  if (registry.has(model2)) {
    return new SemanticOccurenceAtPositionRequest(model2, selection, wordSeparators, registry);
  }
  return new TextualOccurenceAtPositionRequest(model2, selection, wordSeparators);
}
editorExtensions.registerModelAndPositionCommand("_executeDocumentHighlights", (accessor, model2, position) => {
  const languageFeaturesService = accessor.get(languageFeatures.ILanguageFeaturesService);
  return getOccurrencesAtPosition(languageFeaturesService.documentHighlightProvider, model2, position, cancellation.CancellationToken.None);
});
class WordHighlighter {
  constructor(editor, providers, contextKeyService) {
    this.toUnhook = new lifecycle.DisposableStore();
    this.workerRequestTokenId = 0;
    this.workerRequestCompleted = false;
    this.workerRequestValue = [];
    this.lastCursorPositionChangeTime = 0;
    this.renderDecorationsTimer = -1;
    this.editor = editor;
    this.providers = providers;
    this._hasWordHighlights = ctxHasWordHighlights.bindTo(contextKeyService);
    this._ignorePositionChangeEvent = false;
    this.occurrencesHighlight = this.editor.getOption(
      72
      /* occurrencesHighlight */
    );
    this.model = this.editor.getModel();
    this.toUnhook.add(editor.onDidChangeCursorPosition((e) => {
      if (this._ignorePositionChangeEvent) {
        return;
      }
      if (!this.occurrencesHighlight) {
        return;
      }
      this._onPositionChanged(e);
    }));
    this.toUnhook.add(editor.onDidChangeModelContent((e) => {
      this._stopAll();
    }));
    this.toUnhook.add(editor.onDidChangeConfiguration((e) => {
      let newValue = this.editor.getOption(
        72
        /* occurrencesHighlight */
      );
      if (this.occurrencesHighlight !== newValue) {
        this.occurrencesHighlight = newValue;
        this._stopAll();
      }
    }));
    this._decorationIds = [];
    this.workerRequestTokenId = 0;
    this.workerRequest = null;
    this.workerRequestCompleted = false;
    this.lastCursorPositionChangeTime = 0;
    this.renderDecorationsTimer = -1;
  }
  hasDecorations() {
    return this._decorationIds.length > 0;
  }
  restore() {
    if (!this.occurrencesHighlight) {
      return;
    }
    this._run();
  }
  _getSortedHighlights() {
    return arrays.coalesce(this._decorationIds.map((id) => this.model.getDecorationRange(id)).sort(range.Range.compareRangesUsingStarts));
  }
  moveNext() {
    let highlights = this._getSortedHighlights();
    let index = highlights.findIndex((range2) => range2.containsPosition(this.editor.getPosition()));
    let newIndex = (index + 1) % highlights.length;
    let dest = highlights[newIndex];
    try {
      this._ignorePositionChangeEvent = true;
      this.editor.setPosition(dest.getStartPosition());
      this.editor.revealRangeInCenterIfOutsideViewport(dest);
      const word = this._getWord();
      if (word) {
        const lineContent = this.editor.getModel().getLineContent(dest.startLineNumber);
        aria.alert(`${lineContent}, ${newIndex + 1} of ${highlights.length} for '${word.word}'`);
      }
    } finally {
      this._ignorePositionChangeEvent = false;
    }
  }
  moveBack() {
    let highlights = this._getSortedHighlights();
    let index = highlights.findIndex((range2) => range2.containsPosition(this.editor.getPosition()));
    let newIndex = (index - 1 + highlights.length) % highlights.length;
    let dest = highlights[newIndex];
    try {
      this._ignorePositionChangeEvent = true;
      this.editor.setPosition(dest.getStartPosition());
      this.editor.revealRangeInCenterIfOutsideViewport(dest);
      const word = this._getWord();
      if (word) {
        const lineContent = this.editor.getModel().getLineContent(dest.startLineNumber);
        aria.alert(`${lineContent}, ${newIndex + 1} of ${highlights.length} for '${word.word}'`);
      }
    } finally {
      this._ignorePositionChangeEvent = false;
    }
  }
  _removeDecorations() {
    if (this._decorationIds.length > 0) {
      this._decorationIds = this.editor.deltaDecorations(this._decorationIds, []);
      this._hasWordHighlights.set(false);
    }
  }
  _stopAll() {
    this._removeDecorations();
    if (this.renderDecorationsTimer !== -1) {
      clearTimeout(this.renderDecorationsTimer);
      this.renderDecorationsTimer = -1;
    }
    if (this.workerRequest !== null) {
      this.workerRequest.cancel();
      this.workerRequest = null;
    }
    if (!this.workerRequestCompleted) {
      this.workerRequestTokenId++;
      this.workerRequestCompleted = true;
    }
  }
  _onPositionChanged(e) {
    if (!this.occurrencesHighlight) {
      this._stopAll();
      return;
    }
    if (e.reason !== 3) {
      this._stopAll();
      return;
    }
    this._run();
  }
  _getWord() {
    let editorSelection = this.editor.getSelection();
    let lineNumber = editorSelection.startLineNumber;
    let startColumn = editorSelection.startColumn;
    return this.model.getWordAtPosition({
      lineNumber,
      column: startColumn
    });
  }
  _run() {
    let editorSelection = this.editor.getSelection();
    if (editorSelection.startLineNumber !== editorSelection.endLineNumber) {
      this._stopAll();
      return;
    }
    let startColumn = editorSelection.startColumn;
    let endColumn = editorSelection.endColumn;
    const word = this._getWord();
    if (!word || word.startColumn > startColumn || word.endColumn < endColumn) {
      this._stopAll();
      return;
    }
    const workerRequestIsValid = this.workerRequest && this.workerRequest.isValid(this.model, editorSelection, this._decorationIds);
    this.lastCursorPositionChangeTime = (/* @__PURE__ */ new Date()).getTime();
    if (workerRequestIsValid) {
      if (this.workerRequestCompleted && this.renderDecorationsTimer !== -1) {
        clearTimeout(this.renderDecorationsTimer);
        this.renderDecorationsTimer = -1;
        this._beginRenderDecorations();
      }
    } else {
      this._stopAll();
      let myRequestId = ++this.workerRequestTokenId;
      this.workerRequestCompleted = false;
      this.workerRequest = computeOccurencesAtPosition(this.providers, this.model, this.editor.getSelection(), this.editor.getOption(
        117
        /* wordSeparators */
      ));
      this.workerRequest.result.then((data) => {
        if (myRequestId === this.workerRequestTokenId) {
          this.workerRequestCompleted = true;
          this.workerRequestValue = data || [];
          this._beginRenderDecorations();
        }
      }, errors.onUnexpectedError);
    }
  }
  _beginRenderDecorations() {
    let currentTime = (/* @__PURE__ */ new Date()).getTime();
    let minimumRenderTime = this.lastCursorPositionChangeTime + 250;
    if (currentTime >= minimumRenderTime) {
      this.renderDecorationsTimer = -1;
      this.renderDecorations();
    } else {
      this.renderDecorationsTimer = setTimeout(() => {
        this.renderDecorations();
      }, minimumRenderTime - currentTime);
    }
  }
  renderDecorations() {
    this.renderDecorationsTimer = -1;
    let decorations = [];
    for (const info of this.workerRequestValue) {
      if (info.range) {
        decorations.push({
          range: info.range,
          options: WordHighlighter._getDecorationOptions(info.kind)
        });
      }
    }
    this._decorationIds = this.editor.deltaDecorations(this._decorationIds, decorations);
    this._hasWordHighlights.set(this.hasDecorations());
  }
  static _getDecorationOptions(kind) {
    if (kind === languages.DocumentHighlightKind.Write) {
      return this._WRITE_OPTIONS;
    } else if (kind === languages.DocumentHighlightKind.Text) {
      return this._TEXT_OPTIONS;
    } else {
      return this._REGULAR_OPTIONS;
    }
  }
  dispose() {
    this._stopAll();
    this.toUnhook.dispose();
  }
}
WordHighlighter._WRITE_OPTIONS = textModel.ModelDecorationOptions.register({
  description: "word-highlight-strong",
  stickiness: 1,
  className: "wordHighlightStrong",
  overviewRuler: {
    color: themeService.themeColorFromId(overviewRulerWordHighlightStrongForeground),
    position: model.OverviewRulerLane.Center
  },
  minimap: {
    color: themeService.themeColorFromId(colorRegistry.minimapSelectionOccurrenceHighlight),
    position: model.MinimapPosition.Inline
  }
});
WordHighlighter._TEXT_OPTIONS = textModel.ModelDecorationOptions.register({
  description: "selection-highlight",
  stickiness: 1,
  className: "selectionHighlight",
  overviewRuler: {
    color: themeService.themeColorFromId(colorRegistry.overviewRulerSelectionHighlightForeground),
    position: model.OverviewRulerLane.Center
  },
  minimap: {
    color: themeService.themeColorFromId(colorRegistry.minimapSelectionOccurrenceHighlight),
    position: model.MinimapPosition.Inline
  }
});
WordHighlighter._REGULAR_OPTIONS = textModel.ModelDecorationOptions.register({
  description: "word-highlight",
  stickiness: 1,
  className: "wordHighlight",
  overviewRuler: {
    color: themeService.themeColorFromId(overviewRulerWordHighlightForeground),
    position: model.OverviewRulerLane.Center
  },
  minimap: {
    color: themeService.themeColorFromId(colorRegistry.minimapSelectionOccurrenceHighlight),
    position: model.MinimapPosition.Inline
  }
});
let WordHighlighterContribution = class WordHighlighterContribution2 extends lifecycle.Disposable {
  constructor(editor, contextKeyService, languageFeaturesService) {
    super();
    this.wordHighlighter = null;
    const createWordHighlighterIfPossible = () => {
      if (editor.hasModel()) {
        this.wordHighlighter = new WordHighlighter(editor, languageFeaturesService.documentHighlightProvider, contextKeyService);
      }
    };
    this._register(editor.onDidChangeModel((e) => {
      if (this.wordHighlighter) {
        this.wordHighlighter.dispose();
        this.wordHighlighter = null;
      }
      createWordHighlighterIfPossible();
    }));
    createWordHighlighterIfPossible();
  }
  static get(editor) {
    return editor.getContribution(WordHighlighterContribution2.ID);
  }
  saveViewState() {
    if (this.wordHighlighter && this.wordHighlighter.hasDecorations()) {
      return true;
    }
    return false;
  }
  moveNext() {
    if (this.wordHighlighter) {
      this.wordHighlighter.moveNext();
    }
  }
  moveBack() {
    if (this.wordHighlighter) {
      this.wordHighlighter.moveBack();
    }
  }
  restoreViewState(state) {
    if (this.wordHighlighter && state) {
      this.wordHighlighter.restore();
    }
  }
  dispose() {
    if (this.wordHighlighter) {
      this.wordHighlighter.dispose();
      this.wordHighlighter = null;
    }
    super.dispose();
  }
};
WordHighlighterContribution.ID = "editor.contrib.wordHighlighter";
WordHighlighterContribution = __decorate([
  __param(1, contextkey.IContextKeyService),
  __param(2, languageFeatures.ILanguageFeaturesService)
], WordHighlighterContribution);
class WordHighlightNavigationAction extends editorExtensions.EditorAction {
  constructor(next, opts) {
    super(opts);
    this._isNext = next;
  }
  run(accessor, editor) {
    const controller = WordHighlighterContribution.get(editor);
    if (!controller) {
      return;
    }
    if (this._isNext) {
      controller.moveNext();
    } else {
      controller.moveBack();
    }
  }
}
class NextWordHighlightAction extends WordHighlightNavigationAction {
  constructor() {
    super(true, {
      id: "editor.action.wordHighlight.next",
      label: nls.localize("wordHighlight.next.label", "Go to Next Symbol Highlight"),
      alias: "Go to Next Symbol Highlight",
      precondition: ctxHasWordHighlights,
      kbOpts: {
        kbExpr: editorContextKeys.EditorContextKeys.editorTextFocus,
        primary: 65,
        weight: 100
        /* EditorContrib */
      }
    });
  }
}
class PrevWordHighlightAction extends WordHighlightNavigationAction {
  constructor() {
    super(false, {
      id: "editor.action.wordHighlight.prev",
      label: nls.localize("wordHighlight.previous.label", "Go to Previous Symbol Highlight"),
      alias: "Go to Previous Symbol Highlight",
      precondition: ctxHasWordHighlights,
      kbOpts: {
        kbExpr: editorContextKeys.EditorContextKeys.editorTextFocus,
        primary: 1024 | 65,
        weight: 100
        /* EditorContrib */
      }
    });
  }
}
class TriggerWordHighlightAction extends editorExtensions.EditorAction {
  constructor() {
    super({
      id: "editor.action.wordHighlight.trigger",
      label: nls.localize("wordHighlight.trigger.label", "Trigger Symbol Highlight"),
      alias: "Trigger Symbol Highlight",
      precondition: ctxHasWordHighlights.toNegated(),
      kbOpts: {
        kbExpr: editorContextKeys.EditorContextKeys.editorTextFocus,
        primary: 0,
        weight: 100
        /* EditorContrib */
      }
    });
  }
  run(accessor, editor, args) {
    const controller = WordHighlighterContribution.get(editor);
    if (!controller) {
      return;
    }
    controller.restoreViewState(true);
  }
}
editorExtensions.registerEditorContribution(WordHighlighterContribution.ID, WordHighlighterContribution);
editorExtensions.registerEditorAction(NextWordHighlightAction);
editorExtensions.registerEditorAction(PrevWordHighlightAction);
editorExtensions.registerEditorAction(TriggerWordHighlightAction);
themeService.registerThemingParticipant((theme, collector) => {
  const selectionHighlight = theme.getColor(colorRegistry.editorSelectionHighlight);
  if (selectionHighlight) {
    collector.addRule(`.monaco-editor .focused .selectionHighlight { background-color: ${selectionHighlight}; }`);
    collector.addRule(`.monaco-editor .selectionHighlight { background-color: ${selectionHighlight.transparent(0.5)}; }`);
  }
  const wordHighlight = theme.getColor(editorWordHighlight);
  if (wordHighlight) {
    collector.addRule(`.monaco-editor .wordHighlight { background-color: ${wordHighlight}; }`);
  }
  const wordHighlightStrong = theme.getColor(editorWordHighlightStrong);
  if (wordHighlightStrong) {
    collector.addRule(`.monaco-editor .wordHighlightStrong { background-color: ${wordHighlightStrong}; }`);
  }
  const selectionHighlightBorder = theme.getColor(colorRegistry.editorSelectionHighlightBorder);
  if (selectionHighlightBorder) {
    collector.addRule(`.monaco-editor .selectionHighlight { border: 1px ${theme.type === "hc" ? "dotted" : "solid"} ${selectionHighlightBorder}; box-sizing: border-box; }`);
  }
  const wordHighlightBorder = theme.getColor(editorWordHighlightBorder);
  if (wordHighlightBorder) {
    collector.addRule(`.monaco-editor .wordHighlight { border: 1px ${theme.type === "hc" ? "dashed" : "solid"} ${wordHighlightBorder}; box-sizing: border-box; }`);
  }
  const wordHighlightStrongBorder = theme.getColor(editorWordHighlightStrongBorder);
  if (wordHighlightStrongBorder) {
    collector.addRule(`.monaco-editor .wordHighlightStrong { border: 1px ${theme.type === "hc" ? "dashed" : "solid"} ${wordHighlightStrongBorder}; box-sizing: border-box; }`);
  }
});
exports.getOccurrencesAtPosition = getOccurrencesAtPosition;
