mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-03-22 15:19:59 +07:00
[kotlin] UpdateKotlinCopyright cleanup
^KTIJ-16705 GitOrigin-RevId: de0e6671db55ae69e334b7dd5f6be29147149dbf
This commit is contained in:
committed by
intellij-monorepo-bot
parent
0a4708a67f
commit
758512f65d
@@ -13,40 +13,33 @@ import org.jetbrains.kotlin.kdoc.psi.api.KDoc
|
||||
import org.jetbrains.kotlin.lexer.KtTokens.SHEBANG_COMMENT
|
||||
import org.jetbrains.kotlin.psi.KtDeclaration
|
||||
|
||||
class UpdateKotlinCopyright(
|
||||
project: Project?,
|
||||
module: Module?,
|
||||
root: VirtualFile?,
|
||||
copyrightProfile: CopyrightProfile?
|
||||
) : UpdatePsiFileCopyright(project, module, root, copyrightProfile) {
|
||||
class UpdateKotlinCopyright(project: Project?, module: Module?, root: VirtualFile?, copyrightProfile: CopyrightProfile?) :
|
||||
UpdatePsiFileCopyright(project, module, root, copyrightProfile) {
|
||||
|
||||
override fun accept(): Boolean =
|
||||
file.fileType === KotlinFileType.INSTANCE
|
||||
|
||||
override fun scanFile() {
|
||||
val firstChild: PsiElement? = file.firstChild
|
||||
val comments = getExistentComments(file)
|
||||
val anchor = if (firstChild.isShebangComment()) firstChild?.nextSibling else comments.lastOrNull()
|
||||
checkComments(anchor, true, comments)
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun getExistentComments(psiFile: PsiFile): List<PsiComment> =
|
||||
SyntaxTraverser.psiTraverser(psiFile)
|
||||
.withTraversal(TreeTraversal.LEAVES_DFS)
|
||||
.traverse()
|
||||
.skipWhile { element: PsiElement -> element.isShebangComment() }
|
||||
.takeWhile { element: PsiElement ->
|
||||
(element is PsiComment && element.getParent() !is KtDeclaration) ||
|
||||
element is PsiWhiteSpace ||
|
||||
element.text.isEmpty() ||
|
||||
element.parent is KDoc
|
||||
}
|
||||
.map { element: PsiElement -> if (element.parent is KDoc) element.parent else element }
|
||||
.filterIsInstance<PsiComment>()
|
||||
.toList()
|
||||
val comments = file.getExistingComments()
|
||||
val anchor = if (file.firstChild?.isShebangComment() == true) file.firstChild?.nextSibling else comments.lastOrNull()
|
||||
checkComments(/* last = */ anchor, /* commentHere = */ true, comments)
|
||||
}
|
||||
}
|
||||
|
||||
private fun PsiElement?.isShebangComment(): Boolean =
|
||||
private fun PsiFile.getExistingComments(): List<PsiComment> =
|
||||
SyntaxTraverser.psiTraverser(/* root = */ this)
|
||||
.withTraversal(TreeTraversal.LEAVES_DFS)
|
||||
.traverse()
|
||||
.skipWhile { element: PsiElement -> element.isShebangComment() }
|
||||
.takeWhile { element: PsiElement ->
|
||||
(element is PsiComment && element.getParent() !is KtDeclaration) ||
|
||||
element is PsiWhiteSpace ||
|
||||
element.text.isEmpty() ||
|
||||
element.parent is KDoc
|
||||
}
|
||||
.map { element: PsiElement -> if (element.parent is KDoc) element.parent else element }
|
||||
.filterIsInstance<PsiComment>()
|
||||
.toList()
|
||||
|
||||
private fun PsiElement.isShebangComment(): Boolean =
|
||||
this is PsiComment && tokenType === SHEBANG_COMMENT
|
||||
|
||||
Reference in New Issue
Block a user