Add --indent option
This commit is contained in:
parent
7f1c71a6e2
commit
5f510bb9b9
3 changed files with 6 additions and 4 deletions
|
|
@ -12,6 +12,7 @@ val lineRe = "^\\s*§(?<block>.*)$".r
|
||||||
|
|
||||||
class Conf(args: Seq[String]) extends ScallopConf(args):
|
class Conf(args: Seq[String]) extends ScallopConf(args):
|
||||||
val path: ScallopOption[Path] = trailArg[Path]()
|
val path: ScallopOption[Path] = trailArg[Path]()
|
||||||
|
val indent: ScallopOption[Int] = opt[Int](default = Some(2))
|
||||||
val blockRegex: ScallopOption[String] = opt[String](default = Some(blockRe.regex))
|
val blockRegex: ScallopOption[String] = opt[String](default = Some(blockRe.regex))
|
||||||
val lineRegex: ScallopOption[String] = opt[String](default = Some(lineRe.regex))
|
val lineRegex: ScallopOption[String] = opt[String](default = Some(lineRe.regex))
|
||||||
verify()
|
verify()
|
||||||
|
|
@ -19,7 +20,8 @@ class Conf(args: Seq[String]) extends ScallopConf(args):
|
||||||
@main
|
@main
|
||||||
def main(args: String*): Unit =
|
def main(args: String*): Unit =
|
||||||
val conf = new Conf(args)
|
val conf = new Conf(args)
|
||||||
val formatter = Formatter(blockRe = Regex(conf.blockRegex()), lineRe = Regex(conf.lineRegex()))
|
val formatter =
|
||||||
|
Formatter(indent = conf.indent(), blockRe = Regex(conf.blockRegex()), lineRe = Regex(conf.lineRegex()))
|
||||||
reformat(conf.path(), formatter)
|
reformat(conf.path(), formatter)
|
||||||
|
|
||||||
def reformat(path: Path, formatter: Formatter): Unit =
|
def reformat(path: Path, formatter: Formatter): Unit =
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ case class Block(lines: Seq[(String, String)]):
|
||||||
|
|
||||||
def last: (String, String) = lines.last
|
def last: (String, String) = lines.last
|
||||||
def content: Seq[String] = lines.map((_, content) => content)
|
def content: Seq[String] = lines.map((_, content) => content)
|
||||||
def toLines: Seq[String] = lines.map((prefix, content) => s"$prefix $content")
|
def toLines(indent: Int): Seq[String] = lines.map((prefix, content) => prefix + " " * indent + content)
|
||||||
|
|
||||||
def extend(block: Block): Block = Block(lines ++ block.lines)
|
def extend(block: Block): Block = Block(lines ++ block.lines)
|
||||||
def extend(prefix: String, content: String): Block = extend(Block(prefix, content))
|
def extend(prefix: String, content: String): Block = extend(Block(prefix, content))
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ import scala.util.boundary
|
||||||
import scala.util.matching.Regex
|
import scala.util.matching.Regex
|
||||||
import scala.util.matching.Regex.Match
|
import scala.util.matching.Regex.Match
|
||||||
|
|
||||||
case class Formatter(blockRe: Regex, lineRe: Regex):
|
case class Formatter(indent: Int, blockRe: Regex, lineRe: Regex):
|
||||||
private val blockReI = blockRe.pattern.namedGroups().get("block")
|
private val blockReI = blockRe.pattern.namedGroups().get("block")
|
||||||
private val lineReI = lineRe.pattern.namedGroups().get("block")
|
private val lineReI = lineRe.pattern.namedGroups().get("block")
|
||||||
|
|
||||||
|
|
@ -46,7 +46,7 @@ case class Formatter(blockRe: Regex, lineRe: Regex):
|
||||||
for info <- findBlocks(cleanText) do
|
for info <- findBlocks(cleanText) do
|
||||||
if resultEnd < info.start then result.append(cleanText.slice(resultEnd, info.start))
|
if resultEnd < info.start then result.append(cleanText.slice(resultEnd, info.start))
|
||||||
val block = info.block.replace(info.tree.formatted.toString.linesIterator.toIndexedSeq) // Clunky :D
|
val block = info.block.replace(info.tree.formatted.toString.linesIterator.toIndexedSeq) // Clunky :D
|
||||||
result.append(block.toLines.mkString("\n"))
|
result.append(block.toLines(indent).mkString("\n"))
|
||||||
if info.endsWithNewline then result.append("\n")
|
if info.endsWithNewline then result.append("\n")
|
||||||
resultEnd = info.end
|
resultEnd = info.end
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue