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

The following examples show how to use org.eclipse.xtext.formatting2.regionaccess.ITextReplacement#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: 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 2
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 3
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 4
Source File: ChangeSerializerTextEditComposer.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected void collectChanges(IEmfResourceChange e, List<TextEdit> edits) {
	if (e instanceof ITextDocumentChange && e.getResource() == recording) {
		ITextDocumentChange change = (ITextDocumentChange) e;
		for (ITextReplacement c : change.getReplacements()) {
			ReplaceEdit edit = new ReplaceEdit(c.getOffset(), c.getLength(), c.getReplacementText());
			edits.add(edit);
		}
	}
}
 
Example 5
Source File: TextReplacerContext.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected boolean isInRequestedRange(ITextReplacement repl) {
	Collection<ITextRegion> regions = document.getRequest().getRegions();
	if (regions.isEmpty())
		return true;
	for (org.eclipse.xtext.util.ITextRegion region : regions)
		if (region.getOffset() <= repl.getOffset()
				&& region.getOffset() + region.getLength() >= repl.getEndOffset())
			return true;
	return false;
}