Skip to content

Commit

Permalink
Merge pull request #6317 from quarto-dev/fix/executable-annotate-only
Browse files Browse the repository at this point in the history
Correctly flush pending annotated cell even when no block is following
  • Loading branch information
dragonstyle authored Jul 25, 2023
2 parents 8a578a5 + 3cc7d6b commit 9f5b942
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 19 deletions.
39 changes: 20 additions & 19 deletions src/resources/filters/quarto-pre/code-annotation.lua
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ local function resolveCellAnnotes(codeBlockEl, processAnnotation)

-- Look and annotation
local annoteNumber = annotationProvider.annotationNumber(line)

if annoteNumber then
-- Capture the annotation number and strip it
local annoteId = toAnnoteId(annoteNumber)
Expand All @@ -121,7 +121,7 @@ local function resolveCellAnnotes(codeBlockEl, processAnnotation)
lineNumbers = pandoc.List({})
end
lineNumbers:insert(i)
annotations[annoteId] = lineNumbers
annotations[annoteId] = lineNumbers
outputs:insert(processAnnotation(line, annoteNumber, annotationProvider))
else
outputs:insert(line)
Expand Down Expand Up @@ -300,17 +300,21 @@ function code_annotations()
pendingCellId = nil
pendingCodeCell = nil
end

local outputBlockClearPending = function(block)

local outputBlock = function(block)
outputs:insert(block)
end

local flushPending = function()
if pendingCodeCell then
outputs:insert(pendingCodeCell)
outputBlock(pendingCodeCell)
clearPending()
end
outputs:insert(block)
clearPending()
end

local outputBlock = function(block)
outputs:insert(block)
local outputBlockClearPending = function(block)
flushPending()
outputBlock(block)
end

local allOutputs = function()
Expand Down Expand Up @@ -391,10 +395,7 @@ function code_annotations()

-- If there is a pending code cell and we get here, just
-- output the pending code cell and continue
if pendingCodeCell then
outputBlock(pendingCodeCell)
clearPending()
end
flushPending()

local cellId = resolveCellId(block.attr.identifier)
local codeCell = processCodeCell(block, cellId)
Expand Down Expand Up @@ -488,21 +489,21 @@ function code_annotations()
-- wrap the definition list in a cell
local dlDiv = pandoc.Div({dl}, pandoc.Attr("", {constants.kCellAnnotationClass}))
pendingCodeCell.content:insert(2, dlDiv)
outputBlock(pendingCodeCell)
clearPending()
flushPending()
else
outputBlockClearPending(dl)
end
else
if pendingCodeCell then
outputBlock(pendingCodeCell)
end
clearPending()
flushPending()
end
else
outputBlockClearPending(block)
end
end

-- Be sure to flush any pending Code Cell (usually when only annotated cell without annotation and no other following blocks)
flushPending()

return allOutputs()
end
end
Expand Down
24 changes: 24 additions & 0 deletions tests/docs/smoke-all/2023/07/24/code-annotation-exec-only.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
title: Only comment code block without annotation
format:
html:
code-annotations: true
_quarto:
tests:
html:
ensureHtmlElements:
- ['div.cell-code pre code']
- []
ensureFileRegexMatches:
- []
- ["# <1>"]
---

From https://github.com/quarto-dev/quarto-cli/issues/6313

Computational code block with annotation comment but no comment.

```{r}
stats::rnorm(2) # <1>
1 + 1
```

0 comments on commit 9f5b942

Please sign in to comment.