Search in sources :

Example 96 with MapBasedPreferenceValues

use of org.eclipse.xtext.preferences.MapBasedPreferenceValues in project xtext-xtend by eclipse.

the class XtendInterfaceFormatterTest method formatMethod01.

@Test
public void formatMethod01() {
    final Procedure1<MapBasedPreferenceValues> _function = (MapBasedPreferenceValues it) -> {
        it.<Boolean>put(XbaseFormatterPreferenceKeys.bracesInNewLine, Boolean.valueOf(false));
    };
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package foo");
    _builder.newLine();
    _builder.newLine();
    _builder.append("interface bar {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def baz()");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    this.assertFormatted(_function, _builder);
}
Also used : MapBasedPreferenceValues(org.eclipse.xtext.preferences.MapBasedPreferenceValues) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) AbstractXtendFormatterTest(org.eclipse.xtend.core.tests.formatting.AbstractXtendFormatterTest) Test(org.junit.Test)

Example 97 with MapBasedPreferenceValues

use of org.eclipse.xtext.preferences.MapBasedPreferenceValues in project xtext-xtend by eclipse.

the class XtendOnelinersFormatterTest method formatMethodWithOneExpression4.

@Test
public void formatMethodWithOneExpression4() {
    final Procedure1<MapBasedPreferenceValues> _function = (MapBasedPreferenceValues it) -> {
        it.<Boolean>put(XtendFormatterPreferenceKeys.keepOneLineMethods, Boolean.valueOf(true));
    };
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("class C {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def m() { \"Foo\" }");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("class C {");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("def m() {       \"Foo\"     }");
    _builder_1.newLine();
    _builder_1.append("}");
    _builder_1.newLine();
    this.assertFormatted(_function, _builder, _builder_1);
}
Also used : MapBasedPreferenceValues(org.eclipse.xtext.preferences.MapBasedPreferenceValues) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) AbstractXtendFormatterTest(org.eclipse.xtend.core.tests.formatting.AbstractXtendFormatterTest) Test(org.junit.Test)

Example 98 with MapBasedPreferenceValues

use of org.eclipse.xtext.preferences.MapBasedPreferenceValues in project xtext-xtend by eclipse.

the class XtendOnelinersFormatterTest method formatMethodWithJustAComment2.

@Test
public void formatMethodWithJustAComment2() {
    final Procedure1<MapBasedPreferenceValues> _function = (MapBasedPreferenceValues it) -> {
        it.<Boolean>put(XtendFormatterPreferenceKeys.keepOneLineMethods, Boolean.valueOf(true));
    };
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("class C {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def m() {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("/*foo*/");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    this.assertFormatted(_function, _builder);
}
Also used : MapBasedPreferenceValues(org.eclipse.xtext.preferences.MapBasedPreferenceValues) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) AbstractXtendFormatterTest(org.eclipse.xtend.core.tests.formatting.AbstractXtendFormatterTest) Test(org.junit.Test)

Example 99 with MapBasedPreferenceValues

use of org.eclipse.xtext.preferences.MapBasedPreferenceValues in project xtext-core by eclipse.

the class FormattingService method format.

/**
 * @since 2.14
 */
public List<TextEdit> format(final XtextResource resource, final Document document, final int offset, final int length, final FormattingOptions options) {
    String indent = this.indentationInformation.getIndentString();
    if ((options != null)) {
        boolean _isInsertSpaces = options.isInsertSpaces();
        if (_isInsertSpaces) {
            indent = Strings.padEnd("", options.getTabSize(), ' ');
        }
    }
    if ((this.formatter2Provider != null)) {
        final MapBasedPreferenceValues preferences = new MapBasedPreferenceValues();
        preferences.put("indentation", indent);
        TextRegion _textRegion = new TextRegion(offset, length);
        final List<ITextReplacement> replacements = this.format2(resource, _textRegion, preferences);
        final Function1<ITextReplacement, TextEdit> _function = (ITextReplacement r) -> {
            return this.toTextEdit(document, r.getReplacementText(), r.getOffset(), r.getLength());
        };
        return IterableExtensions.<TextEdit>toList(ListExtensions.<ITextReplacement, TextEdit>map(replacements, _function));
    } else {
        return CollectionLiterals.<TextEdit>newArrayList();
    }
}
Also used : TextRegion(org.eclipse.xtext.util.TextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) TextEdit(org.eclipse.lsp4j.TextEdit) MapBasedPreferenceValues(org.eclipse.xtext.preferences.MapBasedPreferenceValues)

Example 100 with MapBasedPreferenceValues

use of org.eclipse.xtext.preferences.MapBasedPreferenceValues in project xtext-core by eclipse.

the class FormatterTestHelper method assertFormatted.

public void assertFormatted(FormatterTestRequest req) {
    checkNotNull(req);
    checkNotNull(req.getToBeFormatted());
    FormatterRequest request = req.getRequest();
    checkArgument(request.getTextRegionAccess() == null);
    String document = req.getToBeFormatted().toString();
    XtextResource parsed = parse(document);
    if (req.isAllowSyntaxErrors()) {
        if (request.getExplicitExceptionHandler() == null) {
            request.setExceptionHandler(ExceptionAcceptor.IGNORING);
        }
    } else {
        assertNoSyntaxErrors(parsed);
        if (request.getExplicitExceptionHandler() == null) {
            request.setExceptionHandler(ExceptionAcceptor.THROWING);
        }
    }
    request.setTextRegionAccess(createRegionAccess(parsed, req));
    if (request.getPreferences() == null)
        request.setPreferences(new MapBasedPreferenceValues(Maps.<String, String>newLinkedHashMap()));
    List<ITextReplacement> replacements = createFormatter(req).format(request);
    assertReplacementsAreInRegion(replacements, request.getRegions(), document);
    if (!req.isAllowUnformattedWhitespace())
        assertAllWhitespaceIsFormatted(request.getTextRegionAccess(), replacements);
    String formatted = request.getTextRegionAccess().getRewriter().renderToString(replacements);
    Assert.assertEquals(req.getExpectationOrToBeFormatted().toString(), formatted);
// TODO: assert formatting a second time only produces identity replacements
// TODO: assert formatting with undefined whitespace only
}
Also used : ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) MapBasedPreferenceValues(org.eclipse.xtext.preferences.MapBasedPreferenceValues) XtextResource(org.eclipse.xtext.resource.XtextResource) TextRegionAccessToString(org.eclipse.xtext.formatting2.debug.TextRegionAccessToString) TextRegionsToString(org.eclipse.xtext.formatting2.debug.TextRegionsToString) FormatterRequest(org.eclipse.xtext.formatting2.FormatterRequest)

Aggregations

MapBasedPreferenceValues (org.eclipse.xtext.preferences.MapBasedPreferenceValues)106 Test (org.junit.Test)100 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)99 AbstractXtendFormatterTest (org.eclipse.xtend.core.tests.formatting.AbstractXtendFormatterTest)91 Procedure1 (org.eclipse.xtext.xbase.lib.Procedures.Procedure1)14 IFormattableDocument (org.eclipse.xtext.formatting2.IFormattableDocument)8 IHiddenRegionFormatter (org.eclipse.xtext.formatting2.IHiddenRegionFormatter)8 GenericFormatter (org.eclipse.xtext.formatting2.internal.GenericFormatter)8 GenericFormatterTestRequest (org.eclipse.xtext.formatting2.internal.GenericFormatterTestRequest)8 ITextRegionExtensions (org.eclipse.xtext.formatting2.regionaccess.ITextRegionExtensions)8 KWList (org.eclipse.xtext.formatting2.internal.formattertestlanguage.KWList)7 FormatterTestRequest (org.eclipse.xtext.testing.formatter.FormatterTestRequest)6 Ignore (org.junit.Ignore)5 IAutowrapFormatter (org.eclipse.xtext.formatting2.IAutowrapFormatter)2 IFormattableSubDocument (org.eclipse.xtext.formatting2.IFormattableSubDocument)2 IHiddenRegionFormatting (org.eclipse.xtext.formatting2.IHiddenRegionFormatting)2 ISubFormatter (org.eclipse.xtext.formatting2.ISubFormatter)2 ITextReplacement (org.eclipse.xtext.formatting2.regionaccess.ITextReplacement)2 ITextSegment (org.eclipse.xtext.formatting2.regionaccess.ITextSegment)2 ITextRegion (org.eclipse.xtext.util.ITextRegion)2