Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/alibaba/p3c
Browse files Browse the repository at this point in the history
  • Loading branch information
gujin520 committed Oct 25, 2017
2 parents 4b5d704 + 15b49f6 commit 2a24e02
Show file tree
Hide file tree
Showing 15 changed files with 286 additions and 264 deletions.
4 changes: 2 additions & 2 deletions eclipse-plugin/README_cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Help -> Install New Software...

![](https://gw.alicdn.com/tfscom/TB1Ud5kifBNTKJjSszcXXbO2VXa.png)

`注意:有同学反映插件扫描会触发JPA插件启动后台线程执行不明任务,如果不需要的话卸载掉JPA插件即可,目前尚未发现原因`
注意:有同学反映插件扫描会触发很多 "JPA Java Change Event Handler (Waiting)" 的任务,这个是Eclipse的一个[bug](https://bugs.eclipse.org/bugs/show_bug.cgi?id=387455),因为插件在扫描的时候会对文件进行标记,所以触发了JPA的任务。卸载JPA插件,或者尝试升级到最新版的Eclipse。附:[JPA project Change Event Handler问题解决](https://my.oschina.net/cimu/blog/278724)


## 插件使用
Expand All @@ -23,7 +23,7 @@ Help -> Install New Software...
* long或者Long初始赋值时,必须使用大写的L,不能是小写的l)
* Object的equals方法容易抛空指针异常,应使用常量或确定有值的对象来调用equals。

目前不支持代码实时检测,需要手动触发,希望更多的人加入进来一起把咱们的插件做得越来越来,尽量提升研发的使用体验。
目前不支持代码实时检测,需要手动触发,希望更多的人加入进来一起把咱们的插件做得越来越好,尽量提升研发的使用体验。


### 代码扫描
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ object CodeAnalysis {
if (monitor.isCanceled) {
return@run Status.CANCEL_STATUS
}
if(it.isAccessible){
if (it.isAccessible) {
it.accept(fileVisitor)
}
}
Expand Down
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'
}

Loading

0 comments on commit 2a24e02

Please sign in to comment.