Skip to content

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

Description

@SimonIT

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

Activity

  1. nedtwigg commented on Jun 26, 2019

    @nedtwigg
    Member

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

  2. SimonIT commented on Jun 26, 2019

    @SimonIT
    Author

    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?

  3. nedtwigg commented on Jun 26, 2019

    @nedtwigg
    Member

    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.

  4. 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
  5. SimonIT commented on Jun 27, 2019

    @SimonIT
    Author

    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.

  6. nedtwigg commented on Jun 27, 2019

    @nedtwigg
    Member

    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.

  7. nedtwigg commented on Sep 10, 2020

    @nedtwigg
    Member

    #691 makes this issue relatively easy to implement now.

  8. 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
  9. nedtwigg commented on Sep 21, 2020

    @nedtwigg
    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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions