-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
implement good part of #312 "doc comments"
- Loading branch information
Showing
41 changed files
with
1,038 additions
and
24 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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,15 @@ | ||
package tscfg | ||
|
||
import java.io.File | ||
import java.nio.charset.StandardCharsets | ||
import java.nio.file.{Files, Path} | ||
import scala.util.{Try, Using} | ||
|
||
object files { | ||
def readFile(file: File): Try[String] = | ||
Using(io.Source.fromFile(file))(_.mkString) | ||
|
||
def writeFile(path: Path, content: String): Try[Unit] = Try { | ||
Files.writeString(path, content, StandardCharsets.UTF_8) | ||
} | ||
} |
This file contains 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 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 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,72 @@ | ||
package tscfg.generators | ||
|
||
import tscfg.model.{AnnType, ObjectType} | ||
|
||
object docUtil { | ||
def getDoc( | ||
a: AnnType, | ||
genOpts: GenOpts, | ||
symbol2id: String => String, | ||
onlyField: Boolean = false, | ||
genScala: Boolean = false, | ||
indent: String = "", | ||
): String = { | ||
if (!genOpts.genDoc) "" | ||
else { | ||
val withParams = genScala || genOpts.genRecords | ||
a.t match { | ||
case ot: ObjectType => | ||
val paramDocs = if (withParams) { | ||
// only reflect params with comments | ||
ot.members.toList.flatMap { case (k, memberAnnType) => | ||
val paramComments = memberAnnType.docComments | ||
if (paramComments.isEmpty) None | ||
else Some(ParamDoc(symbol2id(k), paramComments)) | ||
} | ||
} | ||
else Nil | ||
if (a.docComments.isEmpty && paramDocs.isEmpty) "" | ||
else formatDocComment(a.docComments, paramDocs, genScala, indent) | ||
|
||
case _ if onlyField => | ||
if (a.docComments.isEmpty) "" | ||
else formatDocComment(a.docComments, Nil, genScala, indent) | ||
|
||
case _ => "" | ||
} | ||
} | ||
} | ||
|
||
private case class ParamDoc(name: String, docLines: List[String]) | ||
|
||
private def formatDocComment( | ||
docComments: List[String], | ||
paramDocs: List[ParamDoc], | ||
genScala: Boolean = false, | ||
indent: String, | ||
): String = { | ||
val lines = collection.mutable.ArrayBuffer[String]() | ||
val (start, sep, end) = if (genScala) { | ||
("\n/** ", "\n * ", "\n */\n") | ||
} | ||
else { | ||
lines += "" | ||
("\n/**", "\n * ", "\n */\n") | ||
} | ||
lines.addAll(docComments.map(_.trim).filter(_.nonEmpty)) | ||
if (paramDocs.nonEmpty) { | ||
lines += "" | ||
paramDocs foreach { pd => | ||
lines += s"@param ${pd.name}" | ||
pd.docLines.foreach(lines += " " + _) | ||
} | ||
} | ||
val res = lines.map(escapeForDoc).mkString(start, sep, end) | ||
if (res.isEmpty) "" | ||
else indent + res.replaceAll("\n", "\n" + indent) | ||
} | ||
|
||
private def escapeForDoc(content: String): String = content | ||
.replace("/*", "/\\*") // start of comment | ||
.replace("*/", "*\\/") // end of comment | ||
} |
Oops, something went wrong.