XtendFormatter configuration

59 views
Skip to first unread message

atao

unread,
Jul 28, 2015, 8:01:49 AM7/28/15
to Xtend Programming Language
Use of org.eclipse.xtend.core.formatting2.XtendFormatterPreferenceKeys values are hardcoded inside org.eclipse.xtend.core.formatting2.XtendFormatter.

Is there any XtendFormatter like class with configurable keys?


Moritz Eysholdt

unread,
Jul 28, 2015, 8:05:50 AM7/28/15
to xtend...@googlegroups.com
hi,

what are you trying to do?

regards,
  Moritz


On 28 Jul 2015, at 14:01, atao <p.m....@gmail.com> wrote:

Use of org.eclipse.xtend.core.formatting2.XtendFormatterPreferenceKeys values are hardcoded inside org.eclipse.xtend.core.formatting2.XtendFormatter.

Is there any XtendFormatter like class with configurable keys?



--
You received this message because you are subscribed to the Google Groups "Xtend Programming Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to xtend-lang+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

atao

unread,
Jul 28, 2015, 9:54:18 AM7/28/15
to Xtend Programming Language, mor...@eysholdt.de
A Java to Xtend batch converter.

org.eclipse.xtend.core.javaconverter.JavaConverter runs fine but with some issues like:

        source = '''
            package toto;
           
            public class Tata{
                public static void main(String[] args) {
                    System.out.println("Hello!");
                    String msg1 = "toto.tata";
                    String msg2 = msg1.replace("tata", "titi");
                    System.out.println(String.format("msg '
%s' ---> %s", msg1, msg2));
                   
                }
            }
        '''



which is converted to:

package toto
class Tata {
   
def static void main(String[] args) {
       
System.out.println("Hello!") var String msg1="toto.tata"
       
var String msg2=msg.replace("tata", "titi")
       
System.out.println(String.format("msg '%s' ---> %s", msg1, msg2))
   
}
   
}
Saisissez le code ici...

It seems to be a bug, but XtendFormatter corrects it. So it's OK as it is.

But at the end I'd like the same flexibility as under Eclipse, e.g. line after package,  space on each side of equals, ...

Regards

Moritz Eysholdt

unread,
Jul 28, 2015, 10:04:05 AM7/28/15
to xtend...@googlegroups.com
hi,

I see. You might want to implement something like 

org.eclipse.xtend.ide.javaconverter.ConvertJavaCode.formatXtendCode(IFile, String)

private String formatXtendCode(IFile xtendFile, final String xtendCode) {
try {
XtextResource resource = (XtextResource) createResource(xtendFile, xtendCode);
ITextRegionAccess regionAccess = regionAccessBuilder.get().forNodeModel(resource).create();
FormatterRequest request = new FormatterRequest();
request.setAllowIdentityEdits(false);
request.setTextRegionAccess(regionAccess);
request.setPreferences(TypedPreferenceValues.castOrWrap(cfgProvider.getPreferenceValues(resource)));
List<ITextReplacement> replacements = formatter.format(request);
String formatted = regionAccess.getRewriter().renderToString(replacements);
return formatted;
} catch (Exception e) {
LOG.error("Formatting step canceled due to an exception.", e);
return null;
}
}

The method takes a string, parses it as Xtend code, formats it, and spits out the formatted code again.

Using 
request.setPreferences(TypedPreferenceValues.castOrWrap(cfgProvider.getPreferenceValues(resource)));
you can set your own preferences for formatting. 


Just do something like

val prefs = org.eclipse.xtext.preferences.MapBasedPreferenceValues
prefs.put(org.eclipse.xtend.core.formatting2.XtendFormatterPreferenceKeys.SOME_KEY, my_value);
request.setPreferences(prefs);

cheers,
  Moritz

atao

unread,
Jul 28, 2015, 1:22:07 PM7/28/15
to Xtend Programming Language, mor...@eysholdt.de
Thank you. It works fine.

Regards

atao

unread,
Aug 11, 2015, 3:09:54 AM8/11/15
to Xtend Programming Language
Just for the fun, I put a Java to Xtend converter online here: http://j2xconverter-atao60.rhcloud.com

Sven Efftinge

unread,
Aug 11, 2015, 3:38:52 AM8/11/15
to xtend...@googlegroups.com
it says “service temporarily unavailable”

On 11 Aug 2015, at 09:09, atao <p.m....@gmail.com> wrote:

Just for the fun, I put a Java to Xtend converter online here: http://j2xconverter-atao60.rhcloud.com


atao

unread,
Aug 11, 2015, 3:59:16 AM8/11/15
to Xtend Programming Language, efft...@itemis.de
I'm still struggling with the auto build of Openshift...

The service is working ATM... at least until the next change I'll commit :-C
Reply all
Reply to author
Forward
0 new messages