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

how to indent multi-line XML #319

Closed
Avinash-Bhat opened this issue Nov 27, 2018 · 7 comments
Closed

how to indent multi-line XML #319

Avinash-Bhat opened this issue Nov 27, 2018 · 7 comments

Comments

@Avinash-Bhat
Copy link

summary of problem

Consider the following unformatted xml snippet below:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="my.app">
...

when running with a configuration of:

spotless {
    ...
    xml {
        target '**/*.xml'
        trimTrailingWhitespace()
        endWithNewline()
    }
    ...

the xml gets formatted to:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="my.app">
...

how ever I want it to indent the multi-line xml to add an additional indent (i.e., here package should start as the 9th character instead of being the 5th)

i.e., like this:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="my.app">

Is is possible to get something like this with the current state of spotless? if not, should & could this be done using custom formatter?

PS the above xml can also be formatted to start indenting at the position of the first argument.
i.e.:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="my.app">
  • gradle: 4.6
  • spotless version: 3.16.0
@fvgh
Copy link
Member

fvgh commented Nov 28, 2018

I assume that you used the eclipse formatter. Your example above is irritating in that respect. Just had a look myself. You are right, Eclipse WTP only offers configuration of argument indentation in case the maximum line width is exceeded, or to place all arguments on separated lines.
The xml step can contain multiple formatters, so you can add a custom one, fixing the things you don't like about the Eclipse WTP formatting.

@Avinash-Bhat
Copy link
Author

Avinash-Bhat commented Nov 28, 2018

@fvgh this is actually the AndroidStudio's Intellij Idea's default formatting for XML.

The most weird thing for this is that when running code format manually in the IDE, the format is changed to match that of the last example, although running spotlessApply after the change doesn't change it back. This to me seems a bit inconsistent as it should stick either one of the formats; spotless seems to not care about the extra indents.

EDIT: I just checked, this is default for Intellij XML formatter, this is not restricted to AndroidStudio

@Avinash-Bhat
Copy link
Author

I guess you are right in that I need to write a custom xml formatter for this additional change.

But maybe this can be handled by adding support for IntelliJ's formatters as well.

@Avinash-Bhat
Copy link
Author

@fvgh This can be handled by adding a custom formatter for Intellij. Is this something that will be pulled to the core if worked on?

I can see that formatting is done generically in com.intellij.formatting.FormatterImpl; maybe that can be an entry-point to get this implemented.

@nedtwigg
Copy link
Member

This would definitely be pulled to core if you follow this.

@fvgh
Copy link
Member

fvgh commented Nov 30, 2018

@Avinash-Bhat
Please have a look at #200 which already requests IntelliJ support.
About the other issue you raise:

spotless seems to not care about the extra indents.

You configured

trimTrailingWhitespace()
endWithNewline()

Both formatter steps do not care about indents. The thing is that you configure a chain of formatters. If you add the Eclipse formatter, it would alter indents, but unfortunately not in the way you want it.

Does this answer settles your questions?

@fvgh
Copy link
Member

fvgh commented Dec 8, 2018

@Avinash-Bhat I consider this issue solved. Let me know if there are still open issues from your side.

@fvgh fvgh closed this as completed Dec 8, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants