Search in sources :

Example 41 with ITextRegionAccess

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

the class FixedContentFormatter method createTextEdit.

@Override
protected TextEdit createTextEdit(List<ITextReplacement> replacements) {
    if (replacements.isEmpty()) {
        return new ReplaceEdit(0, 0, "");
    }
    ITextRegionAccess regionAccess = replacements.get(0).getTextRegionAccess();
    String newDocument = regionAccess.getRewriter().renderToString(replacements);
    return new ReplaceEdit(0, regionAccess.regionForDocument().getLength(), newDocument);
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit)

Example 42 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 43 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 44 with ITextRegionAccess

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

the class PartialSerializer method findRegions.

protected List<IAstRegion> findRegions(IEObjectRegion owner, FeatureChange change) {
    EStructuralFeature feature = change.getFeature();
    if (feature instanceof EReference && ((EReference) feature).isContainment()) {
        ITextRegionAccess access = owner.getTextRegionAccess();
        Set<EObject> children = Sets.newHashSet();
        for (ListChange lc : change.getListChanges()) {
            children.addAll(lc.getReferenceValues());
        }
        for (Object obj : (List<?>) owner.getSemanticElement().eGet(feature)) {
            children.add((EObject) obj);
        }
        List<IEObjectRegion> result = Lists.newArrayList();
        for (EObject obj : children) {
            IEObjectRegion region = access.regionForEObject(obj);
            if (region != null) {
                result.add(region);
            }
        }
        Collections.sort(result, (a, b) -> a.getOffset() - b.getOffset());
        return ImmutableList.copyOf(result);
    } else {
        return ImmutableList.copyOf(owner.getRegionFor().features(feature));
    }
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) IEObjectRegion(org.eclipse.xtext.formatting2.regionaccess.IEObjectRegion) EObject(org.eclipse.emf.ecore.EObject) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) EObject(org.eclipse.emf.ecore.EObject) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) LinkedList(java.util.LinkedList) EList(org.eclipse.emf.common.util.EList) ListChange(org.eclipse.emf.ecore.change.ListChange) EReference(org.eclipse.emf.ecore.EReference)

Example 45 with ITextRegionAccess

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

the class RegionDiffFormatter method format.

public List<ITextReplacement> format(ITextRegionAccess regions) {
    IFormatter2 fmt = getFormatter();
    if (regions instanceof ITextRegionAccessDiff) {
        ITextRegionAccessDiff diff = (ITextRegionAccessDiff) regions;
        FormatterRequest request = createFormatterRequest(diff);
        List<ITextReplacement> replacements = fmt.format(request);
        List<ITextReplacement> merged = mergeReplacements(diff, replacements);
        return merged;
    } else {
        FormatterRequest request = createFormatterRequest(regions);
        List<ITextReplacement> replacements = fmt.format(request);
        return replacements;
    }
}
Also used : ITextRegionAccessDiff(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccessDiff) IFormatter2(org.eclipse.xtext.formatting2.IFormatter2) ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) 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