Skip to content

Multiple fixes & features: cite_packages, align helpers, header toggler, hotkeys, chunknamer (#20, #29, #57, #58, #59, #60, #67, #70, #73)#74

Open
VincentGuyader wants to merge 9 commits into
masterfrom
fix/multiple-issues
Open

Multiple fixes & features: cite_packages, align helpers, header toggler, hotkeys, chunknamer (#20, #29, #57, #58, #59, #60, #67, #70, #73)#74
VincentGuyader wants to merge 9 commits into
masterfrom
fix/multiple-issues

Conversation

@VincentGuyader

Copy link
Copy Markdown
Member

Summary

Refactored the addins around pure helpers so the suite can run headless (28 new assertions in CI without an RStudio session).

Bug fixes

  • chunknamer (Ignore stan block when adding names #57): tighten the chunk-header regex (^\\\\`\`\{r(\\s|,|\})) so stan/python/sql/bash` chunks are no longer renamed.
  • chunknamer: extracted into a pure helper rename_chunks() and fixed counter width — >= 100 unnamed chunks now zero-pad to 3 digits, restoring lexical = numeric order.
  • cite_packages: regex extraction now uses an explicit capture group, so library("dplyr") yields dplyr (was previously "dplyr with the leading quote, which broke utils::citation()).
  • default_hotkeys (the package should detect if the user is in mac or windows to set default keyshort cuts considering that #70): the Cmd → Ctrl rewrite on non-macOS was collapsing chunk = Ctrl+Alt+Cmd+C into Ctrl+Alt+C, colliding with htmlcomment. Moved chunk to a Shift-bearing combo and added a runtime collision check that fails fast if any future addition reintroduces a duplicate.
  • align_ (Align | elements in a table #58, Align tilde #67)*: addins now route through the pure helper align_text() via internal align_addin() (the previous commit advertised the helper as “mutualisé” but the addins still used the legacy chain).

Features

Pure helpers (new exports)

rename_chunks(), align_text(), align_pipe_text(), toggle_header(), default_hotkeys(), packages_in_text(), cite_packages().

Note on CI

The repo has no R-CMD-check workflow. I prepared a standard r-lib/actions v2 file but pushing it requires workflow scope on the PAT. Refresh with gh auth refresh -s workflow and I can add it; otherwise drop in a .github/workflows/R-CMD-check.yaml from the r-lib/actions standard template.

Test plan

  • Headless testthat::test_local() (no RStudio): 0 FAIL / 26 SKIP (RStudio-only) / 54 PASS.
  • Local R CMD check: 0 ERROR / 2 WARNINGs (pre-existing — vignette inst/doc missing).

Closes #20
Closes #29
Closes #57
Closes #58
Closes #59
Closes #60
Closes #67
Closes #70
Closes #73

- Add pure helper rename_chunks() and refactor chunknamer() around it.
- Tighten header regex so stan/python/sql/bash chunks are left untouched (#57).
- Treat name = "" (or NA) as a no-op to disable auto-naming (#73).
- align_tilde()/align_pipe() addins and their pure helpers align_text() /
  align_pipe_text() (#58, #67).
- default_hotkeys() returns the default shortcut map; rewrites Cmd to Ctrl on
  non-macOS platforms (#70). Adds align_* and br entries (#29).
- br_r() addin inserts <br> (#60).
- headr_toggler() addin + toggle_header() pure helper cycle H1..H6..plain (#59).
The previous regex relied on a lookbehind, then matched the outer
literal (whitespace + optional quote + name) but kept the lookbehind's
right-hand side in the match. So library("dplyr") yielded '"dplyr',
which never matched utils::citation() and showed up as a duplicate of
'dplyr' in the output.

Switch to a capture-group extraction (regmatches_group helper) so the
returned name is exactly the package identifier. Also tighten the
regex to require a leading letter, matching valid R package names.
…lcomment

The Cmd->Ctrl rewrite used on non-macOS turned chunk's
'Ctrl+Alt+Cmd+C' into 'Ctrl+Alt+C', which collides with
htmlcomment's existing 'Ctrl+Alt+C'. Move chunk to a Shift-bearing
chord that survives the rewrite as a unique combo, and assert at
runtime that the produced map has no duplicates so future additions
catch their own collisions.
The previous commit advertised align_text() as a 'shared pure helper'
but the addins continued to use the legacy find_regex/assemble_insert
chain; align_text() was exported and only exercised by tests.

Introduce an internal align_addin(find, fixed) that captures the
selection rows, runs align_text(), and writes the result back via a
single rstudioapi::modifyRange call. align_arrow / align_equal /
align_tilde are now one-line wrappers around it, so the same code
path runs in tests and in RStudio.
The previous formula (nchar(N) - 1, floored at 2) under-sized the
zero-padding once the document had 100+ unnamed chunks: 'remedy01,
remedy02, ..., remedy99, remedy100' with mismatched widths broke
lexical ordering. Drop the -1 so width tracks length(no_name)
exactly.
@VincentGuyader VincentGuyader self-assigned this Apr 26, 2026
…-review)

knitr accepts both ```{r} and ```{R}. The regex used to anchor on
lowercase r only, so any R chunk in the source was left unnamed
forever. Make all three regex spots case-insensitive to [rR] and add
a regression test.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment