Skip to content

Commit 4854fb6

Browse files
committed
A single leading space (such as in the copyright header) should not override an otherwise 100% tab-indented file. (fixes #506)
1 parent 6183019 commit 4854fb6

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

jvm/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
1111
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
1212

1313
## [Unreleased]
14+
### Fixed
15+
- A single leading space (such as in the copyright header) should not override an otherwise 100% tab-indented file. ([#506](https://github.com/diffplug/selfie/issues/506))
1416

1517
## [2.4.1] - 2024-10-07
1618
### Fixed

jvm/selfie-lib/src/commonMain/kotlin/com/diffplug/selfie/guts/EscapeLeadingWhitespace.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ internal enum class EscapeLeadingWhitespace {
3535
.lineSequence()
3636
.mapNotNull { line ->
3737
val whitespace = line.takeWhile { it.isWhitespace() }
38-
if (whitespace.isEmpty()) null
38+
if (whitespace.isEmpty() || whitespace == " ") null
3939
else if (whitespace.all { it == ' ' }) ' '
4040
else if (whitespace.all { it == '\t' }) '\t' else MIXED
4141
}

jvm/selfie-lib/src/commonTest/kotlin/com/diffplug/selfie/guts/EscapeLeadingWhitespaceTest.kt

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,28 @@ class EscapeLeadingWhitespaceTest {
2929
appropriateFor("abc\nabc") shouldBe ALWAYS
3030

3131
// all spaces -> only tabs need escape
32-
appropriateFor(" ") shouldBe ONLY_ON_TAB
32+
appropriateFor(" ") shouldBe ALWAYS
3333
appropriateFor(" ") shouldBe ONLY_ON_TAB
34-
appropriateFor(" \n ") shouldBe ONLY_ON_TAB
34+
appropriateFor(" \n ") shouldBe ONLY_ON_TAB
3535

3636
// all tabs -> only space needs escape
3737
appropriateFor("\t") shouldBe ONLY_ON_SPACE
3838
appropriateFor("\t\t") shouldBe ONLY_ON_SPACE
3939
appropriateFor("\t\n\t") shouldBe ONLY_ON_SPACE
4040

4141
// it's a mess -> everything needs escape
42-
appropriateFor("\t\n ") shouldBe ALWAYS
42+
appropriateFor("\t\n ") shouldBe ALWAYS
43+
44+
// single spaces and tabs -> only tabs need escape
45+
appropriateFor(
46+
"""
47+
/*
48+
${' '}* Copyright
49+
${' '}*/
50+
interface Foo {
51+
${'\t'}fun bar()
52+
}
53+
""") shouldBe
54+
ONLY_ON_SPACE
4355
}
4456
}

0 commit comments

Comments
 (0)