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

[com.pinterest.ktlint.core.KtLint.FILE_PATH_USER_DATA_KEY] is marked as deprecated #1921

Closed
nulls opened this issue Apr 7, 2023 · 1 comment · Fixed by #1922
Closed

[com.pinterest.ktlint.core.KtLint.FILE_PATH_USER_DATA_KEY] is marked as deprecated #1921

nulls opened this issue Apr 7, 2023 · 1 comment · Fixed by #1922
Milestone

Comments

@nulls
Copy link
Contributor

nulls commented Apr 7, 2023

Current Behavior

Diktat uses path to file in some rules. We take it from org.jetbrains.kotlin.com.intellij.openapi.util.UserDataHolder#getUserData in root node using com.pinterest.ktlint.core.KtLint#getFILE_PATH_USER_DATA_KEY.

It's going to be removed in version 0.49.x:

    @Deprecated(
        """
            Marked for removal in KtLint 0.49.0. Use:
                if (node.isRoot()) {
                    val fileName = (node.psi as? KtFile)?.name
                    ...
                }
            """,
    )
    public val FILE_PATH_USER_DATA_KEY: Key<String> = Key<String>("FILE_PATH")

The kdoc suggests to use org.jetbrains.kotlin.psi.KtFile#name, but unfortunately it contains only file name, not a path:
com.pinterest.ktlint.core.internal.RuleExecutionContext.Companion#createRuleExecutionContext$ktlint_core:

            val psiFileName =
                code.fileName // <-- fileName, not filePath
                    ?: if (code.script) {
                        "File.kts"
                    } else {
                        "File.kt"
                    }
            val psiFile = psiFileFactory.createFileFromText(
                psiFileName,

Is it any way to keep logic to set FILE_PATH in UserDataHolder?

P.S. As I got #1434, the user data was removed in favor of .editorconfig. But FILE_PATH is dynamic property, when .editorconfig is more about static properties

Additional information

  • Current version of ktlint: 0.48.2
@paul-dingemans
Copy link
Collaborator

FILE_PATH_USER_DATA_KEY has already been removed. But the problem was easily resolved by passing the path instead of the filename to he PsiFileFactory. Now the path instead of the filename can be retrieved in the rule with:

        if (node.isRoot()) {
            node as FileASTNode? ?: error("node is not ${FileASTNode::class} but ${node::class}")

            val filePath = (node.psi as? KtFile)?.virtualFilePath
            ...
      }

@paul-dingemans paul-dingemans added this to the 0.49.0 milestone Apr 7, 2023
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 a pull request may close this issue.

2 participants