Skip to content

Commit

Permalink
Add default include patterns when only negate patterns are given (#1864)
Browse files Browse the repository at this point in the history
* Add the default include patterns when only negate patterns are specified in Ktlint CLI

Closes #1847
  • Loading branch information
paul-dingemans authored Mar 17, 2023
1 parent fac12ee commit faa5e42
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ Previously the default value for `.editorconfig` property `max_line_length` was
* Fix false positive when a single line contains multiple dot qualified expressions and/or safe expressions `indent` ([#1830](https://github.com/pinterest/ktlint/issues/1830))
* Enforce spacing around rangeUntil operator `..<` similar to the range operator `..` in `range-spacing` ([#1858](https://github.com/pinterest/ktlint/issues/1858))
* When `.editorconfig` property `ij_kotlin_imports_layout` contains a `|` but no import exists that match any pattern before the first `|` then do not report a violation nor insert a blank line `import-ordering` ([#1845](https://github.com/pinterest/ktlint/issues/1845))
* When negate-patterns only are specified in Ktlint CLI then automatically add the default include patterns (`**/*.kt` and `**/*.kts`) so that all Kotlin files excluding the files matching the negate-patterns will be processed ([#1847](https://github.com/pinterest/ktlint/issues/1847))

### Changed
* Wrap the parameters of a function literal containing a multiline parameter list (only in `ktlint_official` code style) `parameter-list-wrapping` ([#1681](https://github.com/pinterest/ktlint/issues/1681)).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,28 @@ internal fun FileSystem.fileSequence(

val globs = expand(patternsExclusiveExistingFiles, rootDir)

val pathMatchers =
globs
.filterNot { it.startsWith(NEGATION_PREFIX) }
.map { getPathMatcher(it) }

val negatedPathMatchers =
globs
.filter { it.startsWith(NEGATION_PREFIX) }
.map { getPathMatcher(it.removePrefix(NEGATION_PREFIX)) }

val pathMatchers =
globs
.filterNot { it.startsWith(NEGATION_PREFIX) }
.let { includeMatchers ->
if (negatedPathMatchers.isNotEmpty() && includeMatchers.isEmpty()) {
LOGGER.info {
"A negate pattern is specified without an include pattern. As default, the include patterns '$DEFAULT_PATTERNS' " +
"are used."
}
includeMatchers.plus(
expand(DEFAULT_PATTERNS, rootDir),
)
} else {
includeMatchers
}
}.map { getPathMatcher(it) }

LOGGER.debug {
"""
Start walkFileTree for rootDir: '$rootDir'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,28 @@ internal class FileUtilsTest {
assertThat(foundFiles).isEmpty()
}

@Test
fun `Issue 1847 - Given a negate pattern only then include the default patters and select all files except files in the negate pattern`() {
val foundFiles = getFiles(
patterns = listOf(
"!project1/**/*.kt",
),
)

assertThat(foundFiles)
.containsExactlyInAnyOrder(
ktFileRootDirectory,
ktsFileRootDirectory,
ktsFileInProjectRootDirectory,
ktsFileInProjectSubDirectory,
)
.doesNotContain(
ktFileInProjectRootDirectory,
ktFile1InProjectSubDirectory,
ktFile2InProjectSubDirectory,
)
}

private fun KtlintTestFileSystem.createFile(fileName: String) =
writeFile(
relativeDirectoryToRoot = fileName.substringBeforeLast("/", ""),
Expand Down

0 comments on commit faa5e42

Please sign in to comment.