Search in sources :

Example 1 with TextReplacement

use of org.eclipse.xtext.xbase.formatting.TextReplacement in project xtext-eclipse by eclipse.

the class FormatterTester method applyDebugEdits.

protected String applyDebugEdits(final String oldDocument, final Collection<TextReplacement> edits) {
    String _xblockexpression = null;
    {
        int lastOffset = 0;
        final StringBuilder debugTrace = new StringBuilder();
        final Function1<TextReplacement, Integer> _function = (TextReplacement it) -> {
            return Integer.valueOf(it.getOffset());
        };
        List<TextReplacement> _sortBy = IterableExtensions.<TextReplacement, Integer>sortBy(edits, _function);
        for (final TextReplacement edit : _sortBy) {
            {
                debugTrace.append(oldDocument.substring(lastOffset, edit.getOffset()));
                StringConcatenation _builder = new StringConcatenation();
                _builder.append("[");
                int _offset = edit.getOffset();
                int _length = edit.getLength();
                int _plus = (_offset + _length);
                String _substring = oldDocument.substring(edit.getOffset(), _plus);
                _builder.append(_substring);
                _builder.append("|");
                String _text = edit.getText();
                _builder.append(_text);
                _builder.append("]");
                debugTrace.append(_builder);
                int _offset_1 = edit.getOffset();
                int _length_1 = edit.getLength();
                int _plus_1 = (_offset_1 + _length_1);
                lastOffset = _plus_1;
            }
        }
        debugTrace.append(oldDocument.substring(lastOffset, oldDocument.length()));
        _xblockexpression = debugTrace.toString();
    }
    return _xblockexpression;
}
Also used : TextReplacement(org.eclipse.xtext.xbase.formatting.TextReplacement) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with TextReplacement

use of org.eclipse.xtext.xbase.formatting.TextReplacement in project xtext-eclipse by eclipse.

the class FormatterTester method assertFormatted.

public void assertFormatted(final AssertingFormatterData it) {
    try {
        String _prefix = it.getPrefix();
        CharSequence _toBeFormatted = it.getToBeFormatted();
        String _plus = (_prefix + _toBeFormatted);
        String _postfix = it.getPostfix();
        final String fullToBeParsed = (_plus + _postfix);
        final EObject parsed = this._parseHelper.parse(fullToBeParsed);
        boolean _isAllowErrors = it.isAllowErrors();
        boolean _not = (!_isAllowErrors);
        if (_not) {
            Assert.assertEquals(IterableExtensions.join(parsed.eResource().getErrors(), "\n"), 0, parsed.eResource().getErrors().size());
        }
        Resource _eResource = parsed.eResource();
        IParseResult _parseResult = ((XtextResource) _eResource).getParseResult();
        ICompositeNode _rootNode = null;
        if (_parseResult != null) {
            _rootNode = _parseResult.getRootNode();
        }
        String _text = null;
        if (_rootNode != null) {
            _text = _rootNode.getText();
        }
        final String oldDocument = _text;
        final IBasicFormatter formatter = this.formatter;
        boolean _matched = false;
        if (formatter instanceof AbstractFormatter) {
            _matched = true;
            ((AbstractFormatter) this.formatter).setAllowIdentityEdits(true);
        }
        final int start = it.getPrefix().length();
        final int length = it.getToBeFormatted().length();
        final LinkedHashSet<TextReplacement> edits = CollectionLiterals.<TextReplacement>newLinkedHashSet();
        Resource _eResource_1 = parsed.eResource();
        List<TextReplacement> _format = this.formatter.format(((XtextResource) _eResource_1), start, length, it.getCfg());
        Iterables.<TextReplacement>addAll(edits, _format);
        final IBasicFormatter formatter_1 = this.formatter;
        boolean _matched_1 = false;
        if (formatter_1 instanceof AbstractFormatter) {
            _matched_1 = true;
            boolean _isConflictOccurred = ((AbstractFormatter) this.formatter).isConflictOccurred();
            if (_isConflictOccurred) {
                throw new RuntimeException("There are conflicting text edits, see console for details.");
            }
        }
        boolean _isAllowErrors_1 = it.isAllowErrors();
        boolean _not_1 = (!_isAllowErrors_1);
        if (_not_1) {
            Resource _eResource_2 = parsed.eResource();
            ArrayList<TextReplacement> _createMissingEditReplacements = this.createMissingEditReplacements(((XtextResource) _eResource_2), edits, start, length);
            Iterables.<TextReplacement>addAll(edits, _createMissingEditReplacements);
        }
        final String newDocument = this.applyEdits(oldDocument, edits);
        try {
            String _prefix_1 = it.getPrefix();
            CharSequence _expectation = it.getExpectation();
            String _plus_1 = (_prefix_1 + _expectation);
            String _postfix_1 = it.getPostfix();
            Assert.assertEquals((_plus_1 + _postfix_1).toString(), newDocument.toString());
        } catch (final Throwable _t) {
            if (_t instanceof AssertionError) {
                final AssertionError e = (AssertionError) _t;
                InputOutput.<String>println(this.applyDebugEdits(oldDocument, edits));
                InputOutput.println();
                throw e;
            } else {
                throw Exceptions.sneakyThrow(_t);
            }
        }
        Resource _eResource_3 = parsed.eResource();
        final String parsed2Doc = this.applyEdits(fullToBeParsed, this.formatter.format(((XtextResource) _eResource_3), 0, fullToBeParsed.length(), it.getCfg()));
        final EObject parsed2 = this._parseHelper.parse(parsed2Doc);
        boolean _isAllowErrors_2 = it.isAllowErrors();
        boolean _not_2 = (!_isAllowErrors_2);
        if (_not_2) {
            Assert.assertEquals(0, parsed2.eResource().getErrors().size());
        }
        Resource _eResource_4 = parsed2.eResource();
        final List<TextReplacement> edits2 = this.formatter.format(((XtextResource) _eResource_4), 0, parsed2Doc.length(), it.getCfg());
        final String newDocument2 = this.applyEdits(parsed2Doc, edits2);
        try {
            Assert.assertEquals(parsed2Doc, newDocument2.toString());
        } catch (final Throwable _t_1) {
            if (_t_1 instanceof AssertionError) {
                final AssertionError e_1 = (AssertionError) _t_1;
                InputOutput.<String>println(this.applyDebugEdits(newDocument, edits2));
                InputOutput.println();
                throw e_1;
            } else {
                throw Exceptions.sneakyThrow(_t_1);
            }
        }
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : TextReplacement(org.eclipse.xtext.xbase.formatting.TextReplacement) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) XtextResource(org.eclipse.xtext.resource.XtextResource) EObject(org.eclipse.emf.ecore.EObject) IBasicFormatter(org.eclipse.xtext.xbase.formatting.IBasicFormatter) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) IParseResult(org.eclipse.xtext.parser.IParseResult) AbstractFormatter(org.eclipse.xtext.xbase.formatting.AbstractFormatter)

Aggregations

TextReplacement (org.eclipse.xtext.xbase.formatting.TextReplacement)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 EObject (org.eclipse.emf.ecore.EObject)1 Resource (org.eclipse.emf.ecore.resource.Resource)1 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)1 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)1 IParseResult (org.eclipse.xtext.parser.IParseResult)1 XtextResource (org.eclipse.xtext.resource.XtextResource)1 AbstractFormatter (org.eclipse.xtext.xbase.formatting.AbstractFormatter)1 IBasicFormatter (org.eclipse.xtext.xbase.formatting.IBasicFormatter)1 Function1 (org.eclipse.xtext.xbase.lib.Functions.Function1)1