Skip to content

Commit

Permalink
Globs like "**/*.kt" should also match files in current directory (#1533
Browse files Browse the repository at this point in the history
)

When a glob does not contain an absolute path then do not prefix it with the root directory but instead ensure that it is prefixed it with the "**/" matcher. In this way files directly inside the workdir are matched as well as files in subdirectories of the workdir.
  • Loading branch information
paul-dingemans committed Jul 17, 2022
1 parent e1772d8 commit 12ea685
Show file tree
Hide file tree
Showing 4 changed files with 278 additions and 237 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ The "visit" life cycle hook will be removed in Ktlint 0.48. In KtLint 0.47 the "
### Fixed

* Fix cli argument "--disabled_rules" ([#1520](https://github.com/pinterest/ktlint/issue/1520)).
* When a glob is specified then ensure that it matches files in the current directory and not only in subdirectories of the current directory ([#1533](https://github.com/pinterest/ktlint/issue/1533)).
* Disable/enable IndentationRule on blocks in middle of file. (`indent`) [#631](https://github.com/pinterest/ktlint/issues/631)

### Changed
Expand Down
46 changes: 20 additions & 26 deletions ktlint/src/main/kotlin/com/pinterest/ktlint/internal/FileUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,21 @@ import java.nio.file.SimpleFileVisitor
import java.nio.file.attribute.BasicFileAttributes
import kotlin.system.exitProcess
import mu.KotlinLogging
import org.jetbrains.kotlin.util.prefixIfNot

private val logger = KotlinLogging.logger {}.initKtLintKLogger()

internal val workDir: String = File(".").canonicalPath
private val tildeRegex = Regex("^(!)?~")

private val os = System.getProperty("os.name")
private val userHome = System.getProperty("user.home")

private val defaultPatterns = setOf(
"**$globSeparator*.kt",
"**$globSeparator*.kts"
)

internal fun FileSystem.fileSequence(
globs: List<String>,
rootDir: Path = Paths.get(".").toAbsolutePath().normalize()
Expand All @@ -44,15 +53,14 @@ internal fun FileSystem.fileSequence(
}

val pathMatchers = if (actualGlobs.isEmpty()) {
setOf(
getPathMatcher("glob:**$globSeparator*.kt"),
getPathMatcher("glob:**$globSeparator*.kts")
)
defaultPatterns
.map { getPathMatcher("glob:$it") }
.toSet()
} else {
actualGlobs
.filterNot { it.startsWith("!") }
.map {
getPathMatcher(toGlob(it, rootDir))
getPathMatcher(toGlob(it))
}
}

Expand All @@ -62,7 +70,7 @@ internal fun FileSystem.fileSequence(
actualGlobs
.filter { it.startsWith("!") }
.map {
getPathMatcher(toGlob(it.removePrefix("!"), rootDir))
getPathMatcher(toGlob(it.removePrefix("!")))
}
}

Expand Down Expand Up @@ -102,41 +110,27 @@ private fun FileSystem.isGlobAbsolutePath(glob: String): Boolean {
return rootDirs.any { glob.removePrefix("!").startsWith(it) }
}

internal fun FileSystem.toGlob(
pattern: String,
rootDir: Path
): String {
val os = System.getProperty("os.name")
internal fun FileSystem.toGlob(pattern: String): String {
val expandedPath = if (os.startsWith("windows", true)) {
// Windows sometimes inserts `~` into paths when using short directory names notation, e.g. `C:\Users\USERNA~1\Documents
pattern
} else {
expandTilde(pattern)
}
}.replace(File.separator, globSeparator)

val fullPath = if (isGlobAbsolutePath(expandedPath)) {
expandedPath
} else {
val rootDirPath = rootDir
.toAbsolutePath()
.toString()
.run {
val normalizedPath = if (!endsWith(File.separator)) "$this${File.separator}" else this
normalizedPath
}
"$rootDirPath$expandedPath"
expandedPath.prefixIfNot("**$globSeparator")
}
.replace(File.separator, globSeparator)
return "glob:$fullPath"
}

private val globSeparator: String get() {
val os = System.getProperty("os.name")
return when {
private val globSeparator: String get() =
when {
os.startsWith("windows", ignoreCase = true) -> "\\\\"
else -> "/"
}
}

/**
* List of paths to Java `jar` files.
Expand All @@ -154,7 +148,7 @@ internal fun JarFiles.toFilesURIList() = map {

// a complete solution would be to implement https://www.gnu.org/software/bash/manual/html_node/Tilde-Expansion.html
// this implementation takes care only of the most commonly used case (~/)
private fun expandTilde(path: String): String = path.replaceFirst(tildeRegex, System.getProperty("user.home"))
private fun expandTilde(path: String): String = path.replaceFirst(tildeRegex, userHome)

internal fun File.location(
relative: Boolean
Expand Down

This file was deleted.

Loading

0 comments on commit 12ea685

Please sign in to comment.