-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
LatexProjectSdkSetupValidator.kt
46 lines (41 loc) · 2.43 KB
/
LatexProjectSdkSetupValidator.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package nl.hannahsten.texifyidea.settings.sdk
import com.intellij.codeInsight.daemon.ProjectSdkSetupValidator
import com.intellij.openapi.module.ModuleManager
import com.intellij.openapi.module.ModuleUtilCore
import com.intellij.openapi.project.Project
import com.intellij.openapi.projectRoots.SdkTypeId
import com.intellij.openapi.roots.ModuleRootManager
import com.intellij.openapi.roots.ProjectRootManager
import com.intellij.openapi.roots.ui.configuration.SdkPopupFactory
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.PsiManager
import com.intellij.ui.EditorNotificationPanel
import nl.hannahsten.texifyidea.LatexLanguage
import nl.hannahsten.texifyidea.file.LatexFileType
import nl.hannahsten.texifyidea.modules.LatexModuleType
/**
* https://jetbrains.org/intellij/sdk/docs/reference_guide/project_model/sdk.html#assisting-in-setting-up-an-sdk
*
* @author Thomas
*/
class LatexProjectSdkSetupValidator : ProjectSdkSetupValidator {
override fun isApplicableFor(project: Project, file: VirtualFile): Boolean {
// Check if setting up a LaTeX SDK would make sense
val isLatexFile = file.fileType is LatexFileType || PsiManager.getInstance(project).findFile(file)?.language?.isKindOf(LatexLanguage) == true
return ModuleManager.getInstance(project).modules.any { it.moduleTypeName == LatexModuleType.ID } && isLatexFile
}
override fun getErrorMessage(project: Project, file: VirtualFile): String? {
// Based on https://github.com/rikvdkleij/intellij-haskell/blob/895a214b174b69f661d4f7d4230633058fca8f1e/src/main/scala/intellij/haskell/notification/HaskellProjectSdkSetupValidator.scala#L24
// Nothing to do if we don't need an SDK
if (LatexSdkUtil.isPdflatexInPath) return null
val module = ModuleUtilCore.findModuleForFile(file, project) ?: return null
if (ModuleRootManager.getInstance(module).sdk?.sdkType is LatexSdk || ProjectRootManager.getInstance(project).projectSdk?.sdkType is LatexSdk) return null
return "No LaTeX installation could be found. Please add it to PATH or set up a LaTeX SDK (and reopen this file)."
}
override fun getFixHandler(project: Project, file: VirtualFile): EditorNotificationPanel.ActionHandler {
return SdkPopupFactory.newBuilder().withProject(project)
.withSdkTypeFilter { id: SdkTypeId -> id is LatexSdk }
.updateProjectSdkFromSelection()
.buildEditorNotificationPanelHandler()
}
}