Skip to content

Commit

Permalink
feat: enable run inspection on project start
Browse files Browse the repository at this point in the history
  • Loading branch information
yaohui-wyh authored and rogeryhwang committed Jul 17, 2022
1 parent df323e8 commit 49a4935
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,31 @@
package io.gitpod.jetbrains.remote

import com.intellij.ProjectTopics
import com.intellij.analysis.AnalysisScope
import com.intellij.codeInspection.actions.RunInspectionIntention
import com.intellij.codeInspection.ex.InspectionManagerEx
import com.intellij.openapi.diagnostic.thisLogger
import com.intellij.openapi.module.Module
import com.intellij.openapi.module.ModuleManager
import com.intellij.openapi.project.DumbService
import com.intellij.openapi.project.ModuleListener
import com.intellij.openapi.project.Project
import com.intellij.openapi.projectRoots.ProjectJdkTable
import com.intellij.openapi.projectRoots.Sdk
import com.intellij.openapi.roots.ModuleRootModificationUtil
import com.intellij.openapi.roots.ProjectRootManager
import com.intellij.openapi.vfs.VfsUtil
import com.intellij.profile.codeInspection.InspectionProfileManager
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiManager
import com.intellij.util.application
import io.gitpod.jetbrains.remote.inspections.GitpodConfigInspection
import io.gitpod.jetbrains.remote.utils.GitpodConfig.gitpodYamlFile
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.future.await
import kotlinx.coroutines.launch
import org.jetbrains.yaml.psi.YAMLFile
import java.nio.file.Paths
import java.util.concurrent.CompletableFuture


Expand All @@ -29,6 +41,34 @@ class GitpodProjectManager(
configureSdks()
}

init {
application.invokeLater {
try {
runInspection()
} catch (ex: Exception) {
thisLogger().error("Failed to run inspection", ex)
}
}
}

private fun runInspection() {
val psiFile = getGitpodYamlPsiFile(project) ?: return
val profile = InspectionProfileManager.getInstance(project).currentProfile
val inspectionName = GitpodConfigInspection::class.java.simpleName
val tool = profile.getInspectionTool(inspectionName, psiFile) ?: return
val manager = InspectionManagerEx.getInstance(project) as InspectionManagerEx
val scope = AnalysisScope(psiFile)
DumbService.getInstance(project).smartInvokeLater {
RunInspectionIntention.rerunInspection(tool, manager, scope, psiFile)
}
}

private fun getGitpodYamlPsiFile(project: Project): PsiFile? {
val basePath = project.basePath ?: return null
val vfile = VfsUtil.findFile(Paths.get(basePath, gitpodYamlFile), true) ?: return null
return PsiManager.getInstance(project).findFile(vfile) as? YAMLFile ?: return null
}

/**
* It is a workaround for https://youtrack.jetbrains.com/issue/GTW-88
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<projectService serviceImplementation="io.gitpod.jetbrains.remote.GitpodProjectManager" preload="true"/>
<gateway.customization.name implementation="io.gitpod.jetbrains.remote.GitpodGatewayClientCustomizationProvider"/>
<localInspection language="yaml" bundle="messages.GitpodBundle"
shortName="GitpodConfigInspection"
groupKey="inspections.group.name"
key="inspections.gitpod.schema.validation.name"
level="WARNING" enabledByDefault="true"
Expand Down

0 comments on commit 49a4935

Please sign in to comment.