Skip to content

pressing Enter in an Rcpp chunk of an R Markdown document throws and skips auto-indent #18468

Closed

Description

@kevinushey

System details

RStudio Edition : Desktop
RStudio Version : 2026.08.0-daily (regressed in 2024.12; commit eafc5faf44)
OS Version      : all
R Version       : all

Steps to reproduce the problem

  1. Open a new R Markdown (or Quarto) document containing a completed chunk followed by an Rcpp chunk, e.g.:

    ```{r}
    1 + 1
    ```
    
    ```{Rcpp}
    int add(int x) {
    ```
  2. Place the cursor at the end of int add(int x) { and press Enter.

Describe the problem in detail

The new line receives no auto-indent, and an uncaught exception is raised (visible in the devtools console):

TypeError: Cannot read properties of undefined (reading 'width')
    at onMatch
    at getLineTokens
    at $tokenizeUpToRow
    at getNextLineIndent
    at insert

TokenUtils.$tokenizeUpToRow (used by CppCodeModel for C++ chunk indentation) calls getLineTokens(line, state, row) without threading a tokenizer context, so every onMatch handler receives a fresh empty object. The chunk-end rule installed by Utils.embedRules dereferences context.chunk.width unguarded, so the exception fires as soon as tokenization crosses any completed chunk fence above the cursor. Broken since commit eafc5fa (September 2024), which introduced context-based chunk tracking.

The same unguarded context reads exist in the quarto (#|), YAML multiline string, and Sweave \Sexpr{} rules.

Describe the behavior you expected

Pressing Enter inside a C++ chunk indents the new line, without raising an exception.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions