-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #553 from henryhchchc/formatting-opts
Add formatting options
- Loading branch information
Showing
7 changed files
with
96 additions
and
15 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
16 changes: 5 additions & 11 deletions
16
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,8 @@ | ||
package org.javacs.kt.formatting | ||
|
||
import com.facebook.ktfmt.format.Formatter | ||
import com.facebook.ktfmt.format.FormattingOptions as KtfmtOptions | ||
import org.eclipse.lsp4j.FormattingOptions | ||
import org.eclipse.lsp4j.FormattingOptions as LspFromattingOptions | ||
|
||
interface Formatter { | ||
fun format(code: String, options: LspFromattingOptions): String | ||
} | ||
|
||
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) |
21 changes: 21 additions & 0 deletions
21
server/src/main/kotlin/org/javacs/kt/formatting/FormattingService.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,21 @@ | ||
package org.javacs.kt.formatting | ||
|
||
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) | ||
} |
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) | ||
} | ||
} | ||
|
8 changes: 8 additions & 0 deletions
8
server/src/main/kotlin/org/javacs/kt/formatting/NopFormatter.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,8 @@ | ||
package org.javacs.kt.formatting | ||
|
||
import org.eclipse.lsp4j.FormattingOptions as LspFormattingOptions | ||
|
||
object NopFormatter : Formatter { | ||
override fun format(code: String, options: LspFormattingOptions): String = code | ||
} | ||
|