Java Code Examples for org.eclipse.xtext.formatting2.regionaccess.ITextSegment#getOffset()

The following examples show how to use org.eclipse.xtext.formatting2.regionaccess.ITextSegment#getOffset() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TextRegionsWithTitleToString.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public String toString() {
	ITextRegionAccess access = getTextRegionAccess();
	ITextSegment frame = getFrame();
	if (access == null || frame == null)
		return "(null)";
	ITextRegionRewriter rewriter = access.getRewriter();
	StringBuilder builder = new StringBuilder();
	List<ITextReplacement> replacements = Lists.newArrayList();
	for (int i = 0; i < this.items.size(); i++) {
		Item item = this.items.get(i);
		ITextSegment region = item.getRegion();
		String regionStr = "offset=" + region.getOffset() + " length=" + region.getLength();
		String open = i < BRACKETS_OPEN.length ? BRACKETS_OPEN[i] : "[" + i + "[";
		String close = i < BRACKETS_CLOSE.length ? BRACKETS_CLOSE[i] : "]" + i + "]";
		builder.append(open + close + ": " + item.getTitle() + " at " + regionStr + "\n");
		replacements.add(rewriter.createReplacement(region.getOffset(), 0, open));
		replacements.add(rewriter.createReplacement(region.getEndOffset(), 0, close));
	}
	String vizualized = rewriter.renderToString(frame, replacements);
	builder.append(box("document snippet", vizualized));
	return builder.toString();
}
 
Example 2
Source File: TextRegionRewriter.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void renderToAppendable(ITextSegment input, Iterable<? extends ITextReplacement> rep, Appendable result)
		throws IOException {
	int offset = input.getOffset();
	String text = input.getText();
	List<ITextReplacement> list = new TextReplacementList<ITextReplacement>(rep);
	Collections.sort(list);
	int lastOffset = 0;
	for (ITextReplacement r : list) {
		result.append(text.subSequence(lastOffset, r.getOffset() - offset));
		result.append(r.getReplacementText());
		lastOffset = (r.getOffset() - offset) + r.getLength();
	}
	if (lastOffset <= text.length())
		result.append(text.subSequence(lastOffset, text.length()));
}
 
Example 3
Source File: FormattableDocument.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
protected String applyTextReplacements(Iterable<ITextReplacement> replacements) {
	ITextSegment region = getRegion();
	String input = region.getText();
	ArrayList<ITextReplacement> list = Lists.newArrayList(replacements);
	Collections.sort(list);
	int startOffset = region.getOffset();
	int lastOffset = 0;
	StringBuilder result = new StringBuilder();
	for (ITextReplacement r : list) {
		int offset = r.getOffset() - startOffset;
		result.append(input.subSequence(lastOffset, offset));
		result.append(r.getReplacementText());
		lastOffset = offset + r.getLength();
	}
	result.append(input.subSequence(lastOffset, input.length()));
	return result.toString();
}
 
Example 4
Source File: TextReplacerContext.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public boolean isWrapInRegion() {
	ITextRegionAccess access = getDocument().getRequest().getTextRegionAccess();
	ITextSegment region = getReplacer().getRegion();
	int lastOffset = region.getOffset();
	for (ITextReplacement rep : this.getLocalReplacements()) {
		int endOffset = rep.getOffset();
		String between = access.textForOffset(lastOffset, endOffset - lastOffset);
		if (between.contains("\n") || rep.getReplacementText().contains("\n")) {
			return true;
		}
		lastOffset = rep.getEndOffset();
	}
	String rest = access.textForOffset(lastOffset, region.getEndOffset() - lastOffset);
	if (rest.contains("\n")) {
		return true;
	}
	return false;
}
 
Example 5
Source File: TestableTextSegmentSet.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public String toString() {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("issues=");
  String _join = IterableExtensions.join(this.issues, ", ");
  _builder.append(_join);
  _builder.newLineIfNotEmpty();
  _builder.append("set=");
  final Function1<ITextSegment, String> _function = (ITextSegment it) -> {
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("[");
    int _offset = it.getOffset();
    _builder_1.append(_offset);
    _builder_1.append(",");
    int _length = it.getLength();
    _builder_1.append(_length);
    _builder_1.append("]");
    return _builder_1.toString();
  };
  String _replace = IterableExtensions.<ITextSegment, String>map(this.set, _function).toString().replace("\n", ", ");
  _builder.append(_replace);
  _builder.newLineIfNotEmpty();
  return _builder.toString();
}
 
Example 6
Source File: ObjectEntry.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public String toString() {

	ITextSegment reg = getRegion();

	return "\"" + reg.getText() + "\" at offset=" + reg.getOffset() + " lenght=" + reg.getLength() + " ("
			+ object.getClass().getSimpleName() + ")";
}
 
Example 7
Source File: AbstractTextSegment.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public int compareTo(ITextSegment o) {
	int cmp1 = getOffset() - o.getOffset();
	if (cmp1 != 0)
		return cmp1;
	int cmp2 = getLength() - o.getLength();
	if (cmp2 != 0)
		return cmp2;
	return 0;
}
 
Example 8
Source File: AbstractHiddenRegion.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected List<ITextSegment> collectAlternatingSpaceAndComments(boolean includeComments) {
	List<IHiddenRegionPart> parts = getParts();
	if (parts.isEmpty()) {
		return Collections.<ITextSegment>singletonList(this);
	} else {
		ITextSegment lastWhitespace = null;
		List<ITextSegment> result = Lists.newArrayList();
		for (IHiddenRegionPart part : parts) {
			if (part instanceof IWhitespace) {
				if (lastWhitespace == null) {
					result.add(part);
					lastWhitespace = part;
				} else {
					int mergedLength = lastWhitespace.getLength() + part.getLength();
					lastWhitespace = new TextSegment(access, lastWhitespace.getOffset(), mergedLength);
					result.set(result.size() - 1, lastWhitespace);
				}
			} else if (part instanceof IComment) {
				if (lastWhitespace == null) {
					result.add(new TextSegment(access, part.getOffset(), 0));
				} else {
					lastWhitespace = null;
				}
				if (includeComments) {
					result.add(part);
				}
			}
		}
		if (lastWhitespace == null) {
			result.add(new TextSegment(access, getEndOffset(), 0));
		}
		return ImmutableList.copyOf(result);
	}
}
 
Example 9
Source File: TextSegmentSet.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected boolean isConflict(ITextSegment region1, ITextSegment region2) {
	int offset1 = region1.getOffset();
	int length1 = region1.getLength();
	int offset2 = region2.getOffset();
	int length2 = region2.getLength();
	if (offset1 < offset2) {
		return offset1 + length1 > offset2;
	} else if (offset1 > offset2) {
		return offset2 + length2 > offset1;
	}
	return !(length1 == 0 ^ length2 == 0);
}
 
Example 10
Source File: TestableTextSegmentSet.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
private String fmt(final ITextSegment region) {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("[");
  int _offset = region.getOffset();
  _builder.append(_offset);
  _builder.append(",");
  int _length = region.getLength();
  _builder.append(_length);
  _builder.append("]");
  return _builder.toString();
}
 
Example 11
Source File: TextRegionsToString.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
protected String getReplacement(ITextSegment region, String replacement) {
	String string = region.getText();
	if (Strings.equal(string, replacement))
		return "<" + region.getOffset() + "|" + replacement + ">";
	return "<" + region.getOffset() + ":" + region.getLength() + "|" + replacement + ">";
}
 
Example 12
Source File: SequentialRegionDiff.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
protected ITextSegment getRegion(ITextSegment first, ITextSegment last) {
	int offset = first instanceof IHiddenRegion ? first.getOffset() : first.getEndOffset();
	int endOffset = last instanceof IHiddenRegion ? last.getEndOffset() : last.getOffset();
	return first.getTextRegionAccess().regionForOffset(offset, endOffset - offset);
}