Skip to content

Commit

Permalink
Merge pull request alibaba#129 from SeanCai/master
Browse files Browse the repository at this point in the history
bug fix for comment rule mark position
  • Loading branch information
xuantan authored Oct 23, 2017
2 parents 274d114 + 4cae72f commit 1ade912
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ package com.alibaba.p3c.idea.inspection

import com.alibaba.p3c.idea.config.P3cConfig
import com.alibaba.p3c.idea.pmd.AliPmdProcessor
import com.alibaba.p3c.idea.util.DocumentUtils.calculateLineStart
import com.alibaba.p3c.idea.util.DocumentUtils.calculateRealOffset
import com.alibaba.p3c.idea.util.ProblemsUtils
import com.alibaba.p3c.pmd.lang.java.rule.comment.RemoveCommentedCodeRule
import com.beust.jcommander.internal.Lists
import com.google.common.cache.Cache
import com.google.common.cache.CacheBuilder
Expand All @@ -39,9 +41,11 @@ import java.util.concurrent.TimeUnit
* @author caikang
* @date 2016/12/13
*/
class AliPmdInspectionInvoker(private val psiFile: PsiFile,
class AliPmdInspectionInvoker(
private val psiFile: PsiFile,
private val manager: InspectionManager,
private val rule: Rule) {
private val rule: Rule
) {
val logger = Logger.getInstance(javaClass)

private var violations: List<RuleViolation> = emptyList()
Expand All @@ -64,15 +68,21 @@ class AliPmdInspectionInvoker(private val psiFile: PsiFile,
val virtualFile = LocalFileSystem.getInstance().findFileByPath(rv.filename) ?: continue
val psiFile = PsiManager.getInstance(manager.project).findFile(virtualFile) ?: continue
val document = FileDocumentManager.getInstance().getDocument(virtualFile) ?: continue
val offset = calculateRealOffset(document, rv.beginLine, rv.beginColumn)
val endOffset = calculateRealOffset(document, rv.endLine, rv.endColumn)

val offsets = if (rv.rule.name == RemoveCommentedCodeRule::class.java.simpleName) {
Offsets(calculateLineStart(document, rv.beginLine),
calculateLineStart(document, rv.endLine + 1) - 1)
} else {
Offsets(calculateRealOffset(document, rv.beginLine, rv.beginColumn),
calculateRealOffset(document, rv.endLine, rv.endColumn))
}
val errorMessage = if (isOnTheFly) {
rv.description
} else {
"${rv.description} (line ${rv.beginLine})"
}
val problemDescriptor = ProblemsUtils.createProblemDescriptorForPmdRule(psiFile, manager,
isOnTheFly, rv.rule.name, errorMessage, offset, endOffset, rv.beginLine) ?: continue
isOnTheFly, rv.rule.name, errorMessage, offsets.start, offsets.end, rv.beginLine) ?: continue
problemDescriptors.add(problemDescriptor)
}
return problemDescriptors.toTypedArray()
Expand Down Expand Up @@ -130,3 +140,4 @@ class AliPmdInspectionInvoker(private val psiFile: PsiFile,
}

data class FileRule(val filePath: String, val ruleName: String)
data class Offsets(val start: Int, val end: Int)
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ object DocumentUtils {
return lineOffset + calculateRealColumn(document, line, pmdColumn)
}

fun calculateLineStart(document: Document, line: Int): Int {
val maxLine = document.lineCount
if (maxLine < line) {
return -1
}
return document.getLineStartOffset(line - 1)
}

fun calculateRealColumn(document: Document, line: Int, pmdColumn: Int): Int {
var realColumn = pmdColumn - 1
val minusSize = PMD_TAB_SIZE - 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.alibaba.p3c.idea.util

import com.alibaba.p3c.pmd.lang.java.rule.comment.AvoidCommentBehindStatementRule
import com.intellij.codeInspection.InspectionManager
import com.intellij.codeInspection.LocalQuickFix
import com.intellij.codeInspection.ProblemDescriptor
Expand All @@ -39,7 +40,8 @@ import com.intellij.psi.impl.source.tree.ElementType
6
*/
object ProblemsUtils {
private val highlightLineRules = setOf("AvoidCommentBehindStatement")
private val highlightLineRules = setOf(AvoidCommentBehindStatementRule::class.java.simpleName)

fun createProblemDescriptorForPmdRule(psiFile: PsiFile, manager: InspectionManager, isOnTheFly: Boolean,
ruleName: String, desc: String, start: Int, end: Int,
checkLine: Int = 0,
Expand Down
3 changes: 2 additions & 1 deletion idea-plugin/p3c-idea/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ version plugin_version

dependencies {
compile group: 'org.freemarker', name: 'freemarker', version: '2.3.25-incubating'
compile 'com.alibaba.p3c.idea:p3c-common:1.0.0'
//compile 'com.alibaba.p3c.idea:p3c-common:1.0.0'
compile project(':p3c-common')
compile group: 'org.javassist', name: 'javassist', version: '3.21.0-GA'
}

0 comments on commit 1ade912

Please sign in to comment.