Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Formatting inception - Extract blocks from a file, and apply different formatters on them than the rest of the file #412

Closed
SimonIT opened this issue Jun 26, 2019 · 7 comments

Comments

@SimonIT
Copy link

SimonIT commented Jun 26, 2019

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

@nedtwigg
Copy link
Member

Which formatter are you using? Spotless has two js formatters, eclipseWtp and prettier.

@SimonIT
Copy link
Author

SimonIT commented Jun 26, 2019

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 method consoleLog gets 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?

@nedtwigg
Copy link
Member

nedtwigg commented Jun 26, 2019

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.

@nedtwigg nedtwigg 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 Jun 26, 2019
@SimonIT
Copy link
Author

SimonIT commented Jun 27, 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.

@nedtwigg
Copy link
Member

nedtwigg commented Jun 27, 2019

Yep. withinBlocks is 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.

@nedtwigg
Copy link
Member

#691 makes this issue relatively easy to implement now.

@nedtwigg nedtwigg 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 Sep 14, 2020
@nedtwigg
Copy link
Member

Shipped in plugin-gradle 5.6.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants