Search in sources :

Example 81 with XtextResource

use of org.eclipse.xtext.resource.XtextResource in project xtext-xtend by eclipse.

the class ConstantExpressionsInterpreter method findVisibleFeatures.

/**
 * looks up the static final fields which are accessible in unqualified form for the given expression.
 * That essentially includes static imports and the fields declared in the containing types
 */
protected Map<String, JvmIdentifiableElement> findVisibleFeatures(final XExpression expression) {
    HashMap<String, JvmIdentifiableElement> _xblockexpression = null;
    {
        Resource _eResource = expression.eResource();
        final Resource res = _eResource;
        boolean _matched = false;
        if (res instanceof StorageAwareResource) {
            boolean _isLoadedFromStorage = ((StorageAwareResource) res).isLoadedFromStorage();
            if (_isLoadedFromStorage) {
                _matched = true;
                return CollectionLiterals.<String, JvmIdentifiableElement>newHashMap();
            }
        }
        JvmDeclaredType _switchResult_1 = null;
        JvmIdentifiableElement _nearestLogicalContainer = this.containerProvider.getNearestLogicalContainer(expression);
        final JvmIdentifiableElement cont = _nearestLogicalContainer;
        boolean _matched_1 = false;
        if (cont instanceof JvmGenericType) {
            _matched_1 = true;
            _switchResult_1 = ((JvmGenericType) cont);
        }
        if (!_matched_1) {
            if (cont instanceof JvmMember) {
                _matched_1 = true;
                _switchResult_1 = ((JvmMember) cont).getDeclaringType();
            }
        }
        final JvmDeclaredType container = _switchResult_1;
        Pair<String, JvmDeclaredType> _mappedTo = Pair.<String, JvmDeclaredType>of("visibleFeaturesForAnnotationValues", container);
        final Provider<HashMap<String, JvmIdentifiableElement>> _function = () -> {
            final HashMap<String, JvmIdentifiableElement> result = CollectionLiterals.<String, JvmIdentifiableElement>newHashMap();
            Resource _eResource_1 = expression.eResource();
            final XImportSection section = this.importSectionLocator.getImportSection(((XtextResource) _eResource_1));
            if ((section != null)) {
                EList<XImportDeclaration> _importDeclarations = section.getImportDeclarations();
                for (final XImportDeclaration imp : _importDeclarations) {
                    boolean _isStatic = imp.isStatic();
                    if (_isStatic) {
                        final String importedTypeName = imp.getImportedTypeName();
                        if ((importedTypeName != null)) {
                            final JvmType type = this.findTypeByName(imp, importedTypeName);
                            boolean _matched_2 = false;
                            if (type instanceof JvmGenericType) {
                                _matched_2 = true;
                                this.collectAllVisibleFields(((JvmDeclaredType) type), result);
                            }
                            if (!_matched_2) {
                                if (type instanceof JvmEnumerationType) {
                                    _matched_2 = true;
                                    EList<JvmEnumerationLiteral> _literals = ((JvmEnumerationType) type).getLiterals();
                                    for (final JvmEnumerationLiteral feature : _literals) {
                                        result.put(feature.getSimpleName(), feature);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            this.collectAllVisibleFields(container, result);
            return result;
        };
        _xblockexpression = this.cache.<HashMap<String, JvmIdentifiableElement>>get(_mappedTo, expression.eResource(), _function);
    }
    return _xblockexpression;
}
Also used : JvmIdentifiableElement(org.eclipse.xtext.common.types.JvmIdentifiableElement) XImportSection(org.eclipse.xtext.xtype.XImportSection) HashMap(java.util.HashMap) Resource(org.eclipse.emf.ecore.resource.Resource) StorageAwareResource(org.eclipse.xtext.resource.persistence.StorageAwareResource) XtextResource(org.eclipse.xtext.resource.XtextResource) TypeResource(org.eclipse.xtext.common.types.access.TypeResource) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) XtextResource(org.eclipse.xtext.resource.XtextResource) StorageAwareResource(org.eclipse.xtext.resource.persistence.StorageAwareResource) JvmType(org.eclipse.xtext.common.types.JvmType) XImportDeclaration(org.eclipse.xtext.xtype.XImportDeclaration) ILogicalContainerProvider(org.eclipse.xtext.xbase.jvmmodel.ILogicalContainerProvider) Provider(com.google.inject.Provider) IScopeProvider(org.eclipse.xtext.scoping.IScopeProvider) JvmEnumerationLiteral(org.eclipse.xtext.common.types.JvmEnumerationLiteral) JvmMember(org.eclipse.xtext.common.types.JvmMember) JvmEnumerationType(org.eclipse.xtext.common.types.JvmEnumerationType) Pair(org.eclipse.xtext.xbase.lib.Pair) XAnnotationElementValuePair(org.eclipse.xtext.xbase.annotations.xAnnotations.XAnnotationElementValuePair)

Example 82 with XtextResource

use of org.eclipse.xtext.resource.XtextResource in project xtext-xtend by eclipse.

the class JvmElementAtOffsetHelperTest method testdeclaration.

@Test
public void testdeclaration() {
    try {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("package testPackage");
        _builder.newLine();
        _builder.append("class TestCase {");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("def fo|o(String a){");
        _builder.newLine();
        _builder.append("\t\t");
        _builder.append("bar(a)");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("}");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("def bar(String a){}");
        _builder.newLine();
        _builder.append("}");
        _builder.newLine();
        final String content = _builder.toString();
        final XtendFile file = this.file(content.replace("|", ""));
        Resource _eResource = file.eResource();
        JvmIdentifiableElement jvmIdentifiableElement = this.jvmElementAtOffsetHelper.getJvmIdentifiableElement(((XtextResource) _eResource), content.indexOf("|"));
        Assert.assertTrue((jvmIdentifiableElement instanceof JvmOperation));
        Assert.assertEquals("testPackage.TestCase.foo", jvmIdentifiableElement.getQualifiedName());
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : XtendFile(org.eclipse.xtend.core.xtend.XtendFile) JvmOperation(org.eclipse.xtext.common.types.JvmOperation) JvmIdentifiableElement(org.eclipse.xtext.common.types.JvmIdentifiableElement) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) XtextResource(org.eclipse.xtext.resource.XtextResource) Test(org.junit.Test)

Example 83 with XtextResource

use of org.eclipse.xtext.resource.XtextResource in project xtext-xtend by eclipse.

the class DocumentRewriterTest method testInsertText.

@Test
public void testInsertText() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("class Foo {");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def foo() {}");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def bar() {}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    final String model = _builder.toString();
    final Procedure2<DocumentRewriter, XtextResource> _function = (DocumentRewriter it, XtextResource r) -> {
        it.newSection(model.indexOf("class"), 0).append("abstract ");
        it.newSection(model.indexOf("foo"), 0).append("static ");
        it.newSection(model.indexOf("bar"), 0).append("private int ");
        it.newSection(model.lastIndexOf("{}"), 2).append("{ 42 }");
    };
    String _rewrite = this.rewrite(model, _function);
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("abstract class Foo {");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("def static foo() {}");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("def private int bar() { 42 }");
    _builder_1.newLine();
    _builder_1.append("}");
    _builder_1.newLine();
    this.andExpect(_rewrite, _builder_1);
}
Also used : DocumentRewriter(org.eclipse.xtext.xbase.ui.document.DocumentRewriter) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) XtextResource(org.eclipse.xtext.resource.XtextResource) Test(org.junit.Test)

Example 84 with XtextResource

use of org.eclipse.xtext.resource.XtextResource in project xtext-xtend by eclipse.

the class DocumentRewriterTest method testInsertExistingType.

@Test
public void testInsertExistingType() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("import java.io.Serializable");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class Foo implements Serializable {");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def foo() {}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    final String model = _builder.toString();
    final Procedure2<DocumentRewriter, XtextResource> _function = (DocumentRewriter it, XtextResource r) -> {
        final DocumentRewriter.Section beforeFoo = it.newSection(model.indexOf("foo"), 0);
        beforeFoo.append(this.services.getTypeReferences().findDeclaredType(Serializable.class, r));
        beforeFoo.append(" ");
    };
    String _rewrite = this.rewrite(model, _function);
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("import java.io.Serializable");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("class Foo implements Serializable {");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("def Serializable foo() {}");
    _builder_1.newLine();
    _builder_1.append("}");
    _builder_1.newLine();
    this.andExpect(_rewrite, _builder_1);
}
Also used : DocumentRewriter(org.eclipse.xtext.xbase.ui.document.DocumentRewriter) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) XtextResource(org.eclipse.xtext.resource.XtextResource) Test(org.junit.Test)

Example 85 with XtextResource

use of org.eclipse.xtext.resource.XtextResource in project xtext-xtend by eclipse.

the class DocumentRewriterTest method testWhitespaces.

@Test
public void testWhitespaces() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("class Foo {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def foo() {}");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def bar() {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    final String model = Strings.toUnixLineSeparator(_builder);
    final Procedure2<DocumentRewriter, XtextResource> _function = (DocumentRewriter it, XtextResource r) -> {
        int _indexOf = model.indexOf("{}");
        int _plus = (_indexOf + 3);
        it.newSection(_plus, 0, true).append("val x = 42");
        int _lastIndexOf = model.lastIndexOf("{\n");
        int _plus_1 = (_lastIndexOf + 1);
        it.newSection(_plus_1, 0, 2, true).append("val y = 43");
        int _indexOf_1 = model.indexOf("\t}");
        int _plus_2 = (_indexOf_1 + 2);
        it.newSection(_plus_2, 0, true).append("val z = 44");
    };
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("class Foo {");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("def foo() {}");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("val x = 42");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("def bar() {");
    _builder_1.newLine();
    _builder_1.append("\t\t");
    _builder_1.newLine();
    _builder_1.append("\t\t");
    _builder_1.append("val y = 43");
    _builder_1.newLine();
    _builder_1.append("\t\t");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("val z = 44");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.newLine();
    _builder_1.append("}");
    _builder_1.newLine();
    this.andExpect(this.rewrite(model, _function), Strings.toUnixLineSeparator(_builder_1));
}
Also used : DocumentRewriter(org.eclipse.xtext.xbase.ui.document.DocumentRewriter) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) XtextResource(org.eclipse.xtext.resource.XtextResource) Test(org.junit.Test)

Aggregations

XtextResource (org.eclipse.xtext.resource.XtextResource)627 Test (org.junit.Test)367 Resource (org.eclipse.emf.ecore.resource.Resource)107 EObject (org.eclipse.emf.ecore.EObject)99 XtextResourceSet (org.eclipse.xtext.resource.XtextResourceSet)67 StringInputStream (org.eclipse.xtext.util.StringInputStream)67 URI (org.eclipse.emf.common.util.URI)62 Diagnostic (org.eclipse.emf.common.util.Diagnostic)55 IXtextDocument (org.eclipse.xtext.ui.editor.model.IXtextDocument)55 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)46 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)40 Grammar (org.eclipse.xtext.Grammar)32 IUnitOfWork (org.eclipse.xtext.util.concurrent.IUnitOfWork)31 XtextEditor (org.eclipse.xtext.ui.editor.XtextEditor)30 IFile (org.eclipse.core.resources.IFile)29 Issue (org.eclipse.xtext.validation.Issue)29 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)28 List (java.util.List)26 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)26 INode (org.eclipse.xtext.nodemodel.INode)23