use of org.eclipse.xtext.formatting2.regionaccess.ITextSegment in project n4js by eclipse.
the class FormatterXpectMethod method formattedLines.
/**
*/
@Xpect
@ParameterParser(syntax = "arg1=INT")
public void formattedLines(@StringExpectation(whitespaceSensitive = true) IStringExpectation exp, // arg1
int lines, XpectInvocation inv, TargetSyntaxSupport syntax, ITextRegionAccess reg, ISetupInitializer<Preferences> prefInit) {
ITextSegment region = findRegion(lines, inv, syntax, reg);
Preferences prefs = new Preferences();
// First put some defaults
prefs.put(N4JSFormatterPreferenceKeys.FORMAT_PARENTHESIS, true);
prefs.put(FormatterPreferenceKeys.lineSeparator, "\n");
// Second init from concrete tests - will override defaults.
prefInit.initialize(prefs);
IFormatter2 formatter = formatterProvider.get();
FormatterRequest request = formatterRequestProvider.get();
request.setTextRegionAccess(reg);
request.setExceptionHandler(ExceptionAcceptor.THROWING);
// needed in case a check like this will be implemented:
// org.eclipse.xtext.testing.formatter.FormatterTester.assertAllWhitespaceIsFormatted()
request.setAllowIdentityEdits(true);
request.setFormatUndefinedHiddenRegionsOnly(false);
request.addRegion(region);
request.setPreferences(prefs);
List<ITextReplacement> replacements = formatter.format(request);
String fmt = reg.getRewriter().renderToString(replacements);
ITextSegment doc = reg.regionForDocument();
int endIndex = region.getEndOffset() + (fmt.length() - doc.getLength()) - 1;
String selection = fmt.substring(region.getOffset(), endIndex);
exp.assertEquals(selection);
}
use of org.eclipse.xtext.formatting2.regionaccess.ITextSegment in project n4js by eclipse.
the class N4MultilineCommentReplacer 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;
String newIndentation2 = newIndentation + " ";
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() + nonWhiteSpaceOffset(text.substring(oldIndentation.length())));
else
target = access.regionForOffset(line.getOffset(), /* 0 */
nonWhiteSpaceOffset(text));
if (line.getEndOffset() - target.getEndOffset() > 0) {
// content left in line
context.addReplacement(target.replaceWith(newIndentation2));
} else {
// no content left
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 n4js by eclipse.
the class FixedMultilineCommentReplacer 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;
for (int i = 1; i < lines.size() - 1; i++) {
ITextSegment line = lines.get(i);
String text = line.getText();
ITextSegment target;
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