Search in sources :

Example 1 with ITextRegionAccess

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

the class FormatterTester method createRegionAccess.

protected ITextRegionAccess createRegionAccess(XtextResource resource, FormatterTestRequest req) {
    boolean useSerializer = req.isUseSerializer() && !req.isAllowSyntaxErrors();
    if (req.isUseNodeModel() && useSerializer) {
        ITextRegionAccess nmRegions = createRegionAccessViaNodeModel(resource);
        ITextRegionAccess serRegions = createRegionAccessViaSerializer(resource);
        String nmString = toString(nmRegions);
        String serString = toString(serRegions);
        Assert.assertEquals(nmString, serString);
        return nmRegions;
    } else if (req.isUseNodeModel()) {
        ITextRegionAccess nmRegions = createRegionAccessViaNodeModel(resource);
        return nmRegions;
    } else if (useSerializer) {
        ITextRegionAccess serRegions = createRegionAccessViaSerializer(resource);
        return serRegions;
    } else
        throw new IllegalStateException("Can't format anything when using neither NodeModel nor Serializer.");
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) TextRegionAccessToString(org.eclipse.xtext.formatting2.debug.TextRegionAccessToString) TextRegionsToString(org.eclipse.xtext.formatting2.debug.TextRegionsToString)

Example 2 with ITextRegionAccess

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

the class ContentFormatter method initRequest.

protected void initRequest(IXtextDocument document, IRegion region, XtextResource resource, FormatterRequest request) {
    ITextRegion textRegion = new TextRegion(region.getOffset(), region.getLength());
    request.setAllowIdentityEdits(false);
    request.setFormatUndefinedHiddenRegionsOnly(false);
    request.setRegions(singletonList(textRegion));
    ITextRegionAccess tokenAccess = regionBuilder.forNodeModel(resource).create();
    IPreferenceValues preferenceValues = preferencesProvider.getPreferenceValues(resource);
    request.setPreferences(TypedPreferenceValues.castOrWrap(preferenceValues));
    request.setTextRegionAccess(tokenAccess);
    if (tokenAccess.hasSyntaxError())
        request.setExceptionHandler(ExceptionAcceptor.IGNORING);
    else
        request.setExceptionHandler(ExceptionAcceptor.LOGGING);
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) TextRegion(org.eclipse.xtext.util.TextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) IPreferenceValues(org.eclipse.xtext.preferences.IPreferenceValues) ITextRegion(org.eclipse.xtext.util.ITextRegion)

Example 3 with ITextRegionAccess

use of org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess in project xtext-xtend by eclipse.

the class RichStringFormatter method setNewLines.

protected void setNewLines(final IFormattableDocument doc, final int offset, final int length, final int indentationIncrease, final int indentationDecrease, final int newLines) {
    IHiddenRegionFormatting _createHiddenRegionFormatting = doc.getFormatter().createHiddenRegionFormatting();
    final Procedure1<IHiddenRegionFormatting> _function = (IHiddenRegionFormatting it) -> {
        it.setIndentationIncrease(Integer.valueOf(indentationIncrease));
        it.setIndentationDecrease(Integer.valueOf(indentationDecrease));
        it.setNewLinesMin(Integer.valueOf(newLines));
        it.setNewLinesDefault(Integer.valueOf(newLines));
        it.setNewLinesMax(Integer.valueOf(newLines));
    };
    final IHiddenRegionFormatting fmt = ObjectExtensions.<IHiddenRegionFormatting>operator_doubleArrow(_createHiddenRegionFormatting, _function);
    AbstractFormatter2 _formatter = doc.getFormatter();
    ITextRegionAccess _textRegionAccess = this._iTextRegionExtensions.getTextRegionAccess();
    TextSegment _textSegment = new TextSegment(_textRegionAccess, offset, length);
    final ITextReplacer replacer = _formatter.createWhitespaceReplacer(_textSegment, fmt);
    doc.addReplacer(replacer);
}
Also used : IHiddenRegionFormatting(org.eclipse.xtext.formatting2.IHiddenRegionFormatting) ITextReplacer(org.eclipse.xtext.formatting2.ITextReplacer) ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) AbstractFormatter2(org.eclipse.xtext.formatting2.AbstractFormatter2) TextSegment(org.eclipse.xtext.formatting2.regionaccess.internal.TextSegment)

Example 4 with ITextRegionAccess

use of org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess in project xtext-xtend by eclipse.

the class RichStringFormatter method _format.

protected void _format(final RichString richString, final IFormattableDocument doc) {
    RichString _containerOfType = EcoreUtil2.<RichString>getContainerOfType(richString.eContainer(), RichString.class);
    boolean _tripleNotEquals = (_containerOfType != null);
    if (_tripleNotEquals) {
        return;
    }
    boolean _hasSyntaxError = this.hasSyntaxError(this._iTextRegionExtensions.regionForEObject(richString));
    if (_hasSyntaxError) {
        return;
    }
    ITextRegionAccess _textRegionAccess = this._iTextRegionExtensions.getTextRegionAccess();
    final RichStringToLineModel impl = new RichStringToLineModel(_textRegionAccess, richString);
    DefaultIndentationHandler _defaultIndentationHandler = new DefaultIndentationHandler();
    this.factory.richStringProcessor.process(richString, impl, _defaultIndentationHandler);
    impl.finish();
    EList<XExpression> _expressions = richString.getExpressions();
    for (final XExpression e : _expressions) {
        this.format(e, doc);
    }
    final List<Line> lines = impl.getModel().getLines();
    final boolean canIndent = ((!lines.isEmpty()) && StringExtensions.isNullOrEmpty(IterableExtensions.<Line>last(lines).getContent()));
    for (final Line line : lines) {
        int _rootIndentLenght = impl.getModel().getRootIndentLenght();
        boolean _greaterThan = (_rootIndentLenght > 0);
        if (_greaterThan) {
            int _xifexpression = (int) 0;
            if ((canIndent && Objects.equal(line, IterableExtensions.<Line>head(lines)))) {
                _xifexpression = 1;
            } else {
                _xifexpression = 0;
            }
            final int increaseIndentationChange = _xifexpression;
            int _xifexpression_1 = (int) 0;
            if ((canIndent && Objects.equal(line, IterableExtensions.<Line>last(lines)))) {
                _xifexpression_1 = 1;
            } else {
                _xifexpression_1 = 0;
            }
            final int decraseIndentationChange = _xifexpression_1;
            int _xifexpression_2 = (int) 0;
            boolean _isLeadingSemanticNewLine = line.isLeadingSemanticNewLine();
            if (_isLeadingSemanticNewLine) {
                int _offset = line.getOffset();
                int _newLineCharCount = line.getNewLineCharCount();
                _xifexpression_2 = (_offset + _newLineCharCount);
            } else {
                _xifexpression_2 = line.getOffset();
            }
            final int nloffset = _xifexpression_2;
            final int i = Math.min(line.getIndentLength(), impl.getModel().getRootIndentLenght());
            int _xifexpression_3 = (int) 0;
            boolean _isLeadingSemanticNewLine_1 = line.isLeadingSemanticNewLine();
            if (_isLeadingSemanticNewLine_1) {
                _xifexpression_3 = i;
            } else {
                int _newLineCharCount_1 = line.getNewLineCharCount();
                _xifexpression_3 = (_newLineCharCount_1 + i);
            }
            final int nllength = _xifexpression_3;
            boolean _isLeadingSemanticNewLine_2 = line.isLeadingSemanticNewLine();
            if (_isLeadingSemanticNewLine_2) {
                this.setNewLines(doc, nloffset, nllength, increaseIndentationChange, decraseIndentationChange, 0);
            } else {
                this.setNewLines(doc, nloffset, nllength, increaseIndentationChange, decraseIndentationChange, 1);
            }
            boolean _isEmpty = line.getChunks().isEmpty();
            boolean _not = (!_isEmpty);
            if (_not) {
                final int offset = (nloffset + nllength);
                int _indentLength = line.getIndentLength();
                int _rootIndentLenght_1 = impl.getModel().getRootIndentLenght();
                final int length = (_indentLength - _rootIndentLenght_1);
                final Function1<Chunk, CharSequence> _function = (Chunk chunk) -> {
                    CharSequence _switchResult = null;
                    boolean _matched = false;
                    if (chunk instanceof SemanticWhitespace) {
                        _matched = true;
                        _switchResult = ((SemanticWhitespace) chunk).getText();
                    }
                    if (!_matched) {
                        if (chunk instanceof TemplateWhitespace) {
                            _matched = true;
                            _switchResult = doc.getFormatter().<String>getPreference(FormatterPreferenceKeys.indentation);
                        }
                    }
                    return _switchResult;
                };
                final String text = IterableExtensions.join(ListExtensions.<Chunk, CharSequence>map(line.getChunks(), _function));
                this.setSpace(doc, offset, length, text);
            }
        }
    }
}
Also used : RichStringToLineModel(org.eclipse.xtend.core.formatting2.RichStringToLineModel) ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) RichString(org.eclipse.xtend.core.xtend.RichString) TemplateWhitespace(org.eclipse.xtend.core.formatting2.TemplateWhitespace) RichString(org.eclipse.xtend.core.xtend.RichString) Chunk(org.eclipse.xtend.core.formatting2.Chunk) DefaultIndentationHandler(org.eclipse.xtend.core.richstring.DefaultIndentationHandler) Line(org.eclipse.xtend.core.formatting2.Line) XExpression(org.eclipse.xtext.xbase.XExpression) SemanticWhitespace(org.eclipse.xtend.core.formatting2.SemanticWhitespace)

Example 5 with ITextRegionAccess

use of org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess in project xtext-xtend by eclipse.

the class ConvertJavaCode method formatXtendCode.

private String formatXtendCode(IFile xtendFile, final String xtendCode) {
    try {
        XtextResource resource = (XtextResource) createResource(xtendFile, xtendCode);
        ITextRegionAccess regionAccess = regionAccessBuilder.get().forNodeModel(resource).create();
        FormatterRequest request = new FormatterRequest();
        request.setAllowIdentityEdits(false);
        request.setTextRegionAccess(regionAccess);
        request.setPreferences(TypedPreferenceValues.castOrWrap(cfgProvider.getPreferenceValues(resource)));
        List<ITextReplacement> replacements = formatter.format(request);
        String formatted = regionAccess.getRewriter().renderToString(replacements);
        return formatted;
    } catch (Exception e) {
        LOG.error("Formatting step canceled due to an exception.", e);
        return null;
    }
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) XtextResource(org.eclipse.xtext.resource.XtextResource) FormatterRequest(org.eclipse.xtext.formatting2.FormatterRequest) CoreException(org.eclipse.core.runtime.CoreException) InvocationTargetException(java.lang.reflect.InvocationTargetException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JavaModelException(org.eclipse.jdt.core.JavaModelException) IOException(java.io.IOException) ExecutionException(org.eclipse.core.commands.ExecutionException)

Aggregations

ITextRegionAccess (org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess)71 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)37 Test (org.junit.Test)37 IChangeSerializer (org.eclipse.xtext.ide.serializer.IChangeSerializer)21 ITextRegionDiffBuilder (org.eclipse.xtext.formatting2.regionaccess.ITextRegionDiffBuilder)17 ISemanticRegion (org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion)16 ITextSegment (org.eclipse.xtext.formatting2.regionaccess.ITextSegment)16 EObject (org.eclipse.emf.ecore.EObject)14 OptionalChildList (org.eclipse.xtext.ide.tests.testlanguage.partialSerializationTestLanguage.OptionalChildList)12 MandatoryValue (org.eclipse.xtext.ide.tests.testlanguage.partialSerializationTestLanguage.MandatoryValue)11 ITextReplacement (org.eclipse.xtext.formatting2.regionaccess.ITextReplacement)10 Procedure1 (org.eclipse.xtext.xbase.lib.Procedures.Procedure1)10 TextRegionAccessToString (org.eclipse.xtext.formatting2.debug.TextRegionAccessToString)9 ILineRegion (org.eclipse.xtext.formatting2.regionaccess.ILineRegion)9 EList (org.eclipse.emf.common.util.EList)8 FormatterRequest (org.eclipse.xtext.formatting2.FormatterRequest)7 IHiddenRegion (org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion)7 IEObjectRegion (org.eclipse.xtext.formatting2.regionaccess.IEObjectRegion)6 ITextRegionExtensions (org.eclipse.xtext.formatting2.regionaccess.ITextRegionExtensions)6 XtextResource (org.eclipse.xtext.resource.XtextResource)5