Skip to content
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

Add formatting options #553

Merged
merged 2 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 14 additions & 1 deletion server/src/main/kotlin/org/javacs/kt/Configuration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ data class InlayHintsConfiguration(
var chainedHints: Boolean = false
)

data class KtFmtConfiguration(
var style: String = "google",
var indent: Int = 4,
var maxWidth: Int = 100,
var continuationIndent: Int = 8,
var removeUnusedImports: Boolean = true,
)

data class FormattingConfiguration(
var formatter: String = "ktfmt",
var ktFmt: KtFmtConfiguration = KtFmtConfiguration()
henryhchchc marked this conversation as resolved.
Show resolved Hide resolved
)

fun getStoragePath(params: InitializeParams): Path? {
params.initializationOptions?.let { initializationOptions ->
Expand Down Expand Up @@ -94,5 +106,6 @@ public data class Configuration(
val scripts: ScriptsConfiguration = ScriptsConfiguration(),
val indexing: IndexingConfiguration = IndexingConfiguration(),
val externalSources: ExternalSourcesConfiguration = ExternalSourcesConfiguration(),
val inlayHints: InlayHintsConfiguration = InlayHintsConfiguration()
val inlayHints: InlayHintsConfiguration = InlayHintsConfiguration(),
val formatting: FormattingConfiguration = FormattingConfiguration(),
)
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import org.javacs.kt.codeaction.codeActions
import org.javacs.kt.completion.completions
import org.javacs.kt.definition.goToDefinition
import org.javacs.kt.diagnostic.convertDiagnostic
import org.javacs.kt.formatting.formatKotlinCode
import org.javacs.kt.formatting.FormattingService
import org.javacs.kt.hover.hoverAt
import org.javacs.kt.position.offset
import org.javacs.kt.position.extractRange
Expand Down Expand Up @@ -45,6 +45,7 @@ class KotlinTextDocumentService(
) : TextDocumentService, Closeable {
private lateinit var client: LanguageClient
private val async = AsyncExecutor()
private val formattingService = FormattingService(config.formatting)

var debounceLint = Debouncer(Duration.ofMillis(config.diagnostics.debounceTime))
val lintTodo = mutableSetOf<URI>()
Expand Down Expand Up @@ -139,7 +140,7 @@ class KotlinTextDocumentService(
val code = extractRange(params.textDocument.content, params.range)
listOf(TextEdit(
params.range,
formatKotlinCode(code, params.options)
formattingService.formatKotlinCode(code, params.options)
))
}

Expand Down Expand Up @@ -215,7 +216,7 @@ class KotlinTextDocumentService(
LOG.info("Formatting {}", describeURI(params.textDocument.uri))
listOf(TextEdit(
Range(Position(0, 0), position(code, code.length)),
formatKotlinCode(code, params.options)
formattingService.formatKotlinCode(code, params.options)
))
}

Expand Down
16 changes: 16 additions & 0 deletions server/src/main/kotlin/org/javacs/kt/KotlinWorkspaceService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,22 @@ class KotlinWorkspaceService(
}
}

// Update options for formatting
get("formatting")?.asJsonObject?.apply {
val formatting = config.formatting
get("formatter")?.asString?.let {
formatting.formatter = it
}
get("ktfmt")?.asJsonObject?.apply {
henryhchchc marked this conversation as resolved.
Show resolved Hide resolved
val ktfmt = formatting.ktFmt
get("style")?.asString?.let { ktfmt.style = it }
get("indent")?.asInt?.let { ktfmt.indent = it }
get("maxWidth")?.asInt?.let { ktfmt.maxWidth = it }
get("continuationIndent")?.asInt?.let { ktfmt.continuationIndent = it }
get("removeUnusedImports")?.asBoolean?.let { ktfmt.removeUnusedImports = it }
}
}

// Update options for inlay hints
get("inlayHints")?.asJsonObject?.apply {
val inlayHints = config.inlayHints
Expand Down
41 changes: 29 additions & 12 deletions server/src/main/kotlin/org/javacs/kt/formatting/Formatter.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
package org.javacs.kt.formatting

import com.facebook.ktfmt.format.Formatter
import com.facebook.ktfmt.format.FormattingOptions as KtfmtOptions
import org.eclipse.lsp4j.FormattingOptions

fun formatKotlinCode(
code: String,
options: FormattingOptions = FormattingOptions(4, true)
): String = Formatter.format(KtfmtOptions(
style = KtfmtOptions.Style.GOOGLE,
blockIndent = options.tabSize,
continuationIndent = 2 * options.tabSize
), code)
import org.javacs.kt.Configuration
import org.javacs.kt.FormattingConfiguration
import org.eclipse.lsp4j.FormattingOptions as LspFromattingOptions

private const val DEFAULT_INDENT = 4

class FormattingService(private val config: FormattingConfiguration) {

private val formatter: Formatter get() = when (config.formatter) {
"ktfmt" -> KtFmtFormatter(config.ktFmt)
"none" -> NopFormatter
else -> KtFmtFormatter(config.ktFmt)
}

fun formatKotlinCode(
code: String,
options: LspFromattingOptions = LspFromattingOptions(DEFAULT_INDENT, true)
): String = this.formatter.format(code, options)
}
henryhchchc marked this conversation as resolved.
Show resolved Hide resolved


interface Formatter {
fun format(code: String, options: LspFromattingOptions): String
}

object NopFormatter : Formatter {
override fun format(code: String, options: LspFromattingOptions): String = code
}

28 changes: 28 additions & 0 deletions server/src/main/kotlin/org/javacs/kt/formatting/KtFmtFormatter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.javacs.kt.formatting

import org.javacs.kt.KtFmtConfiguration
import com.facebook.ktfmt.format.Formatter as KtFmt
import com.facebook.ktfmt.format.FormattingOptions as KtfmtOptions
import org.eclipse.lsp4j.FormattingOptions as LspFormattingOptions

class KtFmtFormatter(private val config: KtFmtConfiguration) : Formatter {
override fun format(
code: String,
options: LspFormattingOptions,
): String {
val style = when (config.style) {
"google" -> KtfmtOptions.Style.GOOGLE
"facebook" -> KtfmtOptions.Style.FACEBOOK
"dropbox" -> KtfmtOptions.Style.DROPBOX
else -> KtfmtOptions.Style.GOOGLE
}
return KtFmt.format(KtfmtOptions(
style = style,
maxWidth = config.maxWidth,
blockIndent = options.tabSize.takeUnless { it == 0 } ?: config.indent,
continuationIndent = config.continuationIndent,
removeUnusedImports = config.removeUnusedImports,
), code)
}
}

Loading