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

Consider the indentation block when working with IndentationSizeOptionVerifier #2

Open
mipo256 opened this issue Nov 27, 2024 · 0 comments
Assignees
Labels
bug Something isn't working
Milestone

Comments

@mipo256
Copy link
Owner

mipo256 commented Nov 27, 2024

The current algorithm for indentation verification just checks the current line length and compares it with the previous line length, If the absolute value of subtraction one from the other gives the values different from what user have configured, we have an error.

This approach is simple, but it does not take into account multiple cases. For instance, here, we have code with configured indent_size = 2:

package com.something.other;

import java.util.function.Consumer;

public class TestJavaClass_IndentationTwo {

  public static final String PARAM = "PARAM";

  private static final Consumer<Object> MY_CONS = o ->
    System.out.println(o);

  public static void main(String[] args) {
    System.out.println("Hey!");

    String property = System
      .getProperties()
      .getProperty(PARAM);

    if (property == null)
      return;

    System.out.println(property);
  }
}

Inside the main() method, we have empty lines. That is totally possible and occurs commonly in java methods. The problem is that the current algorithm (in case this empty line is not populated with trailing whitespaces, which it almost certainly does not) will detect that it is an error having this code:

    System.out.println("Hey!"); // indent = 4
// indent = 0
    String property = System // indent = 4 
      .getProperties()
      .getProperty(PARAM);

In reality, this code is fine, and it should not trigger any error. To do that, we need to move from this algorithm to another:

  1. Detect current block indentation. The block of code is started with either {, ( or [ and closed with }, ), ] respectively. Initially, we start with the root code block that has indent level of 0 columns

  2. If we met the new code block, we increase the expected indentation level for this code block (until terminated) by the size of the indent_size setting. Thus, first nesting level has indentation equal to the indent_size, second level of nesting code block has the indentation size equal to 2 * indent_size and so on.

  3. If we met an empty line anywhere, it is always ignored and considered as a line with the correct indentation.

  4. The line within the code block is considered as indented well only it met one of the following criteria:
    a) It either has the indentation that is the same as the indentation of the owning code block
    b) Or it has the indentation level that is higher than the indentation level of the previous line in the block by indent_size.

@mipo256 mipo256 added the bug Something isn't working label Nov 27, 2024
@mipo256 mipo256 self-assigned this Nov 27, 2024
@mipo256 mipo256 added this to the 1.0.0-M1 milestone Nov 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant