use of org.eclipse.xtext.formatting2.ITextReplacerContext in project xtext-core by eclipse.
the class TextReplacerContext method getRegion.
protected ITextSegment getRegion(int index) {
ITextReplacerContext current = this;
while (current != null) {
ITextReplacer replacer2 = current.getReplacer();
if (replacer2 != null) {
if (index == 0) {
return replacer2.getRegion();
} else
index--;
}
current = current.getPreviousContext();
}
return null;
}
use of org.eclipse.xtext.formatting2.ITextReplacerContext in project xtext-core by eclipse.
the class WhitespaceReplacer method createReplacements.
@Override
public ITextReplacerContext createReplacements(ITextReplacerContext context) {
if (formatting.getAutowrap() != null && formatting.getAutowrap() >= 0)
context.setCanAutowrap(formatting.getAutowrap());
String space = formatting.getSpace();
int trailingNewLinesOfPreviousRegion = trailingNewLinesOfPreviousRegion();
int computedNewLineCount = computeNewLineCount(context);
int newLineCount = Math.max(computedNewLineCount - trailingNewLinesOfPreviousRegion, 0);
if (newLineCount == 0 && context.isAutowrap()) {
IAutowrapFormatter onAutowrap = formatting.getOnAutowrap();
if (onAutowrap != null) {
onAutowrap.format(region, formatting, context.getDocument());
}
newLineCount = 1;
}
int indentationCount = computeNewIndentation(context);
if (newLineCount == 0 && trailingNewLinesOfPreviousRegion == 0) {
if (space != null)
context.addReplacement(region.replaceWith(space));
} else {
boolean noIndentation = formatting.getNoIndentation() == Boolean.TRUE;
String newLines = context.getNewLinesString(newLineCount);
String indentation = noIndentation ? "" : context.getIndentationString(indentationCount);
context.addReplacement(region.replaceWith(newLines + indentation));
}
return context.withIndentation(indentationCount);
}
use of org.eclipse.xtext.formatting2.ITextReplacerContext 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.ITextReplacerContext in project xtext-core by eclipse.
the class FormattableDocument method createReplacements.
protected ITextReplacerContext createReplacements(ITextReplacerContext previous) {
Integer maxLineWidth = getRequest().getPreferences().getPreference(FormatterPreferenceKeys.maxLineWidth);
ITextReplacerContext context = previous.withDocument(this);
ITextReplacerContext wrappable = null;
Set<ITextReplacer> wrapped = Sets.newHashSet();
Iterator<ITextReplacer> replacers = getReplacers().iterator();
while (replacers.hasNext()) {
ITextReplacer replacer = replacers.next();
context = context.withReplacer(replacer);
if (wrappable != null && context.isWrapSincePrevious()) {
wrappable = null;
}
if (wrappable != null && needsAutowrap(wrappable, context, maxLineWidth)) {
// then doesn't
while (context != wrappable) {
context = context.getPreviousContext();
}
replacer = context.getReplacer();
replacers = getReplacers().iteratorAfter(replacer);
context.setAutowrap(true);
wrappable = null;
}
ITextReplacerContext nextContext = replacer.createReplacements(context);
if (wrappable != null && context.isWrapInRegion()) {
wrappable = null;
} else {
Integer canAutowrap = context.canAutowrap();
if (canAutowrap != null && canAutowrap >= 0 && !context.isAutowrap() && !wrapped.contains(replacer)) {
boolean can = true;
if (wrappable != null) {
int lastEndOffset = wrappable.canAutowrap() + wrappable.getReplacer().getRegion().getEndOffset();
int thisEndOffset = canAutowrap + context.getReplacer().getRegion().getEndOffset();
can = lastEndOffset < thisEndOffset;
}
if (can) {
wrappable = context;
wrapped.add(replacer);
}
}
}
context = nextContext;
}
return context.withDocument(previous.getDocument());
}
use of org.eclipse.xtext.formatting2.ITextReplacerContext 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;
}
Aggregations