
import { registerCss } from '../../../../../../../css.js';
import { __decorate, __param } from '../../../../../../../external/tslib/tslib.es6.js';
import { $ as $$1, clearNode, Dimension, isActiveElement, append, getWindow, addStandardDisposableListener, EventType, getActiveWindow } from '../../../../base/browser/dom.js';
import { createStyleSheet } from '../../../../base/browser/domStylesheets.js';
import { status } from '../../../../base/browser/ui/aria/aria.js';
import { MOUSE_CURSOR_TEXT_CSS_CLASS_NAME } from '../../../../base/browser/ui/mouseCursor/mouseCursor.js';
import { timeout, RunOnceScheduler } from '../../../../base/common/async.js';
import { Codicon } from '../../../../base/common/codicons.js';
import { memoize } from '../../../../base/common/decorators.js';
import { Emitter } from '../../../../base/common/event.js';
import { HistoryNavigator } from '../../../../base/common/history.js';
import { KeyCode, KeyMod as KeyMod$1 } from '../../../../base/common/keyCodes.js';
import { Disposable } from '../../../../base/common/lifecycle.js';
import { removeAnsiEscapeCodes } from '../../../../base/common/strings.js';
import { ThemeIcon } from '../../../../base/common/themables.js';
import { URI } from '../../../../base/common/uri.js';
import { isCodeEditor } from '../../../../editor/browser/editorBrowser.js';
import { EditorAction, registerEditorAction } from '../../../../editor/browser/editorExtensions.js';
import { ICodeEditorService } from '../../../../editor/browser/services/codeEditorService.service.js';
import { CodeEditorWidget } from '../../../../editor/browser/widget/codeEditor/codeEditorWidget.js';
import { EditorOption } from '../../../../editor/common/config/editorOptions.js';
import { EDITOR_FONT_DEFAULTS } from '../../../../editor/common/config/fontInfo.js';
import { Range } from '../../../../editor/common/core/range.js';
import { EditorContextKeys } from '../../../../editor/common/editorContextKeys.js';
import { CompletionItemInsertTextRule, CompletionItemKinds, CompletionItemKind } from '../../../../editor/common/languages.js';
import { ILanguageFeaturesService } from '../../../../editor/common/services/languageFeatures.service.js';
import { IModelService } from '../../../../editor/common/services/model.service.js';
import { ITextResourcePropertiesService } from '../../../../editor/common/services/textResourceConfiguration.service.js';
import { SuggestController } from '../../../../editor/contrib/suggest/browser/suggestController.js';
import { localize, localize2 } from '../../../../nls.js';
import { AccessibilitySignal } from '../../../../platform/accessibilitySignal/browser/accessibilitySignalService.js';
import { IAccessibilitySignalService } from '../../../../platform/accessibilitySignal/browser/accessibilitySignalService.service.js';
import { getFlatContextMenuActions } from '../../../../platform/actions/browser/menuEntryActionViewItem.js';
import { MenuId, registerAction2, Action2 } from '../../../../platform/actions/common/actions.js';
import { IMenuService } from '../../../../platform/actions/common/actions.service.js';
import { IClipboardService } from '../../../../platform/clipboard/common/clipboardService.service.js';
import { IConfigurationService } from '../../../../platform/configuration/common/configuration.service.js';
import { ContextKeyExpr } from '../../../../platform/contextkey/common/contextkey.js';
import { IContextKeyService } from '../../../../platform/contextkey/common/contextkey.service.js';
import { IContextMenuService } from '../../../../platform/contextview/browser/contextView.service.js';
import { registerAndCreateHistoryNavigationContext } from '../../../../platform/history/browser/contextScopedHistoryWidget.js';
import { IHoverService } from '../../../../platform/hover/browser/hover.service.js';
import { IInstantiationService } from '../../../../platform/instantiation/common/instantiation.js';
import { ServiceCollection } from '../../../../platform/instantiation/common/serviceCollection.js';
import { IKeybindingService } from '../../../../platform/keybinding/common/keybinding.service.js';
import { KeybindingWeight } from '../../../../platform/keybinding/common/keybindingsRegistry.js';
import { WorkbenchAsyncDataTree } from '../../../../platform/list/browser/listService.js';
import { ILogService } from '../../../../platform/log/common/log.service.js';
import { IOpenerService } from '../../../../platform/opener/common/opener.service.js';
import { StorageScope, StorageTarget } from '../../../../platform/storage/common/storage.js';
import { IStorageService } from '../../../../platform/storage/common/storage.service.js';
import { resolveColorValue } from '../../../../platform/theme/common/colorUtils.js';
import '../../../../platform/theme/common/colors/baseColors.js';
import '../../../../platform/theme/common/colors/chartsColors.js';
import { editorForeground } from '../../../../platform/theme/common/colors/editorColors.js';
import '../../../../platform/theme/common/colors/inputColors.js';
import '../../../../platform/theme/common/colors/listColors.js';
import '../../../../platform/theme/common/colors/menuColors.js';
import '../../../../platform/theme/common/colors/minimapColors.js';
import '../../../../platform/theme/common/colors/miscColors.js';
import '../../../../platform/theme/common/colors/quickpickColors.js';
import '../../../../platform/theme/common/colors/searchColors.js';
import { IThemeService } from '../../../../platform/theme/common/themeService.service.js';
import { registerNavigableContainer } from '../../../browser/actions/widgetNavigationCommands.js';
import { FilterViewPane, ViewAction } from '../../../browser/parts/views/viewPane.js';
import { IViewDescriptorService } from '../../../common/views.service.js';
import { IEditorService } from '../../../services/editor/common/editorService.service.js';
import { IViewsService } from '../../../services/views/common/viewsService.service.js';
import { AccessibilityVerbositySettingId } from '../../accessibility/browser/accessibilityConfiguration.js';
import { AccessibilityCommandId } from '../../accessibility/common/accessibilityCommands.js';
import { getSimpleEditorOptions, getSimpleCodeEditorWidgetOptions } from '../../codeEditor/browser/simpleEditorOptions.js';
import { DEBUG_SCHEME, CONTEXT_MULTI_SESSION_REPL, State, CONTEXT_IN_DEBUG_REPL, REPL_VIEW_ID, CONTEXT_DEBUG_STATE, getStateLabel } from '../common/debug.js';
import { IDebugService } from '../common/debug.service.js';
import { Variable } from '../common/debugModel.js';
import { resolveChildSession } from '../common/debugUtils.js';
import { ReplGroup, ReplEvaluationResult } from '../common/replModel.js';
import { FocusSessionActionViewItem } from './debugActionViewItems.js';
import { DEBUG_COMMAND_CATEGORY, FOCUS_REPL_ID } from './debugCommands.js';
import { DebugExpressionRenderer } from './debugExpressionRenderer.js';
import { debugConsoleEvaluationPrompt, debugConsoleClearAll } from './debugIcons.js';
import * as repl from './media/repl.css';
import { ReplFilter } from './replFilter.js';
import { ReplDelegate, ReplDataSource, ReplEvaluationInputsRenderer, ReplEvaluationResultsRenderer, ReplRawObjectsRenderer, ReplVariablesRenderer, ReplOutputElementRenderer, ReplGroupRenderer, ReplAccessibilityProvider } from './replViewer.js';

var Repl_1, ReplOptions_1;
registerCss(repl);
const $ = $$1;
const HISTORY_STORAGE_KEY = "debug.repl.history";
const FILTER_HISTORY_STORAGE_KEY = "debug.repl.filterHistory";
const FILTER_VALUE_STORAGE_KEY = "debug.repl.filterValue";
const DECORATION_KEY = "replinputdecoration";
function revealLastElement(tree) {
    tree.scrollTop = tree.scrollHeight - tree.renderHeight;
}
const sessionsToIgnore = ( new Set());
const identityProvider = {
    getId: element => element.getId()
};
let Repl = class Repl extends FilterViewPane {
    static {
        Repl_1 = this;
    }
    static {
        this.REFRESH_DELAY = 50;
    }
    static {
        this.URI = URI.parse(`${DEBUG_SCHEME}:replinput`);
    }
    constructor(
        options,
        debugService,
        instantiationService,
        storageService,
        themeService,
        modelService,
        contextKeyService,
        codeEditorService,
        viewDescriptorService,
        contextMenuService,
        configurationService,
        textResourcePropertiesService,
        editorService,
        keybindingService,
        openerService,
        hoverService,
        menuService,
        languageFeaturesService,
        logService
    ) {
        const filterText = storageService.get(FILTER_VALUE_STORAGE_KEY, StorageScope.WORKSPACE, "");
        super({
            ...options,
            filterOptions: {
                placeholder: ( localize(9269, "Filter (e.g. text, !exclude, \\escape)")),
                text: filterText,
                history: JSON.parse(
                    storageService.get(FILTER_HISTORY_STORAGE_KEY, StorageScope.WORKSPACE, "[]")
                )
            }
        }, keybindingService, contextMenuService, configurationService, contextKeyService, viewDescriptorService, instantiationService, openerService, themeService, hoverService);
        this.debugService = debugService;
        this.storageService = storageService;
        this.modelService = modelService;
        this.configurationService = configurationService;
        this.textResourcePropertiesService = textResourcePropertiesService;
        this.editorService = editorService;
        this.keybindingService = keybindingService;
        this.languageFeaturesService = languageFeaturesService;
        this.logService = logService;
        this.previousTreeScrollHeight = 0;
        this.styleChangedWhenInvisible = false;
        this.modelChangeListener = Disposable.None;
        this.findIsOpen = false;
        this.menu = menuService.createMenu(MenuId.DebugConsoleContext, contextKeyService);
        this._register(this.menu);
        this.history = this._register(( new HistoryNavigator(( new Set(
            JSON.parse(this.storageService.get(HISTORY_STORAGE_KEY, StorageScope.WORKSPACE, "[]"))
        )), 100)));
        this.filter = ( new ReplFilter());
        this.filter.filterQuery = filterText;
        this.multiSessionRepl = CONTEXT_MULTI_SESSION_REPL.bindTo(contextKeyService);
        this.replOptions = this._register(
            this.instantiationService.createInstance(ReplOptions, this.id, () => this.getLocationBasedColors().background)
        );
        this._register(this.replOptions.onDidChange(() => this.onDidStyleChange()));
        this._register(
            codeEditorService.registerDecorationType("repl-decoration", DECORATION_KEY, {})
        );
        this.multiSessionRepl.set(this.isMultiSessionView);
        this.registerListeners();
    }
    registerListeners() {
        if (this.debugService.getViewModel().focusedSession) {
            this.onDidFocusSession(this.debugService.getViewModel().focusedSession);
        }
        this._register(this.debugService.getViewModel().onDidFocusSession(session => {
            this.onDidFocusSession(session);
        }));
        this._register(this.debugService.getViewModel().onDidEvaluateLazyExpression(async e => {
            if (e instanceof Variable && this.tree?.hasNode(e)) {
                await this.tree.updateChildren(e, false, true);
                await this.tree.expand(e);
            }
        }));
        this._register(this.debugService.onWillNewSession(async newSession => {
            const input = this.tree?.getInput();
            if (!input || input.state === State.Inactive) {
                await this.selectSession(newSession);
            }
            this.multiSessionRepl.set(this.isMultiSessionView);
        }));
        this._register(this.debugService.onDidEndSession(async () => {
            await Promise.resolve();
            this.multiSessionRepl.set(this.isMultiSessionView);
        }));
        this._register(this.themeService.onDidColorThemeChange(() => {
            this.refreshReplElements(false);
            if (this.isVisible()) {
                this.updateInputDecoration();
            }
        }));
        this._register(this.onDidChangeBodyVisibility(visible => {
            if (!visible) {
                return;
            }
            if (!this.model) {
                this.model = this.modelService.getModel(Repl_1.URI) || this.modelService.createModel("", null, Repl_1.URI, true);
            }
            const focusedSession = this.debugService.getViewModel().focusedSession;
            if (this.tree && this.tree.getInput() !== focusedSession) {
                this.onDidFocusSession(focusedSession);
            }
            this.setMode();
            this.replInput.setModel(this.model);
            this.updateInputDecoration();
            this.refreshReplElements(true);
            if (this.styleChangedWhenInvisible) {
                this.styleChangedWhenInvisible = false;
                if (this.tree?.getInput()) {
                    this.tree.updateChildren(undefined, true, false);
                }
                this.onDidStyleChange();
            }
        }));
        this._register(this.configurationService.onDidChangeConfiguration(e => {
            if (e.affectsConfiguration("debug.console.wordWrap") && this.tree) {
                this.tree.dispose();
                this.treeContainer.innerText = "";
                clearNode(this.treeContainer);
                this.createReplTree();
            }
            if (e.affectsConfiguration("debug.console.acceptSuggestionOnEnter")) {
                const config = this.configurationService.getValue("debug");
                this.replInput.updateOptions({
                    acceptSuggestionOnEnter: config.console.acceptSuggestionOnEnter === "on" ? "on" : "off"
                });
            }
        }));
        this._register(this.editorService.onDidActiveEditorChange(() => {
            this.setMode();
        }));
        this._register(this.filterWidget.onDidChangeFilterText(() => {
            this.filter.filterQuery = this.filterWidget.getFilterText();
            if (this.tree) {
                this.tree.refilter();
                revealLastElement(this.tree);
            }
        }));
    }
    async onDidFocusSession(session) {
        if (session) {
            sessionsToIgnore.delete(session);
            this.completionItemProvider?.dispose();
            if (session.capabilities.supportsCompletionsRequest) {
                this.completionItemProvider = this.languageFeaturesService.completionProvider.register({
                    scheme: DEBUG_SCHEME,
                    pattern: "**/replinput",
                    hasAccessToAllModels: true
                }, {
                    _debugDisplayName: "debugConsole",
                    triggerCharacters: session.capabilities.completionTriggerCharacters || ["."],
                    provideCompletionItems: async (_, position, _context, token) => {
                        this.setHistoryNavigationEnablement(false);
                        const model = this.replInput.getModel();
                        if (model) {
                            const text = model.getValue();
                            const focusedStackFrame = this.debugService.getViewModel().focusedStackFrame;
                            const frameId = focusedStackFrame ? focusedStackFrame.frameId : undefined;
                            const response = await session.completions(frameId, focusedStackFrame?.thread.threadId || 0, text, position, token);
                            const suggestions = [];
                            const computeRange = length => Range.fromPositions(position.delta(0, -length), position);
                            if (response && response.body && response.body.targets) {
                                response.body.targets.forEach(item => {
                                    if (item && item.label) {
                                        let insertTextRules = undefined;
                                        let insertText = item.text || item.label;
                                        if (typeof item.selectionStart === "number") {
                                            insertTextRules = CompletionItemInsertTextRule.InsertAsSnippet;
                                            const selectionLength = typeof item.selectionLength === "number" ? item.selectionLength : 0;
                                            const placeholder = selectionLength > 0 ? "${1:" + insertText.substring(item.selectionStart, item.selectionStart + selectionLength) + "}$0" : "$0";
                                            insertText = insertText.substring(0, item.selectionStart) + placeholder + insertText.substring(item.selectionStart + selectionLength);
                                        }
                                        const label = item.detail ? {
                                            label: item.label,
                                            description: item.detail
                                        } : item.label;
                                        suggestions.push({
                                            label,
                                            insertText,
                                            kind: CompletionItemKinds.fromString(item.type || "property"),
                                            filterText: (item.start && item.length) ? text.substring(item.start, item.start + item.length).concat(item.label) : undefined,
                                            range: computeRange(item.length || 0),
                                            sortText: item.sortText,
                                            insertTextRules
                                        });
                                    }
                                });
                            }
                            if (this.configurationService.getValue("debug").console.historySuggestions) {
                                const history = this.history.getHistory();
                                const idxLength = String(history.length).length;
                                history.forEach((h, i) => suggestions.push({
                                    label: h,
                                    insertText: h,
                                    kind: CompletionItemKind.Text,
                                    range: computeRange(h.length),
                                    sortText: "ZZZ" + String(history.length - i).padStart(idxLength, "0")
                                }));
                            }
                            return {
                                suggestions
                            };
                        }
                        return Promise.resolve({
                            suggestions: []
                        });
                    }
                });
            }
        }
        await this.selectSession();
    }
    getFilterStats() {
        return {
            total: this.tree?.getNode().children.length ?? 0,
            filtered: this.tree?.getNode().children.filter(c => c.visible).length ?? 0
        };
    }
    get isReadonly() {
        const session = this.tree?.getInput();
        if (session && session.state !== State.Inactive) {
            return false;
        }
        return true;
    }
    showPreviousValue() {
        if (!this.isReadonly) {
            this.navigateHistory(true);
        }
    }
    showNextValue() {
        if (!this.isReadonly) {
            this.navigateHistory(false);
        }
    }
    focusFilter() {
        this.filterWidget.focus();
    }
    openFind() {
        this.tree?.openFind();
    }
    setMode() {
        if (!this.isVisible()) {
            return;
        }
        const activeEditorControl = this.editorService.activeTextEditorControl;
        if (isCodeEditor(activeEditorControl)) {
            this.modelChangeListener.dispose();
            this.modelChangeListener = activeEditorControl.onDidChangeModelLanguage(() => this.setMode());
            if (this.model && activeEditorControl.hasModel()) {
                this.model.setLanguage(activeEditorControl.getModel().getLanguageId());
            }
        }
    }
    onDidStyleChange() {
        if (!this.isVisible()) {
            this.styleChangedWhenInvisible = true;
            return;
        }
        if (this.styleElement) {
            this.replInput.updateOptions({
                fontSize: this.replOptions.replConfiguration.fontSize,
                lineHeight: this.replOptions.replConfiguration.lineHeight,
                fontFamily: this.replOptions.replConfiguration.fontFamily === "default" ? EDITOR_FONT_DEFAULTS.fontFamily : this.replOptions.replConfiguration.fontFamily
            });
            const replInputLineHeight = this.replInput.getOption(EditorOption.lineHeight);
            this.styleElement.textContent = `
                .repl .repl-input-wrapper .repl-input-chevron {
                    line-height: ${replInputLineHeight}px
                }

                .repl .repl-input-wrapper .monaco-editor .lines-content {
                    background-color: ${this.replOptions.replConfiguration.backgroundColor};
                }
            `;
            const cssFontFamily = this.replOptions.replConfiguration.fontFamily === "default" ? "var(--monaco-monospace-font)" : this.replOptions.replConfiguration.fontFamily;
            this.container.style.setProperty(`--vscode-repl-font-family`, cssFontFamily);
            this.container.style.setProperty(
                `--vscode-repl-font-size`,
                `${this.replOptions.replConfiguration.fontSize}px`
            );
            this.container.style.setProperty(
                `--vscode-repl-font-size-for-twistie`,
                `${this.replOptions.replConfiguration.fontSizeForTwistie}px`
            );
            this.container.style.setProperty(
                `--vscode-repl-line-height`,
                this.replOptions.replConfiguration.cssLineHeight
            );
            this.tree?.rerender();
            if (this.bodyContentDimension) {
                this.layoutBodyContent(this.bodyContentDimension.height, this.bodyContentDimension.width);
            }
        }
    }
    navigateHistory(previous) {
        const historyInput = (previous ? (this.history.previous() ?? this.history.first()) : this.history.next()) ?? "";
        this.replInput.setValue(historyInput);
        status(historyInput);
        this.replInput.setPosition({
            lineNumber: 1,
            column: historyInput.length + 1
        });
        this.setHistoryNavigationEnablement(true);
    }
    async selectSession(session) {
        const treeInput = this.tree?.getInput();
        if (!session) {
            const focusedSession = this.debugService.getViewModel().focusedSession;
            if (focusedSession) {
                session = focusedSession;
            } else if (!treeInput || ( sessionsToIgnore.has(treeInput))) {
                session = this.debugService.getModel().getSessions(true).find(s => !( sessionsToIgnore.has(s)));
            }
        }
        if (session) {
            this.replElementsChangeListener?.dispose();
            this.replElementsChangeListener = session.onDidChangeReplElements(() => {
                this.refreshReplElements(session.getReplElements().length === 0);
            });
            if (this.tree && treeInput !== session) {
                try {
                    await this.tree.setInput(session);
                } catch (err) {
                    this.logService.error(err);
                }
                revealLastElement(this.tree);
            }
        }
        this.replInput?.updateOptions({
            readOnly: this.isReadonly
        });
        this.updateInputDecoration();
    }
    async clearRepl() {
        const session = this.tree?.getInput();
        if (session) {
            session.removeReplExpressions();
            if (session.state === State.Inactive) {
                sessionsToIgnore.add(session);
                await this.selectSession();
                this.multiSessionRepl.set(this.isMultiSessionView);
            }
        }
        this.replInput.focus();
    }
    acceptReplInput() {
        const session = this.tree?.getInput();
        if (session && !this.isReadonly) {
            session.addReplExpression(
                this.debugService.getViewModel().focusedStackFrame,
                this.replInput.getValue()
            );
            revealLastElement(this.tree);
            this.history.add(this.replInput.getValue());
            this.replInput.setValue("");
            if (this.bodyContentDimension) {
                this.layoutBodyContent(this.bodyContentDimension.height, this.bodyContentDimension.width);
            }
        }
    }
    sendReplInput(input) {
        const session = this.tree?.getInput();
        if (session && !this.isReadonly) {
            session.addReplExpression(this.debugService.getViewModel().focusedStackFrame, input);
            revealLastElement(this.tree);
            this.history.add(input);
        }
    }
    getVisibleContent() {
        let text = "";
        if (this.model && this.tree) {
            const lineDelimiter = this.textResourcePropertiesService.getEOL(this.model.uri);
            const traverseAndAppend = node => {
                node.children.forEach(child => {
                    if (child.visible) {
                        text += ( child.element.toString()).trimRight() + lineDelimiter;
                        if (!child.collapsed && child.children.length) {
                            traverseAndAppend(child);
                        }
                    }
                });
            };
            traverseAndAppend(this.tree.getNode());
        }
        return removeAnsiEscapeCodes(text);
    }
    layoutBodyContent(height, width) {
        this.bodyContentDimension = new Dimension(width, height);
        const replInputHeight = Math.min(this.replInput.getContentHeight(), height);
        if (this.tree) {
            const lastElementVisible = this.tree.scrollTop + this.tree.renderHeight >= this.tree.scrollHeight;
            const treeHeight = height - replInputHeight;
            this.tree.getHTMLElement().style.height = `${treeHeight}px`;
            this.tree.layout(treeHeight, width);
            if (lastElementVisible) {
                revealLastElement(this.tree);
            }
        }
        this.replInputContainer.style.height = `${replInputHeight}px`;
        this.replInput.layout({
            width: width - 30,
            height: replInputHeight
        });
    }
    collapseAll() {
        this.tree?.collapseAll();
    }
    getDebugSession() {
        return this.tree?.getInput();
    }
    getReplInput() {
        return this.replInput;
    }
    getReplDataSource() {
        return this.replDataSource;
    }
    getFocusedElement() {
        return this.tree?.getFocus()?.[0];
    }
    focusTree() {
        this.tree?.domFocus();
    }
    async focus() {
        super.focus();
        await timeout(0);
        this.replInput.focus();
    }
    createActionViewItem(action) {
        if (action.id === selectReplCommandId) {
            const session = (this.tree ? this.tree.getInput() : undefined) ?? this.debugService.getViewModel().focusedSession;
            return this.instantiationService.createInstance(SelectReplActionViewItem, action, session);
        }
        return super.createActionViewItem(action);
    }
    get isMultiSessionView() {
        return this.debugService.getModel().getSessions(true).filter(s => s.hasSeparateRepl() && !( sessionsToIgnore.has(s))).length > 1;
    }
    get refreshScheduler() {
        const autoExpanded = ( new Set());
        return (new RunOnceScheduler(async () => {
            if (!this.tree || !this.tree.getInput() || !this.isVisible()) {
                return;
            }
            await this.tree.updateChildren(undefined, true, false, {
                diffIdentityProvider: identityProvider
            });
            const session = this.tree.getInput();
            if (session) {
                const autoExpandElements = async elements => {
                    for (const element of elements) {
                        if (element instanceof ReplGroup) {
                            if (element.autoExpand && !( autoExpanded.has(element.getId()))) {
                                autoExpanded.add(element.getId());
                                await this.tree.expand(element);
                            }
                            if (!this.tree.isCollapsed(element)) {
                                await autoExpandElements(element.getChildren());
                            }
                        }
                    }
                };
                await autoExpandElements(session.getReplElements());
            }
            const {
                total,
                filtered
            } = this.getFilterStats();
            this.filterWidget.updateBadge(total === filtered || total === 0 ? undefined : ( localize(9270, "Showing {0} of {1}", filtered, total)));
        }, Repl_1.REFRESH_DELAY));
    }
    render() {
        super.render();
        this._register(registerNavigableContainer({
            name: "repl",
            focusNotifiers: [this, this.filterWidget],
            focusNextWidget: () => {
                const element = this.tree?.getHTMLElement();
                if (this.filterWidget.hasFocus()) {
                    this.tree?.domFocus();
                } else if (element && isActiveElement(element)) {
                    this.focus();
                }
            },
            focusPreviousWidget: () => {
                const element = this.tree?.getHTMLElement();
                if (this.replInput.hasTextFocus()) {
                    this.tree?.domFocus();
                } else if (element && isActiveElement(element)) {
                    this.focusFilter();
                }
            }
        }));
    }
    renderBody(parent) {
        super.renderBody(parent);
        this.container = append(parent, $(".repl"));
        this.treeContainer = append(this.container, $(`.repl-tree.${MOUSE_CURSOR_TEXT_CSS_CLASS_NAME}`));
        this.createReplInput(this.container);
        this.createReplTree();
    }
    createReplTree() {
        this.replDelegate = ( new ReplDelegate(this.configurationService, this.replOptions));
        const wordWrap = this.configurationService.getValue("debug").console.wordWrap;
        this.treeContainer.classList.toggle("word-wrap", wordWrap);
        const expressionRenderer = this.instantiationService.createInstance(DebugExpressionRenderer);
        this.replDataSource = ( new ReplDataSource());
        const tree = this.tree = this.instantiationService.createInstance(
            WorkbenchAsyncDataTree,
            "DebugRepl",
            this.treeContainer,
            this.replDelegate,
            [
                this.instantiationService.createInstance(ReplVariablesRenderer, expressionRenderer),
                this.instantiationService.createInstance(ReplOutputElementRenderer, expressionRenderer),
                ( new ReplEvaluationInputsRenderer()),
                this.instantiationService.createInstance(ReplGroupRenderer, expressionRenderer),
                ( new ReplEvaluationResultsRenderer(expressionRenderer)),
                ( new ReplRawObjectsRenderer(expressionRenderer))
            ],
            this.replDataSource,
            {
                filter: this.filter,
                accessibilityProvider: ( new ReplAccessibilityProvider()),
                identityProvider,
                userSelection: true,
                mouseSupport: false,
                findWidgetEnabled: true,
                keyboardNavigationLabelProvider: {
                    getKeyboardNavigationLabel: e => ( e.toString(true))
                },
                horizontalScrolling: !wordWrap,
                setRowLineHeight: false,
                supportDynamicHeights: wordWrap,
                overrideStyles: this.getLocationBasedColors().listOverrideStyles
            }
        );
        this._register(tree.onDidChangeContentHeight(() => {
            if (tree.scrollHeight !== this.previousTreeScrollHeight) {
                const lastElementWasVisible = tree.scrollTop + tree.renderHeight >= this.previousTreeScrollHeight - 2;
                if (lastElementWasVisible) {
                    setTimeout(() => {
                        revealLastElement(tree);
                    }, 0);
                }
            }
            this.previousTreeScrollHeight = tree.scrollHeight;
        }));
        this._register(tree.onContextMenu(e => this.onContextMenu(e)));
        this._register(tree.onDidChangeFindOpenState(open => this.findIsOpen = open));
        let lastSelectedString;
        this._register(tree.onMouseClick(() => {
            if (this.findIsOpen) {
                return;
            }
            const selection = getWindow(this.treeContainer).getSelection();
            if (!selection || selection.type !== "Range" || lastSelectedString === ( selection.toString())) {
                this.replInput.focus();
            }
            lastSelectedString = selection ? ( selection.toString()) : "";
        }));
        this.selectSession();
        this.styleElement = createStyleSheet(this.container, undefined, this._store);
        this.onDidStyleChange();
    }
    createReplInput(container) {
        this.replInputContainer = append(container, $(".repl-input-wrapper"));
        append(this.replInputContainer, $(
            ".repl-input-chevron" + ThemeIcon.asCSSSelector(debugConsoleEvaluationPrompt)
        ));
        const {
            historyNavigationBackwardsEnablement,
            historyNavigationForwardsEnablement
        } = this._register(
            registerAndCreateHistoryNavigationContext(this.scopedContextKeyService, this)
        );
        this.setHistoryNavigationEnablement = enabled => {
            historyNavigationBackwardsEnablement.set(enabled);
            historyNavigationForwardsEnablement.set(enabled);
        };
        CONTEXT_IN_DEBUG_REPL.bindTo(this.scopedContextKeyService).set(true);
        this.scopedInstantiationService = this._register(this.instantiationService.createChild(( new ServiceCollection([IContextKeyService, this.scopedContextKeyService]))));
        const options = getSimpleEditorOptions(this.configurationService);
        options.readOnly = true;
        options.suggest = {
            showStatusBar: true
        };
        const config = this.configurationService.getValue("debug");
        options.acceptSuggestionOnEnter = config.console.acceptSuggestionOnEnter === "on" ? "on" : "off";
        options.ariaLabel = this.getAriaLabel();
        this.replInput = this.scopedInstantiationService.createInstance(
            CodeEditorWidget,
            this.replInputContainer,
            options,
            getSimpleCodeEditorWidgetOptions()
        );
        let lastContentHeight = -1;
        this._register(this.replInput.onDidChangeModelContent(() => {
            const model = this.replInput.getModel();
            this.setHistoryNavigationEnablement(!!model && model.getValue() === "");
            const contentHeight = this.replInput.getContentHeight();
            if (contentHeight !== lastContentHeight) {
                lastContentHeight = contentHeight;
                if (this.bodyContentDimension) {
                    this.layoutBodyContent(this.bodyContentDimension.height, this.bodyContentDimension.width);
                }
            }
        }));
        this._register(this.replInput.onDidFocusEditorText(() => this.updateInputDecoration()));
        this._register(this.replInput.onDidBlurEditorText(() => this.updateInputDecoration()));
        this._register(addStandardDisposableListener(
            this.replInputContainer,
            EventType.FOCUS,
            () => this.replInputContainer.classList.add("synthetic-focus")
        ));
        this._register(addStandardDisposableListener(
            this.replInputContainer,
            EventType.BLUR,
            () => this.replInputContainer.classList.remove("synthetic-focus")
        ));
    }
    getAriaLabel() {
        let ariaLabel = ( localize(9271, "Debug Console"));
        if (!this.configurationService.getValue(AccessibilityVerbositySettingId.Debug)) {
            return ariaLabel;
        }
        const keybinding = this.keybindingService.lookupKeybinding(AccessibilityCommandId.OpenAccessibilityHelp)?.getAriaLabel();
        if (keybinding) {
            ariaLabel = ( localize(9272, "{0}, use ({1}) for accessibility help", ariaLabel, keybinding));
        } else {
            ariaLabel = ( localize(
                9273,
                "{0}, run the command Open Accessibility Help which is currently not triggerable via keybinding.",
                ariaLabel
            ));
        }
        return ariaLabel;
    }
    onContextMenu(e) {
        const actions = getFlatContextMenuActions(this.menu.getActions({
            arg: e.element,
            shouldForwardArgs: false
        }));
        this.contextMenuService.showContextMenu({
            getAnchor: () => e.anchor,
            getActions: () => actions,
            getActionsContext: () => e.element
        });
    }
    refreshReplElements(noDelay) {
        if (this.tree && this.isVisible()) {
            if (this.refreshScheduler.isScheduled()) {
                return;
            }
            this.refreshScheduler.schedule(noDelay ? 0 : undefined);
        }
    }
    updateInputDecoration() {
        if (!this.replInput) {
            return;
        }
        const decorations = [];
        if (this.isReadonly && this.replInput.hasTextFocus() && !this.replInput.getValue()) {
            const transparentForeground = resolveColorValue(editorForeground, this.themeService.getColorTheme())?.transparent(0.4);
            decorations.push({
                range: {
                    startLineNumber: 0,
                    endLineNumber: 0,
                    startColumn: 0,
                    endColumn: 1
                },
                renderOptions: {
                    after: {
                        contentText: ( localize(9274, "Please start a debug session to evaluate expressions")),
                        color: transparentForeground ? ( transparentForeground.toString()) : undefined
                    }
                }
            });
        }
        this.replInput.setDecorationsByType("repl-decoration", DECORATION_KEY, decorations);
    }
    saveState() {
        const replHistory = this.history.getHistory();
        if (replHistory.length) {
            this.storageService.store(
                HISTORY_STORAGE_KEY,
                JSON.stringify(replHistory),
                StorageScope.WORKSPACE,
                StorageTarget.MACHINE
            );
        } else {
            this.storageService.remove(HISTORY_STORAGE_KEY, StorageScope.WORKSPACE);
        }
        const filterHistory = this.filterWidget.getHistory();
        if (filterHistory.length) {
            this.storageService.store(
                FILTER_HISTORY_STORAGE_KEY,
                JSON.stringify(filterHistory),
                StorageScope.WORKSPACE,
                StorageTarget.MACHINE
            );
        } else {
            this.storageService.remove(FILTER_HISTORY_STORAGE_KEY, StorageScope.WORKSPACE);
        }
        const filterValue = this.filterWidget.getFilterText();
        if (filterValue) {
            this.storageService.store(
                FILTER_VALUE_STORAGE_KEY,
                filterValue,
                StorageScope.WORKSPACE,
                StorageTarget.MACHINE
            );
        } else {
            this.storageService.remove(FILTER_VALUE_STORAGE_KEY, StorageScope.WORKSPACE);
        }
        super.saveState();
    }
    dispose() {
        this.replInput?.dispose();
        this.replElementsChangeListener?.dispose();
        this.refreshScheduler.dispose();
        this.modelChangeListener.dispose();
        super.dispose();
    }
};
Repl.__decorator = ( __decorate([memoize], Repl.prototype, "refreshScheduler", null));
Repl = Repl_1 = ( __decorate([( __param(1, IDebugService)), ( __param(2, IInstantiationService)), ( __param(3, IStorageService)), ( __param(4, IThemeService)), ( __param(5, IModelService)), ( __param(6, IContextKeyService)), ( __param(7, ICodeEditorService)), ( __param(8, IViewDescriptorService)), ( __param(9, IContextMenuService)), ( __param(10, IConfigurationService)), ( __param(11, ITextResourcePropertiesService)), ( __param(12, IEditorService)), ( __param(13, IKeybindingService)), ( __param(14, IOpenerService)), ( __param(15, IHoverService)), ( __param(16, IMenuService)), ( __param(17, ILanguageFeaturesService)), ( __param(18, ILogService))], Repl));
let ReplOptions = class ReplOptions extends Disposable {
    static {
        ReplOptions_1 = this;
    }
    static {
        this.lineHeightEm = 1.4;
    }
    get replConfiguration() {
        return this._replConfig;
    }
    constructor(
        viewId,
        backgroundColorDelegate,
        configurationService,
        themeService,
        viewDescriptorService
    ) {
        super();
        this.backgroundColorDelegate = backgroundColorDelegate;
        this.configurationService = configurationService;
        this.themeService = themeService;
        this.viewDescriptorService = viewDescriptorService;
        this._onDidChange = this._register(( new Emitter()));
        this.onDidChange = this._onDidChange.event;
        this._register(this.themeService.onDidColorThemeChange(e => this.update()));
        this._register(this.viewDescriptorService.onDidChangeLocation(e => {
            if (( e.views.some(v => v.id === viewId))) {
                this.update();
            }
        }));
        this._register(this.configurationService.onDidChangeConfiguration(e => {
            if (e.affectsConfiguration("debug.console.lineHeight") || e.affectsConfiguration("debug.console.fontSize") || e.affectsConfiguration("debug.console.fontFamily")) {
                this.update();
            }
        }));
        this.update();
    }
    update() {
        const debugConsole = this.configurationService.getValue("debug").console;
        this._replConfig = {
            fontSize: debugConsole.fontSize,
            fontFamily: debugConsole.fontFamily,
            lineHeight: debugConsole.lineHeight ? debugConsole.lineHeight : ReplOptions_1.lineHeightEm * debugConsole.fontSize,
            cssLineHeight: debugConsole.lineHeight ? `${debugConsole.lineHeight}px` : `${ReplOptions_1.lineHeightEm}em`,
            backgroundColor: this.themeService.getColorTheme().getColor(this.backgroundColorDelegate()),
            fontSizeForTwistie: debugConsole.fontSize * ReplOptions_1.lineHeightEm / 2 - 8
        };
        this._onDidChange.fire();
    }
};
ReplOptions = ReplOptions_1 = ( __decorate([( __param(2, IConfigurationService)), ( __param(3, IThemeService)), ( __param(4, IViewDescriptorService))], ReplOptions));
class AcceptReplInputAction extends EditorAction {
    constructor() {
        super({
            id: "repl.action.acceptInput",
            label: ( localize2(9275, "Debug Console: Accept Input")),
            precondition: CONTEXT_IN_DEBUG_REPL,
            kbOpts: {
                kbExpr: EditorContextKeys.textInputFocus,
                primary: KeyCode.Enter,
                weight: KeybindingWeight.EditorContrib
            }
        });
    }
    run(accessor, editor) {
        SuggestController.get(editor)?.cancelSuggestWidget();
        const repl = getReplView(accessor.get(IViewsService));
        repl?.acceptReplInput();
    }
}
class FilterReplAction extends ViewAction {
    constructor() {
        super({
            viewId: REPL_VIEW_ID,
            id: "repl.action.filter",
            title: ( localize(9276, "Debug Console: Focus Filter")),
            precondition: CONTEXT_IN_DEBUG_REPL,
            keybinding: [{
                when: EditorContextKeys.textInputFocus,
                primary: KeyMod$1.CtrlCmd | KeyCode.KeyF,
                weight: KeybindingWeight.EditorContrib
            }]
        });
    }
    runInView(accessor, repl) {
        repl.focusFilter();
    }
}
class FindReplAction extends ViewAction {
    constructor() {
        super({
            viewId: REPL_VIEW_ID,
            id: "repl.action.find",
            title: ( localize(9277, "Debug Console: Focus Find")),
            precondition: CONTEXT_IN_DEBUG_REPL,
            keybinding: [{
                when: ( ContextKeyExpr.or(CONTEXT_IN_DEBUG_REPL, ( ContextKeyExpr.equals("focusedView", "workbench.panel.repl.view")))),
                primary: KeyMod$1.CtrlCmd | KeyMod$1.Alt | KeyCode.KeyF,
                weight: KeybindingWeight.EditorContrib
            }],
            icon: Codicon.search,
            menu: [{
                id: MenuId.ViewTitle,
                group: "navigation",
                when: ( ContextKeyExpr.equals("view", REPL_VIEW_ID)),
                order: 15
            }, {
                id: MenuId.DebugConsoleContext,
                group: "z_commands",
                order: 25
            }]
        });
    }
    runInView(accessor, view) {
        view.openFind();
    }
}
class ReplCopyAllAction extends EditorAction {
    constructor() {
        super({
            id: "repl.action.copyAll",
            label: ( localize(9278, "Debug: Console Copy All")),
            alias: "Debug Console Copy All",
            precondition: CONTEXT_IN_DEBUG_REPL
        });
    }
    run(accessor, editor) {
        const clipboardService = accessor.get(IClipboardService);
        const repl = getReplView(accessor.get(IViewsService));
        if (repl) {
            return clipboardService.writeText(repl.getVisibleContent());
        }
    }
}
registerEditorAction(AcceptReplInputAction);
registerEditorAction(ReplCopyAllAction);
registerAction2(FilterReplAction);
registerAction2(FindReplAction);
class SelectReplActionViewItem extends FocusSessionActionViewItem {
    getSessions() {
        return this.debugService.getModel().getSessions(true).filter(s => s.hasSeparateRepl() && !( sessionsToIgnore.has(s)));
    }
    mapFocusedSessionToSelected(focusedSession) {
        while (focusedSession.parentSession && !focusedSession.hasSeparateRepl()) {
            focusedSession = focusedSession.parentSession;
        }
        return focusedSession;
    }
}
function getReplView(viewsService) {
    return viewsService.getActiveViewWithId(REPL_VIEW_ID) ?? undefined;
}
const selectReplCommandId = "workbench.action.debug.selectRepl";
registerAction2(class extends ViewAction {
    constructor() {
        super({
            id: selectReplCommandId,
            viewId: REPL_VIEW_ID,
            title: ( localize(9279, "Select Debug Console")),
            f1: false,
            menu: {
                id: MenuId.ViewTitle,
                group: "navigation",
                when: ( ContextKeyExpr.and(( ContextKeyExpr.equals("view", REPL_VIEW_ID)), CONTEXT_MULTI_SESSION_REPL)),
                order: 20
            }
        });
    }
    async runInView(accessor, view, session) {
        const debugService = accessor.get(IDebugService);
        if (session && session.state !== State.Inactive && session !== debugService.getViewModel().focusedSession) {
            session = resolveChildSession(session, debugService.getModel().getSessions());
            await debugService.focusStackFrame(undefined, undefined, session, {
                explicit: true
            });
        }
        await view.selectSession(session);
    }
});
registerAction2(class extends ViewAction {
    constructor() {
        super({
            id: "workbench.debug.panel.action.clearReplAction",
            viewId: REPL_VIEW_ID,
            title: ( localize2(9280, "Clear Console")),
            metadata: {
                description: ( localize2(9281, "Clears all program output from your debug REPL"))
            },
            f1: true,
            icon: debugConsoleClearAll,
            menu: [{
                id: MenuId.ViewTitle,
                group: "navigation",
                when: ( ContextKeyExpr.equals("view", REPL_VIEW_ID)),
                order: 30
            }, {
                id: MenuId.DebugConsoleContext,
                group: "z_commands",
                order: 20
            }],
            keybinding: [{
                primary: 0,
                mac: {
                    primary: KeyMod$1.CtrlCmd | KeyCode.KeyK
                },
                weight: KeybindingWeight.WorkbenchContrib + 1,
                when: ( ContextKeyExpr.equals("focusedView", "workbench.panel.repl.view"))
            }]
        });
    }
    runInView(_accessor, view) {
        const accessibilitySignalService = _accessor.get(IAccessibilitySignalService);
        view.clearRepl();
        accessibilitySignalService.playSignal(AccessibilitySignal.clear);
    }
});
registerAction2(class extends ViewAction {
    constructor() {
        super({
            id: "debug.collapseRepl",
            title: ( localize(9282, "Collapse All")),
            viewId: REPL_VIEW_ID,
            menu: {
                id: MenuId.DebugConsoleContext,
                group: "z_commands",
                order: 10
            }
        });
    }
    runInView(_accessor, view) {
        view.collapseAll();
        view.focus();
    }
});
registerAction2(class extends ViewAction {
    constructor() {
        super({
            id: "debug.replPaste",
            title: ( localize(9283, "Paste")),
            viewId: REPL_VIEW_ID,
            precondition: ( CONTEXT_DEBUG_STATE.notEqualsTo(getStateLabel(State.Inactive))),
            menu: {
                id: MenuId.DebugConsoleContext,
                group: "2_cutcopypaste",
                order: 30
            }
        });
    }
    async runInView(accessor, view) {
        const clipboardService = accessor.get(IClipboardService);
        const clipboardText = await clipboardService.readText();
        if (clipboardText) {
            const replInput = view.getReplInput();
            replInput.setValue(replInput.getValue().concat(clipboardText));
            view.focus();
            const model = replInput.getModel();
            const lineNumber = model ? model.getLineCount() : 0;
            const column = model?.getLineMaxColumn(lineNumber);
            if (typeof lineNumber === "number" && typeof column === "number") {
                replInput.setPosition({
                    lineNumber,
                    column
                });
            }
        }
    }
});
registerAction2(class extends ViewAction {
    constructor() {
        super({
            id: "workbench.debug.action.copyAll",
            title: ( localize(9284, "Copy All")),
            viewId: REPL_VIEW_ID,
            menu: {
                id: MenuId.DebugConsoleContext,
                group: "2_cutcopypaste",
                order: 20
            }
        });
    }
    async runInView(accessor, view) {
        const clipboardService = accessor.get(IClipboardService);
        await clipboardService.writeText(view.getVisibleContent());
    }
});
registerAction2(class extends Action2 {
    constructor() {
        super({
            id: "debug.replCopy",
            title: ( localize(9285, "Copy")),
            menu: {
                id: MenuId.DebugConsoleContext,
                group: "2_cutcopypaste",
                order: 10
            }
        });
    }
    async run(accessor, element) {
        const clipboardService = accessor.get(IClipboardService);
        const debugService = accessor.get(IDebugService);
        const nativeSelection = getActiveWindow().getSelection();
        const selectedText = nativeSelection?.toString();
        if (selectedText && selectedText.length > 0) {
            return clipboardService.writeText(selectedText);
        } else if (element) {
            const retValue = await this.tryEvaluateAndCopy(debugService, element);
            const textToCopy = retValue || removeAnsiEscapeCodes(( element.toString()));
            return clipboardService.writeText(textToCopy);
        }
    }
    async tryEvaluateAndCopy(debugService, element) {
        if (!(element instanceof ReplEvaluationResult)) {
            return;
        }
        const stackFrame = debugService.getViewModel().focusedStackFrame;
        const session = debugService.getViewModel().focusedSession;
        if (!stackFrame || !session || !session.capabilities.supportsClipboardContext) {
            return;
        }
        try {
            const evaluation = await session.evaluate(element.originalExpression, stackFrame.frameId, "clipboard");
            return evaluation?.body.result;
        } catch (e) {
            return;
        }
    }
});
registerAction2(class extends Action2 {
    constructor() {
        super({
            id: FOCUS_REPL_ID,
            category: DEBUG_COMMAND_CATEGORY,
            title: ( localize2(9286, "Focus on Debug Console View"))
        });
    }
    async run(accessor) {
        const viewsService = accessor.get(IViewsService);
        const repl = await viewsService.openView(REPL_VIEW_ID);
        await repl?.focus();
    }
});

export { Repl, getReplView };
