<commit_before>/**
 * Copyright (c) 2015 by Brainwy Software Ltda. All Rights Reserved.
 * Licensed under the terms of the Eclipse Public License (EPL).
 * Please see the license.txt included with this distribution for details.
 * Any modifications to this file must keep this entire header intact.
 */
package org.python.pydev.debug.console;

import java.io.IOException;

import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.internal.ui.views.console.ProcessConsole;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentPartitioner;
import org.eclipse.jface.text.source.SourceViewerConfiguration;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Layout;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.console.IConsoleConstants;
import org.eclipse.ui.console.IOConsoleOutputStream;
import org.eclipse.ui.console.TextConsoleViewer;
import org.eclipse.ui.internal.console.IOConsolePage;
import org.eclipse.ui.internal.console.IOConsolePartitioner;
import org.python.pydev.core.log.Log;
import org.python.pydev.debug.newconsole.CurrentPyStackFrameForConsole;
import org.python.pydev.debug.newconsole.PydevConsoleConstants;
import org.python.pydev.debug.newconsole.PydevConsoleFactory;
import org.python.pydev.debug.newconsole.PydevDebugConsole;
import org.python.pydev.debug.newconsole.PydevDebugConsoleCommunication;
import org.python.pydev.debug.newconsole.prefs.ColorManager;
import org.python.pydev.shared_interactive_console.console.IScriptConsoleCommunication;
import org.python.pydev.shared_interactive_console.console.InterpreterResponse;
import org.python.pydev.shared_interactive_console.console.ScriptConsolePrompt;
import org.python.pydev.shared_interactive_console.console.ui.IConsoleStyleProvider;
import org.python.pydev.shared_interactive_console.console.ui.IScriptConsoleListener;
import org.python.pydev.shared_interactive_console.console.ui.internal.IScriptConsoleContentHandler;
import org.python.pydev.shared_interactive_console.console.ui.internal.ScriptConsoleViewer;

@SuppressWarnings("restriction")
public class PromptOverlay implements DisposeListener, Listener, IScriptConsoleContentHandler {

    private StyledText interactiveConsoleTextWidget;
    private StyledText styledText;
    private Composite styledTextParent;
    private CustomPageBookLayout customLayout;
    private final CurrentPyStackFrameForConsole currentPyStackFrameForConsole;
    private ScriptConsoleViewer viewer;
    private PromptOverlayReplaceGlobalActionHandlers promptOverlayActionHandlers;
    private boolean overlayVisible = true;
    private double percSize = .3;
    private PydevDebugConsole debugConsole;
    private boolean bufferedOutput = false;

    public PromptOverlay(IOConsolePage consolePage, final ProcessConsole processConsole,
            CurrentPyStackFrameForConsole currentPyStackFrameForConsole) {

        this.currentPyStackFrameForConsole = currentPyStackFrameForConsole;
        SourceViewerConfiguration cfg;
        try {
            ILaunch launch = processConsole.getProcess().getLaunch();
            debugConsole = new PydevConsoleFactory().createDebugConsole(launch, "", false, bufferedOutput,
                    currentPyStackFrameForConsole);
            cfg = debugConsole.createSourceViewerConfiguration();
            processConsole.setAttribute(PydevDebugConsole.SCRIPT_DEBUG_CONSOLE_IN_PROCESS_CONSOLE, debugConsole);
        } catch (Exception e) {
            // If we can't create the debug console, bail out and do nothing else.
            Log.log(e);
            return;
        }

        TextConsoleViewer consoleViewer = consolePage.getViewer();
        final StyledText styledText = (StyledText) consoleViewer.getControl();
        this.styledText = styledText;
        styledTextParent = styledText.getParent();

        final IConsoleStyleProvider styleProvider = debugConsole.createStyleProvider();
        viewer = new ScriptConsoleViewer(styledTextParent, debugConsole, this, styleProvider,
                debugConsole.getInitialCommands(), debugConsole.getFocusOnStart(), debugConsole.getBackspaceAction(),
                debugConsole.getAutoEditStrategy(), debugConsole.getTabCompletionEnabled(), false);
        viewer.configure(cfg);

        Layout currentLayout = styledTextParent.getLayout();
        this.customLayout = new CustomPageBookLayout(currentLayout);
        this.interactiveConsoleTextWidget = viewer.getTextWidget();

        final IOConsoleOutputStream streamPrompt = processConsole.newOutputStream();
        final IOConsoleOutputStream stream = processConsole.newOutputStream();
        this.promptOverlayActionHandlers = new PromptOverlayReplaceGlobalActionHandlers(consolePage, viewer);

        IActionBars bars = consolePage.getSite().getActionBars();
        IToolBarManager toolbarManager = bars.getToolBarManager();

        ShowPromptOverlayAction showPromptOverlayAction = new ShowPromptOverlayAction(this);
        toolbarManager.prependToGroup(IConsoleConstants.LAUNCH_GROUP, showPromptOverlayAction);
        bars.updateActionBars();

        debugConsole.addListener(new IScriptConsoleListener() {

            @Override
            public void userRequest(String text, ScriptConsolePrompt prompt) {
                try {
                    if (!bufferedOutput) {
                        streamPrompt.setColor(ColorManager.getDefault().getPreferenceColor(
                                PydevConsoleConstants.CONSOLE_PROMPT_COLOR));

                        stream.setColor(ColorManager.getDefault().getPreferenceColor(
                                PydevConsoleConstants.CONSOLE_INPUT_COLOR));

                        IDocument document = processConsole.getDocument();
                        IDocumentPartitioner partitioner = document.getDocumentPartitioner();
                        IOConsolePartitioner ioConsolePartitioner = (IOConsolePartitioner) partitioner;
<commit_msg>Setting attribute to identify when it's the prompt overlay.<commit_after>/**
 * Copyright (c) 2015 by Brainwy Software Ltda. All Rights Reserved.
 * Licensed under the terms of the Eclipse Public License (EPL).
 * Please see the license.txt included with this distribution for details.
 * Any modifications to this file must keep this entire header intact.
 */
package org.python.pydev.debug.console;

import java.io.IOException;

import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.internal.ui.views.console.ProcessConsole;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentPartitioner;
import org.eclipse.jface.text.source.SourceViewerConfiguration;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Layout;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.console.IConsoleConstants;
import org.eclipse.ui.console.IOConsoleOutputStream;
import org.eclipse.ui.console.TextConsoleViewer;
import org.eclipse.ui.internal.console.IOConsolePage;
import org.eclipse.ui.internal.console.IOConsolePartitioner;
import org.python.pydev.core.log.Log;
import org.python.pydev.debug.newconsole.CurrentPyStackFrameForConsole;
import org.python.pydev.debug.newconsole.PydevConsoleConstants;
import org.python.pydev.debug.newconsole.PydevConsoleFactory;
import org.python.pydev.debug.newconsole.PydevDebugConsole;
import org.python.pydev.debug.newconsole.PydevDebugConsoleCommunication;
import org.python.pydev.debug.newconsole.prefs.ColorManager;
import org.python.pydev.shared_interactive_console.console.IScriptConsoleCommunication;
import org.python.pydev.shared_interactive_console.console.InterpreterResponse;
import org.python.pydev.shared_interactive_console.console.ScriptConsolePrompt;
import org.python.pydev.shared_interactive_console.console.ui.IConsoleStyleProvider;
import org.python.pydev.shared_interactive_console.console.ui.IScriptConsoleListener;
import org.python.pydev.shared_interactive_console.console.ui.internal.IScriptConsoleContentHandler;
import org.python.pydev.shared_interactive_console.console.ui.internal.ScriptConsoleViewer;

@SuppressWarnings("restriction")
public class PromptOverlay implements DisposeListener, Listener, IScriptConsoleContentHandler {

    private static final String IS_PROMPT_OVERLAY_STYLED_TEXT = "IS_PROMPT_OVERLAY_STYLED_TEXT";
    private StyledText interactiveConsoleTextWidget;
    private StyledText styledText;
    private Composite styledTextParent;
    private CustomPageBookLayout customLayout;
    private final CurrentPyStackFrameForConsole currentPyStackFrameForConsole;
    private ScriptConsoleViewer viewer;
    private PromptOverlayReplaceGlobalActionHandlers promptOverlayActionHandlers;
    private boolean overlayVisible = true;
    private double percSize = .3;
    private PydevDebugConsole debugConsole;
    private boolean bufferedOutput = false;

    public PromptOverlay(IOConsolePage consolePage, final ProcessConsole processConsole,
            CurrentPyStackFrameForConsole currentPyStackFrameForConsole) {

        this.currentPyStackFrameForConsole = currentPyStackFrameForConsole;
        SourceViewerConfiguration cfg;
        try {
            ILaunch launch = processConsole.getProcess().getLaunch();
            debugConsole = new PydevConsoleFactory().createDebugConsole(launch, "", false, bufferedOutput,
                    currentPyStackFrameForConsole);
            cfg = debugConsole.createSourceViewerConfiguration();
            processConsole.setAttribute(PydevDebugConsole.SCRIPT_DEBUG_CONSOLE_IN_PROCESS_CONSOLE, debugConsole);
        } catch (Exception e) {
            // If we can't create the debug console, bail out and do nothing else.
            Log.log(e);
            return;
        }

        TextConsoleViewer consoleViewer = consolePage.getViewer();
        final StyledText styledText = (StyledText) consoleViewer.getControl();
        this.styledText = styledText;
        styledTextParent = styledText.getParent();

        final IConsoleStyleProvider styleProvider = debugConsole.createStyleProvider();
        viewer = new ScriptConsoleViewer(styledTextParent, debugConsole, this, styleProvider,
                debugConsole.getInitialCommands(), debugConsole.getFocusOnStart(), debugConsole.getBackspaceAction(),
                debugConsole.getAutoEditStrategy(), debugConsole.getTabCompletionEnabled(), false);
        viewer.configure(cfg);

        Layout currentLayout = styledTextParent.getLayout();
        this.customLayout = new CustomPageBookLayout(currentLayout);
        this.interactiveConsoleTextWidget = viewer.getTextWidget();
        this.interactiveConsoleTextWidget.setData(IS_PROMPT_OVERLAY_STYLED_TEXT, Boolean.TRUE);

        final IOConsoleOutputStream streamPrompt = processConsole.newOutputStream();
        final IOConsoleOutputStream stream = processConsole.newOutputStream();
        this.promptOverlayActionHandlers = new PromptOverlayReplaceGlobalActionHandlers(consolePage, viewer);

        IActionBars bars = consolePage.getSite().getActionBars();
        IToolBarManager toolbarManager = bars.getToolBarManager();

        ShowPromptOverlayAction showPromptOverlayAction = new ShowPromptOverlayAction(this);
        toolbarManager.prependToGroup(IConsoleConstants.LAUNCH_GROUP, showPromptOverlayAction);
        bars.updateActionBars();

        debugConsole.addListener(new IScriptConsoleListener() {

            @Override
            public void userRequest(String text, ScriptConsolePrompt prompt) {
                try {
                    if (!bufferedOutput) {
                        streamPrompt.setColor(ColorManager.getDefault().getPreferenceColor(
                                PydevConsoleConstants.CONSOLE_PROMPT_COLOR));

                        stream.setColor(ColorManager.getDefault().getPreferenceColor(
                                PydevConsoleConstants.CONSOLE_INPUT_COLOR));

                        IDocument document = processConsole.getDocument();
                        IDocumentPartitioner partitioner = document.getDocumentPartitioner();
                        IOConsolePartitioner ioConsolePartitioner = (IOConsolePartitioner) partitioner;

{'fraction_non_alphanumeric': 0.07577755371131621, 'fraction_numerical': 0.000775614674629644, 'mean_word_length': 4.211802748585287, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4744317', 'n_tokens_mistral': 3382, 'n_tokens_neox': 3293, 'n_words': 606}