use of org.eclipse.xtext.formatting2.IFormattableDocument in project xtext-core by eclipse.
the class AbstractFormatter2 method postProcess.
protected List<ITextReplacement> postProcess(IFormattableDocument document, List<ITextReplacement> replacements) {
List<ITextSegment> expected = Lists.newArrayList();
IHiddenRegion current = getTextRegionAccess().regionForRootEObject().getPreviousHiddenRegion();
while (current != null) {
if (current.isUndefined() && isInRequestedRange(current.getOffset(), current.getEndOffset()))
expected.addAll(current.getMergedSpaces());
current = current.getNextHiddenRegion();
}
if (expected.isEmpty())
return replacements;
List<ITextSegment> missing = TextRegions.difference(expected, replacements);
if (missing.isEmpty())
return replacements;
List<ITextReplacement> result = Lists.newArrayList(replacements);
for (ITextSegment seg : missing) {
IHiddenRegion h = null;
if (seg instanceof IHiddenRegion)
h = (IHiddenRegion) seg;
if (seg instanceof IHiddenRegionPart)
h = ((IHiddenRegionPart) seg).getHiddenRegion();
if (h != null && (h.getNextSemanticRegion() == null || h.getPreviousSemanticRegion() == null))
result.add(seg.replaceWith(""));
else
result.add(seg.replaceWith(" "));
}
return result;
}
use of org.eclipse.xtext.formatting2.IFormattableDocument in project xtext-core by eclipse.
the class AbstractFormatter2 method format.
@Override
public final List<ITextReplacement> format(FormatterRequest request) {
try {
initialize(request);
XtextResource xtextResource = request.getTextRegionAccess().getResource();
IFormattableDocument document = createFormattableRootDocument();
try {
format(xtextResource, document);
} catch (RegionTraceMissingException e) {
document = handleTraceMissing(document, e);
}
List<ITextReplacement> rendered = document.renderToTextReplacements();
List<ITextReplacement> postprocessed = postProcess(document, rendered);
return postprocessed;
} finally {
reset();
}
}
use of org.eclipse.xtext.formatting2.IFormattableDocument in project xtext-core by eclipse.
the class CommentFormatterTest method MLML_paragraph.
@Test
public void MLML_paragraph() {
final Procedure1<GenericFormatterTestRequest> _function = (GenericFormatterTestRequest it) -> {
StringConcatenation _builder = new StringConcatenation();
_builder.append("idlist");
_builder.newLine();
_builder.newLine();
_builder.newLine();
_builder.append("/*");
_builder.newLine();
_builder.append("x");
_builder.newLine();
_builder.append("*/");
_builder.newLine();
_builder.newLine();
_builder.newLine();
_builder.append("a");
_builder.newLine();
it.setToBeFormatted(_builder);
final GenericFormatter<IDList> _function_1 = new GenericFormatter<IDList>() {
@Override
protected void format(final IDList model, @Extension final ITextRegionExtensions regions, @Extension final IFormattableDocument document) {
final Procedure1<IHiddenRegionFormatter> _function = (IHiddenRegionFormatter it_1) -> {
it_1.oneSpace();
};
document.append(regions.regionFor(model).keyword("idlist"), _function);
}
};
it.setFormatter(_function_1);
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("idlist");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("/*");
_builder_1.newLine();
_builder_1.append(" ");
_builder_1.append("* x");
_builder_1.newLine();
_builder_1.append(" ");
_builder_1.append("*/");
_builder_1.newLine();
_builder_1.append("a");
_builder_1.newLine();
it.setExpectation(_builder_1);
};
this._genericFormatterTester.assertFormatted(_function);
}
use of org.eclipse.xtext.formatting2.IFormattableDocument in project xtext-core by eclipse.
the class CommentFormatterTest method SL_multiline.
@Test
public void SL_multiline() {
final Procedure1<GenericFormatterTestRequest> _function = (GenericFormatterTestRequest it) -> {
StringConcatenation _builder = new StringConcatenation();
_builder.append("idlist ");
_builder.newLine();
_builder.newLine();
_builder.append("//x");
_builder.newLine();
_builder.newLine();
_builder.newLine();
_builder.append("a");
_builder.newLine();
it.setToBeFormatted(_builder);
final GenericFormatter<IDList> _function_1 = new GenericFormatter<IDList>() {
@Override
protected void format(final IDList model, @Extension final ITextRegionExtensions regions, @Extension final IFormattableDocument document) {
final Procedure1<IHiddenRegionFormatter> _function = (IHiddenRegionFormatter it_1) -> {
it_1.oneSpace();
};
document.append(regions.regionFor(model).keyword("idlist"), _function);
}
};
it.setFormatter(_function_1);
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("idlist //x");
_builder_1.newLine();
_builder_1.append("a");
_builder_1.newLine();
it.setExpectation(_builder_1);
};
this._genericFormatterTester.assertFormatted(_function);
}
use of org.eclipse.xtext.formatting2.IFormattableDocument in project xtext-core by eclipse.
the class CommentFormatterTest method SL_inline.
@Test
public void SL_inline() {
final Procedure1<GenericFormatterTestRequest> _function = (GenericFormatterTestRequest it) -> {
StringConcatenation _builder = new StringConcatenation();
_builder.append("idlist //x");
_builder.newLine();
_builder.append("a");
_builder.newLine();
it.setToBeFormatted(_builder);
final GenericFormatter<IDList> _function_1 = new GenericFormatter<IDList>() {
@Override
protected void format(final IDList model, @Extension final ITextRegionExtensions regions, @Extension final IFormattableDocument document) {
final Procedure1<IHiddenRegionFormatter> _function = (IHiddenRegionFormatter it_1) -> {
it_1.oneSpace();
};
document.append(regions.regionFor(model).keyword("idlist"), _function);
}
};
it.setFormatter(_function_1);
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("idlist //x");
_builder_1.newLine();
_builder_1.append("a");
_builder_1.newLine();
it.setExpectation(_builder_1);
};
this._genericFormatterTester.assertFormatted(_function);
}
Aggregations