Open leinardi opened 3 days ago
You call the KtLintRuleEngine
incorrectly:
ktLintRuleEngine.format(Code.fromSnippet(content))
You have provide a code snippet without providing its virtual path. As of that ktlint can not locate the proper .editorconfig
file. Instead of fromSnippet
use fromSnippetWithPath
. The provide virtualPath
to this snippet should be in the same directory, or a subdirectory of the directory where your .editorconfig
is located.
Thank you for your suggestion. I updated my code to use Code.fromSnippetWithPath
, ensuring the virtualPath
points to the generated file's location. Here are the changes I made:
// Provide a virtual path for the code snippet as a Path
val virtualPath = kotlinFile.toPath()
// Format and write the file
kotlinFile.writeText(
ktLintRuleEngine.format(Code.fromSnippetWithPath(content, virtualPath)) { _ ->
AutocorrectDecision.ALLOW_AUTOCORRECT
},
)
The .editorconfig
file is located at the root of the project, and the virtualPath
points to a subdirectory of the root, as seen in this file: AboutDialog.kt.
However, the formatting behavior remains inconsistent with version 1.3.1.
Do you have any other suggestions that I can try?
Suggestions:
virtualPath
. It must be an absolute path.root = true
property in .editorconfig
(https://editorconfig.org/)
Expected Behavior
When formatting Kotlin code using KtLint 1.4.1, the formatter should produce output that respects the
max_line_length
setting defined in the.editorconfig
file. Additionally, the formatted code should follow Kotlin Coding Conventions for readability, ensuring appropriate line breaks for long function calls.For example, formatting this code:
with
max_line_length=120
should produce this result, as in KtLint 1.3.1:This output is both more readable and adheres to the
max_line_length
constraint.Observed Behavior
Using KtLint 1.4.1, the same input code is formatted as:
This output violates the
max_line_length
constraint and reduces readability.Steps to Reproduce
.editorconfig
file with the following configuration: .editorconfigCommand executed:
Your Environment
.editorconfig
settings:KtLintRuleEngine
directly.