Skip to content

Commit

Permalink
fix Inconsistency results in different scans
Browse files Browse the repository at this point in the history
  • Loading branch information
caikang.ck committed Jun 21, 2020
1 parent 2ca345b commit b9adb32
Show file tree
Hide file tree
Showing 9 changed files with 730 additions and 206 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package com.alibaba.p3c.idea.action

import com.alibaba.p3c.idea.compatible.inspection.InspectionProfileService
import com.alibaba.p3c.idea.compatible.inspection.Inspections
import com.alibaba.p3c.idea.ep.InspectionActionExtensionPoint
import com.alibaba.p3c.idea.i18n.P3cBundle
import com.alibaba.p3c.idea.inspection.AliBaseInspection
import com.alibaba.p3c.idea.util.NumberConstants
Expand All @@ -26,10 +25,10 @@ import com.intellij.analysis.AnalysisScope
import com.intellij.analysis.AnalysisUIOptions
import com.intellij.analysis.BaseAnalysisActionDialog
import com.intellij.codeInspection.InspectionManager
import com.intellij.codeInspection.InspectionsBundle
import com.intellij.codeInspection.ex.GlobalInspectionContextImpl
import com.intellij.codeInspection.ex.InspectionManagerEx
import com.intellij.codeInspection.ex.InspectionToolWrapper
import com.intellij.codeInspection.ui.InspectionResultsView
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.CommonDataKeys
Expand All @@ -38,9 +37,13 @@ import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.module.Module
import com.intellij.openapi.module.ModuleUtilCore
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VfsUtilCore
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.wm.ToolWindowId
import com.intellij.openapi.wm.ToolWindowManager
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiFileSystemItem
import com.intellij.psi.PsiManager
import java.awt.event.KeyEvent

Expand Down Expand Up @@ -97,8 +100,10 @@ class AliInspectionAction : AnAction() {
val element = psiFile ?: psiElement
analysisScope.isIncludeTestSource = false
analysisScope.setSearchInLibraries(true)
createContext(toolWrappers, managerEx, element,
projectDir).doInspections(analysisScope)
createContext(
toolWrappers, managerEx, element,
projectDir, analysisScope
).doInspections(analysisScope)
}

private fun isBaseDir(file: VirtualFile, project: Project): Boolean {
Expand All @@ -108,86 +113,97 @@ class AliInspectionAction : AnAction() {
return project.basePath == file.canonicalPath
}

private fun inspectForKeyEvent(project: Project, managerEx: InspectionManagerEx,
toolWrappers: List<InspectionToolWrapper<*, *>>, psiElement: PsiElement?, psiFile: PsiFile?,
virtualFile: VirtualFile?, analysisScope: AnalysisScope) {
private fun inspectForKeyEvent(
project: Project, managerEx: InspectionManagerEx,
toolWrappers: List<InspectionToolWrapper<*, *>>, psiElement: PsiElement?, psiFile: PsiFile?,
virtualFile: VirtualFile?, analysisScope: AnalysisScope
) {
var module: Module? = null
if (virtualFile != null && project.baseDir != virtualFile) {
module = ModuleUtilCore.findModuleForFile(virtualFile, project)
}

val uiOptions = AnalysisUIOptions.getInstance(project)
uiOptions.ANALYZE_TEST_SOURCES = false
val dialog = BaseAnalysisActionDialog("Select Analyze Scope", "Analyze Scope", project, analysisScope,
if (module != null) module.name else null, true, uiOptions, psiElement)
val dialog = BaseAnalysisActionDialog(
"Select Analyze Scope", "Analyze Scope", project, analysisScope,
module?.name, true, uiOptions, psiElement
)

if (!dialog.showAndGet()) {
return
}
val scope = dialog.getScope(uiOptions, analysisScope, project, module)
scope.setSearchInLibraries(true)
val element = psiFile ?: psiElement
createContext(toolWrappers, managerEx, element,
dialog.isProjectScopeSelected).doInspections(scope)
createContext(
toolWrappers, managerEx, element,
dialog.isProjectScopeSelected, scope
).doInspections(scope)
}

override fun update(e: AnActionEvent) {
e.presentation.text = P3cBundle.getMessage("com.alibaba.p3c.idea.action.AliInspectionAction.text")
}

companion object {
val logger = Logger.getInstance(AliInspectionAction::class.java)
private val logger = Logger.getInstance(AliInspectionAction::class.java)

fun createContext(toolWrapperList: List<InspectionToolWrapper<*, *>>,
managerEx: InspectionManagerEx, psiElement: PsiElement?, projectScopeSelected: Boolean):
GlobalInspectionContextImpl {
private fun getTitle(element: PsiElement?, isProjectScopeSelected: Boolean): String? {
if (element == null) {
return null
}
if (isProjectScopeSelected) {
return "Project"
}
if (element is PsiFileSystemItem) {
return VfsUtilCore.getRelativePath(element.virtualFile, element.project.baseDir)
}
return null
}

fun createContext(
toolWrapperList: List<InspectionToolWrapper<*, *>>,
managerEx: InspectionManagerEx, psiElement: PsiElement?,
projectScopeSelected: Boolean, scope: AnalysisScope
):
GlobalInspectionContextImpl {
// remove last same scope content
val project = managerEx.project
val title = getTitle(psiElement, projectScopeSelected)
val model = InspectionProfileService.createSimpleProfile(toolWrapperList, managerEx, psiElement)
title?.let {
model.name = it
}

val inspectionContext = createNewGlobalContext(
managerEx, projectScopeSelected)
managerEx, projectScopeSelected
)
InspectionProfileService.setExternalProfile(model, inspectionContext)
return inspectionContext
}

private fun createNewGlobalContext(managerEx: InspectionManagerEx,
projectScopeSelected: Boolean): GlobalInspectionContextImpl {
return object : GlobalInspectionContextImpl(managerEx.project, managerEx.contentManager) {
override fun runTools(scope: AnalysisScope, runGlobalToolsOnly: Boolean,
isOfflineInspections: Boolean) {
super.runTools(scope, runGlobalToolsOnly, isOfflineInspections)
if (myProgressIndicator.isCanceled) {
return
}
InspectionActionExtensionPoint.extension.extensions.forEach {
try {
it.doOnInspectionFinished(this, projectScopeSelected)
} catch(e: Exception) {
logger.warn(e)
}
}
}
val toolWindow = ToolWindowManager.getInstance(project).getToolWindow(ToolWindowId.INSPECTION)

override fun close(noSuspiciousCodeFound: Boolean) {
super.close(noSuspiciousCodeFound)
InspectionActionExtensionPoint.extension.extensions.forEach {
try {
it.doOnClose(noSuspiciousCodeFound, project)
} catch(e: Exception) {
logger.warn(e)
}
}
if (toolWindow != null) {
val contentManager = toolWindow.contentManager
val contentTitle = title?.let {
InspectionsBundle.message("inspection.results.for.profile.toolwindow.title", it, scope.shortenName)
}

override fun addView(view: InspectionResultsView) {
super.addView(view)
InspectionActionExtensionPoint.extension.extensions.forEach {
try {
it.doOnView(view)
} catch(e: Exception) {
logger.warn(e)
}
}
val content = contentManager.contents.firstOrNull {
contentTitle != null && (it.tabName == contentTitle || it.tabName.endsWith(contentTitle))
}
content?.let {
contentManager.removeContent(content, true)
}
}

return inspectionContext
}

private fun createNewGlobalContext(
managerEx: InspectionManagerEx,
projectScopeSelected: Boolean
): GlobalInspectionContextImpl {
return PmdGlobalInspectionContextImpl(managerEx.project, managerEx.contentManager, projectScopeSelected)
}
}
}
Loading

0 comments on commit b9adb32

Please sign in to comment.