Search in sources :

Example 1 with Preferences

use of org.eclipse.n4js.xpect.config.Preferences 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 = getRegionForLines(reg, syntax.findFirstSemanticCharAfterStatement(inv), lines);
    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);
}
Also used : IFormatter2(org.eclipse.xtext.formatting2.IFormatter2) ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment) Preferences(org.eclipse.n4js.xpect.config.Preferences) FormatterRequest(org.eclipse.xtext.formatting2.FormatterRequest) Xpect(org.eclipse.xpect.runner.Xpect) ParameterParser(org.eclipse.xpect.parameter.ParameterParser)

Aggregations

Preferences (org.eclipse.n4js.xpect.config.Preferences)1 ParameterParser (org.eclipse.xpect.parameter.ParameterParser)1 Xpect (org.eclipse.xpect.runner.Xpect)1 FormatterRequest (org.eclipse.xtext.formatting2.FormatterRequest)1 IFormatter2 (org.eclipse.xtext.formatting2.IFormatter2)1 ITextReplacement (org.eclipse.xtext.formatting2.regionaccess.ITextReplacement)1 ITextSegment (org.eclipse.xtext.formatting2.regionaccess.ITextSegment)1