use of org.eclipse.xtext.util.ITextRegion in project xtext-core by eclipse.
the class DefaultDocumentHighlightService method isDocumentHighlightAvailableFor.
/**
* Returns with {@code true} if the AST element selected from the resource
* can provide document highlights, otherwise returns with {@code false}.
*
* <p>
* Clients may override this method to change the default behavior.
*
* @param selectedElement
* the selected element resolved via the offset from the
* resource. Can be {@code null}.
* @param resource
* the resource for the document.
* @param offset
* the offset of the selection.
*
* @return {@code true} if the document highlight is available for the
* selected element, otherwise {@code false}.
*/
protected boolean isDocumentHighlightAvailableFor(final EObject selectedElement, final XtextResource resource, final int offset) {
if (selectedElement == null || !getSelectedElementFilter().apply(selectedElement)) {
return false;
}
// This code handles the special case where your language has constructs that can refer to
// themselves. For example "function MyFunction begin ... end MyFunction" defines the function "MyFunction" and
// terminates its implementation block with an additional repetition of the word "MyFunction". Normally, when
// you are positioned on a selected element, you only want to highlight that selected element when you are
// positioned directly on top of the name of the selected element. However, when the selected element can refer
// to itself then there are references inside the element that must trigger highlighting. In the example,
// we also want to highlight "MyFunction" when we are positioned on the "end MyFunction".
INode crossReferenceNode = offsetHelper.getCrossReferenceNode(resource, new TextRegion(offset, 0));
if (crossReferenceNode != null) {
EObject crossReferencedElement = offsetHelper.getCrossReferencedElement(crossReferenceNode);
if (crossReferencedElement != null && crossReferencedElement == selectedElement) {
return true;
}
}
final EObject containedElement = offsetHelper.resolveContainedElementAt(resource, offset);
if (selectedElement == containedElement) {
final ITextRegion region = locationInFileProvider.getSignificantTextRegion(containedElement);
return !isNullOrEmpty(region) && // therefore the end position is exclusive.
(region.contains(offset) || (region.getOffset() + region.getLength()) == offset);
}
return true;
}
use of org.eclipse.xtext.util.ITextRegion in project xtext-core by eclipse.
the class TextRegions method difference.
public static <T extends ITextRegion> List<T> difference(Iterable<T> r1, Iterable<? extends ITextRegion> r2) {
ArrayList<T> regions1 = Lists.newArrayList(r1);
ArrayList<ITextRegion> regions2 = Lists.newArrayList(r2);
Collections.sort(regions1, Comp.INSTANCE);
Collections.sort(regions2, Comp.INSTANCE);
List<T> missing = Lists.newArrayList();
int i1 = 0, i2 = 0;
while (i1 < regions1.size() && i2 < regions2.size()) {
T t1 = regions1.get(i1);
ITextRegion t2 = regions2.get(i2);
int compareTo = Comp.INSTANCE.compare(t1, t2);
if (compareTo == 0) {
i1++;
i2++;
} else if (compareTo < 1) {
missing.add(t1);
i1++;
} else {
i2++;
}
}
while (i1 < regions1.size()) {
missing.add(regions1.get(i1));
i1++;
}
return ImmutableList.copyOf(missing);
}
use of org.eclipse.xtext.util.ITextRegion in project xtext-core by eclipse.
the class FormatterTestHelper method assertReplacementsAreInRegion.
protected void assertReplacementsAreInRegion(List<ITextReplacement> rep, Collection<ITextRegion> regions, String doc) {
Set<ITextReplacement> invalid = Sets.newHashSet();
ALLOWED: for (ITextRegion allowed : regions) for (ITextReplacement r : rep) {
if (allowed.contains(r))
continue ALLOWED;
invalid.add(r);
}
if (!invalid.isEmpty()) {
String visualized = new TextRegionsToString().addAllReplacements(invalid).toString();
fail("One or more TextReplacements are outside of the allowed region. Region: " + regions, visualized);
}
}
use of org.eclipse.xtext.util.ITextRegion in project xtext-core by eclipse.
the class NodeModelStreamer method feedTokenStream.
@Override
public ITextRegion feedTokenStream(ITokenStream out, ICompositeNode in, int offset, int length) throws IOException {
List<INode> nodes = getLeafs(in, offset, offset + length);
if (nodes.isEmpty())
return new TextRegion(in.getOffset(), 0);
if (out instanceof ITokenStreamExtension)
((ITokenStreamExtension) out).init(findRootRuleForRegion(nodes.get(0)));
boolean lastIsTokenOrComment = false;
for (INode node : nodes) {
boolean currentIsTokenOrComment = tokenUtil.isCommentNode(node) || tokenUtil.isToken(node);
if (lastIsTokenOrComment && currentIsTokenOrComment)
writeHiddenEmpty(out);
lastIsTokenOrComment = currentIsTokenOrComment;
if (node instanceof ILeafNode) {
ILeafNode leaf = (ILeafNode) node;
if (leaf.isHidden())
writeHidden(out, leaf);
else
writeSemantic(out, leaf);
} else if (node instanceof ICompositeNode)
writeSemantic(out, (ICompositeNode) node);
}
out.flush();
int rStart = nodes.get(0).getOffset();
int rLength = nodes.get(nodes.size() - 1).getEndOffset() - rStart;
return new TextRegion(rStart, rLength);
}
use of org.eclipse.xtext.util.ITextRegion in project xtext-core by eclipse.
the class DefaultNodeModelFormatter method format.
@Override
public IFormattedRegion format(ICompositeNode root, int offset, int length) {
String indent = getIndentation(root, offset);
TokenStringBuffer buf = new TokenStringBuffer();
ITokenStream out = offset == 0 ? buf : new FilterFirstWhitespaceStream(buf);
ITokenStream fmt;
if (formatter instanceof IFormatterExtension) {
EObject semanticElement = NodeModelUtils.findActualSemanticObjectFor(root);
if (semanticElement != null)
fmt = ((IFormatterExtension) formatter).createFormatterStream(semanticElement, indent, out, false);
else {
// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=380406
ITextRegion rootRegion = root.getTextRegion();
return new FormattedRegion(rootRegion.getOffset(), rootRegion.getLength(), root.getText());
}
} else
fmt = formatter.createFormatterStream(indent, out, false);
try {
ITextRegion range = nodeModelStreamer.feedTokenStream(fmt, root, offset, length);
return new FormattedRegion(range.getOffset(), range.getLength(), buf.toString());
} catch (IOException e) {
// this should never happen since TokenStringBuffer doesn't throw IOEs.
throw new RuntimeException(e);
}
}
Aggregations