-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eee8afa
commit da6dc0f
Showing
5 changed files
with
91 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 29 additions & 12 deletions
41
server/src/main/kotlin/org/javacs/kt/formatting/Formatter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
|
||
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
28
server/src/main/kotlin/org/javacs/kt/formatting/KtFmtFormatter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
|