Skip to content

Commit 11614e7

Browse files
committed
Daemon plain file and plain text format support
1 parent ef56be9 commit 11614e7

3 files changed

Lines changed: 83 additions & 2 deletions

File tree

kls_database.db

132 KB
Binary file not shown.

kotlin-format/src/main/kotlin/xyz/block/kotlinformatter/Daemon.kt

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package xyz.block.kotlinformatter
22

3+
import java.io.ByteArrayInputStream
34
import java.io.RandomAccessFile
45
import java.net.ServerSocket
56
import java.net.Socket
@@ -132,6 +133,38 @@ class Daemon(
132133
clientSocket.write(ExitCode.SUCCESS, response)
133134
}
134135

136+
"format" -> {
137+
lastCommandTime = Instant.now()
138+
val fileArgs = messageParts.drop(1)
139+
if (fileArgs.isEmpty()) {
140+
clientSocket.write(ExitCode.FAILURE, "No files provided for format command")
141+
} else if (fileArgs.size == 1 && fileArgs[0] == "-") {
142+
// Process the code starting from a newline and ending at ASCII EOT (04)
143+
// Usage: (echo "format -"; echo <CODE>; echo -ne '\04\n') | nc localhost <PORT>
144+
// Or replace "echo <CODE>" to "cat" for standard input
145+
val inputLines = mutableListOf<String>()
146+
while (true) {
147+
val line = reader.readLine() ?: break
148+
if (line.trim() == "\u0004") break
149+
inputLines.add(line)
150+
}
151+
val inputContent = inputLines.joinToString("\n")
152+
val inputStream = ByteArrayInputStream(inputContent.toByteArray())
153+
lateinit var formatted: String
154+
val result = KotlinFormatter(
155+
files = listOf("-"),
156+
inputStream = inputStream,
157+
outputCallback = { formatted = it }
158+
).format()
159+
val exitCode = if (result.hasFailure) ExitCode.FAILURE else ExitCode.SUCCESS
160+
clientSocket.write(exitCode, if (result.hasFailure) result.output else formatted)
161+
} else {
162+
val result = KotlinFormatter(files = fileArgs).format()
163+
val exitCode = if (result.hasFailure) ExitCode.FAILURE else ExitCode.SUCCESS
164+
clientSocket.write(exitCode, result.output)
165+
}
166+
}
167+
135168
"pre-commit" -> {
136169
lastCommandTime = Instant.now()
137170
val files = messageParts.drop(1)
@@ -245,4 +278,4 @@ class Daemon(
245278
val LOCK_FILE_PATH = Paths.get(".kotlinformatter/kf.lock")
246279
val rootGitPath by lazy { Paths.get(GitProcessRunner.run("rev-parse", "--show-toplevel").trim()) }
247280
}
248-
}
281+
}

kotlin-format/src/test/kotlin/xyz/block/kotlinformatter/DaemonTest.kt

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,54 @@ class DaemonTest {
112112
assertThat(secondDaemonStarted).isFalse()
113113
}
114114

115+
@Test
116+
fun `format command with file arguments`() {
117+
val testFile = File(tempDir.toFile(), "FormatTest.kt")
118+
val unformatted = "fun foo()=println(\"Hello\")"
119+
val expectedFormatted = "fun foo() = println(\"Hello\")\n"
120+
testFile.writeText(unformatted)
121+
122+
val daemonData = daemon.readLockFile()
123+
assertThat(daemonData).isNotNull
124+
125+
// Send format command
126+
Socket("localhost", daemonData!!.port).use { socket ->
127+
socket.getOutputStream().write("format ${testFile.absolutePath}\n".toByteArray())
128+
val reader = socket.getInputStream().bufferedReader()
129+
val exitCode = reader.readLine()
130+
assertThat(exitCode).isEqualTo(ExitCode.SUCCESS.toString())
131+
val output = reader.readText()
132+
assertThat(output).contains("✅ Formatted")
133+
}
134+
135+
// Verify file content
136+
val formattedContent = testFile.readText()
137+
assertThat(formattedContent).isEqualTo(expectedFormatted)
138+
}
139+
140+
@Test
141+
fun `format command with 'stdin' mode`() {
142+
val unformatted = "fun foo()=println(\"Hello\")"
143+
val expectedFormatted = "fun foo() = println(\"Hello\")\n"
144+
145+
val daemonData = daemon.readLockFile()
146+
assertThat(daemonData).isNotNull
147+
148+
// Send format -
149+
Socket("localhost", daemonData!!.port).use { socket ->
150+
val outputStream = socket.getOutputStream()
151+
outputStream.write("format -\n".toByteArray())
152+
outputStream.write(unformatted.toByteArray())
153+
outputStream.write("\n\u0004\n".toByteArray())
154+
155+
val reader = socket.getInputStream().bufferedReader()
156+
val exitCode = reader.readLine()
157+
assertThat(exitCode).isEqualTo(ExitCode.SUCCESS.toString())
158+
val output = reader.readText()
159+
assertThat(output).isEqualTo(expectedFormatted)
160+
}
161+
}
162+
115163
@Test
116164
fun `handles pre-commit command`() {
117165
val testFile = File(tempDir.toFile(), "Test.kt")
@@ -330,4 +378,4 @@ class DaemonTest {
330378
val MAX_RUNTIME = Duration.ofSeconds(5)
331379
val TIMEOUT_CHECKER_INTERVAL = Duration.ofSeconds(1)
332380
}
333-
}
381+
}

0 commit comments

Comments
 (0)