Skip to content
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

Don't insert blank lines between line comments at the end of files #401

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2390,16 +2390,19 @@ class KotlinInputAstVisitor(

override fun visitKtFile(file: KtFile) {
markForPartialFormat()
var importListEmpty = false
val importListEmpty = file.importList?.text?.isBlank() ?: true

var isFirst = true
for (child in file.children) {
if (child.text.isBlank()) {
importListEmpty = child is KtImportList
continue
}
if (!isFirst && child !is PsiComment && (child !is KtScript || !importListEmpty)) {
builder.blankLineWanted(OpsBuilder.BlankLineWanted.YES)
}
if (child.text.isBlank()) continue

builder.blankLineWanted(when {
isFirst -> OpsBuilder.BlankLineWanted.NO
child is PsiComment -> OpsBuilder.BlankLineWanted.NO
child is KtScript && importListEmpty -> OpsBuilder.BlankLineWanted.PRESERVE
else -> OpsBuilder.BlankLineWanted.YES
})

visit(child)
isFirst = false
}
Expand Down
33 changes: 32 additions & 1 deletion core/src/test/java/com/facebook/ktfmt/format/FormatterTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ class FormatterTest {
deduceMaxWidth = true)

@Test
fun `don't keep adding newlines between these two comments when they're at end of file`() =
fun `don't keep adding newlines between these two comments when they're at end of file`() {
assertFormatted(
"""
|package foo
Expand All @@ -450,6 +450,37 @@ class FormatterTest {
|"""
.trimMargin())

assertFormatted(
"""
|// Comment as first element
|package foo
|// a
|
|/* Another comment */
|"""
.trimMargin())

assertFormatted(
"""
|// Comment as first element then blank line
|
|package foo
|// a
|
|/* Another comment */
|"""
.trimMargin())

assertFormatted(
"""
|// Comment as first element
|package foo
|// Adjacent line comments
|// Don't separate
|"""
.trimMargin())
}

@Test
fun `properties with line comment above initializer`() =
assertFormatted(
Expand Down