Search in sources :

Example 6 with ITextRegionAccess

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

the class FormattingService method format2.

protected List<ITextReplacement> format2(final XtextResource resource, final ITextRegion selection, final ITypedPreferenceValues preferences) {
    final FormatterRequest request = this.formatterRequestProvider.get();
    request.setAllowIdentityEdits(false);
    request.setFormatUndefinedHiddenRegionsOnly(false);
    if ((selection != null)) {
        request.setRegions(Collections.<ITextRegion>unmodifiableList(CollectionLiterals.<ITextRegion>newArrayList(selection)));
    }
    if ((preferences != null)) {
        request.setPreferences(preferences);
    }
    final ITextRegionAccess regionAccess = this.regionBuilder.forNodeModel(resource).create();
    request.setTextRegionAccess(regionAccess);
    final IFormatter2 formatter2 = this.formatter2Provider.get();
    final List<ITextReplacement> replacements = formatter2.format(request);
    return replacements;
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) IFormatter2(org.eclipse.xtext.formatting2.IFormatter2) ITextRegion(org.eclipse.xtext.util.ITextRegion) ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) FormatterRequest(org.eclipse.xtext.formatting2.FormatterRequest)

Example 7 with ITextRegionAccess

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

the class FormatterTestHelper 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)

Example 8 with ITextRegionAccess

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

the class PartialSerializer method findRegion.

protected IAstRegion findRegion(IEObjectRegion owner, FeatureChange change) {
    EStructuralFeature feature = change.getFeature();
    if (feature instanceof EReference && ((EReference) feature).isContainment()) {
        ITextRegionAccess access = owner.getTextRegionAccess();
        EObject oldValue = change.getReferenceValue();
        if (oldValue != null) {
            return access.regionForEObject(oldValue);
        }
        EObject value = (EObject) owner.getSemanticElement().eGet(feature);
        if (value != null) {
            return access.regionForEObject(value);
        }
        return null;
    } else {
        return owner.getRegionFor().feature(feature);
    }
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) EObject(org.eclipse.emf.ecore.EObject) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) EReference(org.eclipse.emf.ecore.EReference)

Example 9 with ITextRegionAccess

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

the class ReferenceUpdater method getRegion.

protected ISemanticRegion getRegion(ITextRegionAccess access, IReferenceSnapshot ref) {
    XtextResource resource = access.getResource();
    URI objectUri = ref.getSourceEObjectUri();
    if (!resource.getURI().equals(objectUri.trimFragment())) {
        return null;
    }
    EObject object = resource.getEObject(objectUri.fragment());
    if (object == null) {
        return null;
    }
    ISemanticRegionsFinder finder = access.getExtensions().regionFor(object);
    int index = ref.getIndexInList();
    if (index < 0) {
        return finder.feature(ref.getEReference());
    } else {
        List<ISemanticRegion> list = finder.features(ref.getEReference());
        if (list != null && index < list.size()) {
            return list.get(index);
        }
    }
    return null;
}
Also used : EObject(org.eclipse.emf.ecore.EObject) XtextResource(org.eclipse.xtext.resource.XtextResource) ISemanticRegion(org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion) URI(org.eclipse.emf.common.util.URI) ISemanticRegionsFinder(org.eclipse.xtext.formatting2.regionaccess.ISemanticRegionsFinder)

Example 10 with ITextRegionAccess

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

the class RegionDiffFormatter method createFormatterRequest.

protected FormatterRequest createFormatterRequest(ITextRegionAccess rewritten) {
    FormatterRequest request = formatterRequest.get();
    request.setTextRegionAccess(rewritten);
    return request;
}
Also used : FormatterRequest(org.eclipse.xtext.formatting2.FormatterRequest)

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