use of org.eclipse.xtext.formatting2.regionaccess.ITextSegment in project xtext-core by eclipse.
the class FormattableDocument method addReplacer.
@Override
public void addReplacer(ITextReplacer replacer) {
if (!this.getRegion().contains(replacer.getRegion())) {
String frameTitle = getClass().getSimpleName();
ITextSegment frameRegion = getRegion();
String replacerTitle = replacer.getClass().getSimpleName();
ITextSegment replacerRegion = replacer.getRegion();
RegionsOutsideFrameException exception = new RegionsOutsideFrameException(frameTitle, frameRegion, Tuples.create(replacerTitle, replacerRegion));
getRequest().getExceptionHandler().accept(exception);
return;
}
try {
getReplacers().add(replacer, getFormatter().createTextReplacerMerger());
} catch (ConflictingRegionsException e) {
getRequest().getExceptionHandler().accept(e);
}
}
use of org.eclipse.xtext.formatting2.regionaccess.ITextSegment in project xtext-core by eclipse.
the class FormattableDocument method applyTextReplacements.
// TODO: use org.eclipse.xtext.formatting2.TextReplacements
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();
}
use of org.eclipse.xtext.formatting2.regionaccess.ITextSegment in project xtext-core by eclipse.
the class FormattableDocumentTest method autoWrapRewrite.
@Test
public void autoWrapRewrite() {
final Procedure1<GenericFormatterTestRequest> _function = (GenericFormatterTestRequest it) -> {
final Procedure1<MapBasedPreferenceValues> _function_1 = (MapBasedPreferenceValues it_1) -> {
it_1.<Integer>put(FormatterPreferenceKeys.maxLineWidth, Integer.valueOf(10));
};
it.preferences(_function_1);
StringConcatenation _builder = new StringConcatenation();
_builder.append("kwlist kw1 kw2");
_builder.newLine();
it.setToBeFormatted(_builder);
final GenericFormatter<KWList> _function_2 = new GenericFormatter<KWList>() {
@Override
protected void format(final KWList model, @Extension final ITextRegionExtensions regions, @Extension final IFormattableDocument document) {
final Procedure1<IHiddenRegionFormatter> _function = (IHiddenRegionFormatter it_1) -> {
it_1.autowrap();
final IAutowrapFormatter _function_1 = (ITextSegment region, IHiddenRegionFormatting wrapped, IFormattableDocument doc) -> {
final Procedure1<IHiddenRegionFormatter> _function_2 = (IHiddenRegionFormatter it_2) -> {
it_2.setSpace("!");
};
doc.append(regions.regionFor(model).keyword("kw1"), _function_2);
};
it_1.setOnAutowrap(_function_1);
final Procedure1<IHiddenRegionFormatter> _function_2 = (IHiddenRegionFormatter it_2) -> {
it_2.setSpace("@");
it_2.lowPriority();
};
document.append(regions.regionFor(model).keyword("kw1"), _function_2);
};
document.append(regions.regionFor(model).keyword("kwlist"), _function);
}
};
it.setFormatter(_function_2);
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("kwlist");
_builder_1.newLine();
_builder_1.append("kw1!kw2");
_builder_1.newLine();
it.setExpectation(_builder_1);
};
this._genericFormatterTester.assertFormatted(_function);
}
use of org.eclipse.xtext.formatting2.regionaccess.ITextSegment in project xtext-core by eclipse.
the class RegionSetTest method test.
private void test(final CharSequence expectation, final Procedure1<? super TestableTextSegmentSet> test) {
Function<ITextSegment, ITextSegment> _identity = Functions.<ITextSegment>identity();
Function<Object, String> _stringFunction = Functions.toStringFunction();
ArrayListTextSegmentSet<ITextSegment> _arrayListTextSegmentSet = new ArrayListTextSegmentSet<ITextSegment>(_identity, _stringFunction, true);
final TestableTextSegmentSet set = new TestableTextSegmentSet(_arrayListTextSegmentSet);
try {
test.apply(set);
} catch (final Throwable _t) {
if (_t instanceof Throwable) {
final Throwable t = (Throwable) _t;
t.printStackTrace();
Assert.fail();
} else {
throw Exceptions.sneakyThrow(_t);
}
}
Assert.assertEquals(expectation.toString(), set.toString());
}
use of org.eclipse.xtext.formatting2.regionaccess.ITextSegment in project xtext-core by eclipse.
the class RegionDiffFormatter method collectRegionsToFormat.
protected Collection<ITextRegion> collectRegionsToFormat(ITextRegionAccessDiff regions) {
List<ITextRegion> result = Lists.newArrayList();
for (ITextSegmentDiff diff : regions.getRegionDifferences()) {
int offset = diff.getModifiedFirstRegion().getOffset();
int length = diff.getModifiedLastRegion().getEndOffset() - offset;
ITextSegment region = regions.regionForOffset(offset, length);
result.add(region);
}
return result;
}
Aggregations