use of org.eclipse.xtext.formatting2.regionaccess.ITextSegment in project xtext-core by eclipse.
the class ConditionalReplacer method createReplacements.
@Override
public ITextReplacerContext createReplacements(ITextReplacerContext context) {
context.setNextReplacerIsChild();
for (ISubFormatter formatter : subFormatters) {
try {
ITextSegment region = getRegion();
SubDocument subDocument = new SubDocument(region, getDocument());
for (ITextReplacer replacer : replacers) subDocument.addReplacer(replacer);
formatter.format(subDocument);
ITextReplacerContext first = context.withReplacer(subDocument);
ITextReplacerContext last = subDocument.createReplacements(first);
return last;
} catch (FormattingNotApplicableException e) {
// no need to do anything.
// Try the next SubFormatter until one doens't throw a FormattingNotApplicableException
}
}
throw new FormattingNotApplicableException();
}
use of org.eclipse.xtext.formatting2.regionaccess.ITextSegment 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.regionaccess.ITextSegment in project xtext-core by eclipse.
the class MultilineCommentReplacer method createReplacements.
@Override
public ITextReplacerContext createReplacements(ITextReplacerContext context) {
if (!multiline)
return context;
IComment comment = getComment();
ITextRegionAccess access = comment.getTextRegionAccess();
List<ILineRegion> lines = comment.getLineRegions();
String oldIndentation = lines.get(0).getIndentation().getText();
String indentationString = context.getIndentationString();
String newIndentation = indentationString + " " + prefix + " ";
for (int i = 1; i < lines.size() - 1; i++) {
ITextSegment line = lines.get(i);
String text = line.getText();
int prefixOffset = prefixOffset(text);
ITextSegment target;
if (prefixOffset >= 0)
target = access.regionForOffset(line.getOffset(), prefixOffset + 1);
else if (text.startsWith(oldIndentation))
target = access.regionForOffset(line.getOffset(), oldIndentation.length());
else
target = access.regionForOffset(line.getOffset(), 0);
context.addReplacement(target.replaceWith(newIndentation));
}
if (lines.size() > 1) {
ILineRegion line = lines.get(lines.size() - 1);
context.addReplacement(line.getIndentation().replaceWith(indentationString + " "));
}
return context;
}
use of org.eclipse.xtext.formatting2.regionaccess.ITextSegment in project xtext-core by eclipse.
the class SinglelineCommentReplacer method getFirstSpace.
protected ITextSegment getFirstSpace() {
IComment comment = getComment();
String text = comment.getText();
if (!text.startsWith(prefix))
return null;
int start = prefix.length();
for (int i = start; i < text.length(); i++) {
char charAt = text.charAt(i);
if (!Character.isWhitespace(charAt) || charAt == '\r' || charAt == '\n')
return new TextSegment(comment.getTextRegionAccess(), comment.getOffset() + start, i - start);
}
return new TextSegment(comment.getTextRegionAccess(), comment.getOffset() + start, text.length() - start);
}
use of org.eclipse.xtext.formatting2.regionaccess.ITextSegment in project xtext-core by eclipse.
the class TextReplacerContext method isWrapSincePrevious.
@Override
public boolean isWrapSincePrevious() {
ITextRegionAccess access = getDocument().getRequest().getTextRegionAccess();
ITextSegment region = getRegion(0);
ITextSegment previousRegion = getRegion(1);
if (previousRegion != null) {
int offset = previousRegion.getEndOffset();
String between = access.textForOffset(offset, region.getOffset() - offset);
if (between.contains("\n")) {
return true;
}
}
return false;
}
Aggregations