org.eclipse.xtext.formatting2.regionaccess.internal.TextSegment Java Examples

The following examples show how to use org.eclipse.xtext.formatting2.regionaccess.internal.TextSegment. 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: ObjectEntry.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
public ITextSegment getRegion() {
	SeparatorEntry<T, R> prev = getLeadingSeparator();
	SeparatorEntry<T, R> trail = getTrailingSeparator();
	final int offset;
	if (prev != null) {
		offset = prev.getSeparator().getEndOffset();
	} else {
		offset = list.getRoot().getOffset();
	}
	final int endOffset;
	if (trail != null) {
		endOffset = trail.getSeparator().getOffset();
	} else {
		endOffset = list.getRoot().getEndOffset();
	}
	return new TextSegment(list.getRoot().getTextRegionAccess(), offset, endOffset - offset);
}
 
Example #2
Source File: FormattingService.java    From xtext-web with Eclipse Public License 2.0 6 votes vote down vote up
protected String format2(XtextResource resource, ITextRegion selection, ITypedPreferenceValues preferences) {
	FormatterRequest request = formatterRequestProvider.get();
	request.setAllowIdentityEdits(false);
	request.setFormatUndefinedHiddenRegionsOnly(false);
	if (selection != null) {
		request.setRegions(Lists.newArrayList(selection));
	}
	if (preferences != null) {
		request.setPreferences(preferences);
	}
	ITextRegionAccess regionAccess = regionBuilder.forNodeModel(resource).create();
	request.setTextRegionAccess(regionAccess);
	IFormatter2 formatter2 = formatter2Provider.get();
	List<ITextReplacement> replacements = formatter2.format(request);
	if (selection != null) {
		return regionAccess.getRewriter().renderToString(
				new TextSegment(regionAccess, selection.getOffset(), selection.getLength()), replacements);
	} else {
		return regionAccess.getRewriter().renderToString(replacements);
	}
}
 
Example #3
Source File: RichStringFormatter.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
protected void setNewLines(final IFormattableDocument doc, final int offset, final int length, final int indentationIncrease, final int indentationDecrease, final int newLines) {
  IHiddenRegionFormatting _createHiddenRegionFormatting = doc.getFormatter().createHiddenRegionFormatting();
  final Procedure1<IHiddenRegionFormatting> _function = (IHiddenRegionFormatting it) -> {
    it.setIndentationIncrease(Integer.valueOf(indentationIncrease));
    it.setIndentationDecrease(Integer.valueOf(indentationDecrease));
    it.setNewLinesMin(Integer.valueOf(newLines));
    it.setNewLinesDefault(Integer.valueOf(newLines));
    it.setNewLinesMax(Integer.valueOf(newLines));
  };
  final IHiddenRegionFormatting fmt = ObjectExtensions.<IHiddenRegionFormatting>operator_doubleArrow(_createHiddenRegionFormatting, _function);
  AbstractFormatter2 _formatter = doc.getFormatter();
  ITextRegionAccess _textRegionAccess = this._iTextRegionExtensions.getTextRegionAccess();
  TextSegment _textSegment = new TextSegment(_textRegionAccess, offset, length);
  final ITextReplacer replacer = _formatter.createWhitespaceReplacer(_textSegment, fmt);
  doc.addReplacer(replacer);
}
 
Example #4
Source File: RegionSetTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testAddValid() {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("issues=");
  _builder.newLine();
  _builder.append("set=[[10,0], [10,1], [11,1], [12,1], [99,1], [100,0]]");
  _builder.newLine();
  final Procedure1<TestableTextSegmentSet> _function = (TestableTextSegmentSet it) -> {
    TextSegment _textSegment = new TextSegment(null, 10, 0);
    it.add(_textSegment);
    TextSegment _textSegment_1 = new TextSegment(null, 11, 1);
    it.add(_textSegment_1);
    TextSegment _textSegment_2 = new TextSegment(null, 99, 1);
    it.add(_textSegment_2);
    TextSegment _textSegment_3 = new TextSegment(null, 10, 1);
    it.add(_textSegment_3);
    TextSegment _textSegment_4 = new TextSegment(null, 100, 0);
    it.add(_textSegment_4);
    TextSegment _textSegment_5 = new TextSegment(null, 12, 1);
    it.add(_textSegment_5);
  };
  this.test(_builder, _function);
}
 
Example #5
Source File: RichStringFormatter.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
protected void setSpace(final IFormattableDocument doc, final int offset, final int length, final String space) {
  IHiddenRegionFormatting _createHiddenRegionFormatting = doc.getFormatter().createHiddenRegionFormatting();
  final Procedure1<IHiddenRegionFormatting> _function = (IHiddenRegionFormatting it) -> {
    it.setSpace(space);
  };
  final IHiddenRegionFormatting fmt = ObjectExtensions.<IHiddenRegionFormatting>operator_doubleArrow(_createHiddenRegionFormatting, _function);
  AbstractFormatter2 _formatter = doc.getFormatter();
  ITextRegionAccess _textRegionAccess = this._iTextRegionExtensions.getTextRegionAccess();
  TextSegment _textSegment = new TextSegment(_textRegionAccess, offset, length);
  final ITextReplacer replacer = _formatter.createWhitespaceReplacer(_textSegment, fmt);
  doc.addReplacer(replacer);
}
 
Example #6
Source File: SinglelineCommentReplacer.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected ITextSegment getFirstSpace() {
	IComment comment = getComment();
	String text = comment.getText();
	if (!text.startsWith(prefix))
		return null;
	int start = prefix.length();
	for (int i = start; i < text.length(); i++) {
		char charAt = text.charAt(i);
		if (!Character.isWhitespace(charAt) || charAt == '\r' || charAt == '\n')
			return new TextSegment(comment.getTextRegionAccess(), comment.getOffset() + start, i - start);
	}
	return new TextSegment(comment.getTextRegionAccess(), comment.getOffset() + start, text.length() - start);
}
 
Example #7
Source File: SubDocument.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public IFormattableSubDocument requireFitsInLine(int offset, int length, int maxLineWidth) {
	TextSegment segment = new TextSegment(getTextRegionAccess(), offset, length);
	MaxLineWidthDocument document = new MaxLineWidthDocument(segment, this, maxLineWidth);
	addReplacer(document);
	return document;
}
 
Example #8
Source File: RegionSetTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testNoMergeConflic1() {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("issues=");
  _builder.newLine();
  _builder.append("set=[[20,0], [20,1]]");
  _builder.newLine();
  final Procedure1<TestableTextSegmentSet> _function = (TestableTextSegmentSet it) -> {
    TextSegment _textSegment = new TextSegment(null, 20, 0);
    it.add(_textSegment);
    TextSegment _textSegment_1 = new TextSegment(null, 20, 1);
    it.add(_textSegment_1);
  };
  this.test(_builder, _function);
}
 
Example #9
Source File: RegionSetTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testNoMergeConflic2() {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("issues=");
  _builder.newLine();
  _builder.append("set=[[20,0], [20,1]]");
  _builder.newLine();
  final Procedure1<TestableTextSegmentSet> _function = (TestableTextSegmentSet it) -> {
    TextSegment _textSegment = new TextSegment(null, 20, 1);
    it.add(_textSegment);
    TextSegment _textSegment_1 = new TextSegment(null, 20, 0);
    it.add(_textSegment_1);
  };
  this.test(_builder, _function);
}
 
Example #10
Source File: RegionSetTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testMergeConflic1() {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("issues=mergeConflict:[20,0]<>[20,0]");
  _builder.newLine();
  _builder.append("set=[]");
  _builder.newLine();
  final Procedure1<TestableTextSegmentSet> _function = (TestableTextSegmentSet it) -> {
    TextSegment _textSegment = new TextSegment(null, 20, 0);
    it.add(_textSegment);
    TextSegment _textSegment_1 = new TextSegment(null, 20, 0);
    it.add(_textSegment_1);
  };
  this.test(_builder, _function);
}
 
Example #11
Source File: RegionSetTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testMergeConflic2() {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("issues=mergeConflict:[21,0]<>[20,2]");
  _builder.newLine();
  _builder.append("set=[[20,2]]");
  _builder.newLine();
  final Procedure1<TestableTextSegmentSet> _function = (TestableTextSegmentSet it) -> {
    TextSegment _textSegment = new TextSegment(null, 20, 2);
    it.add(_textSegment);
    TextSegment _textSegment_1 = new TextSegment(null, 21, 0);
    it.add(_textSegment_1);
  };
  this.test(_builder, _function);
}
 
Example #12
Source File: RegionSetTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testMergeConflic3() {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("issues=mergeConflict:[20,2]<>[21,0]");
  _builder.newLine();
  _builder.append("set=[[20,2]]");
  _builder.newLine();
  final Procedure1<TestableTextSegmentSet> _function = (TestableTextSegmentSet it) -> {
    TextSegment _textSegment = new TextSegment(null, 21, 0);
    it.add(_textSegment);
    TextSegment _textSegment_1 = new TextSegment(null, 20, 2);
    it.add(_textSegment_1);
  };
  this.test(_builder, _function);
}
 
Example #13
Source File: RegionSetTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testMergeConflic4() {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("issues=mergeConflict:[20,2]<>[21,5]");
  _builder.newLine();
  _builder.append("set=[[21,5]]");
  _builder.newLine();
  final Procedure1<TestableTextSegmentSet> _function = (TestableTextSegmentSet it) -> {
    TextSegment _textSegment = new TextSegment(null, 21, 5);
    it.add(_textSegment);
    TextSegment _textSegment_1 = new TextSegment(null, 20, 2);
    it.add(_textSegment_1);
  };
  this.test(_builder, _function);
}
 
Example #14
Source File: RegionSetTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testMergeConflic5() {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("issues=mergeConflict:[20,2]<>[21,5]");
  _builder.newLine();
  _builder.append("set=[[21,5]]");
  _builder.newLine();
  final Procedure1<TestableTextSegmentSet> _function = (TestableTextSegmentSet it) -> {
    TextSegment _textSegment = new TextSegment(null, 21, 5);
    it.add(_textSegment);
    TextSegment _textSegment_1 = new TextSegment(null, 20, 2);
    it.add(_textSegment_1);
  };
  this.test(_builder, _function);
}
 
Example #15
Source File: ConditionalReplacer.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
public ConditionalReplacer(FormattableDocument document, int offset, int length, List<ISubFormatter> subFormatters) {
	super(document, new TextSegment(document.getTextRegionAccess(), offset, length));
	Preconditions.checkArgument(subFormatters.size() > 0);
	this.subFormatters = subFormatters;
}