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

Add date tag on ClassMustHaveAuthorQuickFix #584

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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 @@ -22,6 +22,8 @@ import com.intellij.psi.JavaPsiFacade
import com.intellij.psi.PsiClass
import com.intellij.psi.javadoc.PsiDocToken
import com.siyeh.ig.InspectionGadgetsFix
import java.time.LocalDate
import java.time.format.DateTimeFormatter

/**
*
Expand All @@ -31,10 +33,13 @@ import com.siyeh.ig.InspectionGadgetsFix
*/
object ClassMustHaveAuthorQuickFix : InspectionGadgetsFix(), AliQuickFix {

val tag = "@author ${System.getProperty("user.name") ?: System.getenv("USER")}"
private var dateFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd")

private val authorTag = "@author ${System.getProperty("user.name") ?: System.getenv("USER")}"

override fun doFix(project: Project?, descriptor: ProblemDescriptor?) {
descriptor ?: return
val dateTag = "@date ${LocalDate.now().format(dateFormatter)}"
val psiClass = descriptor.psiElement as? PsiClass ?: descriptor.psiElement?.parent as? PsiClass ?: return

val document = psiClass.docComment
Expand All @@ -43,7 +48,8 @@ object ClassMustHaveAuthorQuickFix : InspectionGadgetsFix(), AliQuickFix {
if (document == null) {
val doc = factory.createDocCommentFromText("""
/**
* $tag
* $authorTag
* $dateTag
*/
""")
if (psiClass.isEnum) {
Expand All @@ -68,11 +74,13 @@ object ClassMustHaveAuthorQuickFix : InspectionGadgetsFix(), AliQuickFix {
}

if (document.tags.isNotEmpty()) {
document.addBefore(factory.createDocTagFromText(tag), document.tags[0])
document.addBefore(factory.createDocTagFromText(dateTag), document.tags[0])
document.addBefore(factory.createDocTagFromText(authorTag), document.tags[0])
return
}

document.add(factory.createDocTagFromText(tag))
document.add(factory.createDocTagFromText(authorTag))
document.add(factory.createDocTagFromText(dateTag))
}

override val ruleName: String
Expand Down