Skip to content

Commit

Permalink
Daily arc lint --take KTFMT
Browse files Browse the repository at this point in the history
Reviewed By: ivanmurashko

Differential Revision: D49274915

fbshipit-source-id: 405a79b9019dae90cd3f2d9e62220df3d9dd942f
  • Loading branch information
generatedunixname89002005325672 authored and facebook-github-bot committed Sep 14, 2023
1 parent 2e52a65 commit a06e353
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1470,7 +1470,8 @@ class KotlinInputAstVisitor(

override fun visitClassOrObject(classOrObject: KtClassOrObject) {
builder.sync(classOrObject)
val contextReceiverList = classOrObject.getStubOrPsiChild(KtStubElementTypes.CONTEXT_RECEIVER_LIST)
val contextReceiverList =
classOrObject.getStubOrPsiChild(KtStubElementTypes.CONTEXT_RECEIVER_LIST)
val modifierList = classOrObject.modifierList
builder.block(ZERO) {
if (contextReceiverList != null) {
Expand Down Expand Up @@ -1657,12 +1658,11 @@ class KotlinInputAstVisitor(
builder.sync(contextReceiverList)
builder.token("context")
visitEachCommaSeparated(
contextReceiverList.contextReceivers(),
prefix = "(",
postfix = ")",
breakAfterPrefix = false,
breakBeforePostfix = false
)
contextReceiverList.contextReceivers(),
prefix = "(",
postfix = ")",
breakAfterPrefix = false,
breakBeforePostfix = false)
builder.forcedBreak()
}

Expand Down Expand Up @@ -2474,7 +2474,9 @@ class KotlinInputAstVisitor(
visit(child)
builder.guessToken(";")
lastChildHadBlankLineBefore = childGetsBlankLineBefore
lastChildIsContextReceiver = child is KtScriptInitializer && child.firstChild?.firstChild?.firstChild?.text == "context"
lastChildIsContextReceiver =
child is KtScriptInitializer &&
child.firstChild?.firstChild?.firstChild?.text == "context"
first = false
}
markForPartialFormat()
Expand Down
93 changes: 87 additions & 6 deletions core/src/test/java/com/facebook/ktfmt/format/TokenizerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -113,25 +113,106 @@ class TokenizerTest {

@Test
fun `Context receivers are parsed correctly`() {
val code = """
val code =
"""
|context(Something)
|class A {
| context(
| // Test comment.
| Logger, Raise<Error>)
| fun test() {}
|}
|""".trimMargin().trimMargin()
|"""
.trimMargin()
.trimMargin()

val file = Parser.parse(code)
val tokenizer = Tokenizer(code, file)
file.accept(tokenizer)

assertThat(tokenizer.toks.map { it.originalText })
.containsExactly("context", "(", "Something", ")", "\n", "class", " ", "A", " ", "{", "\n", " ", "context", "(", "\n", " ", "// Test comment.", "\n", " ", "Logger", ",", " ", "Raise", "<", "Error", ">", ")", "\n", " ", "fun", " ", "test", "(", ")", " ", "{", "}", "\n", "}")
.inOrder()
.containsExactly(
"context",
"(",
"Something",
")",
"\n",
"class",
" ",
"A",
" ",
"{",
"\n",
" ",
"context",
"(",
"\n",
" ",
"// Test comment.",
"\n",
" ",
"Logger",
",",
" ",
"Raise",
"<",
"Error",
">",
")",
"\n",
" ",
"fun",
" ",
"test",
"(",
")",
" ",
"{",
"}",
"\n",
"}")
.inOrder()
assertThat(tokenizer.toks.map { it.index })
.containsExactly(0, 1, 2, 3, -1, 4, -1, 5, -1, 6, -1, -1, 7, 8, -1, -1, 9, -1, -1, 10, 11, -1, 12, 13, 14, 15, 16, -1, -1, 17, -1, 18, 19, 20, -1, 21, 22, -1, 23)
.inOrder()
.containsExactly(
0,
1,
2,
3,
-1,
4,
-1,
5,
-1,
6,
-1,
-1,
7,
8,
-1,
-1,
9,
-1,
-1,
10,
11,
-1,
12,
13,
14,
15,
16,
-1,
-1,
17,
-1,
18,
19,
20,
-1,
21,
22,
-1,
23)
.inOrder()
}
}

0 comments on commit a06e353

Please sign in to comment.