|  | 
| 5 | 5 | import com.intellij.openapi.editor.Document; | 
| 6 | 6 | import com.intellij.openapi.project.Project; | 
| 7 | 7 | import com.intellij.openapi.vfs.ReadonlyStatusHandler; | 
| 8 |  | -import com.intellij.openapi.vfs.VirtualFile; | 
| 9 | 8 | import com.intellij.psi.PsiDocumentManager; | 
| 10 |  | -import com.intellij.psi.PsiFile; | 
| 11 | 9 | import org.jetbrains.annotations.NotNull; | 
| 12 | 10 | 
 | 
| 13 | 11 | import java.time.Duration; | 
| @@ -52,29 +50,53 @@ public void format(@NotNull Document document, @NotNull Project project) { | 
| 52 | 50 | 
 | 
| 53 | 51 |         var currentDocumentText = document.getText(); | 
| 54 | 52 | 
 | 
| 55 |  | -        var cSharpierProcessProvider = CSharpierProcessProvider.getInstance(project); | 
|  | 53 | +        var csharpierProcess = CSharpierProcessProvider.getInstance(project).getProcessFor(filePath); | 
| 56 | 54 |         this.logger.info("Formatting started for " + filePath + "."); | 
| 57 | 55 |         var start = Instant.now(); | 
| 58 |  | -        var result = cSharpierProcessProvider.getProcessFor(filePath).formatFile(currentDocumentText, filePath); | 
| 59 |  | - | 
| 60 |  | -        var end = Instant.now(); | 
| 61 |  | -        this.logger.info("Formatted in " + (Duration.between(start, end).toMillis()) + "ms"); | 
| 62 |  | - | 
| 63 |  | -        if (result.length() == 0 || currentDocumentText.equals(result)) { | 
| 64 |  | -            this.logger.debug("Skipping write because " + (result.length() == 0 ? "result is empty" : "current document equals result")); | 
| 65 |  | -        } else { | 
| 66 |  | -            WriteCommandAction.runWriteCommandAction(project, () -> { | 
| 67 |  | -                var finalResult = result; | 
| 68 |  | -                if (result.indexOf('\r') >= 0) { | 
| 69 |  | -                    // rider always wants \n in files so remove any \r | 
| 70 |  | -                    finalResult = result.replaceAll("\\r", ""); | 
|  | 56 | +        if (csharpierProcess instanceof ICSharpierProcess2) { | 
|  | 57 | +            var csharpierProcess2 = (ICSharpierProcess2) csharpierProcess; | 
|  | 58 | +            var parameter = new FormatFileParameter(); | 
|  | 59 | +            parameter.fileContents = currentDocumentText; | 
|  | 60 | +            parameter.fileName = filePath; | 
|  | 61 | +            var result = csharpierProcess2.formatFile(parameter); | 
|  | 62 | + | 
|  | 63 | +            var end = Instant.now(); | 
|  | 64 | +            this.logger.info("Formatted in " + (Duration.between(start, end).toMillis()) + "ms"); | 
|  | 65 | + | 
|  | 66 | +            if (result != null) { | 
|  | 67 | +                switch (result.status) { | 
|  | 68 | +                    case Formatted -> updateText(document, project, result.formattedFile, currentDocumentText); | 
|  | 69 | +                    case Ignored -> this.logger.info("File is ignored by csharpier cli."); | 
|  | 70 | +                    case Failed -> this.logger.warn("CSharpier cli failed to format the file and returned the following error: " + result.errorMessage); | 
| 71 | 71 |                 } | 
|  | 72 | +            } | 
|  | 73 | +        } | 
|  | 74 | +        else { | 
|  | 75 | +            var result = csharpierProcess.formatFile(currentDocumentText, filePath); | 
|  | 76 | + | 
|  | 77 | +            var end = Instant.now(); | 
|  | 78 | +            this.logger.info("Formatted in " + (Duration.between(start, end).toMillis()) + "ms"); | 
| 72 | 79 | 
 | 
| 73 |  | -                document.replaceString(0, currentDocumentText.length(), finalResult); | 
| 74 |  | -            }); | 
|  | 80 | +            if (result.length() == 0 || currentDocumentText.equals(result)) { | 
|  | 81 | +                this.logger.debug("Skipping write because " + (result.length() == 0 ? "result is empty" : "current document equals result")); | 
|  | 82 | +            } else { | 
|  | 83 | +                updateText(document, project, result, currentDocumentText); | 
|  | 84 | +            } | 
| 75 | 85 |         } | 
| 76 | 86 |     } | 
| 77 | 87 | 
 | 
|  | 88 | +    private static void updateText(@NotNull Document document, @NotNull Project project, String result, String currentDocumentText) { | 
|  | 89 | +        WriteCommandAction.runWriteCommandAction(project, () -> { | 
|  | 90 | +            var finalResult = result; | 
|  | 91 | +            if (result.indexOf('\r') >= 0) { | 
|  | 92 | +                // rider always wants \n in files so remove any \r | 
|  | 93 | +                finalResult = result.replaceAll("\\r", ""); | 
|  | 94 | +            } | 
|  | 95 | + | 
|  | 96 | +            document.replaceString(0, currentDocumentText.length(), finalResult); | 
|  | 97 | +        }); | 
|  | 98 | +    } | 
|  | 99 | + | 
| 78 | 100 |     public boolean getCanFormat(String filePath, Project project) { | 
| 79 | 101 |         var cSharpierProcess = CSharpierProcessProvider.getInstance(project).getProcessFor(filePath); | 
| 80 | 102 |         return !NullCSharpierProcess.Instance.equals(cSharpierProcess); | 
|  | 
0 commit comments