use of org.eclipse.xtext.formatting2.ITextReplacer in project xtext-core by eclipse.
the class HiddenRegionReplacer method createReplacers.
protected List<ITextReplacer> createReplacers(AbstractFormatter2 formatter) {
List<ITextSegment> regions = region.getAlternatingMergedSpaceAndComments();
List<ITextReplacer> replacers = Lists.newArrayListWithCapacity(regions.size());
for (ITextSegment region : regions) {
if (region instanceof IComment)
replacers.add(formatter.createCommentReplacer((IComment) region));
else
replacers.add(formatter.createWhitespaceReplacer(region, formatter.createHiddenRegionFormatting()));
}
return replacers;
}
use of org.eclipse.xtext.formatting2.ITextReplacer 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.ITextReplacer in project xtext-core by eclipse.
the class HiddenRegionReplacer method findWhitespaceThatSeparatesSemanticRegions.
protected WhitespaceReplacer findWhitespaceThatSeparatesSemanticRegions(List<ITextReplacer> replacers) {
boolean hasSeenWrap = false;
for (ITextReplacer replacer : replacers) {
if (replacer instanceof WhitespaceReplacer) {
WhitespaceReplacer whitespaceReplacer = (WhitespaceReplacer) replacer;
hasSeenWrap |= whitespaceReplacer.getRegion().isMultiline();
if (hasSeenWrap)
return whitespaceReplacer;
}
}
return (WhitespaceReplacer) replacers.get(replacers.size() - 1);
}
use of org.eclipse.xtext.formatting2.ITextReplacer in project xtext-core by eclipse.
the class TextReplacerContext method withReplacer.
@Override
public ITextReplacerContext withReplacer(ITextReplacer replacer) {
ITextReplacerContext current = this;
while (current != null) {
ITextReplacer lastReplacer = current.getReplacer();
if (lastReplacer != null) {
if (nextReplacerIsChild) {
Preconditions.checkArgument(lastReplacer.getRegion().contains(replacer.getRegion()));
} else {
Preconditions.checkArgument(lastReplacer.getRegion().getEndOffset() <= replacer.getRegion().getOffset());
}
break;
}
current = current.getPreviousContext();
}
return new TextReplacerContext(document, this, indentation, replacer);
}
use of org.eclipse.xtext.formatting2.ITextReplacer 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;
}
Aggregations