forked from pinterest/ktlint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve support of default editorconfig properties
Deprecate ExperimentalParams.editorConfigDefaults in favor of new parameter ExperimentalParams.editorConfigDefaults. When used in the old implementation this resulted in ignoring all ".editorconfig" files on the path to the file. The new implementation uses properties from the "editorConfigDefaults" parameter only when no ".editorconfig" files on the path to the file supplies this property for the filepath. Closes pinterest#1551 API consumers can easily create the EditConfigDefaults by calling "EditConfigDefaults.load(path)" or creating it programmatically. The CLI still supports the "--editorconfig=" option but has improved support. The path given can be either be a path to file or directory. In case of a directory path, it is expected that the directory does contain a file with name ".editorconfig". In of a file path, any valid file name is accepted. The path can be relative or absolute. Depending on the OS, the "~" at the start of the path is accepted as well. BaseCLITest no longer always waits 3 seconds for completion of the asynchronous process. Once the process is started, it checks every 100 ms whether the process is still alive (e.g. is running) and stops polling otherwise resulting in better performance (most notable on local machine). The maximum duration of the CLI test has been increased to 10 seconds.
- Loading branch information
1 parent
6fdb80e
commit 54f8194
Showing
28 changed files
with
1,193 additions
and
175 deletions.
There are no files selected for viewing
This file contains 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 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 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
36 changes: 36 additions & 0 deletions
36
ktlint-core/src/main/kotlin/com/pinterest/ktlint/core/api/EditorConfigDefaults.kt
This file contains 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,36 @@ | ||
package com.pinterest.ktlint.core.api | ||
|
||
import com.pinterest.ktlint.core.internal.EditorConfigDefaultsLoader | ||
import java.nio.file.Path | ||
import org.ec4j.core.model.EditorConfig | ||
|
||
/** | ||
* Wrapper around the [EditorConfig]. Only to be used only for the default value of properties. | ||
*/ | ||
public data class EditorConfigDefaults(public val value: EditorConfig) { | ||
public companion object { | ||
private val editorConfigDefaultsLoader = EditorConfigDefaultsLoader() | ||
|
||
/** | ||
* Loads properties from [path]. [path] may either locate a file (also allows specifying a file with a name other | ||
* than ".editorconfig") or a directory in which a file with name ".editorconfig" is expected to exist. Properties | ||
* from all globs are returned. | ||
* | ||
* If [path] is not valid then the [emptyEditorConfigDefaults] is returned. | ||
* | ||
* The property "root" which denotes whether the parent directory is to be checked for the existence of a fallback | ||
* ".editorconfig" is ignored entirely. | ||
*/ | ||
public fun load(path: Path?): EditorConfigDefaults = | ||
if (path == null) { | ||
emptyEditorConfigDefaults | ||
} else { | ||
editorConfigDefaultsLoader.load(path) | ||
} | ||
|
||
/** | ||
* Empty representation of [EditorConfigDefaults]. | ||
*/ | ||
public val emptyEditorConfigDefaults: EditorConfigDefaults = EditorConfigDefaults(EditorConfig.builder().build()) | ||
} | ||
} |
This file contains 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
77 changes: 77 additions & 0 deletions
77
ktlint-core/src/main/kotlin/com/pinterest/ktlint/core/internal/EditorConfigDefaultsLoader.kt
This file contains 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,77 @@ | ||
package com.pinterest.ktlint.core.internal | ||
|
||
import com.pinterest.ktlint.core.api.EditorConfigDefaults | ||
import com.pinterest.ktlint.core.api.EditorConfigDefaults.Companion.emptyEditorConfigDefaults | ||
import com.pinterest.ktlint.core.initKtLintKLogger | ||
import com.pinterest.ktlint.core.internal.ThreadSafeEditorConfigCache.Companion.threadSafeEditorConfigCache | ||
import java.nio.charset.StandardCharsets | ||
import java.nio.file.FileSystem | ||
import java.nio.file.FileSystems | ||
import java.nio.file.Path | ||
import kotlin.io.path.isDirectory | ||
import kotlin.io.path.notExists | ||
import kotlin.io.path.pathString | ||
import mu.KotlinLogging | ||
import org.ec4j.core.EditorConfigLoader | ||
import org.ec4j.core.Resource | ||
import org.ec4j.core.model.Version | ||
|
||
private val logger = KotlinLogging.logger {}.initKtLintKLogger() | ||
|
||
/** | ||
* Load all properties from an ".editorconfig" file without filtering on a glob. | ||
*/ | ||
internal class EditorConfigDefaultsLoader( | ||
private val fileSystem: FileSystem = FileSystems.getDefault() | ||
) { | ||
private val editorConfigLoader: EditorConfigLoader = EditorConfigLoader.of(Version.CURRENT) | ||
|
||
/** | ||
* Loads properties from [path]. [path] may either locate a file (also allows specifying a file with a name other | ||
* than ".editorconfig") or a directory in which a file with name ".editorconfig" is expected to exist. Properties | ||
* from all globs are returned. | ||
* | ||
* If [path] is not valid then the [emptyEditorConfigDefaults] is returned. | ||
* | ||
* The property "root" which denotes whether the parent directory is to be checked for the existence of a fallback | ||
* ".editorconfig" is ignored entirely. | ||
*/ | ||
fun load(path: Path?): EditorConfigDefaults { | ||
if (path == null || path.pathString.isBlank()) { | ||
return emptyEditorConfigDefaults | ||
} | ||
|
||
val editorConfigFilePath = path.editorConfigFilePath() | ||
if (editorConfigFilePath.notExists()) { | ||
logger.warn { "File or directory '$path' is not found. Can not load '.editorconfig' properties" } | ||
return emptyEditorConfigDefaults | ||
} | ||
|
||
return threadSafeEditorConfigCache | ||
.get(editorConfigFilePath.resource(), editorConfigLoader) | ||
.also { | ||
logger.trace { | ||
it | ||
.toString() | ||
.split("\n") | ||
.joinToString( | ||
prefix = "Loaded .editorconfig-properties from file '$editorConfigFilePath':\n\t", | ||
separator = "\n\t" | ||
) | ||
} | ||
}.let { EditorConfigDefaults(it) } | ||
} | ||
|
||
private fun Path.editorConfigFilePath() = | ||
if (isDirectory()) { | ||
pathString | ||
.plus( | ||
fileSystem.separator.plus(".editorconfig") | ||
).let { path -> fileSystem.getPath(path) } | ||
} else { | ||
this | ||
} | ||
|
||
private fun Path.resource() = | ||
Resource.Resources.ofPath(this, StandardCharsets.UTF_8) | ||
} |
Oops, something went wrong.