Search in sources :

Example 81 with ITextRegionAccess

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

the class LineRegion method getNextLine.

@Override
public ILineRegion getNextLine() {
    ITextRegionAccess access = getTextRegionAccess();
    int start = getEndOffset() + 1;
    String text = access.regionForDocument().getText();
    while (true) {
        if (start >= text.length())
            return null;
        char c = text.charAt(start);
        if (c == '\n' || c == '\r')
            start++;
        else
            break;
    }
    int end = text.indexOf('\n', start);
    if (end > 0) {
        if (text.charAt(end - 1) == '\r')
            end = end - 1;
    } else
        end = text.length();
    return new LineRegion(access, start, end - start);
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) ILineRegion(org.eclipse.xtext.formatting2.regionaccess.ILineRegion)

Example 82 with ITextRegionAccess

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

the class AbstractFormatter2 method isInRequestedRange.

protected boolean isInRequestedRange(EObject obj) {
    Collection<ITextRegion> regions = request.getRegions();
    if (regions.isEmpty())
        return true;
    ITextRegionAccess access = request.getTextRegionAccess();
    IEObjectRegion objRegion = access.regionForEObject(obj);
    if (objRegion == null)
        return false;
    IHiddenRegion previousHidden = objRegion.getPreviousHiddenRegion();
    IHiddenRegion nextHidden = objRegion.getNextHiddenRegion();
    int objOffset = previousHidden != null ? previousHidden.getOffset() : 0;
    int objEnd = nextHidden != null ? nextHidden.getEndOffset() : access.regionForRootEObject().getEndOffset();
    for (ITextRegion region : regions) {
        int regionOffset = region.getOffset();
        int regionEnd = regionOffset + region.getLength();
        if (regionOffset <= objEnd && regionEnd >= objOffset)
            return true;
    }
    return false;
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) IEObjectRegion(org.eclipse.xtext.formatting2.regionaccess.IEObjectRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) IHiddenRegion(org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion)

Example 83 with ITextRegionAccess

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

the class RichStringFormatter method setSpace.

protected void setSpace(final IFormattableDocument doc, final int offset, final int length, final String space) {
    IHiddenRegionFormatting _createHiddenRegionFormatting = doc.getFormatter().createHiddenRegionFormatting();
    final Procedure1<IHiddenRegionFormatting> _function = (IHiddenRegionFormatting it) -> {
        it.setSpace(space);
    };
    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 84 with ITextRegionAccess

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

the class FormatterFacade method format.

public String format(final String xtendCode) {
    try {
        final XtextResourceSet resourceSet = new XtextResourceSet();
        Resource _createResource = this.resourceFactory.createResource(URI.createURI("synthetic://to-be-formatted.xtend"));
        final XtextResource resource = ((XtextResource) _createResource);
        EList<Resource> _resources = resourceSet.getResources();
        _resources.add(resource);
        StringInputStream _stringInputStream = new StringInputStream(xtendCode);
        resource.load(_stringInputStream, CollectionLiterals.<Object, Object>emptyMap());
        final ITextRegionAccess regionAccess = this.regionAccessBuilder.get().forNodeModel(resource).create();
        FormatterRequest _formatterRequest = new FormatterRequest();
        final Procedure1<FormatterRequest> _function = (FormatterRequest it) -> {
            it.setAllowIdentityEdits(false);
            it.setTextRegionAccess(regionAccess);
            it.setPreferences(TypedPreferenceValues.castOrWrap(this.cfgProvider.getPreferenceValues(resource)));
        };
        FormatterRequest request = ObjectExtensions.<FormatterRequest>operator_doubleArrow(_formatterRequest, _function);
        List<ITextReplacement> replacements = this.formatter.format(request);
        return regionAccess.getRewriter().renderToString(replacements);
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) StringInputStream(org.eclipse.xtext.util.StringInputStream) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) XtextResource(org.eclipse.xtext.resource.XtextResource) FormatterRequest(org.eclipse.xtext.formatting2.FormatterRequest)

Example 85 with ITextRegionAccess

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

the class FormatterTester method assertAllWhitespaceIsFormatted.

protected void assertAllWhitespaceIsFormatted(ITextRegionAccess access, List<ITextReplacement> replacements) {
    List<ITextSegment> expected = Lists.newArrayList();
    IHiddenRegion current = access.regionForRootEObject().getPreviousHiddenRegion();
    while (current != null) {
        expected.addAll(current.getMergedSpaces());
        current = current.getNextHiddenRegion();
    }
    List<ITextSegment> missing = TextRegions.difference(expected, replacements);
    if (!missing.isEmpty()) {
        TextRegionsToString toString = new TextRegionsToString().setTextRegionAccess(access);
        for (ITextSegment region : missing) toString.add(region, region.getClass().getSimpleName());
        String msg = "The following regions are not formatted:\n" + toString;
        System.err.println(msg);
        Assert.fail(msg);
    }
}
Also used : IHiddenRegion(org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment) TextRegionAccessToString(org.eclipse.xtext.formatting2.debug.TextRegionAccessToString) TextRegionsToString(org.eclipse.xtext.formatting2.debug.TextRegionsToString) TextRegionsToString(org.eclipse.xtext.formatting2.debug.TextRegionsToString)

Aggregations

ITextRegionAccess (org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess)78 Test (org.junit.Test)39 ISemanticRegion (org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion)18 ITextSegment (org.eclipse.xtext.formatting2.regionaccess.ITextSegment)18 ITextRegionDiffBuilder (org.eclipse.xtext.formatting2.regionaccess.ITextRegionDiffBuilder)17 TextRegionAccessToString (org.eclipse.xtext.formatting2.debug.TextRegionAccessToString)13 OptionalChildList (org.eclipse.xtext.ide.tests.testlanguage.partialSerializationTestLanguage.OptionalChildList)12 ITextReplacement (org.eclipse.xtext.formatting2.regionaccess.ITextReplacement)11 MandatoryValue (org.eclipse.xtext.ide.tests.testlanguage.partialSerializationTestLanguage.MandatoryValue)11 EObject (org.eclipse.emf.ecore.EObject)10 IHiddenRegion (org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion)10 ILineRegion (org.eclipse.xtext.formatting2.regionaccess.ILineRegion)9 FormatterRequest (org.eclipse.xtext.formatting2.FormatterRequest)8 XtextResource (org.eclipse.xtext.resource.XtextResource)7 TextRegionsToString (org.eclipse.xtext.formatting2.debug.TextRegionsToString)6 IEObjectRegion (org.eclipse.xtext.formatting2.regionaccess.IEObjectRegion)6 ITextRegionExtensions (org.eclipse.xtext.formatting2.regionaccess.ITextRegionExtensions)6 IFormatter2 (org.eclipse.xtext.formatting2.IFormatter2)5 String (java.lang.String)3 IComment (org.eclipse.xtext.formatting2.regionaccess.IComment)3