question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unexpected behaviour from `indentWithSpaces(int)`

See original GitHub issue

Given the following build script and Java file on Gradle 3.2.1: build.gradle

plugins {
    id 'java'
    id 'com.diffplug.gradle.spotless' version '2.4.0'
}

group 'org.jbduncan'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

spotless {
    format('example') {
        target '**/*.java'
        indentWithSpaces(4)
    }
}

src/main/java/HelloWorld.java

public final class HelloWorld {
      public String greeting() {
        return "Hello, world!";
    }
}

…I would have expected the Java file to turn into:

public final class HelloWorld {
    public String greeting() {
        return "Hello, world!";
    }
}

or:

public final class HelloWorld {
        public String greeting() {
        return "Hello, world!";
    }
}

…but it seems it doesn’t.

Have I misunderstood what indentWithSpaces(4) is supposed to do here? I understand that it turns all tab indents into 4-space indents, but by my intuitive understanding it should also make sure that all space-indents have a number of spaces equal to a multiple of 4.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
kekko1212commented, Mar 30, 2020

Yeah I was thinking to help tbh. I have a lot of spare time these days, so, why not contributing to OS? 😄

Thanks for the info, I’ll see if I can get a PR anytime soon. Would be my pleasure.

1reaction
nedtwiggcommented, Dec 28, 2016

At first glance, the behavior you expect would be better than the behavior it actually has. But consider this case:

/**
 * An implementation of this class specifies a single step in a formatting process.
 *
 * The input is guaranteed to have unix-style newlines, and the output is required
 * to not introduce any windows-style newlines as well.
 */
public interface FormatterStep extends Serializable {
    ...
}

The behavior you expect would clobber those leading spaces, which seem to be the appropriate formatting even for indentWithTabs(). Many languages with block comments have indentation exceptions which are difficult to code in a general-purpose way.

So the current behavior is to just ensure that if the formatting looks right, then it is right. Tabs and spaces are invisible, so you can’t see if you used the right one. With the current behavior, so long as it looks right, spotless will make sure that you used the right characters, and if you used the wrong one, it can swap it for you automatically.

I’m open to a more powerful compromise if we think of one 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unexpected behavior in JAVA - Stack Overflow
1 Answer 1 · The problem occurs in the first loop , while "fromSuraIndex" equal "i" equal 1, Why the debugger enter the...
Read more >
Developers Who Use Spaces Make More Money Than Those ...
Analyzing the data leads us to an interesting conclusion. Coders who use spaces for indentation make more money than ones who use tabs,...
Read more >
Almost Always Unsigned : r/programming - Reddit
Unsigned integers behave like modular arithmetic while they are used mainly for integer arithmetic. This leads to unexpected behaviour any ...
Read more >
FormatExtension (plugin-gradle 5.6.1 API) - Javadoc.io
void, indentWithSpaces() ... void, indentWithSpaces(int numSpacesPerTab) ... it can't know if perhaps the behavior of your custom function has changed.
Read more >
The point where you cease to understand how your code works
int b = 2+2; ... Do NOT indent with spaces, use Tab for that. ... You could of course add the targetable behaviour...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found