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

Retrieve value from ".editorconfig" properties directly from ASTNode #1389

Merged

Conversation

paul-dingemans
Copy link
Collaborator

Description

From the perspective of the Rule developer, simplify the retrieval of an ".editorconfig" property for the current ASTNode. By using the ASTNode as receiver, it is no longer needed to pass the "isAndroidCodeStyle" boolean as well.
Example of new usage:

if (node.isRoot()) {
    maxLineLength = node.getEditorConfigValue(maxLineLengthProperty)
}

All logic regarding the determination of the actual value of the property is now defined as part of the definition of that property instead of being spread around multiple places in the ktlint code. Especially the handling of value "unset" (for properties max_line_length and indent_size) and "off" (property max_line_length) is now clearly centralized. This closes #1387.
Most extensive definition at this moment:

public val maxLineLengthProperty: UsesEditorConfigProperties.EditorConfigProperty<Int> =
    UsesEditorConfigProperties.EditorConfigProperty(
        type = PropertyType.max_line_length,
        defaultValue = -1,
        propertyMapper = { property, isAndroidCodeStyle ->
            when {
                property == null || property.isUnset -> {
                    if (isAndroidCodeStyle) {
                        // https://developer.android.com/kotlin/style-guide#line_wrapping
                        100
                    } else {
                        property?.getValueAs()
                    }
                }
                property.sourceValue == "off" -> -1
                else -> property.getValueAs()
            }
        }
    )

Property definitions indentStyleProperty, indentSizeProperty, insertNewLineProperty and maxLineLengthProperty are moved to the DefaultEditorConfigProperties as those properties are based on types provided by the ec4j library.

Class IndentConfig now needs to be initialized based on the values of ".editorconfig" properties indentStyleProperty and indentSizeProperty. Although, this is less convenient, it is more explicit and consistent with how rules should interact with the ".editorconfig" properties.

if (node.isRoot()) {
    indentConfig = IndentConfig(
        indentStyle = node.getEditorConfigValue(indentStyleProperty),
        tabWidth = node.getEditorConfigValue(indentSizeProperty)
    )
    maxLineLength = node.getEditorConfigValue(maxLineLengthProperty)
    return
}

All rules provided by KtLint itself, now only use the interface UsesEditorConfigProperties to retrieve values for the ".editorconfig" property. As a result, the EditConfig became obsolete. It is marked for deletion in Ktlint 0.46.

Checklist

  • tests are added
  • CHANGELOG.md is updated

From the perspective of the Rule developer, simplify the
retrieval of an ".editorconfig" property for the current
ASTNode. By using the ASTNode as receiver, it is no
longer needed to pass the "isAndroidCodeStyle" boolean
as well.

All logic regarding the determination of the actual
value of the property is now defined as part of the
definition of that property instead of being spread
around multiple places in the ktlint code. Especially
the handling of value "unset" (for properties
max_line_length and indent_size) and "off" (property
max_line_length) is now clearly centralized. This
closes pinterest#1387.

Property definitions indentStyleProperty,
indentSizeProperty, insertNewLineProperty and
maxLineLengthProperty are moved to the
DefaultEditorConfigProperties as those properties are
based on types provided by the ec4j library.

Class IndentConfig now needs to be initialized based
on the values of ".editorconfig" properties
indentStyleProperty and indentSizeProperty. Although,
this is less convenient, it is more explicit and
consistent with how rules should interact with the
".editorconfig" properties.

All rules provided by KtLint itself, now only use the
interface UsesEditorConfigProperties to retrieve values
for the ".editorconfig" property. As a result, the
EditConfig became obsolete. It is marked for deletion
in Ktlint 0.46.
@paul-dingemans paul-dingemans added this to the 0.45.0 milestone Feb 27, 2022
…erties

# Conflicts:
#	ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/IndentationRule.kt
@paul-dingemans paul-dingemans merged commit 68990c8 into pinterest:master Mar 12, 2022
@paul-dingemans paul-dingemans deleted the editorconfig-properties branch March 12, 2022 15:26
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

Successfully merging this pull request may close these issues.

Max line length for Android is not enforced when max_line_length has value "unset" or is not defined
1 participant