renderverbatim.nim 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import std/strutils
  2. import ast, options, msgs
  3. when defined(nimPreviewSlimSystem):
  4. import std/assertions
  5. const isDebug = false
  6. when isDebug:
  7. import renderer
  8. import astalgo
  9. proc lastNodeRec(n: PNode): PNode =
  10. result = n
  11. while result.safeLen > 0: result = result[^1]
  12. proc isInIndentationBlock(src: string, indent: int): bool =
  13. #[
  14. we stop at the first de-indentation; there's an inherent ambiguity with non
  15. doc comments since they can have arbitrary indentation, so we just take the
  16. practical route and require a runnableExamples to keep its code (including non
  17. doc comments) to its indentation level.
  18. ]#
  19. for j in 0..<indent:
  20. if src.len <= j: return true
  21. if src[j] != ' ': return false
  22. return true
  23. type LineData = object
  24. ## keep track of which lines are starting inside a multiline doc comment.
  25. ## We purposefully avoid re-doing parsing which is already done (we get a PNode)
  26. ## so we don't worry about whether we're inside (nested) doc comments etc.
  27. ## But we sill need some logic to disambiguate different multiline styles.
  28. conf: ConfigRef
  29. lineFirst: int
  30. lines: seq[bool]
  31. ## lines[index] is true if line `lineFirst+index` starts inside a multiline string
  32. ## Using a HashSet (extra dependency) would simplify but not by much.
  33. proc tripleStrLitStartsAtNextLine(conf: ConfigRef, n: PNode): bool =
  34. # enabling TLineInfo.offsetA,offsetB would probably make this easier
  35. result = false
  36. const tripleQuote = "\"\"\""
  37. let src = sourceLine(conf, n.info)
  38. let col = n.info.col
  39. doAssert src.continuesWith(tripleQuote, col) # sanity check
  40. var i = col + 3
  41. var onlySpace = true
  42. while true:
  43. if src.len <= i:
  44. doAssert src.len == i
  45. return onlySpace
  46. elif src.continuesWith(tripleQuote, i) and (src.len == i+3 or src[i+3] != '\"'):
  47. return false # triple lit is in 1 line
  48. elif src[i] != ' ': onlySpace = false
  49. i.inc
  50. proc visitMultilineStrings(ldata: var LineData, n: PNode) =
  51. var cline = ldata.lineFirst
  52. template setLine() =
  53. let index = cline - ldata.lineFirst
  54. if ldata.lines.len < index+1: ldata.lines.setLen index+1
  55. ldata.lines[index] = true
  56. case n.kind
  57. of nkTripleStrLit:
  58. # same logic should be applied for any multiline token
  59. # we could also consider nkCommentStmt but right now we just assume doc comments,
  60. # unlike triple string litterals, don't de-indent from runnableExamples.
  61. cline = n.info.line.int
  62. if tripleStrLitStartsAtNextLine(ldata.conf, n):
  63. cline.inc
  64. setLine()
  65. for ai in n.strVal:
  66. case ai
  67. of '\n':
  68. cline.inc
  69. setLine()
  70. else: discard
  71. else:
  72. for i in 0..<n.safeLen:
  73. visitMultilineStrings(ldata, n[i])
  74. proc startOfLineInsideTriple(ldata: LineData, line: int): bool =
  75. let index = line - ldata.lineFirst
  76. if index >= ldata.lines.len: false
  77. else: ldata.lines[index]
  78. proc extractRunnableExamplesSource*(conf: ConfigRef; n: PNode, indent = 0): string =
  79. ## TLineInfo.offsetA,offsetB would be cleaner but it's only enabled for nimpretty,
  80. ## we'd need to check performance impact to enable it for nimdoc.
  81. var first = n.lastSon.info
  82. if first.line == n[0].info.line:
  83. #[
  84. runnableExamples: assert true
  85. ]#
  86. discard
  87. else:
  88. #[
  89. runnableExamples:
  90. # non-doc comment that we want to capture even though `first` points to `assert true`
  91. assert true
  92. ]#
  93. first.line = n[0].info.line + 1
  94. let last = n.lastNodeRec.info
  95. var info = first
  96. var indent2 = info.col
  97. let numLines = numLines(conf, info.fileIndex).uint16
  98. var lastNonemptyPos = 0
  99. var ldata = LineData(lineFirst: first.line.int, conf: conf)
  100. visitMultilineStrings(ldata, n[^1])
  101. when isDebug:
  102. debug(n)
  103. for i in 0..<ldata.lines.len:
  104. echo (i+ldata.lineFirst, ldata.lines[i])
  105. result = ""
  106. for line in first.line..numLines: # bugfix, see `testNimDocTrailingExample`
  107. info.line = line
  108. let src = sourceLine(conf, info)
  109. let special = startOfLineInsideTriple(ldata, line.int)
  110. if line > last.line and not special and not isInIndentationBlock(src, indent2):
  111. break
  112. if line > first.line: result.add "\n"
  113. if special:
  114. result.add src
  115. lastNonemptyPos = result.len
  116. elif src.len > indent2:
  117. for i in 0..<indent: result.add ' '
  118. result.add src[indent2..^1]
  119. lastNonemptyPos = result.len
  120. result.setLen lastNonemptyPos