Repository navigation
ADFA-3021: Format Kotlin code with ktfmt #2089
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
aca5b95
style: spotless reformat, no functional change
Daniel-ADFA 480e21c
ADFA-3021: Format Kotlin with ktfmt
Daniel-ADFA 6176b40
ADFA-3021: Report a failed format instead of doing nothing
Daniel-ADFA 81f5bda
ADFA-3021: Count a stripped shebang line in parse error positions
Daniel-ADFA 53b5811
Merge branch 'stage' into bugfix/ADFA-3021-kotlin-format-code
Daniel-ADFA File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
31 changes: 31 additions & 0 deletions
31
editor/src/test/java/com/itsaky/androidide/editor/language/LSPFormatterTest.kt
This file contains hidden or 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,31 @@ | ||
| package com.itsaky.androidide.editor.language | ||
|
|
||
| import com.google.common.truth.Truth.assertThat | ||
| import com.itsaky.androidide.lsp.api.ILanguageServer | ||
| import io.github.rosemoe.sora.text.CharPosition | ||
| import io.github.rosemoe.sora.text.Content | ||
| import io.github.rosemoe.sora.text.TextRange | ||
| import io.mockk.every | ||
| import io.mockk.mockk | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import org.robolectric.RobolectricTestRunner | ||
|
|
||
| @RunWith(RobolectricTestRunner::class) | ||
| class LSPFormatterTest { | ||
| @Test | ||
| fun `reports a server failure instead of letting it escape`() { | ||
| val failure = StackOverflowError() | ||
| val server = mockk<ILanguageServer> { every { formatCode(any()) } throws failure } | ||
| val reported = mutableListOf<Throwable>() | ||
| val text = Content("fun main(") | ||
| val cursor = TextRange(CharPosition(0, 3, 3), CharPosition(0, 3, 3)) | ||
|
|
||
| val range = LSPFormatter(server, reported::add).formatAsync(text, cursor) | ||
|
|
||
| assertThat(reported).containsExactly(failure) | ||
| assertThat(text.toString()).isEqualTo("fun main(") | ||
| assertThat(range.start).isEqualTo(cursor.start) | ||
| assertThat(range.end).isEqualTo(cursor.start) | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
20 changes: 20 additions & 0 deletions
20
lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/format/KotlinCodeFormatter.kt
This file contains hidden or 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,20 @@ | ||
| package com.itsaky.androidide.lsp.kotlin.format | ||
|
|
||
| import com.facebook.ktfmt.format.Formatter | ||
| import com.facebook.ktfmt.format.ParseError | ||
| import com.itsaky.androidide.lsp.models.CodeFormatException | ||
| import com.itsaky.androidide.lsp.models.CodeFormatResult | ||
|
|
||
| internal object KotlinCodeFormatter { | ||
| fun format(content: CharSequence): CodeFormatResult { | ||
| val formatted = | ||
| try { | ||
| Formatter.format(Formatter.KOTLINLANG_FORMAT, content.toString()) | ||
| } catch (e: ParseError) { | ||
| val strippedShebangLines = if (content.startsWith("#!")) 1 else 0 | ||
| val line = e.lineColumn.line + 1 + strippedShebangLines | ||
| throw CodeFormatException("$line:${e.lineColumn.column + 1}: ${e.errorDescription}", e) | ||
| } | ||
| return CodeFormatResult.forWholeContent(content, formatted) | ||
| } | ||
| } |
21 changes: 21 additions & 0 deletions
21
lsp/kotlin/src/main/java/org/jetbrains/kotlin/cli/common/messages/MessageRenderer.kt
This file contains hidden or 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.jetbrains.kotlin.cli.common.messages | ||
|
|
||
| fun interface MessageRenderer { | ||
| fun render( | ||
| severity: CompilerMessageSeverity, | ||
| message: String, | ||
| location: CompilerMessageSourceLocation?, | ||
| ): String | ||
|
|
||
| companion object { | ||
| @JvmField | ||
| val PLAIN_RELATIVE_PATHS: MessageRenderer = | ||
| MessageRenderer { severity, message, location -> | ||
| if (location == null) { | ||
| "${severity.presentableName}: $message" | ||
| } else { | ||
| "${location.path}:${location.line}:${location.column}: ${severity.presentableName}: $message" | ||
| } | ||
| } | ||
| } | ||
| } |
25 changes: 25 additions & 0 deletions
25
...kotlin/src/main/java/org/jetbrains/kotlin/cli/common/messages/PrintingMessageCollector.kt
This file contains hidden or 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,25 @@ | ||
| package org.jetbrains.kotlin.cli.common.messages | ||
|
|
||
| import java.io.PrintStream | ||
|
|
||
| class PrintingMessageCollector( | ||
| private val errStream: PrintStream, | ||
| private val messageRenderer: MessageRenderer, | ||
| private val verbose: Boolean, | ||
| ) : MessageCollector { | ||
| private var hasErrors = false | ||
|
|
||
| override fun clear() = Unit | ||
|
|
||
| override fun report( | ||
| severity: CompilerMessageSeverity, | ||
| message: String, | ||
| location: CompilerMessageSourceLocation?, | ||
| ) { | ||
| if (!verbose && severity in CompilerMessageSeverity.VERBOSE) return | ||
| hasErrors = hasErrors || severity.isError | ||
| errStream.println(messageRenderer.render(severity, message, location)) | ||
| } | ||
|
|
||
| override fun hasErrors(): Boolean = hasErrors | ||
| } |
47 changes: 47 additions & 0 deletions
47
lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/format/KotlinCodeFormatterTest.kt
This file contains hidden or 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,47 @@ | ||
| package com.itsaky.androidide.lsp.kotlin.format | ||
|
|
||
| import com.facebook.ktfmt.format.ParseError | ||
| import com.itsaky.androidide.lsp.models.CodeFormatException | ||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Assert.assertThrows | ||
| import org.junit.Assert.assertTrue | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import org.robolectric.RobolectricTestRunner | ||
|
|
||
| @RunWith(RobolectricTestRunner::class) | ||
| class KotlinCodeFormatterTest { | ||
| @Test | ||
| fun `formats the whole file as one edit`() { | ||
| val source = "fun main( ) {println( \"hi\" )}\n" | ||
|
|
||
| val result = KotlinCodeFormatter.format(source) | ||
|
|
||
| val edit = result.indexedTextEdits.single() | ||
| assertTrue(result.isIndexed) | ||
| assertEquals(0, edit.start) | ||
| assertEquals(source.length, edit.end) | ||
| assertEquals("fun main() {\n println(\"hi\")\n}\n", edit.newText) | ||
| } | ||
|
|
||
| @Test | ||
| fun `reports a syntax error with its position`() { | ||
| val error = | ||
| assertThrows(CodeFormatException::class.java) { | ||
| KotlinCodeFormatter.format("fun main() {\n println(\"hi\"\n}\n") | ||
| } | ||
|
|
||
| assertTrue(error.cause is ParseError) | ||
| assertTrue(error.message, error.message!!.matches(Regex("""\d+:\d+: .+"""))) | ||
| } | ||
|
|
||
| @Test | ||
| fun `counts the shebang line in a syntax error position`() { | ||
| val error = | ||
| assertThrows(CodeFormatException::class.java) { | ||
| KotlinCodeFormatter.format("#!/usr/bin/env kotlin\n\nfun broken( {\n println(\"hi\")\n}\n") | ||
| } | ||
|
|
||
| assertEquals("3:12: Expecting ')'", error.message) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.