diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 8208dc76..167e4e5e 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -49,5 +49,5 @@ org-jetbrains-kotlin-gradle-plugin = { module = "org.jetbrains.kotlin:kotlin-gra [plugins] com-github-jk1-tcdeps = { id = "com.github.jk1.tcdeps", version = "1.6.2" } -com-jaredsburrows-license = { id = "com.jaredsburrows.license", version = "0.8.42" } +com-jaredsburrows-license = { id = "com.jaredsburrows.license", version = "0.9.8" } io-gitlab-arturbosch-detekt = { id = "io.gitlab.arturbosch.detekt", version = "1.22.0" } diff --git a/server/src/main/kotlin/org/javacs/kt/KotlinLanguageServer.kt b/server/src/main/kotlin/org/javacs/kt/KotlinLanguageServer.kt index e8da0ff9..d4a46f94 100644 --- a/server/src/main/kotlin/org/javacs/kt/KotlinLanguageServer.kt +++ b/server/src/main/kotlin/org/javacs/kt/KotlinLanguageServer.kt @@ -91,6 +91,7 @@ class KotlinLanguageServer( serverCapabilities.codeActionProvider = Either.forLeft(true) serverCapabilities.documentFormattingProvider = Either.forLeft(true) serverCapabilities.documentRangeFormattingProvider = Either.forLeft(true) + serverCapabilities.documentOnTypeFormattingProvider = DocumentOnTypeFormattingOptions("}", listOf(";", "\n")) serverCapabilities.executeCommandProvider = ExecuteCommandOptions(ALL_COMMANDS) serverCapabilities.documentHighlightProvider = Either.forLeft(true) diff --git a/server/src/main/kotlin/org/javacs/kt/KotlinTextDocumentService.kt b/server/src/main/kotlin/org/javacs/kt/KotlinTextDocumentService.kt index 2ec1e522..ffd9c79c 100644 --- a/server/src/main/kotlin/org/javacs/kt/KotlinTextDocumentService.kt +++ b/server/src/main/kotlin/org/javacs/kt/KotlinTextDocumentService.kt @@ -120,8 +120,27 @@ class KotlinTextDocumentService( documentHighlightsAt(file, cursor) } - override fun onTypeFormatting(params: DocumentOnTypeFormattingParams): CompletableFuture> { - TODO("not implemented") + override fun onTypeFormatting(params: DocumentOnTypeFormattingParams): CompletableFuture> = async.compute { + val code = params.textDocument.content + val position = params.position + val offset = offset(code, position.line, position.character) + + // Get the line up to the cursor position + val lineStart = code.lastIndexOf('\n', offset - 1) + 1 + val lineEnd = code.indexOf('\n', offset) + val line = if (lineEnd == -1) code.substring(lineStart) else code.substring(lineStart, lineEnd) + + // Format the line + val formattedLine = formattingService.formatKotlinCode(line, params.options) + + // Create a range for the line + val range = Range( + Position(position.line, 0), + Position(position.line, line.length) + ) + + // Return the edit + listOf(TextEdit(range, formattedLine)) } override fun definition(position: DefinitionParams): CompletableFuture, List>> = async.compute {