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;
}
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);
}
}
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);
}
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);
}
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));
}
Aggregations