From fd63e995ff7cbb1845577707ce745c7e1984efa7 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Tue, 6 Apr 2021 22:32:12 +0200 Subject: [PATCH] Add command parameter name saving --- build.sbt | 103 +++++++++++++++-------- commenter/.idea/.gitignore | 8 -- commenter/build.sbt | 10 --- commenter/src/main/scala/Commenter.scala | 18 ---- 4 files changed, 68 insertions(+), 71 deletions(-) delete mode 100644 commenter/.idea/.gitignore delete mode 100644 commenter/build.sbt delete mode 100644 commenter/src/main/scala/Commenter.scala diff --git a/build.sbt b/build.sbt index 8909643..8b710ca 100644 --- a/build.sbt +++ b/build.sbt @@ -51,50 +51,83 @@ saveConfigComments := { val cdataRegex = Pattern.compile("(?:def|val|var) (\\w+)(?::[^=]+)? = (?:(?:get(?:I)?Config)|(?:DPUtils.\\w+Data))") //Hack: DPUtils val clRegex = Pattern.compile("class (\\w+) extends (\\w+)") val objRegex = Pattern.compile("object (\\w+)") + val subRegex = Pattern.compile("def `?(\\w+)`?\\((?:((?:\\w|\\d)+): ((?:\\w|[\\[\\].]|\\d)+),?\\s*)+\\)") val config = new YamlConfiguration() + + def getConfigComments(line: String, clKey: String, comment: String, justCommented: Boolean): (String, String, Boolean) = { + val clMatcher = clRegex.matcher(line) + if (clKey == null && clMatcher.find()) { //First occurrence + (if (clMatcher.group(2).contains("Component")) + "components." + clMatcher.group(1) + else "global", comment, justCommented) + } else if (line.contains("/**")) { + (clKey, "", false) + } else if (line.contains("*/") && comment != null) + (clKey, comment, true) + else if (comment != null) { + if (justCommented) { + if (clKey != null) { + val matcher = cdataRegex.matcher(line) + if (matcher.find()) + config.set(s"$clKey.${matcher.group(1)}", comment.trim) + } + else { + val matcher = objRegex.matcher(line) + if (matcher.find()) { + val clName = matcher.group(1) + val compKey = if (clName.contains("Module")) s"component.$clName" + else if (clName.contains("Plugin")) "global" + else null + if (compKey != null) + config.set(s"${compKey}.generalDescriptionInsteadOfAConfig", comment.trim) + } + } + (clKey, null, false) + } + else (clKey, comment + line.replaceFirst("^\\s*\\*\\s+", "") + "\n", justCommented) + } + else (clKey, comment, justCommented) + } + for (file <- sv) { Using(Source.fromFile(file)) { src => var clKey: String = null var comment: String = null var justCommented: Boolean = false + + var subCommand = false + var pkg: String = null + var clName: String = null for (line <- src.getLines) { + val (clk, c, jc) = getConfigComments(line, clKey, comment, justCommented) + clKey = clk; comment = c; justCommented = jc + + val objMatcher = objRegex.matcher(line) val clMatcher = clRegex.matcher(line) - if (clKey == null && clMatcher.find()) { //First occurrence - clKey = if (clMatcher.group(2).contains("Component")) - "components." + clMatcher.group(1) - else - "global" - /*println("Class: "+clKey) - println("Comment: "+comment) - println("Just commented: "+justCommented) - if (comment != null) { //Not checking justCommented because the object may have the comment and not extend anything - config.set(s"$clKey.generalDescriptionInsteadOfAConfig", comment.trim) - justCommented = false - comment = null - println("Found class comment for " + clKey) - }*/ - } else if (line.contains("/**")) { - comment = "" - justCommented = false - } else if (line.contains("*/") && comment != null) - justCommented = true - else if (comment != null) { - if (justCommented) { - if (clKey != null) { - val matcher = cdataRegex.matcher(line) - if (matcher.find()) - config.set(s"$clKey.${matcher.group(1)}", comment.trim) - } - else { - val matcher = objRegex.matcher(line) - if (matcher.find()) - config.set(s"${matcher.group(1)}.generalDescriptionInsteadOfAConfig", comment.trim) - } - justCommented = false - comment = null + if (pkg == null && line.startsWith("package ")) + pkg = line.substring("package ".length) + /*else if (clName == null && (line.contains("object") || line.contains("class")) + && !line.contains("import")) { + val i = line.indexOf("class") + val j = if (i == -1) line.indexOf("object") + "object ".length else i + "class ".length + clName = line.substring(j) + }*/ + else if (clName == null && objMatcher.find()) + clName = objMatcher.group(1) + else if (clName == null && clMatcher.find()) + clName = clMatcher.group(1) + val subMatcher = subRegex.matcher(line) + val sub = line.contains("@Subcommand") || line.contains("@Command2.Subcommand") + if (subCommand || sub) //This line or the previous one had the annotation + if (subMatcher.find()) { + val groups = (2 to subMatcher.groupCount()).map(subMatcher.group) + val pairs = for (i <- groups.indices by 2) yield (groups(i), groups(i + 1)) + val mname = subMatcher.group(1) + print(s"$pkg.$clName.$mname(") + for ((name, ty) <- pairs) print(s"$name: $ty, ") + println(")") } - else comment += line.replaceFirst("^\\s*\\*\\s+", "") + "\n" - } + subCommand = sub } config.save("target/configHelp.yml") }.recover[Unit]({ case t => t.printStackTrace() }) diff --git a/commenter/.idea/.gitignore b/commenter/.idea/.gitignore deleted file mode 100644 index 73f69e0..0000000 --- a/commenter/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml -# Editor-based HTTP Client requests -/httpRequests/ diff --git a/commenter/build.sbt b/commenter/build.sbt deleted file mode 100644 index edf8f98..0000000 --- a/commenter/build.sbt +++ /dev/null @@ -1,10 +0,0 @@ -name := "Chroma-Commenter" -version := "1.0" -organization := "com.github.TBMCPlugins" -scalaVersion := "2.13.4" - -resolvers += Resolver.mavenLocal - -libraryDependencies += "org.reflections" % "reflections" % "0.9.12" -libraryDependencies += "com.github.TBMCPlugins.ChromaCore" % "ButtonProcessor" % "master-SNAPSHOT" -libraryDependencies += "com.github.TBMCPlugins.ChromaCore" % "Chroma-Core" % "v1.0.0" diff --git a/commenter/src/main/scala/Commenter.scala b/commenter/src/main/scala/Commenter.scala deleted file mode 100644 index e9c653d..0000000 --- a/commenter/src/main/scala/Commenter.scala +++ /dev/null @@ -1,18 +0,0 @@ -import buttondevteam.buttonproc.HasConfig -import buttondevteam.lib.architecture.ConfigData -import org.reflections.Reflections - -import scala.jdk.javaapi.CollectionConverters.asScala - -object Commenter extends App { - val ref = new Reflections("buttondevteam.discordplugin") - val types = asScala(ref.getTypesAnnotatedWith(classOf[HasConfig], true)) - for (ty <- types) { - val path = if (ty.getAnnotation(classOf[HasConfig]).global()) "global" - else s"components.${ty.getSimpleName}" - val cdcl = classOf[ConfigData[_]] - ty.getDeclaredMethods.filter(m => cdcl.isAssignableFrom(m.getReturnType)) - .concat(ty.getDeclaredFields.filter(f => cdcl.isAssignableFrom(f.getType))) - .map(path + "." + _.getName) - } -}