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;
}
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;
}
}
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);
}
}
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);
}
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);
}
Aggregations