Skip to content

Commit

Permalink
code refine
Browse files Browse the repository at this point in the history
add a few quickfix
  • Loading branch information
yupeng committed Nov 16, 2021
1 parent 0f4a415 commit 836ce29
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 26 deletions.
10 changes: 5 additions & 5 deletions build.gradle
@@ -1,7 +1,7 @@
plugins {
id 'java'
id 'org.jetbrains.intellij' version '1.1.4'
id 'org.jetbrains.kotlin.jvm' version '1.5.21'
id 'org.jetbrains.intellij' version '1.3.0'
id 'org.jetbrains.kotlin.jvm' version '1.5.31'
}

static def gitTag() {
Expand Down Expand Up @@ -31,14 +31,14 @@ repositories {
}

dependencies {
compileOnly 'org.jetbrains.kotlin:kotlin-stdlib:1.5.21'
compileOnly 'org.jetbrains.kotlin:kotlin-stdlib:1.5.31'
}

// See https://github.com/JetBrains/gradle-intellij-plugin/
intellij {
version = '212.4746.92'
version = '212.5457.46'
type = 'IU'
plugins = ['org.jetbrains.plugins.go:212.4746.92']
plugins = ['org.jetbrains.plugins.go:212.5457.20']
updateSinceUntilBuild = false
}

Expand Down
5 changes: 5 additions & 0 deletions change-notes.htm
@@ -1,3 +1,8 @@
<p><strong>1.5.6 (2021-11-16)</strong></p>
<ul>
<li>code refine</li>
<li>add a few quickfix</li>
</ul>
<p><strong>1.5.5 (2021-08-26)</strong></p>
<ul>
<li>fix: lint failed on UTF8 character files (eg: Chinese)</li>
Expand Down
13 changes: 4 additions & 9 deletions src/main/kotlin/com/ypwang/plugin/GoLinterExternalAnnotator.kt
Expand Up @@ -274,18 +274,15 @@ class GoLinterExternalAnnotator : ExternalAnnotator<PsiFile, GoLinterExternalAnn
"--max-issues-per-linter", "0", "--max-same-issues", "0"
)

customConfigDetected(GoLinterConfig.customProjectDir.orElse(project.basePath!!)).ifPresentOrElse(
{
parameters.add("-c")
parameters.add(it)
},
{
GoLinterConfig.customConfigFile.ifPresentOrElse(
customConfigDetected(GoLinterConfig.customProjectDir.orElse(project.basePath!!))
.or { GoLinterConfig.customConfigFile }
.ifPresentOrElse(
{
parameters.add("-c")
parameters.add(it)
},
{
parameters.add("--no-config")
// use default linters
val enabledLinters = GoLinterConfig.enabledLinters
if (enabledLinters != null) {
Expand All @@ -299,8 +296,6 @@ class GoLinterExternalAnnotator : ExternalAnnotator<PsiFile, GoLinterExternalAnn
}
}
)
}
)

val module = ModuleUtilCore.findModuleForFile(file)
if (module != null) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/com/ypwang/plugin/GoLinterQuickFix.kt
Expand Up @@ -32,7 +32,8 @@ val quickFixHandler: Map<String, ProblemHandler> = mutableMapOf(
"errorlint" to ErrorLintHandler,
"ifshort" to IfShortHandler,
"forcetypeassert" to ForceTypeAssertHandler,
"predeclared" to PreDeclaredHandler
"predeclared" to PreDeclaredHandler,
"varnamelen" to varNameLenHandler
).apply {
this.putAll(listOf("structcheck", "varcheck", "deadcode", "unused").map { it to NamedElementHandler })
this.putAll(ProblemHandler.FuncLinters.map { it to funcNoLintHandler(it) })
Expand Down
33 changes: 23 additions & 10 deletions src/main/kotlin/com/ypwang/plugin/handler/GoCriticHandler.kt
Expand Up @@ -171,22 +171,35 @@ object GoCriticHandler : ProblemHandler() {
}

addHandler(this, "singleCaseSwitch: should rewrite switch statement to if statement") { _, _, _, _, element: GoSwitchStatement ->
// cannot handle type switch with var assign
val fix = if (element is GoTypeSwitchStatement && element.statement != null) EmptyLocalQuickFix
else arrayOf<IntentionAction>(GoSingleCaseSwitchFix(element))
fix to element.switchStart?.textRange
// cannot handle type switch with var assign
val fix = if (element is GoTypeSwitchStatement && element.statement != null) EmptyLocalQuickFix
else arrayOf<IntentionAction>(GoSingleCaseSwitchFix(element))
fix to element.switchStart?.textRange
}

addHandler(this, "ifElseChain: rewrite if-else to switch statement") { _, _, _, _, element: GoIfStatement ->
arrayOf<IntentionAction>(GoIfToSwitchFix(element)) to element.`if`.textRange
arrayOf<IntentionAction>(GoIfToSwitchFix(element)) to element.`if`.textRange
}

addHandler(this, "commentFormatting: put a space between `//` and comment text") { _, _, _, _, element: PsiCommentImpl ->
if (element.text.startsWith("//"))
arrayOf<IntentionAction>(GoCommentFix(element, "Add space") { project, comment ->
GoElementFactory.createComment(project, "// " + comment.text.substring(2))
}) to element.textRange
else NonAvailableFix
if (element.text.startsWith("//"))
arrayOf<IntentionAction>(GoCommentFix(element, "Add space") { project, comment ->
GoElementFactory.createComment(project, "// " + comment.text.substring(2))
}) to element.textRange
else NonAvailableFix
}

addHandler(this, "unlambda: replace") { _, _, issue, _, element: GoFunctionLit ->
val (_, replace) = extractQuote(issue.Text, 2)
arrayOf<IntentionAction>(GoReplaceElementFix(replace, element, GoExpression::class.java)) to element.textRange
}

addHandler(this, "sloppyTypeAssert:") { _, _, _, _, element: GoTypeAssertionExpr ->
arrayOf<IntentionAction>(GoReplaceElementFix(element.expression.text, element, GoExpression::class.java)) to element.textRange
}

addHandler(this, "badCall:") { _, _, _, _, element: GoCallExpr ->
arrayOf<IntentionAction>(GoEscapeCallExprFix(element)) to element.textRange
}
}
}
5 changes: 5 additions & 0 deletions src/main/kotlin/com/ypwang/plugin/handler/GoSimpleHandler.kt
Expand Up @@ -140,6 +140,11 @@ object GoSimpleHandler : ProblemHandler() {
arrayOf<IntentionAction>(GoEscapeCallExprFix(element)) to element.textRange
else NonAvailableFix
}
// type assertion to the same type
"S1040" ->
chainFindAndHandle(file, document, issue, overrideLine) { element: GoTypeAssertionExpr ->
arrayOf<IntentionAction>(GoReplaceElementFix(element.expression.text, element, GoExpression::class.java)) to element.textRange
}
else -> NonAvailableFix
}
}
Expand Up @@ -13,7 +13,7 @@ abstract class ProblemHandler {
val EmptyLocalQuickFix = arrayOf<IntentionAction>()
val NonAvailableFix = EmptyLocalQuickFix to null
// they reports issue of whole function
val FuncLinters = setOf("funlen", "gocognit", "gochecknoinits", "gocyclo", "nakedret")
val FuncLinters = setOf("cyclop", "funlen", "gocognit", "gochecknoinits", "gocyclo", "nakedret")
}

protected fun calcPos(document: Document, issue: LintIssue, overrideLine: Int): Int =
Expand Down
7 changes: 7 additions & 0 deletions src/main/kotlin/com/ypwang/plugin/handler/SimpleHandlers.kt
Expand Up @@ -206,6 +206,13 @@ object IfShortHandler : ProblemHandler() {
}
}

object varNameLenHandler : ProblemHandler() {
override fun doSuggestFix(file: PsiFile, document: Document, issue: LintIssue, overrideLine: Int): Pair<Array<IntentionAction>, TextRange?> =
chainFindAndHandle(file, document, issue, overrideLine) { element: GoNamedElement ->
EmptyLocalQuickFix to element.textRange
}
}

// explanation linter
fun explanationHandler(url: String): ProblemHandler = object : ProblemHandler() {
override fun doSuggestFix(file: PsiFile, document: Document, issue: LintIssue, overrideLine: Int): Pair<Array<IntentionAction>, TextRange?> =
Expand Down

0 comments on commit 836ce29

Please sign in to comment.