Repository navigation
Formatting inception - Extract blocks from a file, and apply different formatters on them than the rest of the file #412
Description
Activity
Which formatter are you using? Spotless has two js formatters, eclipseWtp and prettier.
Oh, I forgot to mention the main thing. It's Java, where the JS could be written inline. So the code looks like this:
native static public void consoleLog(String message) /*-{ console.log( message ); }-*/;Inside of
/*-{ }-*/is the JavaScript Code, if the methodconsoleLoggets called. But the method is of an java class. (Example)
I used spotless like described in the README of the gradle plugin. Here is my current configuration if you wanna look at it. I think that the js formatter would not format the these blocks, or am I wrong?Ahh, okay. So you want to format javascript within the comment blocks of java code. There's two main paths you can take. One is to file a feature request with one of our java formatters (either google-java-format or eclipse), and get them to add this feature. Once they had implemented the feature, you could use it with any of Spotless' plugins. That's out-of-scope for Spotless, you'll have to file an issue with the formatter library itself.
A more flexible way to do this is for Spotless to have the general ability to extract blocks from the text, and apply a chain of formatters to them, which could work like this:
spotless { java { eclipse() withinBlocks('gwtJavascript', '/*-{', '}-*/') { eclipseWtp('js') } } }
This would create a step called
gwtJavascript, and that step would find all the blocks in each file which match the starting/trailing strings, and then apply some "sub"-steps on their content and put that back in to the file.This is doable with the Spotless' model, but I suspect that it is more trouble than it is worth. My personal opinion on this is that the whole point of Spotless is that formatting doesn't matter, so let the tool pick the formatting and be done with it. But it is a general and flexible capability that is relatively easy to build and document. If anybody builds and documents this feature to high quality, I will be happy to merge it.Now implemented.- changed the title
[-]Feature Request: Format JSNI of GWT[/-][+]Extract blocks from a file, and apply different formatters on them than the rest of the file[/+]on Jun 26, 2019 I tried it with your extract block like this:
eclipse().configFile new File(rootProject.projectDir.absolutePath, 'eclipse-formatter.xml') withinBlocks('gwtJavascript', '/*-{', '}-*/') { eclipseWtp('js') }But I get this error:
> Could not find method withinBlocks() for arguments [gwtJavascript, /*-{, }-*/, build_3j47s52r53kdb08ni4u1z9tf4$_run_closure2$_closure11$_closure12$_closure15@43a e204c] on object of type com.diffplug.gradle.spotless.JavaExtension.Yep.
withinBlocksis not implemented - it would be a new feature.I don't plan on building it, I've just provided a spec that I would be happy to merge if someone wants to contribute it.Now implemented.#691 makes this issue relatively easy to implement now.
- changed the title
[-]Extract blocks from a file, and apply different formatters on them than the rest of the file[/-][+]Formatting inception - Extract blocks from a file, and apply different formatters on them than the rest of the file[/+]on Sep 14, 2020 Shipped in
plugin-gradle 5.6.1
The Google Web Toolkit provides with the JavaScript Native Interface an option to write inline JavaScript. The current formatter indents this native code on one level. It would be nice if it would be formatted like the normal JavaScript code or an extra option would also be fine.
You can find more information about the syntax here