Search in sources :

Example 11 with ITextRegionExtensions

use of org.eclipse.xtext.formatting2.regionaccess.ITextRegionExtensions in project xtext-core by eclipse.

the class FormatterReplacementsTest method testIdentityEditsAreFiltered.

@Test
public void testIdentityEditsAreFiltered() {
    try {
        final GenericFormatter<IDList> _function = new GenericFormatter<IDList>() {

            @Override
            protected void format(final IDList model, @Extension final ITextRegionExtensions regions, @Extension final IFormattableDocument document) {
                final Consumer<ISemanticRegion> _function = (ISemanticRegion it) -> {
                    final Procedure1<IHiddenRegionFormatter> _function_1 = (IHiddenRegionFormatter it_1) -> {
                        it_1.autowrap();
                        it_1.oneSpace();
                    };
                    document.prepend(it, _function_1);
                };
                regions.regionFor(model).ruleCallsTo(FormatterReplacementsTest.this._formatterTestLanguageGrammarAccess.getIDRule()).forEach(_function);
            }
        };
        final GenericFormatter<IDList> formatter = _function;
        final IDList parsed = this.parseHelper.parse("idlist  aaa bbb");
        final FormatterRequest request = this.requestProvider.get();
        request.setAllowIdentityEdits(false);
        Resource _eResource = parsed.eResource();
        request.setTextRegionAccess(this.regionBuilder.forNodeModel(((XtextResource) _eResource)).create());
        final List<ITextReplacement> replacements = formatter.format(request);
        final Function1<ITextReplacement, String> _function_1 = (ITextReplacement it) -> {
            StringConcatenation _builder = new StringConcatenation();
            _builder.append("\"");
            String _text = it.getText();
            _builder.append(_text);
            _builder.append("\" -> \"");
            String _replacementText = it.getReplacementText();
            _builder.append(_replacementText);
            _builder.append("\"");
            _builder.append("\n");
            return _builder.toString();
        };
        final String actual = IterableExtensions.join(ListExtensions.<ITextReplacement, String>map(replacements, _function_1));
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("\"  \" -> \" \"");
        _builder.newLine();
        final String expected = _builder.toString();
        Assert.assertEquals(expected, actual);
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : IHiddenRegionFormatter(org.eclipse.xtext.formatting2.IHiddenRegionFormatter) ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) IFormattableDocument(org.eclipse.xtext.formatting2.IFormattableDocument) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) XtextResource(org.eclipse.xtext.resource.XtextResource) IDList(org.eclipse.xtext.formatting2.internal.formattertestlanguage.IDList) ITextRegionExtensions(org.eclipse.xtext.formatting2.regionaccess.ITextRegionExtensions) Extension(org.eclipse.xtext.xbase.lib.Extension) GenericFormatter(org.eclipse.xtext.formatting2.internal.GenericFormatter) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) ISemanticRegion(org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion) FormatterRequest(org.eclipse.xtext.formatting2.FormatterRequest) Test(org.junit.Test)

Example 12 with ITextRegionExtensions

use of org.eclipse.xtext.formatting2.regionaccess.ITextRegionExtensions in project xtext-core by eclipse.

the class FormattingConflictTest method enableDebugTracingFalse.

@Test
public void enableDebugTracingFalse() {
    final Wrapper<Throwable> wrapper = new Wrapper<Throwable>();
    final Wrapper<Integer> execution = new Wrapper<Integer>(Integer.valueOf(0));
    final Procedure1<GenericFormatterTestRequest> _function = (GenericFormatterTestRequest it) -> {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("idlist a");
        _builder.newLine();
        it.setToBeFormatted(_builder);
        final GenericFormatter<IDList> _function_1 = new GenericFormatter<IDList>() {

            @Override
            protected void format(final IDList model, @Extension final ITextRegionExtensions regions, @Extension final IFormattableDocument document) {
                Integer _get = execution.get();
                int _plus = ((_get).intValue() + 1);
                execution.set(Integer.valueOf(_plus));
                final Procedure1<IHiddenRegionFormatter> _function = (IHiddenRegionFormatter it_1) -> {
                    it_1.setSpace(" ");
                };
                document.append(regions.regionFor(model).keyword("idlist"), _function);
                final Procedure1<IHiddenRegionFormatter> _function_1 = (IHiddenRegionFormatter it_1) -> {
                    it_1.setSpace("\t");
                };
                document.append(regions.regionFor(model).keyword("idlist"), _function_1);
            }
        };
        it.setFormatter(_function_1);
        FormatterRequest _request = it.getRequest();
        _request.setEnableDebugTracing(false);
        FormatterRequest _request_1 = it.getRequest();
        final IAcceptor<Exception> _function_2 = (Exception e) -> {
            wrapper.set(e);
        };
        _request_1.setExceptionHandler(_function_2);
    };
    this._genericFormatterTester.assertFormatted(_function);
    Throwable _get = wrapper.get();
    final ConflictingRegionsException exception = ((ConflictingRegionsException) _get);
    Assert.assertEquals(2, (execution.get()).intValue());
    Assert.assertEquals(2, exception.getTraces().size());
}
Also used : Wrapper(org.eclipse.xtext.util.Wrapper) GenericFormatterTestRequest(org.eclipse.xtext.formatting2.internal.GenericFormatterTestRequest) IHiddenRegionFormatter(org.eclipse.xtext.formatting2.IHiddenRegionFormatter) IFormattableDocument(org.eclipse.xtext.formatting2.IFormattableDocument) IDList(org.eclipse.xtext.formatting2.internal.formattertestlanguage.IDList) ITextRegionExtensions(org.eclipse.xtext.formatting2.regionaccess.ITextRegionExtensions) ConflictingRegionsException(org.eclipse.xtext.formatting2.internal.ConflictingRegionsException) ConflictingRegionsException(org.eclipse.xtext.formatting2.internal.ConflictingRegionsException) GenericFormatter(org.eclipse.xtext.formatting2.internal.GenericFormatter) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) IAcceptor(org.eclipse.xtext.util.IAcceptor) FormatterRequest(org.eclipse.xtext.formatting2.FormatterRequest) Test(org.junit.Test)

Example 13 with ITextRegionExtensions

use of org.eclipse.xtext.formatting2.regionaccess.ITextRegionExtensions in project xtext-core by eclipse.

the class FormattingConflictTest method enableDebugTracingTrue.

@Test
public void enableDebugTracingTrue() {
    final Wrapper<Throwable> wrapper = new Wrapper<Throwable>();
    final Wrapper<Integer> execution = new Wrapper<Integer>(Integer.valueOf(0));
    final Procedure1<GenericFormatterTestRequest> _function = (GenericFormatterTestRequest it) -> {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("idlist a");
        _builder.newLine();
        it.setToBeFormatted(_builder);
        final GenericFormatter<IDList> _function_1 = new GenericFormatter<IDList>() {

            @Override
            protected void format(final IDList model, @Extension final ITextRegionExtensions regions, @Extension final IFormattableDocument document) {
                Integer _get = execution.get();
                int _plus = ((_get).intValue() + 1);
                execution.set(Integer.valueOf(_plus));
                final Procedure1<IHiddenRegionFormatter> _function = (IHiddenRegionFormatter it_1) -> {
                    it_1.setSpace(" ");
                };
                document.append(regions.regionFor(model).keyword("idlist"), _function);
                final Procedure1<IHiddenRegionFormatter> _function_1 = (IHiddenRegionFormatter it_1) -> {
                    it_1.setSpace("\t");
                };
                document.append(regions.regionFor(model).keyword("idlist"), _function_1);
            }
        };
        it.setFormatter(_function_1);
        FormatterRequest _request = it.getRequest();
        _request.setEnableDebugTracing(true);
        FormatterRequest _request_1 = it.getRequest();
        final IAcceptor<Exception> _function_2 = (Exception e) -> {
            wrapper.set(e);
        };
        _request_1.setExceptionHandler(_function_2);
    };
    this._genericFormatterTester.assertFormatted(_function);
    Throwable _get = wrapper.get();
    final ConflictingRegionsException exception = ((ConflictingRegionsException) _get);
    Assert.assertEquals(1, (execution.get()).intValue());
    Assert.assertEquals(2, exception.getTraces().size());
}
Also used : Wrapper(org.eclipse.xtext.util.Wrapper) GenericFormatterTestRequest(org.eclipse.xtext.formatting2.internal.GenericFormatterTestRequest) IHiddenRegionFormatter(org.eclipse.xtext.formatting2.IHiddenRegionFormatter) IFormattableDocument(org.eclipse.xtext.formatting2.IFormattableDocument) IDList(org.eclipse.xtext.formatting2.internal.formattertestlanguage.IDList) ITextRegionExtensions(org.eclipse.xtext.formatting2.regionaccess.ITextRegionExtensions) ConflictingRegionsException(org.eclipse.xtext.formatting2.internal.ConflictingRegionsException) ConflictingRegionsException(org.eclipse.xtext.formatting2.internal.ConflictingRegionsException) GenericFormatter(org.eclipse.xtext.formatting2.internal.GenericFormatter) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) IAcceptor(org.eclipse.xtext.util.IAcceptor) FormatterRequest(org.eclipse.xtext.formatting2.FormatterRequest) Test(org.junit.Test)

Example 14 with ITextRegionExtensions

use of org.eclipse.xtext.formatting2.regionaccess.ITextRegionExtensions in project xtext-core by eclipse.

the class RegionAccessDiffTest method testInsertReplaceReplace.

@Test
public void testInsertReplaceReplace() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("8 a");
    _builder.newLine();
    final ITextRegionAccess access = this._regionAccessTestHelper.toTextRegionAccess(_builder);
    final Procedure1<ITextRegionDiffBuilder> _function = (ITextRegionDiffBuilder it) -> {
        @Extension final ITextRegionExtensions ext = access.getExtensions();
        EObject _semanticElement = access.regionForRootEObject().getSemanticElement();
        final ValueList rootObj = ((ValueList) _semanticElement);
        final ISemanticRegion a = ext.regionFor(rootObj).keyword("8").getNextSemanticRegion();
        it.replace(a, "b");
        it.replace(a, "c");
    };
    ITextRegionAccess _modify = this._regionAccessTestHelper.modify(access, _function);
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("0 0   H");
    _builder_1.newLine();
    _builder_1.append("      ");
    _builder_1.append("B ValueList\'[a]\' Root");
    _builder_1.newLine();
    _builder_1.append("0 1    S \"8\"        Root:\'8\'");
    _builder_1.newLine();
    _builder_1.append("1 1    H \" \"        Whitespace:TerminalRule\'WS\'");
    _builder_1.newLine();
    _builder_1.append("2 1 1  S \"c\"        ValueList:name+=ID");
    _builder_1.newLine();
    _builder_1.append("      ");
    _builder_1.append("E ValueList\'[a]\' Root");
    _builder_1.newLine();
    _builder_1.append("3 0   H");
    _builder_1.newLine();
    _builder_1.append("------------ diff 1 ------------");
    _builder_1.newLine();
    _builder_1.append("2 1 S \"a\"        ValueList:name+=ID");
    _builder_1.newLine();
    this._regionAccessTestHelper.operator_tripleEquals(_modify, _builder_1);
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) ValueList(org.eclipse.xtext.formatting2.regionaccess.internal.regionaccesstestlanguage.ValueList) EObject(org.eclipse.emf.ecore.EObject) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) ITextRegionDiffBuilder(org.eclipse.xtext.formatting2.regionaccess.ITextRegionDiffBuilder) ISemanticRegion(org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion) ITextRegionExtensions(org.eclipse.xtext.formatting2.regionaccess.ITextRegionExtensions) Test(org.junit.Test)

Example 15 with ITextRegionExtensions

use of org.eclipse.xtext.formatting2.regionaccess.ITextRegionExtensions in project xtext-core by eclipse.

the class RegionAccessDiffTest method testSingleSemanticToken.

@Test
public void testSingleSemanticToken() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("1 foo");
    _builder.newLine();
    final ITextRegionAccess access = this._regionAccessTestHelper.toTextRegionAccess(_builder);
    final Procedure1<ITextRegionDiffBuilder> _function = (ITextRegionDiffBuilder it) -> {
        @Extension final ITextRegionExtensions ext = access.getExtensions();
        final EObject root = access.regionForRootEObject().getSemanticElement();
        final ISemanticRegion foo = ext.allRegionsFor(root).feature(RegionaccesstestlanguagePackage.Literals.SIMPLE__NAME);
        it.replace(foo, "baaar");
    };
    ITextRegionAccess _modify = this._regionAccessTestHelper.modify(access, _function);
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("0 0   H");
    _builder_1.newLine();
    _builder_1.append("      ");
    _builder_1.append("B Simple\'foo\' Root");
    _builder_1.newLine();
    _builder_1.append("0 1    S \"1\"        Simple:\'1\'");
    _builder_1.newLine();
    _builder_1.append("1 1    H \" \"        Whitespace:TerminalRule\'WS\'");
    _builder_1.newLine();
    _builder_1.append("2 5 1  S \"baaar\"    Simple:name=ID");
    _builder_1.newLine();
    _builder_1.append("      ");
    _builder_1.append("E Simple\'foo\' Root");
    _builder_1.newLine();
    _builder_1.append("7 0   H");
    _builder_1.newLine();
    _builder_1.append("------------ diff 1 ------------");
    _builder_1.newLine();
    _builder_1.append("2 3 S \"foo\"      Simple:name=ID");
    _builder_1.newLine();
    this._regionAccessTestHelper.operator_tripleEquals(_modify, _builder_1);
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) EObject(org.eclipse.emf.ecore.EObject) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) ITextRegionDiffBuilder(org.eclipse.xtext.formatting2.regionaccess.ITextRegionDiffBuilder) ISemanticRegion(org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion) ITextRegionExtensions(org.eclipse.xtext.formatting2.regionaccess.ITextRegionExtensions) Test(org.junit.Test)

Aggregations

StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)28 ITextRegionExtensions (org.eclipse.xtext.formatting2.regionaccess.ITextRegionExtensions)28 Test (org.junit.Test)28 IFormattableDocument (org.eclipse.xtext.formatting2.IFormattableDocument)22 GenericFormatter (org.eclipse.xtext.formatting2.internal.GenericFormatter)22 IHiddenRegionFormatter (org.eclipse.xtext.formatting2.IHiddenRegionFormatter)20 GenericFormatterTestRequest (org.eclipse.xtext.formatting2.internal.GenericFormatterTestRequest)20 Procedure1 (org.eclipse.xtext.xbase.lib.Procedures.Procedure1)20 IDList (org.eclipse.xtext.formatting2.internal.formattertestlanguage.IDList)13 ISemanticRegion (org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion)9 EObject (org.eclipse.emf.ecore.EObject)8 MapBasedPreferenceValues (org.eclipse.xtext.preferences.MapBasedPreferenceValues)8 KWList (org.eclipse.xtext.formatting2.internal.formattertestlanguage.KWList)7 ITextRegionAccess (org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess)6 ITextRegionDiffBuilder (org.eclipse.xtext.formatting2.regionaccess.ITextRegionDiffBuilder)6 FormatterRequest (org.eclipse.xtext.formatting2.FormatterRequest)4 Extension (org.eclipse.xtext.xbase.lib.Extension)4 Collection (java.util.Collection)3 ValueList (org.eclipse.xtext.formatting2.regionaccess.internal.regionaccesstestlanguage.ValueList)3 ITextRegion (org.eclipse.xtext.util.ITextRegion)3