use of org.eclipse.xtext.formatting2.regionaccess.ITextReplacement 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.ITextReplacement in project xtext-core by eclipse.
the class FormatterReplacementsTest method testUndefinedIdentityEditsAreNotFiltered.
@Test
public void testUndefinedIdentityEditsAreNotFiltered() {
final GenericFormatter<IDList> _function = new GenericFormatter<IDList>() {
@Override
protected void format(final IDList model, @Extension final ITextRegionExtensions regions, @Extension final IFormattableDocument document) {
final Consumer<ISemanticRegion> _function = (ISemanticRegion it) -> {
final Procedure1<IHiddenRegionFormatter> _function_1 = (IHiddenRegionFormatter it_1) -> {
it_1.setSpace("");
};
document.prepend(it, _function_1);
};
regions.regionFor(model).ruleCallsTo(FormatterReplacementsTest.this._formatterTestLanguageGrammarAccess.getIDRule()).forEach(_function);
}
};
final GenericFormatter<IDList> formatter = _function;
IDList _createIDList = this.fact.createIDList();
final Procedure1<IDList> _function_1 = (IDList it) -> {
EList<String> _ids = it.getIds();
_ids.add("a");
};
final IDList model = ObjectExtensions.<IDList>operator_doubleArrow(_createIDList, _function_1);
Resource _createResource = this.resFact.createResource(URI.createURI("foo.ext"));
final Procedure1<Resource> _function_2 = (Resource it) -> {
EList<Resource> _resources = new XtextResourceSet().getResources();
_resources.add(it);
EList<EObject> _contents = it.getContents();
_contents.add(model);
};
ObjectExtensions.<Resource>operator_doubleArrow(_createResource, _function_2);
final FormatterRequest request = this.requestProvider.get();
request.setAllowIdentityEdits(false);
request.setTextRegionAccess(this.serializer.serializeToRegions(model));
final List<ITextReplacement> replacements = formatter.format(request);
final Function1<ITextReplacement, String> _function_3 = (ITextReplacement it) -> {
StringConcatenation _builder = new StringConcatenation();
_builder.append("\"");
String _text = it.getText();
_builder.append(_text);
_builder.append("\" -> \"");
String _replacementText = it.getReplacementText();
_builder.append(_replacementText);
_builder.append("\"");
_builder.append("\n");
return _builder.toString();
};
final String actual = IterableExtensions.join(ListExtensions.<ITextReplacement, String>map(replacements, _function_3));
StringConcatenation _builder = new StringConcatenation();
_builder.append("\"\" -> \"\"");
_builder.newLine();
_builder.append("\"\" -> \"\"");
_builder.newLine();
_builder.append("\"\" -> \"\"");
_builder.newLine();
final String expected = _builder.toString();
Assert.assertEquals(expected, actual);
}
use of org.eclipse.xtext.formatting2.regionaccess.ITextReplacement in project xtext-core by eclipse.
the class FormatterReplacementsTest method testIdentityEditsAreFiltered.
@Test
public void testIdentityEditsAreFiltered() {
try {
final GenericFormatter<IDList> _function = new GenericFormatter<IDList>() {
@Override
protected void format(final IDList model, @Extension final ITextRegionExtensions regions, @Extension final IFormattableDocument document) {
final Consumer<ISemanticRegion> _function = (ISemanticRegion it) -> {
final Procedure1<IHiddenRegionFormatter> _function_1 = (IHiddenRegionFormatter it_1) -> {
it_1.autowrap();
it_1.oneSpace();
};
document.prepend(it, _function_1);
};
regions.regionFor(model).ruleCallsTo(FormatterReplacementsTest.this._formatterTestLanguageGrammarAccess.getIDRule()).forEach(_function);
}
};
final GenericFormatter<IDList> formatter = _function;
final IDList parsed = this.parseHelper.parse("idlist aaa bbb");
final FormatterRequest request = this.requestProvider.get();
request.setAllowIdentityEdits(false);
Resource _eResource = parsed.eResource();
request.setTextRegionAccess(this.regionBuilder.forNodeModel(((XtextResource) _eResource)).create());
final List<ITextReplacement> replacements = formatter.format(request);
final Function1<ITextReplacement, String> _function_1 = (ITextReplacement it) -> {
StringConcatenation _builder = new StringConcatenation();
_builder.append("\"");
String _text = it.getText();
_builder.append(_text);
_builder.append("\" -> \"");
String _replacementText = it.getReplacementText();
_builder.append(_replacementText);
_builder.append("\"");
_builder.append("\n");
return _builder.toString();
};
final String actual = IterableExtensions.join(ListExtensions.<ITextReplacement, String>map(replacements, _function_1));
StringConcatenation _builder = new StringConcatenation();
_builder.append("\" \" -> \" \"");
_builder.newLine();
final String expected = _builder.toString();
Assert.assertEquals(expected, actual);
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
use of org.eclipse.xtext.formatting2.regionaccess.ITextReplacement in project xtext-core by eclipse.
the class Serializer method serialize.
protected void serialize(EObject obj, Appendable appendable, SaveOptions options) throws IOException {
ITextRegionAccess regionAccess = serializeToRegions(obj);
FormatterRequest request = formatterRequestProvider.get();
request.setFormatUndefinedHiddenRegionsOnly(!options.isFormatting());
request.setTextRegionAccess(regionAccess);
IFormatter2 formatter2 = formatter2Provider.get();
List<ITextReplacement> replacements = formatter2.format(request);
regionAccess.getRewriter().renderToAppendable(replacements, appendable);
}
use of org.eclipse.xtext.formatting2.regionaccess.ITextReplacement in project n4js by eclipse.
the class N4MFFormatterTest method formatted.
/**
* @param expectation
* the expected formatted code
* @param resource
* the Xtext resource to be formatted
* @param offset
* the optional start offset from where formatting should be applied
* @param to
* the optional to offset to which formatting should be applied
*/
@ParameterParser(syntax = "('from' arg2=OFFSET 'to' arg3=OFFSET)?")
@Xpect
public void formatted(@StringExpectation(whitespaceSensitive = true) IStringExpectation expectation, @ThisResource XtextResource resource, int offset, int to) {
ICompositeNode rootNode = resource.getParseResult().getRootNode();
FormatterRequest request = requestProvider.get().setTextRegionAccess(regionAccessBuilderProvider.get().forNodeModel(resource).create());
int totalLength = rootNode.getTotalLength();
if (offset >= 0 && to > offset) {
request.setRegions(Collections.singleton(new TextRegion(offset, to - offset)));
} else {
request.setRegions(Collections.singleton(new TextRegion(offset, totalLength - offset)));
}
List<ITextReplacement> replacements = formatter.format(request);
String formatted = request.getTextRegionAccess().getRewriter().renderToString(replacements);
if (offset >= 0 && to > offset) {
formatted = formatted.substring(offset, formatted.length() - (totalLength - to));
} else {
formatted = formatted.substring(offset);
}
if (isUnixEnding()) {
formatted = formatted.replaceAll("\r\n", "\n");
} else if (isWindowsEnding()) {
if (!rootNode.getText().contains("\r\n")) {
formatted = formatted.replaceAll("\r\n", "\n");
} else {
formatted = formatted.replaceAll("(!\r)\n", "\r\n");
}
}
expectation.assertEquals(formatted);
}
Aggregations