<commit_before>package com.kodcu.component;

import com.kodcu.controller.ApplicationController;
import com.kodcu.other.IOHelper;
import com.kodcu.service.DirectoryService;
import com.kodcu.service.ParserService;
import com.kodcu.service.ThreadService;
import com.kodcu.service.convert.markdown.MarkdownService;
import com.kodcu.service.extension.AsciiTreeGenerator;
import com.kodcu.service.shortcut.ShortcutProvider;
import com.kodcu.service.ui.TabService;
import javafx.application.Platform;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.Event;
import javafx.scene.Node;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.input.Dragboard;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.util.Callback;
import netscape.javascript.JSObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileTime;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * Created by usta on 09.04.2015.
 */
@Component
@Scope("prototype")
public class EditorPane extends AnchorPane {

    private final WebView webView;
    private final Logger logger = LoggerFactory.getLogger(EditorPane.class);
    private final ApplicationController controller;
    private final ThreadService threadService;
    private final ShortcutProvider shortcutProvider;
    private final ApplicationContext applicationContext;
    private final TabService tabService;
    private final AsciiTreeGenerator asciiTreeGenerator;
    private final ParserService parserService;
    private final ObservableList<Runnable> handleReadyTasks;
    private String mode = "ace/mode/asciidoc";
    private String initialEditorValue = "";
    private Path path;
    private FileTime lastModifiedTime;
    private final String escapeBackSlash = "(?<!\\\\)"; // ignores if word started with \
    private final String ignoreSuffix = "(?<!\\\\)"; // ignores if word started with \
    private final BooleanProperty ready = new SimpleBooleanProperty(false);

    @Value("${application.live.url}")
    private String liveUrl;

    @Value("${application.preview.url}")
    private String previewUrl;

    private final DirectoryService directoryService;

    @Autowired
    public EditorPane(ApplicationController controller, ThreadService threadService, ShortcutProvider shortcutProvider, ApplicationContext applicationContext, TabService tabService, AsciiTreeGenerator asciiTreeGenerator, ParserService parserService, DirectoryService directoryService) {
        this.controller = controller;
        this.threadService = threadService;
        this.shortcutProvider = shortcutProvider;
        this.applicationContext = applicationContext;
        this.tabService = tabService;
        this.asciiTreeGenerator = asciiTreeGenerator;
        this.directoryService = directoryService;
        this.handleReadyTasks = FXCollections.observableArrayList();
        this.parserService = parserService;
        this.webView = new WebView();
        webEngine().setConfirmHandler(this::handleConfirm);
        initializeMargins();
        initializeEditorContextMenus();
    }

    private Boolean handleConfirm(String param) {
        if ("command:ready".equals(param)) {
            handleEditorReady();
            ObservableList<Runnable> runnables = FXCollections.observableArrayList(handleReadyTasks);
            handleReadyTasks.clear();
            for (Runnable runnable : runnables) {
                runnable.run();
            }
            ready.setValue(true);
            updatePreviewUrl();
        }
        return false;
    }

    private void handleEditorReady() {
        getWindow().setMember("afx", controller);
        updateOptions();

        if (Objects.nonNull(path)) {
            threadService.runTaskLater(() -> {
                final String content = IOHelper.readFile(path);
                setLastModifiedTime(IOHelper.getLastModifiedTime(path));
                threadService.runActionLater(() -> {
                    changeEditorMode();
                    setInitialized();
                    setEditorValue(content);
                    resetUndoManager();
                });
            });
        } else {
            setInitialized();
            setEditorValue(initialEditorValue);
            resetUndoManager();
        }

        this.getChildren().add(webView);
        webView.requestFocus();
    }

    public void updatePreviewUrl() {
        threadService.runActionLater(() -> {
            if (is("asciidoc") || is("markdown")) {
                applicationContext.getBean(HtmlPane.class)
                        .load(String.format(previewUrl, controller.getPort(), directoryService.interPath(path)));
            } else if (is("html")) {
                applicationContext.getBean(LiveReloadPane.class)
                        .load(String.format(liveUrl, controller.getPort(), directoryService.interPath(path)));
            }
        }, true);
    }

    private void updateOptions() {
        webEngine().executeScript("updateOptions()");
    }

    private void setInitialized() {
        webEngine().executeScript("setInitialized()");
    }

    private void resetUndoManager() {
        webEngine().executeScript("resetUndoManager()");<commit_msg>Fix issue in editorpane<commit_after>package com.kodcu.component;

import com.kodcu.controller.ApplicationController;
import com.kodcu.other.IOHelper;
import com.kodcu.service.DirectoryService;
import com.kodcu.service.ParserService;
import com.kodcu.service.ThreadService;
import com.kodcu.service.convert.markdown.MarkdownService;
import com.kodcu.service.extension.AsciiTreeGenerator;
import com.kodcu.service.shortcut.ShortcutProvider;
import com.kodcu.service.ui.TabService;
import javafx.application.Platform;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.Event;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.input.Dragboard;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.util.Callback;
import netscape.javascript.JSObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileTime;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * Created by usta on 09.04.2015.
 */
@Component
@Scope("prototype")
public class EditorPane extends AnchorPane {

    private final WebView webView;
    private final Logger logger = LoggerFactory.getLogger(EditorPane.class);
    private final ApplicationController controller;
    private final ThreadService threadService;
    private final ShortcutProvider shortcutProvider;
    private final ApplicationContext applicationContext;
    private final TabService tabService;
    private final AsciiTreeGenerator asciiTreeGenerator;
    private final ParserService parserService;
    private final ObservableList<Runnable> handleReadyTasks;
    private String mode = "ace/mode/asciidoc";
    private String initialEditorValue = "";
    private Path path;
    private FileTime lastModifiedTime;
    private final String escapeBackSlash = "(?<!\\\\)"; // ignores if word started with \
    private final String ignoreSuffix = "(?<!\\\\)"; // ignores if word started with \
    private final BooleanProperty ready = new SimpleBooleanProperty(false);

    @Value("${application.live.url}")
    private String liveUrl;

    @Value("${application.preview.url}")
    private String previewUrl;

    private final DirectoryService directoryService;

    @Autowired
    public EditorPane(ApplicationController controller, ThreadService threadService, ShortcutProvider shortcutProvider, ApplicationContext applicationContext, TabService tabService, AsciiTreeGenerator asciiTreeGenerator, ParserService parserService, DirectoryService directoryService) {
        this.controller = controller;
        this.threadService = threadService;
        this.shortcutProvider = shortcutProvider;
        this.applicationContext = applicationContext;
        this.tabService = tabService;
        this.asciiTreeGenerator = asciiTreeGenerator;
        this.directoryService = directoryService;
        this.handleReadyTasks = FXCollections.observableArrayList();
        this.parserService = parserService;
        this.webView = new WebView();
        this.ready.addListener(this::afterEditorReady);
        webEngine().setConfirmHandler(this::handleConfirm);
        initializeMargins();
        initializeEditorContextMenus();
    }

    private Boolean handleConfirm(String param) {
        if ("command:ready".equals(param)) {
            afterEditorLoaded();
        }
        return false;
    }

    private void afterEditorLoaded() {
        getWindow().setMember("afx", controller);
        updateOptions();

        if (Objects.nonNull(path)) {
            threadService.runTaskLater(() -> {
                final String content = IOHelper.readFile(path);
                setLastModifiedTime(IOHelper.getLastModifiedTime(path));
                threadService.runActionLater(() -> {
                    changeEditorMode();
                    setInitialized();
                    setEditorValue(content);
                    resetUndoManager();
                    ready.setValue(true);
                });
            });
        } else {
            setInitialized();
            setEditorValue(initialEditorValue);
            resetUndoManager();
            ready.setValue(true);
        }

        this.getChildren().add(webView);
        webView.requestFocus();
    }

    private void afterEditorReady(ObservableValue observable, boolean oldValue, boolean newValue) {
        if (newValue) {
            ObservableList<Runnable> runnables = FXCollections.observableArrayList(handleReadyTasks);
            handleReadyTasks.clear();
            for (Runnable runnable : runnables) {
                runnable.run();
            }

            updatePreviewUrl();
        }
    }

    ;

    public void updatePreviewUrl() {
        threadService.runActionLater(() -> {
            if (is("asciidoc") || is("markdown")) {
                applicationContext.getBean(HtmlPane.class)
                        .load(String.format(previewUrl, controller.getPort(), directoryService.interPath(path)));
            } else if (is("html")) {
                applicationContext.getBean(LiveReloadPane.class)
                        .load(String.format(liveUrl, controller.getPort(), directoryService.interPath(path)));
            }
        }, true);
    }

    private void updateOptions() {
        webEngine().executeScript("updateOptions()");
    }

    private void setInitialized() {
        webEngine().executeScript("setInitialized()");
    }

    private void resetUndoManager() {
        webEngine().executeScript("resetUndoManager()");
{'fraction_non_alphanumeric': 0.09742284963887066, 'fraction_numerical': 0.0016414970453053185, 'mean_word_length': 3.317859673990078, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, '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': '6265067', 'n_tokens_mistral': 3163, 'n_tokens_neox': 3068, 'n_words': 622}