Search in sources :

Example 16 with Add

use of org.eclipse.xtext.formatting2.regionaccess.internal.regionaccesstestlanguage.Add in project xtext-core by eclipse.

the class PartialSerializerTest method testOptionalChildListInsertIntoFirst.

@Test
public void testOptionalChildListInsertIntoFirst() {
    ITextRegionAccess diff = recordDiff(OptionalChildList.class, "#13 x2", (OptionalChildList it) -> {
        MandatoryValue newMandatoryValue = fac.createMandatoryValue();
        newMandatoryValue.setName("x1");
        it.getChildren().add(0, newMandatoryValue);
    });
    String expectation = "0 0   H\n" + "      B OptionalChildList    Model\n" + "0 3    S \"#13\"                Model:'#13'\n" + "3 1 1  H \" \"                  Whitespace:TerminalRule'WS'\n" + "       B MandatoryValue'x1'   MandatoryValue path:OptionalChildList/children[0]\n" + "4 2 1   S \"x1\"                 MandatoryValue:name=ID\n" + "       E MandatoryValue'x1'   MandatoryValue path:OptionalChildList/children[0]\n" + "6 0 1  H\n" + "       B MandatoryValue'x2'   OptionalChildList:children+=MandatoryValue path:OptionalChildList/children[1]\n" + "6 2     S \"x2\"                 MandatoryValue:name=ID\n" + "       E MandatoryValue'x2'   OptionalChildList:children+=MandatoryValue path:OptionalChildList/children[1]\n" + "      E OptionalChildList    Model\n" + "8 0   H\n" + "------------ diff 1 ------------\n" + "3 1 H \" \"                  Whitespace:TerminalRule'WS'\n" + "4 2 S \"x2\"                 MandatoryValue:name=ID\n";
    changeSerializerTestHelper.operator_tripleEquals(diff, expectation);
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) OptionalChildList(org.eclipse.xtext.ide.tests.testlanguage.partialSerializationTestLanguage.OptionalChildList) MandatoryValue(org.eclipse.xtext.ide.tests.testlanguage.partialSerializationTestLanguage.MandatoryValue) Test(org.junit.Test)

Example 17 with Add

use of org.eclipse.xtext.formatting2.regionaccess.internal.regionaccesstestlanguage.Add in project xtext-core by eclipse.

the class PartialSerializerTest method testOptionalChildListInsertIntoEmpty.

@Test
public void testOptionalChildListInsertIntoEmpty() {
    ITextRegionAccess diff = recordDiff(OptionalChildList.class, "#13", (OptionalChildList it) -> {
        MandatoryValue newMandatoryValue = fac.createMandatoryValue();
        newMandatoryValue.setName("foo");
        it.getChildren().add(newMandatoryValue);
    });
    String expectation = "0 0   H\n" + "      B OptionalChildList    Model\n" + "0 3 1  S \"#13\"                Model:'#13'\n" + "3 0 1  H\n" + "       B MandatoryValue'foo'  MandatoryValue path:OptionalChildList/children[0]\n" + "3 3 1   S \"foo\"                MandatoryValue:name=ID\n" + "       E MandatoryValue'foo'  MandatoryValue path:OptionalChildList/children[0]\n" + "      E OptionalChildList    Model\n" + "6 0 1 H\n" + "------------ diff 1 ------------\n" + "0 3  S \"#13\"                Model:'#13'\n" + "3 0  H\n";
    changeSerializerTestHelper.operator_tripleEquals(diff, expectation);
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) OptionalChildList(org.eclipse.xtext.ide.tests.testlanguage.partialSerializationTestLanguage.OptionalChildList) MandatoryValue(org.eclipse.xtext.ide.tests.testlanguage.partialSerializationTestLanguage.MandatoryValue) Test(org.junit.Test)

Example 18 with Add

use of org.eclipse.xtext.formatting2.regionaccess.internal.regionaccesstestlanguage.Add in project xtext-core by eclipse.

the class ChangeSerializerTestHelper method operator_tripleEquals.

public void operator_tripleEquals(Collection<IEmfResourceChange> actual, CharSequence expected) {
    String actualString = new TextDocumentChangeToString().add(actual).toString();
    Assert.assertEquals(Strings.toPlatformLineSeparator(expected).trim(), Strings.toPlatformLineSeparator(actualString).trim());
}
Also used : TextDocumentChangeToString(org.eclipse.xtext.ide.serializer.debug.TextDocumentChangeToString) TextRegionAccessToString(org.eclipse.xtext.formatting2.debug.TextRegionAccessToString) TextDocumentChangeToString(org.eclipse.xtext.ide.serializer.debug.TextDocumentChangeToString)

Example 19 with Add

use of org.eclipse.xtext.formatting2.regionaccess.internal.regionaccesstestlanguage.Add in project xtext-core by eclipse.

the class FormattableDocumentTest method shouldFormat.

/**
 * see https://bugs.eclipse.org/bugs/show_bug.cgi?id=482110
 */
@Test
public void shouldFormat() {
    genericFormatterTester.assertFormatted((GenericFormatterTestRequest it) -> {
        it.getRequest().getRegions().add(new TextRegion(0, 6));
        String model = "idlist";
        it.setToBeFormatted(model);
        it.setFormatter(new GenericFormatter<EObject>() {

            @Override
            protected void format(EObject model, ITextRegionExtensions regionAccess, IFormattableDocument document) {
                throw new IllegalStateException("this method should never be called");
            }

            @Override
            public boolean shouldFormat(Object obj, IFormattableDocument document) {
                return false;
            }
        });
        String expectation = "idlist";
        it.setExpectation(expectation);
    });
}
Also used : TextRegion(org.eclipse.xtext.util.TextRegion) EObject(org.eclipse.emf.ecore.EObject) IFormattableDocument(org.eclipse.xtext.formatting2.IFormattableDocument) EObject(org.eclipse.emf.ecore.EObject) ITextRegionExtensions(org.eclipse.xtext.formatting2.regionaccess.ITextRegionExtensions) Test(org.junit.Test)

Example 20 with Add

use of org.eclipse.xtext.formatting2.regionaccess.internal.regionaccesstestlanguage.Add in project xtext-core by eclipse.

the class HiddenRegionReplacer method applyPriorityAndDefaultFormatting.

/**
 * @since 2.23
 */
protected void applyPriorityAndDefaultFormatting(List<ITextReplacer> replacers, IHiddenRegionFormatting separator) {
    for (int i = 0; i < replacers.size(); i++) {
        ITextReplacer replacer = replacers.get(i);
        if (replacer instanceof WhitespaceReplacer) {
            IHiddenRegionFormatting formatting2 = ((WhitespaceReplacer) replacer).getFormatting();
            formatting2.setPriority(formatting.getPriority());
            if (formatting2 != separator) {
                ITextReplacer previous = (i == 0) ? null : replacers.get(i - 1);
                ITextReplacer next = (i + 1 >= replacers.size()) ? null : replacers.get(i + 1);
                // Don't add single spaces after comments
                if (previous == null || !(previous instanceof CommentReplacer)) {
                    // starts with a space
                    if (next == null || !(next instanceof MultilineCommentReplacer)) {
                        formatting2.setSpace(replacer.getRegion().getOffset() > 0 ? " " : "");
                    }
                }
                formatting2.setNewLinesMin(0);
                formatting2.setNewLinesMax(1);
            }
        }
    }
}
Also used : ITextReplacer(org.eclipse.xtext.formatting2.ITextReplacer) IHiddenRegionFormatting(org.eclipse.xtext.formatting2.IHiddenRegionFormatting)

Aggregations

Test (org.junit.Test)14 ITextRegionAccess (org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess)7 MandatoryValue (org.eclipse.xtext.ide.tests.testlanguage.partialSerializationTestLanguage.MandatoryValue)7 OptionalChildList (org.eclipse.xtext.ide.tests.testlanguage.partialSerializationTestLanguage.OptionalChildList)7 TextRegionAccessToString (org.eclipse.xtext.formatting2.debug.TextRegionAccessToString)5 IFormattableDocument (org.eclipse.xtext.formatting2.IFormattableDocument)4 ITextRegionExtensions (org.eclipse.xtext.formatting2.regionaccess.ITextRegionExtensions)4 Resource (org.eclipse.emf.ecore.resource.Resource)3 IDList (org.eclipse.xtext.formatting2.internal.formattertestlanguage.IDList)3 TextRegion (org.eclipse.xtext.util.TextRegion)3 EObject (org.eclipse.emf.ecore.EObject)2 TextDocumentChangeToString (org.eclipse.xtext.ide.serializer.debug.TextDocumentChangeToString)2 XtextResource (org.eclipse.xtext.resource.XtextResource)2 XtextResourceSet (org.eclipse.xtext.resource.XtextResourceSet)2 Joiner (com.google.common.base.Joiner)1 Lists (com.google.common.collect.Lists)1 Inject (com.google.inject.Inject)1 Provider (com.google.inject.Provider)1 BufferedOutputStream (java.io.BufferedOutputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1