Skip to content

Commit a1f16b2

Browse files
committed
implemented onTypeFormatting
1 parent 3bde12a commit a1f16b2

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

server/src/main/kotlin/org/javacs/kt/KotlinLanguageServer.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ class KotlinLanguageServer(
9191
serverCapabilities.codeActionProvider = Either.forLeft(true)
9292
serverCapabilities.documentFormattingProvider = Either.forLeft(true)
9393
serverCapabilities.documentRangeFormattingProvider = Either.forLeft(true)
94+
serverCapabilities.documentOnTypeFormattingProvider = DocumentOnTypeFormattingOptions("}", listOf(";", "\n"))
9495
serverCapabilities.executeCommandProvider = ExecuteCommandOptions(ALL_COMMANDS)
9596
serverCapabilities.documentHighlightProvider = Either.forLeft(true)
9697

server/src/main/kotlin/org/javacs/kt/KotlinTextDocumentService.kt

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,27 @@ class KotlinTextDocumentService(
120120
documentHighlightsAt(file, cursor)
121121
}
122122

123-
override fun onTypeFormatting(params: DocumentOnTypeFormattingParams): CompletableFuture<List<TextEdit>> {
124-
TODO("not implemented")
123+
override fun onTypeFormatting(params: DocumentOnTypeFormattingParams): CompletableFuture<List<TextEdit>> = async.compute {
124+
val code = params.textDocument.content
125+
val position = params.position
126+
val offset = offset(code, position.line, position.character)
127+
128+
// Get the line up to the cursor position
129+
val lineStart = code.lastIndexOf('\n', offset - 1) + 1
130+
val lineEnd = code.indexOf('\n', offset)
131+
val line = if (lineEnd == -1) code.substring(lineStart) else code.substring(lineStart, lineEnd)
132+
133+
// Format the line
134+
val formattedLine = formattingService.formatKotlinCode(line, params.options)
135+
136+
// Create a range for the line
137+
val range = Range(
138+
Position(position.line, 0),
139+
Position(position.line, line.length)
140+
)
141+
142+
// Return the edit
143+
listOf(TextEdit(range, formattedLine))
125144
}
126145

127146
override fun definition(position: DefinitionParams): CompletableFuture<Either<List<Location>, List<LocationLink>>> = async.compute {

0 commit comments

Comments
 (0)