Search in sources :

Example 26 with ISemanticRegion

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

the class TextRegionAccessToString method toTokenAndGapList.

protected List<ITextSegment> toTokenAndGapList() {
    int range = this.highlightOrigin ? 4 : (Integer.MAX_VALUE / 2);
    ITextSegment first = null;
    ITextSegment current = origin;
    for (int i = 0; i < range && current != null; i++) {
        first = current;
        if (current instanceof ITextRegionAccess)
            current = ((ITextRegionAccess) current).regionForRootEObject().getPreviousHiddenRegion();
        else if (current instanceof ISequentialRegion)
            current = ((ISequentialRegion) current).getPreviousHiddenRegion();
        else if (current instanceof IHiddenRegionPart)
            current = ((IHiddenRegionPart) current).getHiddenRegion();
        else
            throw new IllegalStateException("Unexpected Type: " + current.getClass());
    }
    if (first == null)
        return Collections.emptyList();
    List<ITextSegment> result = new ArrayList<>();
    ITextSegment currentRegion = first;
    for (int i = 0; i <= (range * 2) && currentRegion != null; i++) {
        result.add(currentRegion);
        if (currentRegion instanceof ISemanticRegion)
            currentRegion = ((ISemanticRegion) currentRegion).getNextHiddenRegion();
        else if (currentRegion instanceof IHiddenRegion)
            currentRegion = ((IHiddenRegion) currentRegion).getNextSemanticRegion();
        else
            throw new IllegalStateException("Unexpected Type: " + currentRegion.getClass());
    }
    return result;
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) ISequentialRegion(org.eclipse.xtext.formatting2.regionaccess.ISequentialRegion) IHiddenRegionPart(org.eclipse.xtext.formatting2.regionaccess.IHiddenRegionPart) ArrayList(java.util.ArrayList) IHiddenRegion(org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment) ISemanticRegion(org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion)

Example 27 with ISemanticRegion

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

the class ReferenceUpdater method updateReference.

@Override
public void updateReference(ITextRegionDiffBuilder rewriter, IUpdatableReference upd) {
    IUpdatableReference updatable = upd;
    if (rewriter.isModified(updatable.getReferenceRegion())) {
        return;
    }
    IScope scope = scopeProvider.getScope(updatable.getSourceEObject(), updatable.getEReference());
    ISemanticRegion region = updatable.getReferenceRegion();
    QualifiedName oldName = nameConverter.toQualifiedName(region.getText());
    IEObjectDescription oldDesc = scope.getSingleElement(oldName);
    if (oldDesc != null && oldDesc.getEObjectOrProxy() == updatable.getTargetEObject()) {
        return;
    }
    String newName = findValidName(updatable, scope);
    if (newName != null) {
        rewriter.replace(region, newName);
    }
}
Also used : IUpdatableReference(org.eclipse.xtext.ide.serializer.hooks.IUpdatableReference) QualifiedName(org.eclipse.xtext.naming.QualifiedName) IScope(org.eclipse.xtext.scoping.IScope) ISemanticRegion(org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Example 28 with ISemanticRegion

use of org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion 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 29 with ISemanticRegion

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

the class TestLanguageFormatter method _format.

protected void _format(final TypeDeclaration type, @Extension final IFormattableDocument document) {
    final Procedure1<IHiddenRegionFormatter> _function = (IHiddenRegionFormatter it) -> {
        it.newLine();
    };
    document.append(this.textRegionExtensions.regionFor(type).keyword(this._testLanguageGrammarAccess.getTypeDeclarationAccess().getLeftCurlyBracketKeyword_3()), _function);
    final Procedure1<IHiddenRegionFormatter> _function_1 = (IHiddenRegionFormatter it) -> {
        it.newLine();
    };
    final Procedure1<IHiddenRegionFormatter> _function_2 = (IHiddenRegionFormatter it) -> {
        it.newLine();
    };
    document.append(document.prepend(this.textRegionExtensions.regionFor(type).keyword(this._testLanguageGrammarAccess.getTypeDeclarationAccess().getRightCurlyBracketKeyword_5()), _function_1), _function_2);
    final Procedure1<IHiddenRegionFormatter> _function_3 = (IHiddenRegionFormatter it) -> {
        it.indent();
    };
    document.<ISemanticRegion, ISemanticRegion>interior(this.textRegionExtensions.regionFor(type).keyword(this._testLanguageGrammarAccess.getTypeDeclarationAccess().getLeftCurlyBracketKeyword_3()), this.textRegionExtensions.regionFor(type).keyword(this._testLanguageGrammarAccess.getTypeDeclarationAccess().getRightCurlyBracketKeyword_5()), _function_3);
    Iterable<Property> _filter = Iterables.<Property>filter(type.getMembers(), Property.class);
    for (final Property property : _filter) {
        document.<Property>format(property);
    }
}
Also used : IHiddenRegionFormatter(org.eclipse.xtext.formatting2.IHiddenRegionFormatter) ISemanticRegion(org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion) Property(org.eclipse.xtext.ide.tests.testlanguage.testLanguage.Property)

Example 30 with ISemanticRegion

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

the class FormattableDocumentTest method autowrap.

@Test
public void autowrap() {
    final Procedure1<GenericFormatterTestRequest> _function = (GenericFormatterTestRequest it) -> {
        final Procedure1<MapBasedPreferenceValues> _function_1 = (MapBasedPreferenceValues it_1) -> {
            it_1.<Integer>put(FormatterPreferenceKeys.maxLineWidth, Integer.valueOf(10));
        };
        it.preferences(_function_1);
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("idlist  aaa  bbb  ccc  ddd  eee  fff");
        _builder.newLine();
        it.setToBeFormatted(_builder);
        final GenericFormatter<IDList> _function_2 = new GenericFormatter<IDList>() {

            @Override
            protected void format(final IDList model, @Extension final ITextRegionExtensions regions, @Extension final IFormattableDocument document) {
                final Consumer<ISemanticRegion> _function = (ISemanticRegion it_1) -> {
                    final Procedure1<IHiddenRegionFormatter> _function_1 = (IHiddenRegionFormatter it_2) -> {
                        it_2.autowrap();
                        it_2.oneSpace();
                    };
                    document.prepend(it_1, _function_1);
                };
                regions.regionFor(model).ruleCallsTo(FormattableDocumentTest.this._formatterTestLanguageGrammarAccess.getIDRule()).forEach(_function);
            }
        };
        it.setFormatter(_function_2);
        StringConcatenation _builder_1 = new StringConcatenation();
        _builder_1.append("idlist aaa");
        _builder_1.newLine();
        _builder_1.append("bbb ccc");
        _builder_1.newLine();
        _builder_1.append("ddd eee fff");
        _builder_1.newLine();
        it.setExpectation(_builder_1);
    };
    this._genericFormatterTester.assertFormatted(_function);
}
Also used : GenericFormatterTestRequest(org.eclipse.xtext.formatting2.internal.GenericFormatterTestRequest) IHiddenRegionFormatter(org.eclipse.xtext.formatting2.IHiddenRegionFormatter) IFormattableDocument(org.eclipse.xtext.formatting2.IFormattableDocument) MapBasedPreferenceValues(org.eclipse.xtext.preferences.MapBasedPreferenceValues) IDList(org.eclipse.xtext.formatting2.internal.formattertestlanguage.IDList) ITextRegionExtensions(org.eclipse.xtext.formatting2.regionaccess.ITextRegionExtensions) GenericFormatter(org.eclipse.xtext.formatting2.internal.GenericFormatter) Consumer(java.util.function.Consumer) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) ISemanticRegion(org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion) Test(org.junit.Test)

Aggregations

ISemanticRegion (org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion)59 Test (org.junit.Test)30 ISemanticRegionsFinder (org.eclipse.xtext.formatting2.regionaccess.ISemanticRegionsFinder)18 ITextRegionAccess (org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess)15 IHiddenRegionFormatter (org.eclipse.xtext.formatting2.IHiddenRegionFormatter)14 ITextRegionDiffBuilder (org.eclipse.xtext.formatting2.regionaccess.ITextRegionDiffBuilder)13 Mixed (org.eclipse.xtext.formatting2.regionaccess.internal.regionaccesstestlanguage.Mixed)12 Procedure1 (org.eclipse.xtext.xbase.lib.Procedures.Procedure1)9 IHiddenRegion (org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion)7 ITextRegionExtensions (org.eclipse.xtext.formatting2.regionaccess.ITextRegionExtensions)7 EObject (org.eclipse.emf.ecore.EObject)4 IEObjectRegion (org.eclipse.xtext.formatting2.regionaccess.IEObjectRegion)4 AssignedAction (org.eclipse.xtext.formatting2.regionaccess.internal.regionaccesstestlanguage.AssignedAction)4 XExpression (org.eclipse.xtext.xbase.XExpression)4 JvmTypeParameter (org.eclipse.xtext.common.types.JvmTypeParameter)3 JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)3 ValueList (org.eclipse.xtext.formatting2.regionaccess.internal.regionaccesstestlanguage.ValueList)3 LinkedList (java.util.LinkedList)2 EReference (org.eclipse.emf.ecore.EReference)2 XtendMember (org.eclipse.xtend.core.xtend.XtendMember)2