Repository: hannah-sten/texify-idea
Issue: SumatraPDF does not open after compilation

#### Type of JetBrains IDE (IntelliJ, PyCharm, etc.) and version
Pycharm Community Edition 2022.1.1


#### Operating System 
Windows 10


#### TeXiFy IDEA version
0.7.19


#### What I did (steps to reproduce)
Install 64-bit SumatraPDF
Install TeXiFy IDEA
Click "Edit Configurations" in Pycharm (top right)
Create new LaTeX Run/Debug Configuration
Under "PDF Viewer" only option available is "No PDF viewer"
Selecting custom path to SumatraPDF does not fix the issue
Also, in Pycharm under Tools > LaTeX (top), SumatraPDF doesn't show up.


#### Minimal example to reproduce the problem
<!-- Make sure you have read and understood how to create a minimal example: https://tex.meta.stackexchange.com/questions/228/ive-just-been-asked-to-write-a-minimal-example-what-is-that -->
Try to create Run/Debug Configuration for LaTeX file, SumatraPDF doesn't work.


#### Expected behavior
Open output PDF on LaTeX file compilation.

#### Actual behavior
No PDF opened. SumatraPDF works just fine in any other situation.

#### (if applicable) The full stacktrace of the exception thrown
```

```

<!-- If you see a minor issue related to commands or environments metadata, e.g. which commands are cite or verbatim commands, you are encouraged to try to add the missing information yourself, see https://github.com/Hannah-Sten/TeXiFy-IDEA/wiki/Contributing-to-TeXiFy#editing-magic -->

Changed files:
src/nl/hannahsten/texifyidea/run/latex/LatexCommandLineState.kt, src/nl/hannahsten/texifyidea/util/Log.kt, src/nl/hannahsten/texifyidea/settings/sdk/DockerSdk.kt, src/nl/hannahsten/texifyidea/settings/sdk/NativeTexliveSdk.kt, src/nl/hannahsten/texifyidea/run/sumatra/SumatraConversation.kt

diff --git a/src/nl/hannahsten/texifyidea/run/latex/LatexCommandLineState.kt b/src/nl/hannahsten/texifyidea/run/latex/LatexCommandLineState.kt
index a6eb1646c..a4a79d37d 100644
--- a/src/nl/hannahsten/texifyidea/run/latex/LatexCommandLineState.kt
+++ b/src/nl/hannahsten/texifyidea/run/latex/LatexCommandLineState.kt
@@ -237,9 +237,9 @@ open class LatexCommandLineState(environment: ExecutionEnvironment, private val
             handler.addProcessListener(OpenCustomPdfViewerListener(commandList.toTypedArray(), runConfig = runConfig))
         }
         // Do nothing if the user selected that they do not want a viewer to open.
-        else if (runConfig.pdfViewer == InternalPdfViewer.NONE) return
+        else if (runConfig.pdfViewer == InternalPdfViewer.NONE && runConfig.sumatraPath == null) return
         // Sumatra does not support DVI
-        else if (runConfig.pdfViewer == InternalPdfViewer.SUMATRA && (runConfig.sumatraPath != null || isSumatraAvailable) && runConfig.outputFormat == LatexCompiler.Format.PDF) {
+        else if ((runConfig.sumatraPath != null || (runConfig.pdfViewer == InternalPdfViewer.SUMATRA && isSumatraAvailable)) && runConfig.outputFormat == LatexCompiler.Format.PDF) {
             // Open Sumatra after compilation & execute inverse search.
             handler.addProcessListener(SumatraForwardSearchListener(runConfig, environment))
         }
diff --git a/src/nl/hannahsten/texifyidea/run/sumatra/SumatraConversation.kt b/src/nl/hannahsten/texifyidea/run/sumatra/SumatraConversation.kt
index d11e15609..666cacdd6 100644
--- a/src/nl/hannahsten/texifyidea/run/sumatra/SumatraConversation.kt
+++ b/src/nl/hannahsten/texifyidea/run/sumatra/SumatraConversation.kt
@@ -6,7 +6,8 @@ import com.pretty_tools.dde.client.DDEClientConversation
 import nl.hannahsten.texifyidea.TeXception
 import nl.hannahsten.texifyidea.run.linuxpdfviewer.ViewerConversation
 import nl.hannahsten.texifyidea.util.Log
-import java.io.IOException
+import nl.hannahsten.texifyidea.util.runCommandWithExitCode
+import nl.hannahsten.texifyidea.util.runCommand
 
 /**
  * Indicates whether SumatraPDF is installed and DDE communication is enabled.
@@ -34,29 +35,15 @@ val isSumatraAvailable: Boolean by lazy {
 }
 
 private fun isSumatraInstalled(): Boolean {
-    // Look up SumatraPDF registry key
-    val process = Runtime.getRuntime().exec(
-        "reg query \\"HKEY_LOCAL_MACHINE\\\\SOFTWARE\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\App Paths\\\\SumatraPDF.exe\\" /ve"
-    )
+    // Try some SumatraPDF registry keys
+    // For some reason this first one isn't always present anymore, it used to be
+    val regQuery1 = runCommand("reg", "query", "HKEY_LOCAL_MACHINE\\\\SOFTWARE\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\App Paths\\\\SumatraPDF.exe", "/ve")?.startsWith("ERROR:") == false
+    val regQuery2 = runCommand("reg", "query", "HKEY_LOCAL_MACHINE\\\\SOFTWARE\\\\Classes\\\\SumatraPDF.pdf", "/ve")?.startsWith("ERROR:") == false
 
-    val br = process.inputStream.bufferedReader()
-    val firstLine = br.readLine() ?: return false
-    br.close()
-
-    val sumatraInRegistry = !firstLine.startsWith("ERROR:")
-
-    if (sumatraInRegistry) {
-        return true
-    }
+    if (regQuery1 || regQuery2) return true
 
     // Try if Sumatra is in PATH
-    return try {
-        Runtime.getRuntime().exec("start SumatraPDF")
-        true
-    }
-    catch (e: IOException) {
-        false
-    }
+    return runCommandWithExitCode("start", "SumatraPDF").second == 0
 }
 
 /**
diff --git a/src/nl/hannahsten/texifyidea/settings/sdk/DockerSdk.kt b/src/nl/hannahsten/texifyidea/settings/sdk/DockerSdk.kt
index 95d49dd15..043f393d1 100644
--- a/src/nl/hannahsten/texifyidea/settings/sdk/DockerSdk.kt
+++ b/src/nl/hannahsten/texifyidea/settings/sdk/DockerSdk.kt
@@ -5,6 +5,7 @@ import com.intellij.openapi.projectRoots.*
 import com.intellij.openapi.projectRoots.impl.SdkConfigurationUtil
 import com.intellij.openapi.ui.ComboBox
 import com.intellij.openapi.ui.DialogBuilder
+import com.intellij.openapi.util.SystemInfo
 import com.intellij.util.Consumer
 import nl.hannahsten.texifyidea.run.latex.LatexDistributionType
 import nl.hannahsten.texifyidea.util.runCommand
@@ -35,6 +36,9 @@ class DockerSdk : LatexSdk("LaTeX Docker SDK") {
     }
 
     override fun suggestHomePaths(): MutableCollection<String> {
+        // Windows is not supported for now
+        if (!SystemInfo.isLinux) return mutableListOf()
+
         // There's not really a path with 'sources' for docker except the location of images, but that's OS-dependent
         // and we only need to be able to execute the 'docker' executable anyway
         return "which docker".runCommand()?.let { output -> mutableListOf(
diff --git a/src/nl/hannahsten/texifyidea/settings/sdk/NativeTexliveSdk.kt b/src/nl/hannahsten/texifyidea/settings/sdk/NativeTexliveSdk.kt
index 57d259e48..fb50bae35 100644
--- a/src/nl/hannahsten/texifyidea/settings/sdk/NativeTexliveSdk.kt
+++ b/src/nl/hannahsten/texifyidea/settings/sdk/NativeTexliveSdk.kt
@@ -1,6 +1,7 @@
 package nl.hannahsten.texifyidea.settings.sdk
 
 import com.intellij.openapi.projectRoots.Sdk
+import com.intellij.openapi.util.SystemInfo
 import nl.hannahsten.texifyidea.util.runCommand
 
 /**
@@ -32,7 +33,7 @@ class NativeTexliveSdk : TexliveSdk("Native TeX Live SDK") {
     override fun suggestHomePaths(): MutableCollection<String> {
         // Note that suggested paths appear under "Detected SDK's" when adding an SDK
         val results = mutableSetOf<String>()
-        val path = "which pdflatex".runCommand()
+        val path = if (SystemInfo.isWindows) "where pdflatex".runCommand() else "which pdflatex".runCommand()
 
         // Avoid duplicates of TexliveSdks, which probably have x86_64-linux in the path
         if (!path.isNullOrEmpty() && !path.contains("x86_64-linux")) {
diff --git a/src/nl/hannahsten/texifyidea/util/Log.kt b/src/nl/hannahsten/texifyidea/util/Log.kt
index d7eebf4d5..28d1b5c37 100644
--- a/src/nl/hannahsten/texifyidea/util/Log.kt
+++ b/src/nl/hannahsten/texifyidea/util/Log.kt
@@ -4,6 +4,10 @@ import com.intellij.openapi.diagnostic.Logger
 
 /**
  * Log messages to idea.log. During plugin development, it logs to build/idea-sandbox/system/log/idea.log.
+ * You can add a tab to the run configuration tool window by adding 'Specify logs to be shown in console' to the runIde run configuration.
+ * To view debug logs, in the dev instance go to Help > Diagnostic Tools > Debug Log Settings and add #nl.hannahsten.texifyidea.util.Log.
+ * Then in the console, make sure the filter is set to All instead of Warning.
+ *
  * @author Hannah Schellekens
  */
 object Log {