Search in sources :

Example 1 with IStyle

use of org.metaborg.core.style.IStyle in project spoofax by metaborg.

the class StylerService method style.

@Nullable
private IRegionStyle<IStrategoTerm> style(StylerFacet facet, IRegionCategory<IStrategoTerm> regionCategory) {
    if (regionCategory.region().length() == 0) {
        // Skip empty regions for styling.
        return null;
    }
    final ICategory category = regionCategory.category();
    // HACK: instanceof checks are nasty, but required since we do not have separate specifications for categories
    // and styles, they are intertwined.
    final IStyle style;
    if (category instanceof SortConsCategory) {
        final SortConsCategory cat = (SortConsCategory) category;
        style = facet.sortConsStyle(cat.sort, cat.cons);
    } else if (category instanceof ConsCategory) {
        final ConsCategory cat = (ConsCategory) category;
        style = facet.consStyle(cat.cons);
    } else if (category instanceof SortCategory) {
        final SortCategory cat = (SortCategory) category;
        style = facet.sortStyle(cat.sort);
    } else if (category instanceof TokenCategory) {
        final TokenCategory cat = (TokenCategory) category;
        style = facet.tokenStyle(cat.token);
    } else {
        style = null;
    }
    if (style == null) {
        logger.warn("Cannot determine style for category " + category.name());
        return null;
    }
    return new RegionStyle<>(regionCategory.region(), style, regionCategory.fragment());
}
Also used : IStyle(org.metaborg.core.style.IStyle) IRegionStyle(org.metaborg.core.style.IRegionStyle) RegionStyle(org.metaborg.core.style.RegionStyle) ICategory(org.metaborg.core.style.ICategory) Nullable(javax.annotation.Nullable)

Example 2 with IStyle

use of org.metaborg.core.style.IStyle in project spoofax by metaborg.

the class StylerFacetFromESV method style.

private static IStyle style(IStrategoAppl attribute) {
    final Color color = color((IStrategoAppl) attribute.getSubterm(0));
    final Color backgroundColor = color((IStrategoAppl) attribute.getSubterm(1));
    final boolean bold;
    final boolean italic;
    final boolean underline = false;
    final boolean strikeout = false;
    final IStrategoAppl fontSetting = (IStrategoAppl) attribute.getSubterm(2);
    final String fontSettingCons = fontSetting.getConstructor().getName();
    switch(fontSettingCons) {
        case "BOLD":
            bold = true;
            italic = false;
            break;
        case "ITALIC":
            bold = false;
            italic = true;
            break;
        case "BOLD_ITALIC":
            bold = true;
            italic = true;
            break;
        default:
            bold = false;
            italic = false;
            break;
    }
    return new Style(color, backgroundColor, bold, italic, underline, strikeout);
}
Also used : Color(java.awt.Color) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) IStyle(org.metaborg.core.style.IStyle) Style(org.metaborg.core.style.Style)

Example 3 with IStyle

use of org.metaborg.core.style.IStyle in project spoofax by metaborg.

the class StylerFacetFromESV method create.

@Nullable
public static StylerFacet create(IStrategoAppl esv) {
    final StylerFacet facet = new StylerFacet();
    final Iterable<IStrategoAppl> styleDefs = ESVReader.collectTerms(esv, "ColorDef");
    final Map<String, IStyle> namedStyles = Maps.newHashMap();
    for (IStrategoAppl styleDef : styleDefs) {
        final IStrategoAppl styleTerm = (IStrategoAppl) styleDef.getSubterm(1);
        final IStrategoConstructor styleCons = styleTerm.getConstructor();
        final IStyle style;
        if (styleCons.getName().equals("Attribute")) {
            style = style(styleTerm);
        } else if (styleCons.getName().equals("AttributeRef")) {
            final String name = Tools.asJavaString(styleTerm.getSubterm(0));
            style = namedStyles.get(name);
            if (style == null) {
                logger.error("Cannot resolve style definition " + name + " in style definition " + styleDef);
                continue;
            }
        } else {
            logger.error("Unhandled style " + styleCons + " in style definition " + styleDef);
            continue;
        }
        namedStyles.put(Tools.asJavaString(styleDef.getSubterm(0)), style);
    }
    final Iterable<IStrategoAppl> styleRules = ESVReader.collectTerms(esv, "ColorRule");
    if (Iterables.isEmpty(styleRules)) {
        return null;
    }
    for (IStrategoAppl styleRule : styleRules) {
        final IStrategoAppl styleTerm = (IStrategoAppl) styleRule.getSubterm(1);
        final IStrategoConstructor styleCons = styleTerm.getConstructor();
        final IStyle style;
        if (styleCons.getName().equals("Attribute")) {
            style = style(styleTerm);
        } else if (styleCons.getName().equals("AttributeRef")) {
            final String name = Tools.asJavaString(styleTerm.getSubterm(0));
            style = namedStyles.get(name);
            if (style == null) {
                logger.error("Cannot resolve style definition " + name + " in style rule " + styleRule);
                continue;
            }
        } else {
            logger.error("Unhandled style " + styleCons + " in style rule " + styleRule);
            continue;
        }
        final IStrategoAppl node = (IStrategoAppl) styleRule.getSubterm(0);
        final IStrategoConstructor nodeCons = node.getConstructor();
        if (nodeCons.getName().equals("SortAndConstructor")) {
            final String sort = Tools.asJavaString(node.getSubterm(0).getSubterm(0));
            final String cons = Tools.asJavaString(node.getSubterm(1).getSubterm(0));
            facet.mapSortConsToStyle(sort, cons, style);
        } else if (nodeCons.getName().equals("ConstructorOnly")) {
            final String cons = Tools.asJavaString(node.getSubterm(0).getSubterm(0));
            facet.mapConsToStyle(cons, style);
        } else if (nodeCons.getName().equals("Sort")) {
            final String sort = Tools.asJavaString(node.getSubterm(0));
            facet.mapSortToStyle(sort, style);
        } else if (nodeCons.getName().equals("Token")) {
            final IStrategoAppl tokenAppl = (IStrategoAppl) node.getSubterm(0);
            final String token = tokenAppl.getConstructor().getName();
            facet.mapTokenToStyle(token, style);
        } else {
            logger.error("Unhandled node " + nodeCons + " in style rule " + styleRule);
            continue;
        }
    }
    return facet;
}
Also used : IStyle(org.metaborg.core.style.IStyle) IStrategoConstructor(org.spoofax.interpreter.terms.IStrategoConstructor) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) Nullable(javax.annotation.Nullable)

Aggregations

IStyle (org.metaborg.core.style.IStyle)3 Nullable (javax.annotation.Nullable)2 IStrategoAppl (org.spoofax.interpreter.terms.IStrategoAppl)2 Color (java.awt.Color)1 ICategory (org.metaborg.core.style.ICategory)1 IRegionStyle (org.metaborg.core.style.IRegionStyle)1 RegionStyle (org.metaborg.core.style.RegionStyle)1 Style (org.metaborg.core.style.Style)1 IStrategoConstructor (org.spoofax.interpreter.terms.IStrategoConstructor)1