<commit_before>package org.metaborg.spoofax.eclipse.util;

import java.util.Collection;
import java.util.Iterator;

import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.TextPresentation;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Display;
import org.metaborg.core.source.ISourceRegion;
import org.metaborg.core.style.IRegionStyle;
import org.metaborg.core.style.IStyle;

import com.google.common.collect.Lists;

/**
 * Utility functions for creating Eclipse text styles.
 */
public final class StyleUtils {
    /**
     * Creates an Eclipse text presentation that colors the entire range as one color.
     * 
     * @param color
     *            Text foreground color to use.
     * @param length
     *            Length of the text.
     * @param display
     *            Display to create the Eclipse text presentation on.
     * @return Eclipse text presentation.
     */
    public static TextPresentation createTextPresentation(java.awt.Color color, int length, Display display) {
        final TextPresentation presentation = new TextPresentation();
        final StyleRange styleRange = new StyleRange();
        styleRange.start = 0;
        styleRange.length = length;
        styleRange.foreground = createColor(color, display);
        presentation.addStyleRange(styleRange);
        return presentation;
    }

    /**
     * Creates an Eclipse text presentation from given Spoofax styles.
     * 
     * @param styles
     *            Stream of Spoofax styles.
     * @param display
     *            Display to create the Eclipse text presentation on.
     * @return Eclipse text presentation.
     */
    public static <T> TextPresentation createTextPresentation(Iterable<IRegionStyle<T>> styles, Display display) {
        final TextPresentation presentation = new TextPresentation();
        for(IRegionStyle<T> regionStyle : styles) {
            final StyleRange styleRange = createStyleRange(regionStyle, display);
            presentation.addStyleRange(styleRange);
        }
        final IRegion extent = presentation.getExtent();
        if(extent != null) {
            final StyleRange defaultStyleRange = new StyleRange();
            defaultStyleRange.start = extent.getOffset();
            defaultStyleRange.length = defaultStyleRange.start + presentation.getExtent().getLength();
            defaultStyleRange.foreground = createColor(java.awt.Color.BLACK, display);
            presentation.setDefaultStyleRange(defaultStyleRange);
        }
        return presentation;
    }

    /**
     * Creates an Eclipse style range from given Spoofax style region.
     * <commit_msg>Fix NPE when styling empty file. Fix editor crash when styling file with layout at the start.<commit_after>package org.metaborg.spoofax.eclipse.util;

import java.util.Collection;
import java.util.Iterator;

import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.text.TextPresentation;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Display;
import org.metaborg.core.source.ISourceRegion;
import org.metaborg.core.style.IRegionStyle;
import org.metaborg.core.style.IStyle;

import com.google.common.collect.Lists;

/**
 * Utility functions for creating Eclipse text styles.
 */
public final class StyleUtils {
    /**
     * Creates an Eclipse text presentation that colors the entire range as one color.
     * 
     * @param color
     *            Text foreground color to use.
     * @param length
     *            Length of the text.
     * @param display
     *            Display to create the Eclipse text presentation on.
     * @return Eclipse text presentation.
     */
    public static TextPresentation createTextPresentation(java.awt.Color color, int length, Display display) {
        final TextPresentation presentation = new TextPresentation();
        final StyleRange styleRange = new StyleRange();
        styleRange.start = 0;
        styleRange.length = length;
        styleRange.foreground = createColor(color, display);
        presentation.addStyleRange(styleRange);
        return presentation;
    }

    /**
     * Creates an Eclipse text presentation from given Spoofax styles.
     * 
     * @param styles
     *            Stream of Spoofax styles.
     * @param display
     *            Display to create the Eclipse text presentation on.
     * @return Eclipse text presentation.
     */
    public static <T> TextPresentation createTextPresentation(Iterable<IRegionStyle<T>> styles, Display display) {
        final TextPresentation presentation = new TextPresentation();
        for(IRegionStyle<T> regionStyle : styles) {
            final StyleRange styleRange = createStyleRange(regionStyle, display);
            presentation.addStyleRange(styleRange);
        }
        IRegion extent = presentation.getExtent();
        if(extent == null) {
            extent = new Region(0, 0);
        }
        final StyleRange defaultStyleRange = new StyleRange();
        defaultStyleRange.start = extent.getOffset();
        defaultStyleRange.length = extent.getLength();
        defaultStyleRange.foreground = createColor(java.awt.Color.BLACK, display);
        presentation.setDefaultStyleRange(defaultStyleRange);

        return presentation;
    }

    /**
     * Creates an Eclipse style range from given Spoofax style region.
     * 
{'fraction_non_alphanumeric': 0.08393501805054152, 'fraction_numerical': 0.0007220216606498195, 'mean_word_length': 2.8425797503467405, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '2775337', 'n_tokens_mistral': 1463, 'n_tokens_neox': 1426, 'n_words': 398}