Repository: hannah-sten/texify-idea
Issue: Reformat File with Latexindent - Windows TeX Live

#### Type of JetBrains IDE (IntelliJ, PyCharm, etc.) and version
IntelliJ IDEA 2021.3.3 (Ultimate Edition)

#### Operating System 
Windows 11


#### TeXiFy IDEA version
0.7.15

#### What I did (steps to reproduce)
TeX Live contains `latexindent.exe`. If someone want to reformat the file using `latexindent`, the IDE will throw an error. `latexindent` seems to work fine in the command line but not in IntelliJ.

#### 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 -->

```latex
\\documentclass{article}
\\begin{document}
    \\begin{itemize}
\\end{itemize}
\\end{document}
```

#### Expected behavior
Reformatting file

#### Actual behavior
Error - cannot run programm "latexindent.pl" 

#### (if applicable) The full stacktrace of the exception thrown
```
Could not run latexindent.pl: Please double check if latexindent.pl is installed correctly: Cannot run program "latexindent.pl" (in directory "C:\\path-to-tex\\main.tex"): CreateProcess error=2, The system cannot find the file specified
```


Changed files:
src/nl/hannahsten/texifyidea/action/reformat/ReformatWithLatexindent.kt, src/nl/hannahsten/texifyidea/action/reformat/ExternalReformatAction.kt

diff --git a/src/nl/hannahsten/texifyidea/action/reformat/ExternalReformatAction.kt b/src/nl/hannahsten/texifyidea/action/reformat/ExternalReformatAction.kt
index 93a0f244b..98fc5fb36 100644
--- a/src/nl/hannahsten/texifyidea/action/reformat/ExternalReformatAction.kt
+++ b/src/nl/hannahsten/texifyidea/action/reformat/ExternalReformatAction.kt
@@ -99,7 +99,8 @@ abstract class ExternalReformatAction(title: String, val isValidFile: (file: Psi
      * If you just modify the file content in the background, only on re-focus will the user be asked if he wants to see the external changes made.
      */
     fun replaceLatexFileContent(output: String, file: PsiFile, project: Project) {
-        val newFile = LatexPsiHelper(project).createFromText(output)
+        // replaceChild does not like Windows line separators
+        val newFile = LatexPsiHelper(project).createFromText(output.replace("\\r\\n", "\\n"))
         runWriteCommandAction(project) {
             file.node.replaceChild(file.node.firstChildNode, newFile.node.firstChildNode)
         }
diff --git a/src/nl/hannahsten/texifyidea/action/reformat/ReformatWithLatexindent.kt b/src/nl/hannahsten/texifyidea/action/reformat/ReformatWithLatexindent.kt
index e5c2b5f69..40cd734fa 100644
--- a/src/nl/hannahsten/texifyidea/action/reformat/ReformatWithLatexindent.kt
+++ b/src/nl/hannahsten/texifyidea/action/reformat/ReformatWithLatexindent.kt
@@ -20,7 +20,8 @@ class ReformatWithLatexindent : ExternalReformatAction("Reformat File with Latex
         if (document != null) {
             FileDocumentManager.getInstance().saveDocument(document)
         }
-        return listOf("latexindent.pl", file.name)
+        // latexindent as command seems to work more often than latexindent.pl
+        return listOf("latexindent", file.name)
     }
 
     override fun processOutput(output: String, file: PsiFile, project: Project) {