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

Support partial formatting #2631

Merged
merged 4 commits into from
Apr 23, 2024
Merged

Support partial formatting #2631

merged 4 commits into from
Apr 23, 2024

Commits on Apr 11, 2024

  1. Support partial formatting (range) in given code

    Allows API consumers like the 'ktlint-intellij-plugin' to format a block of code inside the given code (for example a file) without autocorrect the given code entirely.
    
    The start offset of the node on which a rule can detect a lint violation should be inside the range which is to be formatted. This has some unexpected side effects for some rules. In most cases it is to be expected that the user won't notice those side effects. And if it happens, the most likely way the user responds is widening the range which is to be formatted, and try to format again.
    
    For example, the given code might contain the when-statement below:
    ```
       // code with lint violations
    
        when(foobar) {
            FOO -> "Single line"
            BAR ->
                """
                Multi line
                """.trimIndent()
            else -> null
        }
    
       // more code with lint violations
    ```
    The `blank-line-between-when-conditions` rule requires blank lines to be added between the conditions. If the when-keyword above is included in the range which is to be formatted then the blank lines before the conditions are added. If only the when-conditions itself are selected, but not the when-keyword, then the blank lines are not added.
    
    This unexpected behavior is a side effect of the way the partial formatting is implemented currently. The side effects can be prevented by delaying the decision to autocorrect as late as possible and the exact offset of the error is known. This however would cause a breaking change, and needs to wait until Ktlint V2.x.
    paul-dingemans committed Apr 11, 2024
    Configuration menu
    Copy the full SHA
    c8251af View commit details
    Browse the repository at this point in the history
  2. Support partial formatting (range) in given code

    Allows API consumers like the 'ktlint-intellij-plugin' to format a block of code inside the given code (for example a file) without autocorrect the given code entirely.
    
    The start offset of the node on which a rule can detect a lint violation should be inside the range which is to be formatted. This has some unexpected side effects for some rules. In most cases it is to be expected that the user won't notice those side effects. And if it happens, the most likely way the user responds is widening the range which is to be formatted, and try to format again.
    
    For example, the given code might contain the when-statement below:
    ```
       // code with lint violations
    
        when(foobar) {
            FOO -> "Single line"
            BAR ->
                """
                Multi line
                """.trimIndent()
            else -> null
        }
    
       // more code with lint violations
    ```
    The `blank-line-between-when-conditions` rule requires blank lines to be added between the conditions. If the when-keyword above is included in the range which is to be formatted then the blank lines before the conditions are added. If only the when-conditions itself are selected, but not the when-keyword, then the blank lines are not added.
    
    This unexpected behavior is a side effect of the way the partial formatting is implemented currently. The side effects can be prevented by delaying the decision to autocorrect as late as possible and the exact offset of the error is known. This however would cause a breaking change, and needs to wait until Ktlint V2.x.
    
    Closes #2629
    paul-dingemans committed Apr 11, 2024
    Configuration menu
    Copy the full SHA
    51d758d View commit details
    Browse the repository at this point in the history
  3. Configuration menu
    Copy the full SHA
    09f2bff View commit details
    Browse the repository at this point in the history
  4. Configuration menu
    Copy the full SHA
    9853ae3 View commit details
    Browse the repository at this point in the history