Skip to content

Commit

Permalink
fix: set format line separators on save (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannesIBK authored Oct 4, 2024
1 parent 8eee09c commit 8f67583
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.github.biomejs.intellijbiome.actions

import com.github.biomejs.intellijbiome.*
import com.github.biomejs.intellijbiome.settings.BiomeSettings
import com.intellij.codeStyle.AbstractConvertLineSeparatorsAction
import com.intellij.lang.javascript.linter.GlobPatternUtil
import com.intellij.openapi.command.WriteCommandAction
import com.intellij.openapi.diagnostic.thisLogger
Expand All @@ -12,6 +13,9 @@ import com.intellij.openapi.progress.ProgressIndicator
import com.intellij.openapi.progress.ProgressManager
import com.intellij.openapi.progress.Task
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.text.StringUtil
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.util.LineSeparator
import java.util.*

class BiomeCheckRunner {
Expand Down Expand Up @@ -77,10 +81,22 @@ class BiomeCheckRunner {
) {
when (response) {
is BiomeRunner.Response.Success -> {
val text = response.code
val lineSeparator = StringUtil.detectSeparators(text)

WriteCommandAction.writeCommandAction(project)
.withName(request.commandDescription)
.run<Exception> {
request.document.setText(response.code)
if (!StringUtil.equals(request.document.text, text)) {
request.document.setText(text)
}

setDetectedLineSeparator(
project,
request.virtualFile,
lineSeparator
)

FileDocumentManager.getInstance().saveDocument(request.document)
}
}
Expand All @@ -90,4 +106,22 @@ class BiomeCheckRunner {
}
}
}

/**
* [Taken from the JetBrains Prettier Plugin](https://github.com/JetBrains/intellij-plugins/blob/5673be79dd9e0fff7ed98e58a7d071a5a5f96d87/prettierJS/src/com/intellij/prettierjs/ReformatWithPrettierAction.java#L486)
* [Apache License 2.0](https://github.com/JetBrains/intellij-plugins/blob/5673be79dd9e0fff7ed98e58a7d071a5a5f96d87/prettierJS/LICENSE.TXT)
*
* @return true if the line separators were updated
*/
private fun setDetectedLineSeparator(project: Project, vFile: VirtualFile, newSeparator: LineSeparator?): Boolean {
if (newSeparator != null) {
val newSeparatorString: String = newSeparator.separatorString

if (!StringUtil.equals(vFile.detectedLineSeparator, newSeparatorString)) {
AbstractConvertLineSeparatorsAction.changeLineSeparators(project, vFile, newSeparatorString)
return true
}
}
return false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.intellij.javascript.nodejs.interpreter.NodeJsInterpreterManager
import com.intellij.javascript.nodejs.interpreter.local.NodeJsLocalInterpreter
import com.intellij.javascript.nodejs.interpreter.wsl.WslNodeInterpreter
import com.intellij.openapi.project.Project
import com.intellij.util.io.BaseOutputReader
import java.io.File
import java.nio.charset.StandardCharsets
import java.util.concurrent.CompletableFuture
Expand All @@ -23,7 +24,15 @@ val ProcessEvent.isSuccess: Boolean get() = exitCode == 0
fun GeneralCommandLine.runProcessFuture(): CompletableFuture<ProcessResult> {
val future = CompletableFuture<ProcessResult>()

val processHandler = CapturingProcessHandler(this.withCharset(StandardCharsets.UTF_8))
val processHandler = object: CapturingProcessHandler(this.withCharset(StandardCharsets.UTF_8)) {
override fun readerOptions(): BaseOutputReader.Options {
return object : BaseOutputReader.Options() {
// This option ensures that line separators are not converted to LF
// when the formatter sends e.g., CRLF
override fun splitToLines(): Boolean = false
}
}
}

processHandler.addProcessListener(object : CapturingProcessAdapter() {
override fun processTerminated(event: ProcessEvent) {
Expand Down

0 comments on commit 8f67583

Please sign in to comment.