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

Indent spacesPerTab doesn't work #1894

Closed
5 of 6 tasks
DrMoriarty opened this issue Nov 20, 2023 · 4 comments
Closed
5 of 6 tasks

Indent spacesPerTab doesn't work #1894

DrMoriarty opened this issue Nov 20, 2023 · 4 comments

Comments

@DrMoriarty
Copy link

DrMoriarty commented Nov 20, 2023

If you are submitting a bug, please include the following:

  • summary of problem
    I'm trying to auto-format my code with 4 spaces indentation. But I get only 2 spaces indentation.

Example of result (2 spaces before fields):

public class Configuration {

  /** Log all requests */
  public static final int REQUEST_STRATEGY_ALL = 0;

  /** Log only requests with bids */
  public static final int REQUEST_STRATEGY_BIDS = 1;

  /** Log only requests with wins */
  public static final int REQUEST_STRATEGY_WINS = 2;
  • Gradle or Maven version
vasya@Mac-mini-Vasiliy RTB_DSP % mvn --version
Apache Maven 3.9.4 (dfbb324ad4a7c8fb0bf182e6d91b0ae20e3d2dd9)
Maven home: /usr/local/Cellar/maven/3.9.4/libexec
Java version: 21, vendor: Homebrew, runtime: /usr/local/Cellar/openjdk/21/libexec/openjdk.jdk/Contents/Home
Default locale: ru_RU, platform encoding: UTF-8
OS name: "mac os x", version: "13.6.1", arch: "x86_64", family: "mac"
  • spotless version
    <spotless.version>2.40.0</spotless.version>

  • operating system and version
    MacOS Ventura 13.6.1

  • copy-paste your full Spotless configuration block(s), and a link to a public git repo that reproduces the problem if possible
    from pom.xml:

     <plugin>
        <groupId>com.diffplug.spotless</groupId>
        <artifactId>spotless-maven-plugin</artifactId>
        <version>${spotless.version}</version>
        <configuration>
          <ratchetFrom>origin/master</ratchetFrom>
          <java>
            <cleanthat/>
            <googleJavaFormat/>
            <importOrder/>
            <removeUnusedImports/>
            <trimTrailingWhitespace/>
            <endWithNewline/>
            <formatAnnotations/>
            <indent>
              <spaces>true</spaces>
              <spacesPerTab>4</spacesPerTab>
            </indent>
          </java>
        </configuration>
      </plugin>

I tried different indent configurations. In all of them I can get only tabs or 2 spaces. No 4 spaces configuration found.

  • copy-paste the full content of any console errors emitted by gradlew spotless[Apply/Check] --stacktrace

I'm not sure that I understand how to get stacktrace from maven spotless:apply.
The output of spotless:apply looks good. It formats all my files (I've done mvn clean before run) but formatting not good enough.

@DrMoriarty
Copy link
Author

I have a feeling that indentation value doesn't work in tabs mode also. When I use this config:

      <plugin>
        <groupId>com.diffplug.spotless</groupId>
        <artifactId>spotless-maven-plugin</artifactId>
        <version>${spotless.version}</version>
        <configuration>
          <ratchetFrom>origin/master</ratchetFrom>
          <java>
            <cleanthat/>
            <googleJavaFormat/>
            <importOrder/>
            <removeUnusedImports/>
            <trimTrailingWhitespace/>
            <endWithNewline/>
            <formatAnnotations/>
            <indent>
              <tabs>true</tabs>
              <spacesPerTab>4</spacesPerTab>
            </indent>
          </java>
        </configuration>
      </plugin>

I get very strange result:
Снимок экрана 2023-11-20 в 14 30 43
Somewhere I have no indentation in code blocks.

@DrMoriarty
Copy link
Author

So, after reading sources of spotless finally I understand what happened with my indentation. Spotless can not change indentation when converting from spaces to spaces. Always I need tabs step in the middle. Another insight was that spacesPerTab in tab's step means "what spaces per tab was used in previous space's step".
Finally the right magic spell is:

            <googleJavaFormat/>
            <indent>
              <tabs>true</tabs>
              <spacesPerTab>2</spacesPerTab>
            </indent>
            <indent>
              <spaces>true</spaces>
              <spacesPerTab>4</spacesPerTab>
            </indent>

Could you make it more clear in documentation? At https://github.com/diffplug/spotless/tree/main/plugin-maven I have very clear example of converting from spaces to tabs. But converting from spaces to different spaces cost me two days of experimenting and source reading.

@nedtwigg
Copy link
Member

Sorry for the lost time, agree the names are misleading.

@blutorange
Copy link
Contributor

Just a random idea, since people are using two indent steps, I could also imagine the following way to configure it

<convertTabsToSpaces><spacesPerTab>4</spacesPerTab></convertTabsToSpaces>

<convertSpacesToTabs><spacesPerTab>4</spacesPerTab></convertSpacesToTabs>

<convertIndentSpaces><from>4</from><to>2</to></convertIndentSpaces>

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