Skip to content

Consider the indentation block when working with IndentationSizeOptionVerifier #2

@mipo256

Description

@mipo256

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.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions