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.
Finish support for proper formatting of file-level annotations
- Loading branch information
Showing
3 changed files
with
146 additions
and
4 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
formatter/src/main/kotlin/org/kotlin/formatter/scanning/FileAnnotationListScanner.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,54 @@ | ||
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.ForceSynchronizedBreaksInBlockToken | ||
import org.kotlin.formatter.ForcedBreakToken | ||
import org.kotlin.formatter.Token | ||
import org.kotlin.formatter.WhitespaceToken | ||
import org.kotlin.formatter.scanning.nodepattern.NodePatternBuilder | ||
import org.kotlin.formatter.scanning.nodepattern.nodePattern | ||
|
||
/** A [NodeScanner] for the list of annotations at the top of a Kotlin file. */ | ||
internal class FileAnnotationListScanner(private val kotlinScanner: KotlinScanner) : NodeScanner { | ||
private val nodePattern = | ||
nodePattern { | ||
zeroOrMore { | ||
either { | ||
simpleAnnotation() thenMapToTokens { nodes -> | ||
kotlinScanner.scanNodes(nodes, ScannerState.STATEMENT) | ||
} | ||
} or { | ||
nodeOfOneOfTypes( | ||
KtNodeTypes.ANNOTATION, | ||
KtNodeTypes.ANNOTATION_ENTRY | ||
) thenMapToTokens { nodes -> | ||
kotlinScanner.scanNodes(nodes, ScannerState.STATEMENT) | ||
.plus(ForceSynchronizedBreaksInBlockToken) | ||
} | ||
} | ||
possibleWhitespace() thenMapToTokens { nodes -> | ||
if (nodes.firstOrNull()?.textContains('\n') == true) { | ||
listOf(ForcedBreakToken(count = 1)) | ||
} else { | ||
listOf(WhitespaceToken(" ")) | ||
} | ||
} | ||
zeroOrOne { | ||
comment() thenMapTokens { tokens -> tokens.plus(ForcedBreakToken(count = 1)) } | ||
} | ||
possibleWhitespace() | ||
} | ||
end() | ||
} | ||
|
||
private fun NodePatternBuilder.simpleAnnotation(): NodePatternBuilder = | ||
nodeMatching { | ||
it.elementType == KtNodeTypes.ANNOTATION_ENTRY && | ||
it.lastChildNode.elementType == KtNodeTypes.CONSTRUCTOR_CALLEE | ||
} | ||
|
||
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