use of org.eclipse.xtext.formatting2.IHiddenRegionFormatting in project xtext-core by eclipse.
the class HiddenRegionReplacer method applyHiddenRegionFormatting.
/**
* This method is when we have multiple TextReplacers for this HiddenRegion because the HiddenRegion contains
* comments. It applies the formatting configuration from {@link #formatting} to the {@link WhitespaceReplacer} that
* surround the comment.
*/
protected void applyHiddenRegionFormatting(List<ITextReplacer> replacers) {
IHiddenRegionFormatting separator = findWhitespaceThatSeparatesSemanticRegions(replacers).getFormatting();
// 1. apply indentation
Integer inc = formatting.getIndentationIncrease();
Integer dec = formatting.getIndentationDecrease();
if (inc != null && dec != null) {
((WhitespaceReplacer) replacers.get(0)).getFormatting().setIndentationIncrease(inc);
((WhitespaceReplacer) replacers.get(replacers.size() - 1)).getFormatting().setIndentationDecrease(dec);
} else if (inc != null) {
separator.setIndentationIncrease(inc);
} else if (dec != null) {
separator.setIndentationDecrease(dec);
}
// 2. apply NewLine and space configuration to the first whitespace region that follows or contains a linewrap.
separator.setNewLinesDefault(formatting.getNewLineDefault());
separator.setNewLinesMax(formatting.getNewLineMax());
separator.setNewLinesMin(formatting.getNewLineMin());
separator.setSpace(formatting.getSpace());
// 3. apply the 'priority' configuration for all and set a default formatting
for (ITextReplacer replacer : replacers) if (replacer instanceof WhitespaceReplacer) {
IHiddenRegionFormatting formatting2 = ((WhitespaceReplacer) replacer).getFormatting();
formatting2.setPriority(formatting.getPriority());
if (formatting2 != separator) {
formatting2.setSpace(replacer.getRegion().getOffset() > 0 ? " " : "");
formatting2.setNewLinesMin(0);
formatting2.setNewLinesMax(1);
}
}
// 4. make sure whitespace before and after multiline comments introduce a NewLine
for (int i = 0; i < replacers.size(); i++) {
ITextReplacer replacer = replacers.get(i);
if (replacer instanceof CommentReplacer) {
CommentReplacer commentReplacer = ((CommentReplacer) replacer);
WhitespaceReplacer leading = (WhitespaceReplacer) replacers.get(i - 1);
WhitespaceReplacer trailing = (WhitespaceReplacer) replacers.get(i + 1);
commentReplacer.configureWhitespace(leading, trailing);
}
}
}
use of org.eclipse.xtext.formatting2.IHiddenRegionFormatting in project xtext-core by eclipse.
the class TextReplacerMerger method mergeHiddenRegionReplacers.
protected ITextReplacer mergeHiddenRegionReplacers(List<? extends ITextReplacer> conflicting) {
List<IHiddenRegionFormatting> formattings = Lists.newArrayList();
IHiddenRegion region = null;
for (ITextReplacer replacer : conflicting) {
if (replacer instanceof HiddenRegionReplacer) {
HiddenRegionReplacer hiddenRegionReplacer = (HiddenRegionReplacer) replacer;
formattings.add(hiddenRegionReplacer.getFormatting());
if (region == null)
region = hiddenRegionReplacer.getRegion();
else if (region != hiddenRegionReplacer.getRegion())
return null;
} else
return null;
}
IHiddenRegionFormatting mergedFormatting = merger.merge(formattings);
if (mergedFormatting != null)
return formatter.createHiddenRegionReplacer(region, mergedFormatting);
return null;
}
use of org.eclipse.xtext.formatting2.IHiddenRegionFormatting in project xtext-core by eclipse.
the class FormattableDocumentTest method autoWrapInsert.
@Test
public void autoWrapInsert() {
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) -> {
String _property = System.getProperty("line.separator");
String _plus = ("@" + _property);
it_2.setSpace(_plus);
};
document.append(regions.regionFor(model).keyword("kw2"), _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);
}
Aggregations