Skip to content

Commit

Permalink
parse comment mode
Browse files Browse the repository at this point in the history
  • Loading branch information
xuwei-k committed Mar 14, 2024
1 parent bf67b88 commit 72b072d
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 4 deletions.
7 changes: 7 additions & 0 deletions core/src/main/scala/scalameta_ast/Args.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ object Args {
dialect: Option[String],
) extends Args

case class Comment(
src: String,
format: Boolean,
scalafmtConfig: Conf,
dialect: Option[String],
) extends Args

case class Raw(
src: String,
format: Boolean,
Expand Down
26 changes: 25 additions & 1 deletion core/src/main/scala/scalameta_ast/ScalametaAST.scala
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,13 @@ class ScalametaAST {
patch = patch,
initialExtractor = initialExtractor,
)
case "comment" =>
Args.Comment(
src = src,
format = format,
scalafmtConfig = scalafmtConfig,
dialect = dialect,
)
case _ =>
Args.Raw(
src = src,
Expand All @@ -334,7 +341,7 @@ class ScalametaAST {
def convert(
args: Args
): Output = {
val input = convert.apply(args.src)
lazy val input = convert.apply(args.src)
val ((ast, parsedOpt), astBuildMs) = stopwatch {
val dialects = args.dialect.fold(dialectsDefault) { x =>
stringToDialects.getOrElse(
Expand Down Expand Up @@ -363,6 +370,23 @@ class ScalametaAST {
}
tokensToString(loop(input, dialects)) -> None

case _: Args.Comment =>
scala.meta.contrib.CommentOps
.docTokens(
new scala.meta.tokens.Token.Comment(
input = Input.String(args.src),
dialect = dialects.head,
start = 0,
end = args.src.length,
value = args.src
)
)
.map(_.map { x =>
val q: String => String = s => scala.meta.Lit.String(s).toString
s"""DocToken(kind = ${x.kind}, name = ${x.name.map(q)}, body = ${x.body.map(q)})"""
})
.toString -> None

case a: NotToken =>
val tree = loopParse(
input,
Expand Down
40 changes: 40 additions & 0 deletions localServer/src/test/resources/comment.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
Some(
List(
DocToken(
kind = Param,
name = Some("a1"),
body = Some("a2")
),
DocToken(
kind = TypeParam,
name = Some("a3"),
body = Some("a4")
),
DocToken(
kind = Throws,
name = Some("a5"),
body = Some("a6")
),
DocToken(
kind = See,
name = None,
body = Some("example.com")
),
DocToken(
kind = Note,
name = None,
body = Some("a7")
),
DocToken(
kind = Paragraph,
name = None,
body = None
),
DocToken(
kind = CodeBlock,
name = None,
body = Some("def x = List(\"y\", 2)")
)
)
)

25 changes: 25 additions & 0 deletions localServer/src/test/scala/scalameta_ast/IntegrationTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,31 @@ abstract class IntegrationTest(
assert(!packageName(page).isEnabled())
assert(!ruleName(page).isEnabled())
}
"Comment" in withBrowser { page =>
changeOutputType(page, "comment")
assert(!wildcardImport(page).isEnabled())
assert(!removeNewFields(page).isEnabled())
assert(!initialExtractor(page).isEnabled())
assert(!packageName(page).isEnabled())
assert(!ruleName(page).isEnabled())
setInput(
page,
Seq(
"""/**""",
""" * @param a1 a2""",
""" * @tparam a3 a4""",
""" * @throws a5 a6""",
""" * @see example.com""",
""" * @note a7""",
""" *""",
""" * {{{""",
""" * def x = List("y", 2)""",
""" * }}}""",
""" */""",
).mkString("\n")
)
assert(output(page).textContent() == fromResource("comment.txt"))
}
}

"format input" in withBrowser { page =>
Expand Down
22 changes: 19 additions & 3 deletions sources/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,14 @@ const App = () => {
});
}

const disableScalafixRuleTemplateInput =
outputType === "raw" || outputType === "tokens";
const disableScalafixRuleTemplateInput = [
"raw",
"tokens",
"comment",
].includes(outputType);

const disableCompat = scalameta != "scalafix" || outputType === "tokens";
const disableCompat =
scalameta != "scalafix" || ["tokens", "comment"].includes(outputType);

return html` <div class="container mw-100">
<details open ontoggle="${(e) => changeDetails(e)}">
Expand Down Expand Up @@ -342,6 +346,18 @@ const App = () => {
<span>Tokens</span>
</label>
</div>
<div>
<label>
<input
type="radio"
name="output_type"
value="comment"
checked=${outputType === "comment"}
onChange=${() => setOutputType("comment")}
/>
<span>Comment</span>
</label>
</div>
</fieldset>
</div>
<div class="row">
Expand Down

0 comments on commit 72b072d

Please sign in to comment.