From 72b072d84ceae66d7b3076689a66c0e0c0445081 Mon Sep 17 00:00:00 2001 From: xuwei-k <6b656e6a69@gmail.com> Date: Thu, 14 Mar 2024 10:56:24 +0900 Subject: [PATCH] parse comment mode --- core/src/main/scala/scalameta_ast/Args.scala | 7 ++++ .../scala/scalameta_ast/ScalametaAST.scala | 26 +++++++++++- localServer/src/test/resources/comment.txt | 40 +++++++++++++++++++ .../scala/scalameta_ast/IntegrationTest.scala | 25 ++++++++++++ sources/main.js | 22 ++++++++-- 5 files changed, 116 insertions(+), 4 deletions(-) create mode 100644 localServer/src/test/resources/comment.txt diff --git a/core/src/main/scala/scalameta_ast/Args.scala b/core/src/main/scala/scalameta_ast/Args.scala index 7a2860d..9e3f29b 100644 --- a/core/src/main/scala/scalameta_ast/Args.scala +++ b/core/src/main/scala/scalameta_ast/Args.scala @@ -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, diff --git a/core/src/main/scala/scalameta_ast/ScalametaAST.scala b/core/src/main/scala/scalameta_ast/ScalametaAST.scala index 73fcc50..5d73191 100644 --- a/core/src/main/scala/scalameta_ast/ScalametaAST.scala +++ b/core/src/main/scala/scalameta_ast/ScalametaAST.scala @@ -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, @@ -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( @@ -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, diff --git a/localServer/src/test/resources/comment.txt b/localServer/src/test/resources/comment.txt new file mode 100644 index 0000000..83fea5c --- /dev/null +++ b/localServer/src/test/resources/comment.txt @@ -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)") + ) + ) +) + diff --git a/localServer/src/test/scala/scalameta_ast/IntegrationTest.scala b/localServer/src/test/scala/scalameta_ast/IntegrationTest.scala index 1933283..262f0fe 100644 --- a/localServer/src/test/scala/scalameta_ast/IntegrationTest.scala +++ b/localServer/src/test/scala/scalameta_ast/IntegrationTest.scala @@ -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 => diff --git a/sources/main.js b/sources/main.js index 85abde0..fe69227 100644 --- a/sources/main.js +++ b/sources/main.js @@ -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`
@@ -342,6 +346,18 @@ const App = () => { Tokens
+
+ +