-
Notifications
You must be signed in to change notification settings - Fork 90
feat: --enable-editorconfig flag and .editorconfig support #570
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
Open
tKe
wants to merge
3
commits into
facebook:main
Choose a base branch
from
tKe:editorconfig
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
core/src/main/java/com/facebook/ktfmt/cli/EditorConfigResolver.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| package com.facebook.ktfmt.cli | ||
|
|
||
| import com.facebook.ktfmt.format.FormattingOptions | ||
| import com.facebook.ktfmt.format.TrailingCommaManagementStrategy | ||
| import java.io.File | ||
| import java.util.concurrent.ConcurrentHashMap | ||
| import org.ec4j.core.EditorConfigLoader | ||
| import org.ec4j.core.PropertyTypeRegistry | ||
| import org.ec4j.core.Resource | ||
| import org.ec4j.core.ResourcePropertiesService | ||
| import org.ec4j.core.model.EditorConfig | ||
| import org.ec4j.core.model.PropertyType | ||
| import org.ec4j.core.model.Version.CURRENT | ||
|
|
||
| object EditorConfigResolver { | ||
|
|
||
| private val ijContinuationIndentSize: PropertyType<Int> = | ||
| PropertyType.LowerCasingPropertyType( | ||
| "ij_continuation_indent_size", | ||
| "Denotes the continuation indent size. Useful to distinguish code blocks versus continuation lines", | ||
| PropertyType.PropertyValueParser.POSITIVE_INT_VALUE_PARSER, | ||
| ) | ||
|
|
||
| private val commaManagementStrategy: PropertyType<TrailingCommaManagementStrategy> = | ||
| PropertyType.LowerCasingPropertyType( | ||
| "ktfmt_trailing_comma_management_strategy", | ||
| "Ktfmt Trailing Comma Management Strategy", | ||
| { _: String, value: String -> | ||
| TrailingCommaManagementStrategy.entries | ||
| .find { it.name.lowercase() == value } | ||
| ?.let { PropertyType.PropertyValue.valid(value, it) } | ||
| ?: PropertyType.PropertyValue.invalid( | ||
| value, | ||
| "Unknown ktfmt_trailing_comma_management_strategy value '$value'", | ||
| ) | ||
| }, | ||
| TrailingCommaManagementStrategy.entries.map { it.name.lowercase() }.toSet(), | ||
| ) | ||
| private val propertyTypeRegistry by lazy { | ||
| PropertyTypeRegistry.builder() | ||
| .defaults() | ||
| .type(PropertyType.max_line_length) // missing from defaults? | ||
| .type(ijContinuationIndentSize) | ||
| .type(commaManagementStrategy) | ||
| .build() | ||
| } | ||
|
|
||
| private object Cache : org.ec4j.core.Cache { | ||
| val cached = ConcurrentHashMap<Resource, EditorConfig>() | ||
|
|
||
| override fun get( | ||
| editorConfigFile: Resource, | ||
| loader: EditorConfigLoader, | ||
| ): EditorConfig = cached.computeIfAbsent(editorConfigFile, loader::load) | ||
| } | ||
|
|
||
| private val resourcePropertiesService: ResourcePropertiesService by lazy { | ||
| ResourcePropertiesService.builder() | ||
| .cache(Cache) | ||
| .loader(EditorConfigLoader.of(CURRENT, propertyTypeRegistry)) | ||
| .build() | ||
| } | ||
|
|
||
| fun resolveFormattingOptions(file: File, baseOptions: FormattingOptions): FormattingOptions { | ||
| val props = | ||
| resourcePropertiesService.queryProperties( | ||
| Resource.Resources.ofPath(file.toPath(), Charsets.UTF_8) | ||
| ) | ||
|
|
||
| // `max_line_length` may return null to indicate 'off', in which case we keep the base maxWidth | ||
| val maxWidth = | ||
| props.getValue(PropertyType.max_line_length, baseOptions.maxWidth, false) | ||
| ?: baseOptions.maxWidth | ||
|
|
||
| // `indent_size` may return null to indicate 'tab', in which case we defer to `tab_width` | ||
| val blockIndent = | ||
| props.getValue(PropertyType.indent_size, baseOptions.blockIndent, false) | ||
| ?: props.getValue(PropertyType.tab_width, baseOptions.blockIndent, false) | ||
|
|
||
| val continuationIndent = | ||
| props.getValue(ijContinuationIndentSize, baseOptions.continuationIndent, false) | ||
|
|
||
| val trailingCommaStrategy = | ||
| props.getValue(commaManagementStrategy, baseOptions.trailingCommaManagementStrategy, false) | ||
|
|
||
| val resolved = | ||
| baseOptions.copy( | ||
| maxWidth = maxWidth, | ||
| blockIndent = blockIndent, | ||
| continuationIndent = continuationIndent, | ||
| trailingCommaManagementStrategy = trailingCommaStrategy, | ||
| ) | ||
| return resolved | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,6 +36,7 @@ data class ParsedArgs( | |
| val setExitIfChanged: Boolean, | ||
| /** File name to report when formating code from stdin */ | ||
| val stdinName: String?, | ||
| val editorConfig: Boolean, | ||
| ) { | ||
| companion object { | ||
|
|
||
|
|
@@ -81,6 +82,7 @@ data class ParsedArgs( | |
| | --set-exit-if-changed Sets exit code to 1 if any input file was not | ||
| | formatted/touched | ||
| | --do-not-remove-unused-imports Leaves all imports in place, even if not used | ||
| | --enable-editorconfig Enable .editorconfig overrides for supported formatting options (limited). | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's document what is this |
||
| | | ||
| |ARGFILE: | ||
| | If the only argument begins with '@', the remainder of the argument is treated | ||
|
|
@@ -106,6 +108,7 @@ data class ParsedArgs( | |
| var setExitIfChanged = false | ||
| var removeUnusedImports = true | ||
| var stdinName: String? = null | ||
| var editorConfig = false | ||
|
|
||
| if ("--help" in args || "-h" in args) return ParseResult.ShowMessage(HELP_TEXT) | ||
| if ("--version" in args || "-v" in args) { | ||
|
|
@@ -120,6 +123,7 @@ data class ParsedArgs( | |
| arg == "--dry-run" || arg == "-n" -> dryRun = true | ||
| arg == "--set-exit-if-changed" -> setExitIfChanged = true | ||
| arg == "--do-not-remove-unused-imports" -> removeUnusedImports = false | ||
| arg == "--enable-editorconfig" -> editorConfig = true | ||
| arg.startsWith("--stdin-name=") -> | ||
| stdinName = | ||
| parseKeyValueArg("--stdin-name", arg) | ||
|
|
@@ -152,6 +156,7 @@ data class ParsedArgs( | |
| dryRun, | ||
| setExitIfChanged, | ||
| stdinName, | ||
| editorConfig, | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ amazon-aws-lambda-core = { module = "com.amazonaws:aws-lambda-java-core", versio | |
| amazon-aws-lambda-events = { module = "com.amazonaws:aws-lambda-java-events", version.ref = "com-amazonaws-aws-lambda-java-events" } | ||
| amazon-aws-sdk-bom = { module = "software.amazon.awssdk:bom", version = "2.10.73" } | ||
| amazon-aws-sdk-lambda = { module = "software.amazon.awssdk:lambda" } | ||
| ec4j = { module = "org.ec4j.core:ec4j-core", version = "1.1.1" } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's use the latest stable 1.2.0 |
||
| googleJavaformat = { module = "com.google.googlejavaformat:google-java-format", version.ref = "com-google-googlejavaformat-google-java-format" } | ||
| googleTruth = { module = "com.google.truth:truth", version.ref = "com-google-truth-truth" } | ||
| gson = { module = "com.google.code.gson:gson", version.ref = "com-google-code-gson-gson" } | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add unit test(s) to this method?