use of org.eclipse.xtext.xbase.lib.Procedures.Procedure1 in project xtext-xtend by eclipse.
the class CodeBuilderQuickfix method getXtendModification.
protected IModification getXtendModification(final ICodeBuilder.Xtend builder) {
final IModification _function = (IModificationContext it) -> {
final XtendTypeDeclaration xtendClass = builder.getXtendType();
final IEditorPart editor = this.editorOpener.open(EcoreUtil.getURI(xtendClass), false);
if ((!(editor instanceof XtextEditor))) {
return;
}
final XtextEditor xtextEditor = ((XtextEditor) editor);
final IXtextDocument document = xtextEditor.getDocument();
final Wrapper<Integer> wrapper = Wrapper.<Integer>forType(Integer.class);
final IUnitOfWork<ReplacingAppendable, XtextResource> _function_1 = (XtextResource resource) -> {
ReplacingAppendable _xblockexpression = null;
{
int offset = builder.getInsertOffset(resource);
wrapper.set(Integer.valueOf(offset));
final int typeIndentation = this.getTypeIndentation(resource, document, xtendClass);
DocumentSourceAppender.Factory.OptionalParameters _optionalParameters = new DocumentSourceAppender.Factory.OptionalParameters();
final Procedure1<DocumentSourceAppender.Factory.OptionalParameters> _function_2 = (DocumentSourceAppender.Factory.OptionalParameters it_1) -> {
int _indentationLevel = builder.getIndentationLevel();
int _plus = (_indentationLevel + typeIndentation);
it_1.baseIndentationLevel = _plus;
it_1.ensureEmptyLinesAround = true;
};
DocumentSourceAppender.Factory.OptionalParameters _doubleArrow = ObjectExtensions.<DocumentSourceAppender.Factory.OptionalParameters>operator_doubleArrow(_optionalParameters, _function_2);
_xblockexpression = this.appendableFactory.create(document, resource, offset, 0, _doubleArrow);
}
return _xblockexpression;
};
final ReplacingAppendable appendable = document.<ReplacingAppendable>readOnly(_function_1);
Integer offset = wrapper.get();
builder.build(appendable);
appendable.commitChanges();
xtextEditor.setHighlightRange(((offset).intValue() + 1), appendable.length(), true);
};
return _function;
}
use of org.eclipse.xtext.xbase.lib.Procedures.Procedure1 in project xtext-xtend by eclipse.
the class CodeBuilderQuickfix method getJavaModification.
protected IModification getJavaModification(final ICodeBuilder.Java builder) {
final IModification _function = (IModificationContext it) -> {
final IType type = builder.getIType();
char _charAt = ".".charAt(0);
final ImportManager importManager = new ImportManager(true, _charAt);
final StringBuilderBasedAppendable content = new StringBuilderBasedAppendable(importManager);
builder.build(content);
final Consumer<String> _function_1 = (String it_1) -> {
try {
ICompilationUnit _compilationUnit = type.getCompilationUnit();
NullProgressMonitor _nullProgressMonitor = new NullProgressMonitor();
_compilationUnit.createImport(it_1, null, _nullProgressMonitor);
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
};
importManager.getImports().forEach(_function_1);
Object _switchResult = null;
boolean _matched = false;
if (builder instanceof JavaFieldBuilder) {
_matched = true;
String _string = content.toString();
NullProgressMonitor _nullProgressMonitor = new NullProgressMonitor();
_switchResult = type.createField(_string, null, true, _nullProgressMonitor);
}
if (!_matched) {
if (builder instanceof JavaConstructorBuilder) {
_matched = true;
String _string = content.toString();
NullProgressMonitor _nullProgressMonitor = new NullProgressMonitor();
_switchResult = type.createMethod(_string, null, true, _nullProgressMonitor);
}
}
if (!_matched) {
if (builder instanceof JavaMethodBuilder) {
_matched = true;
String _string = content.toString();
NullProgressMonitor _nullProgressMonitor = new NullProgressMonitor();
_switchResult = type.createMethod(_string, null, true, _nullProgressMonitor);
}
}
if (!_matched) {
_switchResult = null;
}
final Object element = ((Object) _switchResult);
if ((element != null)) {
JdtHyperlink _jdtHyperlink = new JdtHyperlink();
final Procedure1<JdtHyperlink> _function_2 = (JdtHyperlink it_1) -> {
it_1.setJavaElement(((IMember) element));
it_1.open();
};
ObjectExtensions.<JdtHyperlink>operator_doubleArrow(_jdtHyperlink, _function_2);
}
};
return _function;
}
use of org.eclipse.xtext.xbase.lib.Procedures.Procedure1 in project xtext-xtend by eclipse.
the class AbstractReusableActiveAnnotationTests method testSetUpperBoundsForMutableTypeParameterDeclaration.
@Test
public void testSetUpperBoundsForMutableTypeParameterDeclaration() {
StringConcatenation _builder = new StringConcatenation();
_builder.append("package myannotation");
_builder.newLine();
_builder.newLine();
_builder.append("import java.util.List");
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.Active");
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.TransformationContext");
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.TransformationParticipant");
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.declaration.MutableTypeParameterDeclarator");
_builder.newLine();
_builder.newLine();
_builder.append("@Active(MyAnnotationProcessor)");
_builder.newLine();
_builder.append("annotation MyAnnotation {");
_builder.newLine();
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("class MyAnnotationProcessor implements TransformationParticipant<MutableTypeParameterDeclarator> {");
_builder.newLine();
_builder.newLine();
_builder.append("\t");
_builder.append("override doTransform(List<? extends MutableTypeParameterDeclarator> annotatedTargetElements,");
_builder.newLine();
_builder.append("\t\t");
_builder.append("extension TransformationContext context) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("for (annotatedTargetElement : annotatedTargetElements) {");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("doTransform(annotatedTargetElement, context)");
_builder.newLine();
_builder.append("\t\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("\t");
_builder.append("def doTransform(MutableTypeParameterDeclarator it, extension TransformationContext context) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("typeParameters.head.upperBounds = #[String.newTypeReference]");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("}");
_builder.newLine();
Pair<String, String> _mappedTo = Pair.<String, String>of("myannotation/MyAnnotation.xtend", _builder.toString());
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("package myusercode");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("import myannotation.MyAnnotation");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("@MyAnnotation");
_builder_1.newLine();
_builder_1.append("class MyClass<T extends CharSequence> {");
_builder_1.newLine();
_builder_1.append("}");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("@MyAnnotation");
_builder_1.newLine();
_builder_1.append("interface MyInterface<T extends CharSequence> {");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("@MyAnnotation");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("abstract def <T extends CharSequence> void foo();");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("}");
_builder_1.newLine();
Pair<String, String> _mappedTo_1 = Pair.<String, String>of("myusercode/UserCode.xtend", _builder_1.toString());
final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
final Procedure1<MutableTypeParameterDeclarator> _function_1 = (MutableTypeParameterDeclarator it_1) -> {
Assert.assertEquals(1, IterableExtensions.size(it_1.getTypeParameters()));
final MutableTypeParameterDeclaration typeParameter = IterableExtensions.head(it_1.getTypeParameters());
Assert.assertEquals(1, IterableExtensions.size(typeParameter.getUpperBounds()));
Assert.assertEquals("java.lang.String", IterableExtensions.head(typeParameter.getUpperBounds()).getName());
};
final Procedure1<? super MutableTypeParameterDeclarator> assertMyAnnotationChanges = _function_1;
assertMyAnnotationChanges.apply(it.getTypeLookup().findClass("myusercode.MyClass"));
final MutableInterfaceDeclaration myInterface = it.getTypeLookup().findInterface("myusercode.MyInterface");
assertMyAnnotationChanges.apply(myInterface);
assertMyAnnotationChanges.apply(IterableExtensions.head(myInterface.getDeclaredMethods()));
};
this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
use of org.eclipse.xtext.xbase.lib.Procedures.Procedure1 in project xtext-xtend by eclipse.
the class AbstractReusableActiveAnnotationTests method testRemoveTypeParameters.
@Test
public void testRemoveTypeParameters() {
StringConcatenation _builder = new StringConcatenation();
_builder.append("package myannotation");
_builder.newLine();
_builder.newLine();
_builder.append("import java.util.List");
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.Active");
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.TransformationContext");
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.TransformationParticipant");
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.declaration.MutableTypeParameterDeclarator");
_builder.newLine();
_builder.newLine();
_builder.append("@Active(MyAnnotationProcessor)");
_builder.newLine();
_builder.append("annotation MyAnnotation {");
_builder.newLine();
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("class MyAnnotationProcessor implements TransformationParticipant<MutableTypeParameterDeclarator> {");
_builder.newLine();
_builder.newLine();
_builder.append("\t");
_builder.append("override doTransform(List<? extends MutableTypeParameterDeclarator> annotatedTargetElements,");
_builder.newLine();
_builder.append("\t\t");
_builder.append("extension TransformationContext context) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("for (annotatedTargetElement : annotatedTargetElements) {");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("doTransform(annotatedTargetElement, context)");
_builder.newLine();
_builder.append("\t\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("\t");
_builder.append("def doTransform(MutableTypeParameterDeclarator it, extension TransformationContext context) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("typeParameters.head.remove");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("}");
_builder.newLine();
Pair<String, String> _mappedTo = Pair.<String, String>of("myannotation/MyAnnotation.xtend", _builder.toString());
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("package myusercode");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("import myannotation.MyAnnotation");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("@MyAnnotation");
_builder_1.newLine();
_builder_1.append("class MyClass<T extends String> {");
_builder_1.newLine();
_builder_1.append("}");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("@MyAnnotation");
_builder_1.newLine();
_builder_1.append("interface MyInterface<T extends String> {");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("@MyAnnotation");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("abstract def <T extends String> void foo();");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("}");
_builder_1.newLine();
Pair<String, String> _mappedTo_1 = Pair.<String, String>of("myusercode/UserCode.xtend", _builder_1.toString());
final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
final Procedure1<MutableTypeParameterDeclarator> _function_1 = (MutableTypeParameterDeclarator it_1) -> {
Assert.assertEquals(0, IterableExtensions.size(it_1.getTypeParameters()));
};
final Procedure1<? super MutableTypeParameterDeclarator> assertMyAnnotationChanges = _function_1;
assertMyAnnotationChanges.apply(it.getTypeLookup().findClass("myusercode.MyClass"));
final MutableInterfaceDeclaration myInterface = it.getTypeLookup().findInterface("myusercode.MyInterface");
assertMyAnnotationChanges.apply(myInterface);
assertMyAnnotationChanges.apply(IterableExtensions.head(myInterface.getDeclaredMethods()));
};
this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
use of org.eclipse.xtext.xbase.lib.Procedures.Procedure1 in project xtext-xtend by eclipse.
the class AbstractReusableActiveAnnotationTests method testModifyTypeParameters.
@Test
public void testModifyTypeParameters() {
StringConcatenation _builder = new StringConcatenation();
_builder.append("package myannotation");
_builder.newLine();
_builder.newLine();
_builder.append("import java.util.List");
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.Active");
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.TransformationContext");
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.TransformationParticipant");
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.declaration.MutableTypeParameterDeclarator");
_builder.newLine();
_builder.newLine();
_builder.append("@Active(MyAnnotationProcessor)");
_builder.newLine();
_builder.append("annotation MyAnnotation {");
_builder.newLine();
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("class MyAnnotationProcessor implements TransformationParticipant<MutableTypeParameterDeclarator> {");
_builder.newLine();
_builder.newLine();
_builder.append("\t");
_builder.append("override doTransform(List<? extends MutableTypeParameterDeclarator> annotatedTargetElements,");
_builder.newLine();
_builder.append("\t\t");
_builder.append("extension TransformationContext context) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("for (annotatedTargetElement : annotatedTargetElements) {");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("doTransform(annotatedTargetElement, context)");
_builder.newLine();
_builder.append("\t\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("\t");
_builder.append("def doTransform(MutableTypeParameterDeclarator it, extension TransformationContext context) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("if (typeParameters.size != 0) {");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("throw new IllegalStateException(\"Before: typeParameters.size != 0\")");
_builder.newLine();
_builder.append("\t\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t\t");
_builder.append("addTypeParameter(\"T\", String.newTypeReference)");
_builder.newLine();
_builder.append("\t\t");
_builder.append("if (typeParameters.size != 1) {");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("throw new IllegalStateException(\"After: typeParameters.size != 1\")");
_builder.newLine();
_builder.append("\t\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val typeParameter = typeParameters.head");
_builder.newLine();
_builder.append("\t\t");
_builder.append("if (!typeParameter.simpleName.equals(\"T\")) {");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("throw new IllegalStateException(\"After: expected type name: \'T\', actual type name: \'\" + typeParameter.simpleName + \"\'\")");
_builder.newLine();
_builder.append("\t\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("}");
_builder.newLine();
Pair<String, String> _mappedTo = Pair.<String, String>of("myannotation/MyAnnotation.xtend", _builder.toString());
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("package myusercode");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("import myannotation.MyAnnotation");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("@MyAnnotation");
_builder_1.newLine();
_builder_1.append("class MyClass {");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("@MyAnnotation");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("new() {");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("}");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("}");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("@MyAnnotation");
_builder_1.newLine();
_builder_1.append("interface MyInterface {");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("@MyAnnotation");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("abstract def void foo();");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("}");
_builder_1.newLine();
Pair<String, String> _mappedTo_1 = Pair.<String, String>of("myusercode/UserCode.xtend", _builder_1.toString());
final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
final Procedure1<MutableTypeParameterDeclarator> _function_1 = (MutableTypeParameterDeclarator it_1) -> {
Assert.assertEquals(1, IterableExtensions.size(it_1.getTypeParameters()));
final MutableTypeParameterDeclaration typeParameter = IterableExtensions.head(it_1.getTypeParameters());
Assert.assertEquals("T", typeParameter.getSimpleName());
Assert.assertEquals(1, IterableExtensions.size(typeParameter.getUpperBounds()));
Assert.assertEquals("java.lang.String", IterableExtensions.head(typeParameter.getUpperBounds()).getName());
};
final Procedure1<? super MutableTypeParameterDeclarator> assertMyAnnotationChanges = _function_1;
final MutableClassDeclaration clazz = it.getTypeLookup().findClass("myusercode.MyClass");
assertMyAnnotationChanges.apply(clazz);
assertMyAnnotationChanges.apply(IterableExtensions.head(clazz.getDeclaredConstructors()));
final MutableInterfaceDeclaration myInterface = it.getTypeLookup().findInterface("myusercode.MyInterface");
assertMyAnnotationChanges.apply(myInterface);
assertMyAnnotationChanges.apply(IterableExtensions.head(myInterface.getDeclaredMethods()));
};
this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
Aggregations