Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Is there a way to autoformat DSL files using the command line?
Is there a way to autoformat DSL files using the command line? [message #1854432] Thu, 18 August 2022 12:54 Go to next message
Simon Kluyskens is currently offline Simon KluyskensFriend
Messages: 2
Registered: August 2022
Junior Member
To keep consistent code formatting, I'd like to have a CI job which validates that all xText DSL files are correctly formatted.

For Java files I'm using "eclipsec.exe -application org.eclipse.jdt.core.JavaCodeFormatter -config org.eclipse.jdt.core.prefs ." and I'm validating that this command didn't change any source file.

Is there a similar command to apply formatting on all the DSL files?
Re: Is there a way to autoformat DSL files using the command line? [message #1854440 is a reply to message #1854432] Thu, 18 August 2022 15:29 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
you would need to implement a java main doing it yourself

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Is there a way to autoformat DSL files using the command line? [message #1854459 is a reply to message #1854440] Fri, 19 August 2022 09:47 Go to previous message
Simon Kluyskens is currently offline Simon KluyskensFriend
Messages: 2
Registered: August 2022
Junior Member
Ok, thanks! It worked.

In the end I used something like (having a model and a common DSL language):
ModelStandaloneSetup.doSetup();
CommonStandaloneSetup.doSetup();
Injector INJECTOR = new CommonStandaloneSetup().createInjectorAndDoEMFRegistration();
ResourceSet rs = INJECTOR.getInstance(XtextResourceSet.class);
URI uri = URI.createFileURI("path to dsl file");
Resource resource = rs.getResource(uri, true);
FormatterRequest formatterRequest = new FormatterRequest();
formatterRequest.setEnableDebugTracing(true);
((MapBasedPreferenceValues) formatterRequest.getPreferences()).put("indentation", "    ");
TextRegionAccessBuilder textRegionAccessBuilder = new TextRegionAccessBuilder();
ITextRegionAccess access = textRegionAccessBuilder.forNodeModel((XtextResource) resource).create();
formatterRequest.setTextRegionAccess(access);
List<ITextReplacement> replacements = formatter.format(formatterRequest);
String formatted = formatterRequest.getTextRegionAccess().getRewriter().renderToString(replacements);
Previous Topic:Standalone application load cross reference
Next Topic:Xtext 2.28.0.M3 is out
Goto Forum:
  


Current Time: Fri Apr 19 00:03:49 GMT 2024

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

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

Back to the top