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

Reformat Selection Support #1056

Open
ragurney opened this issue Dec 29, 2021 · 2 comments
Open

Reformat Selection Support #1056

ragurney opened this issue Dec 29, 2021 · 2 comments

Comments

@ragurney
Copy link

ragurney commented Dec 29, 2021

Hello,

Does Spotless support reformatting a text range rather than the whole file? Thinking about creating a plugin for an IDE many IDEs support reformatting both the entire file and a text selection, so having that parity with a spotless plugin would be great.

Thanks!

@nedtwigg
Copy link
Member

nedtwigg commented Dec 30, 2021

Does Spotless support reformatting a text range rather than the whole file?

No it does not. The Spotless model is that every formatter is just f(String) -> String, we describe the benefits of that here.

But it sorta does, in that we have a PipeStepPair whose purpose is to:

  • store the unformatted code in the first step of the pair
  • run the rest of the formatters
  • as a last step, recombine the original unformatted text with the formatted text

For now, that mechanism is used to support spotless:off / on tags (#691) and also to support formatting within blocks, e.g. formatting javascript within html, or java code examples within markdown (#696).

So you might be able to use a mechanism like that to accomplish your goals. You would need to:

  1. add a step which injects markers into the file which signifies the selection being formatted (e.g. adds a comment)
  2. add the store part of PipeStepPair
  3. run the other formatters
  4. add the read part of PipeStepPair
  5. find the markers which you injected in step 1, and use that to recombine the stored and formatted text

The failure mode is that a formatter might remove your markers, which is a difficult failure mode to explain.

@ragurney
Copy link
Author

This is great, thanks so much for the the response @nedtwigg! I'll do some more digging, and perhaps not support reformat by text selection in the first version of the plugin 😄

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