Tangle :comments noweb with :noweb-ref blocks !148

merged merged by cmc on 2026-10-07 23:32 UTC · krz/orgstar:noweb-ref-comments into main

3 files changed, +21 −4

Layout: unified · split

Sources/OrgCore/Compute/Babel.swift +6 −2
@@ -481,12 +481,16 @@ public enum Babel {
481481 ? try expandNoweb(other, params: otherParams, model: model, text: text, tangling: tangling, depth: depth + 1, links: links)
482482 : normalizedBody(other)
483483 guard commenting, let links else { return b }
484 guard other.name != nil else { throw Failure.message(":comments noweb with :noweb-ref blocks isn't supported yet; nothing was tangled.") }
485484 // `c-wrap`: commented in this block's language, trimmed.
486485 func wrap(_ s: String) throws -> String {
487486 try Tangle.commentRegion(s, language: block.language ?? "").trimmingCharacters(in: .whitespacesAndNewlines)
488487 }
489 let (begin, end) = try links(other, otherParams)
488 var (begin, end) = try links(other.name == nil ? block : other, other.name == nil ? params : otherParams)
489 // A `:noweb-ref` block without a name: the link is the including block's, the name empty.
490 if other.name == nil {
491 if let split = begin.range(of: "][") { begin = begin[..<split.lowerBound] + "][]]" }
492 end = " ends here"
493 }
490494 return try wrap(begin) + "\n" + b + "\n" + (try wrap(end)) + "\n"
491495 }
492496 func inCommentedHeading(_ b: SrcBlockInfo) -> Bool {
Sources/OrgCore/Compute/Tangle.swift +5 −2
@@ -315,8 +315,11 @@ public enum Tangle {
315315 guard let syntax = commentSyntax[language] else {
316316 throw Babel.Failure.message(":comments needs the comment syntax of \(language.isEmpty ? "the block's language" : language), which isn't known yet; nothing was tangled.")
317317 }
318 return text.components(separatedBy: "\n").map { line in
319 line.contains(where: { !$0.isWhitespace }) ? syntax.start + line + syntax.end : line
318 // The comment starts at the lines' least indentation, as `comment-region` puts it.
319 let lines = text.components(separatedBy: "\n")
320 let indent = lines.filter { $0.contains(where: { !$0.isWhitespace }) }.map { $0.prefix { $0 == " " || $0 == "\t" }.count }.min() ?? 0
321 return lines.map { line in
322 line.contains(where: { !$0.isWhitespace }) ? String(line.prefix(indent)) + syntax.start + line.dropFirst(indent) + syntax.end : line
320323 }.joined(separator: "\n")
321324 }
322325
Tests/OrgCoreTests/TangleTests.swift +10
@@ -143,6 +143,16 @@ struct TangleTests {
143143 #+begin_src conf :tangle out.conf :comments noweb :noweb yes
144144 a=<<named-py>>
145145 #+end_src
146 #+begin_src sh :noweb-ref setup
147 echo one
148 #+end_src
149 #+begin_src sh :noweb-ref setup
150 echo two
151 #+end_src
152 #+begin_src sh :tangle refs.sh :noweb yes :comments noweb
153 <<setup>>
154 echo main
155 #+end_src
146156 #+begin_src lua :tangle out.lua
147157 x = 1
148158 #+end_src