Skip to content

Feat: Implemented onTypeFormatting #654

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
23 changes: 21 additions & 2 deletions server/src/main/kotlin/org/javacs/kt/KotlinTextDocumentService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,27 @@ class KotlinTextDocumentService(
documentHighlightsAt(file, cursor)
}

override fun onTypeFormatting(params: DocumentOnTypeFormattingParams): CompletableFuture<List<TextEdit>> {
TODO("not implemented")
override fun onTypeFormatting(params: DocumentOnTypeFormattingParams): CompletableFuture<List<TextEdit>> = 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<Either<List<Location>, List<LocationLink>>> = async.compute {
Expand Down