use of org.metaborg.core.source.ISourceRegion 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.metaborg.core.source.ISourceRegion in project spoofax by metaborg.
the class CategorizerValidator method validate.
public static <T> Iterable<IRegionCategory<T>> validate(Iterable<IRegionCategory<T>> categorization) {
int offset = -1;
final Collection<IRegionCategory<T>> validated = Lists.newLinkedList();
for (IRegionCategory<T> regionCategory : categorization) {
final ISourceRegion region = regionCategory.region();
if (offset >= region.startOffset()) {
logger.warn("Invalid {}, starting offset is greater than offset in previous regions, " + "region category will be skipped", regionCategory);
} else if (offset >= region.endOffset()) {
logger.warn("Invalid {}, ending offset is greater than offset in previous regions, " + "region category will be skipped", regionCategory);
} else if (region.startOffset() > region.endOffset()) {
logger.warn("Invalid {}, starting offset is greater than ending offset, " + "region category will be skipped", regionCategory);
} else {
validated.add(regionCategory);
offset = regionCategory.region().endOffset();
}
}
return validated;
}
use of org.metaborg.core.source.ISourceRegion 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.metaborg.core.source.ISourceRegion 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);
}
use of org.metaborg.core.source.ISourceRegion in project spoofax by metaborg.
the class TracingService method tokenLocation.
private ISourceLocation tokenLocation(IStrategoTerm fragment) {
final IToken left = ImploderAttachment.getLeftToken(fragment);
final IToken right = ImploderAttachment.getRightToken(fragment);
if (left == null || right == null) {
return null;
}
final ISourceRegion region = JSGLRSourceRegionFactory.fromTokens(left, right);
final FileObject resource = SourceAttachment.getResource(fragment, resourceService);
return new SourceLocation(region, resource);
}
Aggregations