Skip to content

Commit

Permalink
add dedent + a workaround for it
Browse files Browse the repository at this point in the history
  • Loading branch information
a-mr committed Mar 10, 2021
1 parent d866d71 commit eb7b5be
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 39 deletions.
4 changes: 2 additions & 2 deletions lib/packages/docutils/rst.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1961,13 +1961,13 @@ proc parseEnumList(p: var RstParser): PRstNode =
# subsequent enum.items parseEnumList will be called second time
if result == nil:
let n = p.line + p.tok[j].line
let msg = "\n" & """
let msg = "\n" & dedent """
not enough indentation on line $2
(if it's continuation of enumeration list),
or no blank line after line $1 (if it should be the next paragraph),
or no escaping \ at the beginning of line $1
(if lines $1..$2 are a normal paragraph, not enum. list)"""
rstMessage(p, mwRstStyle, msg % [$(n-1), $n])
rstMessage(p, mwRstStyle, indent(msg, 2) % [$(n-1), $n])
return
checkAfterNewline
result = newRstNodeA(p, rnEnumList)
Expand Down
80 changes: 43 additions & 37 deletions lib/pure/strutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1438,43 +1438,49 @@ func unindent*(s: string, count: Natural = int.high,
result.add(line[indentCount*padding.len .. ^1])
i.inc

func indentation*(s: string): Natural {.since: (1, 3).} =
## Returns the amount of indentation all lines of `s` have in common,
## ignoring lines that consist only of whitespace.
result = int.high
for line in s.splitLines:
for i, c in line:
if i >= result: break
elif c != ' ':
result = i
break
if result == int.high:
result = 0

func dedent*(s: string, count: Natural = indentation(s)): string {.rtl,
extern: "nsuDedent", since: (1, 3).} =
## Unindents each line in `s` by `count` amount of `padding`.
## The only difference between this and the
## `unindent func<#unindent,string,Natural,string>`_ is that this by default
## only cuts off the amount of indentation that all lines of `s` share as
## opposed to all indentation. It only supports spaces as padding.
##
## **Note:** This does not preserve the new line characters used in `s`.
##
## See also:
## * `unindent func<#unindent,string,Natural,string>`_
## * `align func<#align,string,Natural,char>`_
## * `alignLeft func<#alignLeft,string,Natural,char>`_
## * `spaces func<#spaces,Natural>`_
## * `indent func<#indent,string,Natural,string>`_
runnableExamples:
let x = """
Hello
There
""".dedent()

doAssert x == "Hello\n There\n"
unindent(s, count, " ")
since (1, 3):
func indentation*(s: string): Natural =
## Returns the amount of indentation all lines of `s` have in common,
## ignoring lines that consist only of whitespace.
result = int.high
for line in s.splitLines:
for i, c in line:
if i >= result: break
elif c != ' ':
result = i
break
if result == int.high:
result = 0

func dedent*(s: string, count: Natural): string {.rtl, extern: "nsuDedent".} =
unindent(s, count, " ")

proc dedent*(a: string): string =
## Unindents each line in `s` by `count` amount of `padding`.
## The only difference between this and the
## `unindent func<#unindent,string,Natural,string>`_ is that this by default
## only cuts off the amount of indentation that all lines of `s` share as
## opposed to all indentation. It only supports spaces as padding.
##
## **Note:** This does not preserve the new line characters used in `s`.
##
## See also:
## * `unindent func<#unindent,string,Natural,string>`_
## * `align func<#align,string,Natural,char>`_
## * `alignLeft func<#alignLeft,string,Natural,char>`_
## * `spaces func<#spaces,Natural>`_
## * `indent func<#indent,string,Natural,string>`_
# workaround, see https://github.com/timotheecour/Nim/issues/521
runnableExamples:
let x = """
Hello
There
""".dedent()

doAssert x == "Hello\n There\n"
let b = a
let i = indentation(b)
dedent(a, i)

func delete*(s: var string, first, last: int) {.rtl, extern: "nsuDelete".} =
## Deletes in `s` (must be declared as `var`) the characters at positions
Expand Down

0 comments on commit eb7b5be

Please sign in to comment.