Search in sources :

Example 6 with IStrategoTerm

use of org.spoofax.interpreter.terms.IStrategoTerm in project spoofax by metaborg.

the class OutlineService method toOutlineNode.

@Nullable
private IOutlineNode toOutlineNode(IStrategoTerm term, @Nullable IOutlineNode parent, FileObject location) {
    if (!(term instanceof IStrategoAppl)) {
        return null;
    }
    final IStrategoAppl appl = (IStrategoAppl) term;
    if (!Tools.hasConstructor(appl, "Node", 2)) {
        return null;
    }
    final IStrategoTerm labelTerm = appl.getSubterm(0);
    final String label = label(labelTerm);
    final FileObject icon = icon(labelTerm, location);
    final ISourceRegion region = region(labelTerm);
    final OutlineNode node = new OutlineNode(label, icon, region, parent);
    final IStrategoTerm nodesTerm = appl.getSubterm(1);
    for (IStrategoTerm nodeTerm : nodesTerm) {
        final IOutlineNode childNode = toOutlineNode(nodeTerm, node, location);
        if (childNode != null) {
            node.addChild(childNode);
        }
    }
    return node;
}
Also used : IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) ISourceRegion(org.metaborg.core.source.ISourceRegion) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) OutlineNode(org.metaborg.core.outline.OutlineNode) IOutlineNode(org.metaborg.core.outline.IOutlineNode) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) FileObject(org.apache.commons.vfs2.FileObject) IOutlineNode(org.metaborg.core.outline.IOutlineNode) Nullable(javax.annotation.Nullable)

Example 7 with IStrategoTerm

use of org.spoofax.interpreter.terms.IStrategoTerm in project spoofax by metaborg.

the class OutlineService method icon.

@Nullable
private FileObject icon(IStrategoTerm term, FileObject location) {
    final IStrategoList annos = term.getAnnotations();
    if (annos == null) {
        return null;
    }
    if (annos.getSubtermCount() != 1) {
        return null;
    }
    final IStrategoTerm iconTerm = annos.getSubterm(0);
    if (!(iconTerm instanceof IStrategoString)) {
        return null;
    }
    final IStrategoString iconTermString = (IStrategoString) iconTerm;
    final String iconLocation = iconTermString.stringValue();
    try {
        return location.resolveFile(iconLocation);
    } catch (FileSystemException e) {
        logger.error("Cannot resolve icon {} in {}", e, iconLocation, location);
        return null;
    }
}
Also used : FileSystemException(org.apache.commons.vfs2.FileSystemException) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) IStrategoList(org.spoofax.interpreter.terms.IStrategoList) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) Nullable(javax.annotation.Nullable)

Example 8 with IStrategoTerm

use of org.spoofax.interpreter.terms.IStrategoTerm in project spoofax by metaborg.

the class HoverService method hover.

@Override
public Hover hover(int offset, ISpoofaxParseUnit result) throws MetaborgException {
    if (!result.valid()) {
        return null;
    }
    final FileObject source = result.source();
    final IProject project = projectService.get(source);
    final ILanguageImpl langImpl = result.input().langImpl();
    @Nullable IContext context;
    if (project == null) {
        context = null;
    } else {
        try {
            context = contextService.get(source, project, langImpl);
        } catch (ContextException | MetaborgRuntimeException e) {
            // Failed to get a context, ignore and use the source file to get a stratego runtime later.
            context = null;
        }
    }
    final FacetContribution<HoverFacet> facetContrib = facet(langImpl);
    final HoverFacet facet = facetContrib.facet;
    final ILanguageComponent contributor = facetContrib.contributor;
    final String strategy = facet.strategyName;
    try {
        final ITermFactory termFactory = termFactoryService.get(contributor, project, true);
        final HybridInterpreter interpreter;
        if (context == null) {
            interpreter = strategoRuntimeService.runtime(contributor, source, true);
        } else {
            interpreter = strategoRuntimeService.runtime(contributor, context, true);
        }
        final Iterable<IStrategoTerm> inRegion = tracingService.fragments(result, new SourceRegion(offset));
        final TermWithRegion tuple = common.outputs(termFactory, interpreter, context.location(), source, result.ast(), inRegion, strategy);
        return hover(tuple);
    } catch (MetaborgException e) {
        throw new MetaborgException("Getting hover tooltip information failed unexpectedly", e);
    }
}
Also used : MetaborgRuntimeException(org.metaborg.core.MetaborgRuntimeException) IContext(org.metaborg.core.context.IContext) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) MetaborgException(org.metaborg.core.MetaborgException) HybridInterpreter(org.strategoxt.HybridInterpreter) ISourceRegion(org.metaborg.core.source.ISourceRegion) SourceRegion(org.metaborg.core.source.SourceRegion) IProject(org.metaborg.core.project.IProject) ContextException(org.metaborg.core.context.ContextException) TermWithRegion(org.metaborg.spoofax.core.tracing.TracingCommon.TermWithRegion) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) FileObject(org.apache.commons.vfs2.FileObject) ILanguageComponent(org.metaborg.core.language.ILanguageComponent) ITermFactory(org.spoofax.interpreter.terms.ITermFactory) Nullable(javax.annotation.Nullable)

Example 9 with IStrategoTerm

use of org.spoofax.interpreter.terms.IStrategoTerm in project spoofax by metaborg.

the class HoverService method hover.

private Hover hover(@Nullable TermWithRegion tuple) {
    if (tuple == null) {
        return null;
    }
    final IStrategoTerm output = tuple.term;
    final ISourceRegion offsetRegion = tuple.region;
    final String text;
    if (output.getTermType() == IStrategoTerm.STRING) {
        text = Tools.asJavaString(output);
    } else {
        text = output.toString();
    }
    final String massagedText = text.replace("\\\"", "\"").replace("\\n", "");
    return new Hover(offsetRegion, massagedText);
}
Also used : IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) Hover(org.metaborg.core.tracing.Hover) ISourceRegion(org.metaborg.core.source.ISourceRegion)

Example 10 with IStrategoTerm

use of org.spoofax.interpreter.terms.IStrategoTerm in project spoofax by metaborg.

the class ResolverService method resolve.

private Resolution resolve(@Nullable TermWithRegion tuple) {
    if (tuple == null) {
        return null;
    }
    final IStrategoTerm output = tuple.term;
    final ISourceRegion offsetRegion = tuple.region;
    final Collection<ISourceLocation> targets = Lists.newLinkedList();
    if (output.getTermType() == IStrategoTerm.LIST) {
        for (IStrategoTerm subterm : output) {
            final ISourceLocation targetLocation = common.getTargetLocation(subterm);
            if (targetLocation == null) {
                logger.debug("Cannot get target location for {}", subterm);
                continue;
            }
            targets.add(targetLocation);
        }
    } else {
        final ISourceLocation targetLocation = common.getTargetLocation(output);
        if (targetLocation == null) {
            logger.debug("Reference resolution failed, cannot get target location for {}", output);
            return null;
        }
        targets.add(targetLocation);
    }
    if (targets.isEmpty()) {
        logger.debug("Reference resolution failed, cannot get target locations for {}", output);
        return null;
    }
    return new Resolution(offsetRegion, targets);
}
Also used : IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) ISourceRegion(org.metaborg.core.source.ISourceRegion) ISourceLocation(org.metaborg.core.source.ISourceLocation) Resolution(org.metaborg.core.tracing.Resolution)

Aggregations

IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)82 IStrategoString (org.spoofax.interpreter.terms.IStrategoString)31 FileObject (org.apache.commons.vfs2.FileObject)27 IStrategoAppl (org.spoofax.interpreter.terms.IStrategoAppl)25 MetaborgException (org.metaborg.core.MetaborgException)19 ITermFactory (org.spoofax.interpreter.terms.ITermFactory)17 HybridInterpreter (org.strategoxt.HybridInterpreter)17 IStrategoList (org.spoofax.interpreter.terms.IStrategoList)16 ILanguageImpl (org.metaborg.core.language.ILanguageImpl)15 Nullable (javax.annotation.Nullable)14 ISourceRegion (org.metaborg.core.source.ISourceRegion)12 ICompletion (org.metaborg.core.completion.ICompletion)11 IStrategoTuple (org.spoofax.interpreter.terms.IStrategoTuple)10 IOException (java.io.IOException)9 IProject (org.metaborg.core.project.IProject)9 IMessage (org.metaborg.core.messages.IMessage)8 IToken (org.spoofax.jsglr.client.imploder.IToken)8 IContext (org.metaborg.core.context.IContext)7 ILanguageComponent (org.metaborg.core.language.ILanguageComponent)7 ListImploderAttachment (org.spoofax.jsglr.client.imploder.ListImploderAttachment)7