This repository has been archived by the owner on Jun 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove extra blank lines which appear when all imports are removed fr…
…om a file.
- Loading branch information
Showing
3 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
formatter/src/main/kotlin/org/kotlin/formatter/scanning/KotlinFileScanner.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,53 @@ | ||
package org.kotlin.formatter.scanning | ||
|
||
import org.jetbrains.kotlin.KtNodeTypes | ||
import org.jetbrains.kotlin.com.intellij.lang.ASTNode | ||
import org.jetbrains.kotlin.psi.psiUtil.children | ||
import org.kotlin.formatter.ForcedBreakToken | ||
import org.kotlin.formatter.LeafNodeToken | ||
import org.kotlin.formatter.Token | ||
import org.kotlin.formatter.scanning.nodepattern.NodePatternBuilder | ||
import org.kotlin.formatter.scanning.nodepattern.nodePattern | ||
|
||
/** A [NodeScanner] for a full Kotlin file. */ | ||
internal class KotlinFileScanner(private val kotlinScanner: KotlinScanner) : NodeScanner { | ||
private val nodePattern = | ||
nodePattern { | ||
either { | ||
nonEmptyPackageDirective() thenMapToTokens { nodes -> | ||
kotlinScanner.scanNodes(nodes, ScannerState.BLOCK) | ||
} | ||
possibleWhitespace() | ||
exactlyOne { | ||
nodeOfType(KtNodeTypes.IMPORT_LIST) thenMapToTokens { nodes -> | ||
kotlinScanner.scanNodes(nodes, ScannerState.BLOCK) | ||
} | ||
} thenMapTokens { tokens -> | ||
if (tokens.filterIsInstance<LeafNodeToken>().isNotEmpty()) { | ||
listOf(ForcedBreakToken(count = 2)).plus(tokens) | ||
} else { | ||
listOf() | ||
} | ||
} | ||
nodeOfType(KtNodeTypes.SCRIPT) thenMapToTokens { nodes -> | ||
kotlinScanner.scanNodes(nodes, ScannerState.BLOCK) | ||
} | ||
} or { | ||
nodeOfType(KtNodeTypes.PACKAGE_DIRECTIVE) | ||
possibleWhitespace() | ||
nodeOfType(KtNodeTypes.IMPORT_LIST) thenMapToTokens { nodes -> | ||
kotlinScanner.scanNodes(nodes, ScannerState.BLOCK) | ||
} | ||
nodeOfType(KtNodeTypes.SCRIPT) thenMapToTokens { nodes -> | ||
kotlinScanner.scanNodes(nodes, ScannerState.BLOCK) | ||
} | ||
} | ||
end() | ||
} | ||
|
||
private fun NodePatternBuilder.nonEmptyPackageDirective(): NodePatternBuilder = | ||
nodeMatching { it.elementType == KtNodeTypes.PACKAGE_DIRECTIVE && it.text.isNotBlank() } | ||
|
||
override fun scan(node: ASTNode, scannerState: ScannerState): List<Token> = | ||
nodePattern.matchSequence(node.children().asIterable()) | ||
} |
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