Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Extend xtend with more extension methods
Extend xtend with more extension methods [message #922052] Mon, 24 September 2012 13:34 Go to next message
Eclipse UserFriend
Hi,

I'm generating code with xtend2 and wonder if I could make the code
nicer by defining some extra operator_doubleArrow method, so the =>
operator can be used with an ITreeAppendable on the left hand side.

E.g. instead of append("something") newLine // it is an ITreeAppendable
I could write it => "something" => '\n'

Is it possible to add such extra methods without rebuilding xtend2? E.g.
by defining an extension class in your xtext project and using some
extension point, thus extending xtend?

Hallvard
Re: Extend xtend with more extension methods [message #922235 is a reply to message #922052] Mon, 24 September 2012 17:23 Go to previous messageGo to next message
Eclipse UserFriend
You can define a class with a method
operator_doubleArrow(ITreeAppendable, CharSequence) and use that one as
an extension, e.g.

extension MyTreeAppendableUtil

That should do the trick. No recompilation, no altering of the language,
just a library / an API in your project

Regards,
Sebastian
--
Looking for professional support for Xtext, Xtend or Eclipse Modeling?
Go visit: http://xtext.itemis.com

Am 24.09.12 19:34, schrieb Hallvard Trætteberg:
> Hi,
>
> I'm generating code with xtend2 and wonder if I could make the code
> nicer by defining some extra operator_doubleArrow method, so the =>
> operator can be used with an ITreeAppendable on the left hand side.
>
> E.g. instead of append("something") newLine // it is an ITreeAppendable
> I could write it => "something" => '\n'
>
> Is it possible to add such extra methods without rebuilding xtend2? E.g.
> by defining an extension class in your xtext project and using some
> extension point, thus extending xtend?
>
> Hallvard
Re: Extend xtend with more extension methods [message #923018 is a reply to message #922235] Tue, 25 September 2012 10:42 Go to previous messageGo to next message
Eclipse UserFriend
I suggest to use the left-shift operator, though:

out << "something" << '\n'


This is used by most other languages and shows the intent better (if you forget for a moment that it also means "shift") since it "points" the data into the right direction.

[Updated on: Tue, 25 September 2012 10:43] by Moderator

Re: Extend xtend with more extension methods [message #925330 is a reply to message #923018] Thu, 27 September 2012 12:10 Go to previous messageGo to next message
Eclipse UserFriend
On 25.09.12 07.42, Aaron Digulla wrote:
> I suggest to use the left-shift operator, though:
>
> out << "something" << '\n'
>
> is used my most other languages and shows the intent better (if you
> forget for a moment that it also means "shift") since it "points" the
> data into the right direction.

Yes, I came to the same conclusion after looking at the available
operators. I now use the following extension class. Note how the injects
allow serialization of expressions and types.

BTW, I also tried to use operators (extension methods) for higher order
functions like filter, map and reduce, but it seemed the type inference
had problems with the Procedure sublasses and their extensive use of
type parameters.

Hallvard

----8<-----------------------------

package org.ptolemy.xtext.generator;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.common.types.JvmTypeReference;
import org.eclipse.xtext.xbase.XExpression;
import org.eclipse.xtext.xbase.compiler.TypeReferenceSerializer;
import org.eclipse.xtext.xbase.compiler.XbaseCompiler;
import org.eclipse.xtext.xbase.compiler.output.ITreeAppendable;
import org.eclipse.xtext.xbase.lib.Inline;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.Pair;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.Functions.Function2;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.eclipse.xtext.xbase.lib.internal.BooleanFunctionDelegate;
import org.eclipse.xtext.xbase.lib.internal.FunctionDelegate;

import com.google.common.collect.Iterables;
import com.google.inject.Inject;

public class TreeAppendableUtil {

@Inject private XbaseCompiler xbaseCompiler;
@Inject private TypeReferenceSerializer typeReferenceSerializer;

public ITreeAppendable operator_doubleLessThan(ITreeAppendable
appendable, CharSequence content) {
if (content.length() > 0) {
preAppend(appendable, content.charAt(0));
}
appendable.append(content);
if (content.length() > 0) {
postAppend(appendable, content.charAt(content.length() - 1));
}
return appendable;
}
public Pair<ITreeAppendable,? extends EObject>
operator_doubleLessThan(Pair<ITreeAppendable,? extends EObject>
appendable, CharSequence content) {
operator_doubleLessThan(appendable.getKey(), content);
return appendable;
}

public ITreeAppendable preAppend(ITreeAppendable appendable, char c) {
switch (c) {
case '}': {
appendable.decreaseIndentation(); appendable.newLine(); break;
}
}
return appendable;
}

public ITreeAppendable postAppend(ITreeAppendable appendable, char c) {
switch (c) {
case '\n': {
appendable.newLine(); break;
}
case '{': {
appendable.increaseIndentation(); appendable.newLine(); break;
}
case ';': {
appendable.newLine(); break;
}
case '}': {
appendable.newLine(); break;
}
}
return appendable;
}

public ITreeAppendable operator_doubleLessThan(ITreeAppendable
appendable, char c) {
preAppend(appendable, c);
appendable.append(String.valueOf(c));
postAppend(appendable, c);
return appendable;
}
public Pair<ITreeAppendable,? extends EObject>
operator_doubleLessThan(Pair<ITreeAppendable,? extends EObject>
appendable, char c) {
operator_doubleLessThan(appendable.getKey(), c);
return appendable;
}

public ITreeAppendable operator_doubleLessThan(ITreeAppendable
appendable, XExpression expression) {
xbaseCompiler.toJavaExpression(expression, appendable);
return appendable;
}
public Pair<ITreeAppendable,? extends EObject>
operator_doubleLessThan(Pair<ITreeAppendable,? extends EObject>
appendable, XExpression expression) {
operator_doubleLessThan(appendable.getKey(), expression);
return appendable;
}

public Pair<ITreeAppendable,? extends EObject>
operator_doubleLessThan(Pair<ITreeAppendable,? extends EObject>
appendable, JvmTypeReference type) {
typeReferenceSerializer.serialize(type, appendable.getValue(),
appendable.getKey());
return appendable;
}
}
Re: Extend xtend with more extension methods [message #927674 is a reply to message #925330] Sat, 29 September 2012 19:19 Go to previous message
Eclipse UserFriend
Hi Hallvard,

please file tickets with your findings about Procedures, their type
arguments and the type inferrence.

Thanks,
Sebastian
--
Looking for professional support for Xtext, Xtend or Eclipse Modeling?
Go visit: http://xtext.itemis.com

Am 27.09.12 09:12, schrieb Hallvard Trætteberg:
> On 25.09.12 07.42, Aaron Digulla wrote:
>> I suggest to use the left-shift operator, though:
>>
>> out << "something" << '\n'
>>
>> is used my most other languages and shows the intent better (if you
>> forget for a moment that it also means "shift") since it "points" the
>> data into the right direction.
>
> Yes, I came to the same conclusion after looking at the available
> operators. I now use the following extension class. Note how the injects
> allow serialization of expressions and types.
>
> BTW, I also tried to use operators (extension methods) for higher order
> functions like filter, map and reduce, but it seemed the type inference
> had problems with the Procedure sublasses and their extensive use of
> type parameters.
>
> Hallvard
>
> ----8<-----------------------------
>
> package org.ptolemy.xtext.generator;
>
> import org.eclipse.emf.ecore.EObject;
> import org.eclipse.xtext.common.types.JvmTypeReference;
> import org.eclipse.xtext.xbase.XExpression;
> import org.eclipse.xtext.xbase.compiler.TypeReferenceSerializer;
> import org.eclipse.xtext.xbase.compiler.XbaseCompiler;
> import org.eclipse.xtext.xbase.compiler.output.ITreeAppendable;
> import org.eclipse.xtext.xbase.lib.Inline;
> import org.eclipse.xtext.xbase.lib.IterableExtensions;
> import org.eclipse.xtext.xbase.lib.Pair;
> import org.eclipse.xtext.xbase.lib.Functions.Function1;
> import org.eclipse.xtext.xbase.lib.Functions.Function2;
> import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
> import org.eclipse.xtext.xbase.lib.internal.BooleanFunctionDelegate;
> import org.eclipse.xtext.xbase.lib.internal.FunctionDelegate;
>
> import com.google.common.collect.Iterables;
> import com.google.inject.Inject;
>
> public class TreeAppendableUtil {
>
> @Inject private XbaseCompiler xbaseCompiler;
> @Inject private TypeReferenceSerializer typeReferenceSerializer;
>
> public ITreeAppendable operator_doubleLessThan(ITreeAppendable
> appendable, CharSequence content) {
> if (content.length() > 0) {
> preAppend(appendable, content.charAt(0));
> }
> appendable.append(content);
> if (content.length() > 0) {
> postAppend(appendable, content.charAt(content.length() - 1));
> }
> return appendable;
> }
> public Pair<ITreeAppendable,? extends EObject>
> operator_doubleLessThan(Pair<ITreeAppendable,? extends EObject>
> appendable, CharSequence content) {
> operator_doubleLessThan(appendable.getKey(), content);
> return appendable;
> }
>
> public ITreeAppendable preAppend(ITreeAppendable appendable, char c) {
> switch (c) {
> case '}': {
> appendable.decreaseIndentation(); appendable.newLine();
> break;
> }
> }
> return appendable;
> }
>
> public ITreeAppendable postAppend(ITreeAppendable appendable, char
> c) {
> switch (c) {
> case '\n': {
> appendable.newLine(); break;
> }
> case '{': {
> appendable.increaseIndentation(); appendable.newLine();
> break;
> }
> case ';': {
> appendable.newLine(); break;
> }
> case '}': {
> appendable.newLine(); break;
> }
> }
> return appendable;
> }
>
> public ITreeAppendable operator_doubleLessThan(ITreeAppendable
> appendable, char c) {
> preAppend(appendable, c);
> appendable.append(String.valueOf(c));
> postAppend(appendable, c);
> return appendable;
> }
> public Pair<ITreeAppendable,? extends EObject>
> operator_doubleLessThan(Pair<ITreeAppendable,? extends EObject>
> appendable, char c) {
> operator_doubleLessThan(appendable.getKey(), c);
> return appendable;
> }
>
> public ITreeAppendable operator_doubleLessThan(ITreeAppendable
> appendable, XExpression expression) {
> xbaseCompiler.toJavaExpression(expression, appendable);
> return appendable;
> }
> public Pair<ITreeAppendable,? extends EObject>
> operator_doubleLessThan(Pair<ITreeAppendable,? extends EObject>
> appendable, XExpression expression) {
> operator_doubleLessThan(appendable.getKey(), expression);
> return appendable;
> }
>
> public Pair<ITreeAppendable,? extends EObject>
> operator_doubleLessThan(Pair<ITreeAppendable,? extends EObject>
> appendable, JvmTypeReference type) {
> typeReferenceSerializer.serialize(type, appendable.getValue(),
> appendable.getKey());
> return appendable;
> }
> }
>
Previous Topic:multiple DSLs, one Xtext project
Next Topic:parsed CompleteOCL file doesn't resolve cross reference to the ecore referenced file
Goto Forum:
  


Current Time: Sun Jul 06 23:22:14 EDT 2025

Powered by FUDForum. Page generated in 0.27198 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top