Search in sources :

Example 1 with ITransformAction

use of org.metaborg.core.action.ITransformAction in project spoofax by metaborg.

the class ActionFacetFromESV method create.

@Nullable
public static ActionFacet create(IStrategoAppl esv) {
    final Iterable<IStrategoAppl> menuTerms = ESVReader.collectTerms(esv, "ToolbarMenu");
    final Collection<IMenu> menus = Lists.newLinkedList();
    final Multimap<ITransformGoal, ITransformAction> actions = HashMultimap.create();
    final ImmutableList<String> nesting = ImmutableList.of();
    for (IStrategoAppl menuTerm : menuTerms) {
        final IMenu submenu = menu(menuTerm, new TransformActionFlags(), nesting, actions);
        menus.add(submenu);
    }
    addCompileGoal(esv, actions);
    if (menus.isEmpty() && actions.isEmpty()) {
        return null;
    }
    return new ActionFacet(actions, menus);
}
Also used : IMenu(org.metaborg.core.menu.IMenu) ITransformGoal(org.metaborg.core.action.ITransformGoal) TransformActionFlags(org.metaborg.core.action.TransformActionFlags) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) ITransformAction(org.metaborg.core.action.ITransformAction) Nullable(javax.annotation.Nullable)

Example 2 with ITransformAction

use of org.metaborg.core.action.ITransformAction in project spoofax by metaborg.

the class ActionFacetFromESV method menu.

private static Menu menu(IStrategoTerm menuTerm, TransformActionFlags flags, ImmutableList<String> nesting, Multimap<ITransformGoal, ITransformAction> actions) {
    final String name = name(menuTerm.getSubterm(0));
    final ImmutableList<String> newNesting = ImmutableList.<String>builder().addAll(nesting).add(name).build();
    final TransformActionFlags extraFlags = flags(menuTerm.getSubterm(1));
    final TransformActionFlags mergedFlags = TransformActionFlags.merge(flags, extraFlags);
    final Iterable<IStrategoTerm> items = menuTerm.getSubterm(2);
    final Menu menu = new Menu(name);
    for (IStrategoTerm item : items) {
        final String constructor = Tools.constructorName(item);
        if (constructor == null) {
            logger.error("Could not interpret menu item from term {}", item);
            continue;
        }
        switch(constructor) {
            case "Submenu":
                final Menu submenu = menu(item, mergedFlags, newNesting, actions);
                menu.add(submenu);
                break;
            case "Action":
                final String actionName = name(item.getSubterm(0));
                final String strategy = Tools.asJavaString(item.getSubterm(1).getSubterm(0));
                final TransformActionFlags actionFlags = flags(item.getSubterm(2));
                final TransformActionFlags mergedActionFlags = TransformActionFlags.merge(mergedFlags, actionFlags);
                final ImmutableList<String> newActionNesting = ImmutableList.<String>builder().addAll(newNesting).add(actionName).build();
                final NamedGoal goal = new NamedGoal(newActionNesting);
                final TransformAction action = new TransformAction(actionName, goal, mergedActionFlags, strategy);
                actions.put(goal, action);
                actions.put(new EndNamedGoal(goal.names.get(goal.names.size() - 1)), action);
                final MenuAction menuAction = new MenuAction(action);
                menu.add(menuAction);
                break;
            case "Separator":
                final Separator separator = new Separator();
                menu.add(separator);
                break;
            default:
                logger.warn("Unhandled menu item term {}", item);
                break;
        }
    }
    return menu;
}
Also used : MenuAction(org.metaborg.core.menu.MenuAction) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) NamedGoal(org.metaborg.core.action.NamedGoal) EndNamedGoal(org.metaborg.core.action.EndNamedGoal) TransformActionFlags(org.metaborg.core.action.TransformActionFlags) EndNamedGoal(org.metaborg.core.action.EndNamedGoal) ITransformAction(org.metaborg.core.action.ITransformAction) IMenu(org.metaborg.core.menu.IMenu) Menu(org.metaborg.core.menu.Menu) Separator(org.metaborg.core.menu.Separator)

Example 3 with ITransformAction

use of org.metaborg.core.action.ITransformAction in project spoofax by metaborg.

the class ActionFacetFromESV method addCompileGoal.

private static void addCompileGoal(IStrategoAppl esv, Multimap<ITransformGoal, ITransformAction> actions) {
    final List<IStrategoAppl> onSaveHandlers = ESVReader.collectTerms(esv, "OnSave");
    if (onSaveHandlers.isEmpty()) {
        return;
    }
    for (IStrategoAppl onSaveHandler : onSaveHandlers) {
        final String strategyName = Tools.asJavaString(onSaveHandler.getSubterm(0).getSubterm(0));
        final ITransformGoal goal = new CompileGoal();
        final ITransformAction action = new TransformAction("Compile", goal, new TransformActionFlags(), strategyName);
        actions.put(goal, action);
    }
}
Also used : CompileGoal(org.metaborg.core.action.CompileGoal) ITransformGoal(org.metaborg.core.action.ITransformGoal) TransformActionFlags(org.metaborg.core.action.TransformActionFlags) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) ITransformAction(org.metaborg.core.action.ITransformAction) ITransformAction(org.metaborg.core.action.ITransformAction)

Example 4 with ITransformAction

use of org.metaborg.core.action.ITransformAction in project spoofax by metaborg.

the class ActionService method actionContributions.

@Override
public Collection<TransformActionContrib> actionContributions(ILanguageImpl language, ITransformGoal goal) {
    final Iterable<FacetContribution<ActionFacet>> facetsContributions = language.facetContributions(ActionFacet.class);
    final Collection<TransformActionContrib> actionContributions = Lists.newLinkedList();
    for (FacetContribution<ActionFacet> facetContribution : facetsContributions) {
        final ActionFacet facet = facetContribution.facet;
        final ILanguageComponent component = facetContribution.contributor;
        for (ITransformAction action : facet.actions(goal)) {
            actionContributions.add(new TransformActionContrib(action, component));
        }
    }
    return actionContributions;
}
Also used : TransformActionContrib(org.metaborg.core.action.TransformActionContrib) FacetContribution(org.metaborg.core.language.FacetContribution) ITransformAction(org.metaborg.core.action.ITransformAction) ILanguageComponent(org.metaborg.core.language.ILanguageComponent)

Aggregations

ITransformAction (org.metaborg.core.action.ITransformAction)4 TransformActionFlags (org.metaborg.core.action.TransformActionFlags)3 ITransformGoal (org.metaborg.core.action.ITransformGoal)2 IMenu (org.metaborg.core.menu.IMenu)2 IStrategoAppl (org.spoofax.interpreter.terms.IStrategoAppl)2 Nullable (javax.annotation.Nullable)1 CompileGoal (org.metaborg.core.action.CompileGoal)1 EndNamedGoal (org.metaborg.core.action.EndNamedGoal)1 NamedGoal (org.metaborg.core.action.NamedGoal)1 TransformActionContrib (org.metaborg.core.action.TransformActionContrib)1 FacetContribution (org.metaborg.core.language.FacetContribution)1 ILanguageComponent (org.metaborg.core.language.ILanguageComponent)1 Menu (org.metaborg.core.menu.Menu)1 MenuAction (org.metaborg.core.menu.MenuAction)1 Separator (org.metaborg.core.menu.Separator)1 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)1