<commit_before>package org.editorconfig.configmanagement;

import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.ex.EditorSettingsExternalizable;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.fileEditor.FileDocumentManagerListener;
import com.intellij.openapi.fileEditor.impl.TrailingSpacesStripper;
import com.intellij.openapi.vfs.VirtualFile;
import org.editorconfig.plugincomponents.SettingsProviderComponent;
import org.editorconfig.core.EditorConfig;
import org.editorconfig.Utils;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public class EditorSettingsManager implements FileDocumentManagerListener {
    // Handles the following EditorConfig settings:
    private static final String trimTrailingWhitespaceKey = "trim_trailing_whitespace";
    private static final String insertFinalNewlineKey = "insert_final_newline";

    private static final Logger LOG = Logger.getInstance("#org.editorconfig.configmanagement.EditorSettingsManager");

    @Override
    public void beforeAllDocumentsSaving() {
        // Not used
    }

    @Override
    public void beforeDocumentSaving(@NotNull Document document) {
        // This is fired when any document is saved, regardless of whether it is part of a save-all or
        // a save-one operation
        LOG.debug("Saving one document");
        VirtualFile file = FileDocumentManager.getInstance().getFile(document);
        applySettings(file);
    }

    @Override
    public void beforeFileContentReload(VirtualFile file, @NotNull Document document) {
        //Not used
    }

    @Override
    public void fileWithNoDocumentChanged(@NotNull VirtualFile file) {
        //Not used
    }

    @Override
    public void fileContentReloaded(VirtualFile file, @NotNull Document document) {
        //Not used
    }

    @Override
    public void fileContentLoaded(@NotNull VirtualFile file, @NotNull Document document) {
        //Not used
    }

    @Override
    public void unsavedDocumentsDropped() {
        //Not used
    }

    private void applySettings(VirtualFile file) {
        // Get editorconfig settings
        String filePath = file.getCanonicalPath();
        SettingsProviderComponent settingsProvider = SettingsProviderComponent.getInstance();
        List<EditorConfig.OutPair> outPairs = settingsProvider.getOutPairs(filePath);
        String trimTrailingWhitespace = Utils.configValueForKey(outPairs, trimTrailingWhitespaceKey);
        String insertFinalNewline = Utils.configValueForKey(outPairs, insertFinalNewlineKey);
        // Apply trailing spaces setting
        if (trimTrailingWhitespace.equals("true")) {
            file.putUserData(TrailingSpacesStripper.OVERRIDE_STRIP_TRAILING_SPACES_KEY,
                             EditorSettingsExternalizable.STRIP_TRAILING_SPACES_WHOLE);
        } else if (trimTrailingWhitespace.equals("false")) {
            file.putUserData(TrailingSpacesStripper.OVERRIDE_STRIP_TRAILING_SPACES_KEY,
                             EditorSettingsExternalizable.STRIP_TRAILING_SPACES_NONE);
        } else {
            if (!trimTrailingWhitespace.isEmpty()) {
                LOG.warn(Utils.invalidConfigMessage(trimTrailingWhitespace, trimTrailingWhitespaceKey, filePath));
            }
            file.putUserData(TrailingSpacesStripper.OVERRIDE_STRIP_TRAILING_SPACES_KEY, null);
        }
        // Apply final newline setting
        if (insertFinalNewline.equals("true")) {
            file.putUserData(TrailingSpacesStripper.OVERRIDE_ENSURE_NEWLINE_KEY,
                             TrailingSpacesStripper.ENSURE_NEWLINE);
        } else if (insertFinalNewline.equals("false")) {
            file.putUserData(TrailingSpacesStripper.OVERRIDE_ENSURE_NEWLINE_KEY,
                             TrailingSpacesStripper.DONT_ENSURE_NEWLINE);
        } else {
            if (!insertFinalNewline.isEmpty()) {
                LOG.warn(Utils.invalidConfigMessage(insertFinalNewline, insertFinalNewlineKey, filePath));
            }
            file.putUserData(TrailingSpacesStripper.OVERRIDE_ENSURE_NEWLINE_KEY, null);
        }
        LOG.debug("Applied editor settings for: " + filePath);
        
    }
}<commit_msg>Tidied up userData logic in EditorSettingsManager<commit_after>package org.editorconfig.configmanagement;

import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.ex.EditorSettingsExternalizable;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.fileEditor.FileDocumentManagerListener;
import com.intellij.openapi.fileEditor.impl.TrailingSpacesStripper;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.vfs.VirtualFile;
import org.editorconfig.plugincomponents.SettingsProviderComponent;
import org.editorconfig.core.EditorConfig;
import org.editorconfig.Utils;
import org.jetbrains.annotations.NotNull;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class EditorSettingsManager implements FileDocumentManagerListener {
    // Handles the following EditorConfig settings:
    private static final String trimTrailingWhitespaceKey = "trim_trailing_whitespace";
    private static final String insertFinalNewlineKey = "insert_final_newline";
    private static final Map<String, String> trimMap;
    static {
        Map<String, String> map = new HashMap<String, String>();
        map.put("true", EditorSettingsExternalizable.STRIP_TRAILING_SPACES_WHOLE);
        map.put("false", EditorSettingsExternalizable.STRIP_TRAILING_SPACES_NONE);
        trimMap = Collections.unmodifiableMap(map);
    }
    private static final Map<String, String> newlineMap;
    static {
        Map<String, String> map = new HashMap<String, String>();
        map.put("true", TrailingSpacesStripper.ENSURE_NEWLINE);
        map.put("false", TrailingSpacesStripper.DONT_ENSURE_NEWLINE);
        newlineMap = Collections.unmodifiableMap(map);
    }

    private static final Logger LOG = Logger.getInstance("#org.editorconfig.configmanagement.EditorSettingsManager");

    @Override
    public void beforeAllDocumentsSaving() {
        // Not used
    }

    @Override
    public void beforeDocumentSaving(@NotNull Document document) {
        // This is fired when any document is saved, regardless of whether it is part of a save-all or
        // a save-one operation
        final VirtualFile file = FileDocumentManager.getInstance().getFile(document);
        applySettings(file);
    }

    @Override
    public void beforeFileContentReload(VirtualFile file, @NotNull Document document) {
        //Not used
    }

    @Override
    public void fileWithNoDocumentChanged(@NotNull VirtualFile file) {
        //Not used
    }

    @Override
    public void fileContentReloaded(VirtualFile file, @NotNull Document document) {
        //Not used
    }

    @Override
    public void fileContentLoaded(@NotNull VirtualFile file, @NotNull Document document) {
        //Not used
    }

    @Override
    public void unsavedDocumentsDropped() {
        //Not used
    }

    private void applySettings(VirtualFile file) {
        // Get editorconfig settings
        final String filePath = file.getCanonicalPath();
        final SettingsProviderComponent settingsProvider = SettingsProviderComponent.getInstance();
        final List<EditorConfig.OutPair> outPairs = settingsProvider.getOutPairs(filePath);
        // Apply trailing spaces setting
        final String trimTrailingWhitespace = Utils.configValueForKey(outPairs, trimTrailingWhitespaceKey);
        applyConfigValueToUserData(file, TrailingSpacesStripper.OVERRIDE_STRIP_TRAILING_SPACES_KEY,
                                   trimTrailingWhitespaceKey, trimTrailingWhitespace, trimMap);
        // Apply final newline setting
        final String insertFinalNewline = Utils.configValueForKey(outPairs, insertFinalNewlineKey);
        applyConfigValueToUserData(file, TrailingSpacesStripper.OVERRIDE_ENSURE_NEWLINE_KEY,
                                   insertFinalNewlineKey, insertFinalNewline, newlineMap);
    }

    private void applyConfigValueToUserData(VirtualFile file, Key userDataKey, String editorConfigKey,
                                            String configValue, Map<String, String> configMap) {
        if (configValue.isEmpty()) {
            file.putUserData(userDataKey, null);
        } else {
            final String data = configMap.get(configValue);
            if (data == null) {
                LOG.warn(Utils.invalidConfigMessage(configValue, editorConfigKey, file.getCanonicalPath()));
            } else {
                file.putUserData(userDataKey, data);
                LOG.debug("Applied " + editorConfigKey + " settings for: " + file.getCanonicalPath());
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.07834359261331841, 'fraction_numerical': 0.0, 'mean_word_length': 3.3357593401261525, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7659807', 'n_tokens_mistral': 2418, 'n_tokens_neox': 2387, 'n_words': 481}