Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better code chunk extraction #356

Merged
merged 11 commits into from
Mar 4, 2025
Merged

Better code chunk extraction #356

merged 11 commits into from
Mar 4, 2025

Conversation

PMassicotte
Copy link
Collaborator

Below a detailed description of this PR using Copilot. Basically, it introduces a new class Chunk and various helper function to get and filter chunks in quarto and rmd documents. It also refactors the sending of current and all above chunks.

The only user-visible change aligns with a discussion point: when sending all above chunks, the current chunk is now excluded. This approach ensures that partial code isn't mistakenly sent to the terminal, preventing execution errors—particularly when sending all above chunks alongside individual lines from the current chunk.

For example:

plot(1)

this_is_a_long_function(
  1,
  2,
  3,  # cursor here
  4,
  5,
  6
)

It will send the incomplete code.

plot(1)

this_is_a_long_function(
  1,
  2,
  3,  

This pull request introduces several significant changes to the handling of code chunks in R and Quarto documents, including the addition of a new Chunk class, refactoring of functions for sending code chunks, and enhancements to the way code chunks are parsed and executed.

Improvements to code chunk handling:

  • lua/r/quarto.lua: Introduced a new Chunk class to encapsulate code chunk properties and added several helper functions to parse and manage code chunks. These functions include get_code_chunks, parse_info_string_params, parse_code_chunk_params, get_current_code_chunk, get_chunks_above_cursor, get_all_code_chunks, filter_code_chunks_by_lang, filter_code_chunks_by_eval, and codelines_from_chunks. [1] [2]

Refactoring of functions for sending code chunks:

  • lua/r/maps.lua: Updated the key mappings to use the new send_current_chunk function instead of send_R_chunk.
  • lua/r/rmd.lua: Refactored the send_R_chunk and send_py_chunk functions into a single send_current_chunk function that handles both R and Python code chunks.
  • lua/r/send.lua: Added a new source_chunk function to send code chunks to the R console and updated the chunks_up_to_here function to use the new chunk handling methods. [1] [2]

Dependency updates:

…arto documents

This commit allow to retrieve all code chunks from rmd/qmd documents as
well as their parameters (eval: true, for example).

Utility functions to filter chunk by the code language (r, python at the
moment) as well if the code chunk is marked as eval: true or eval =
TRUE.
…Console

Introduce `source_chunk` function to send a command to the R Console
for sourcing the current chunk. This enhances the modularity and
reusability of the code by separating the logic for sending chunks
from other functionalities. Update `chunks_up_to_here` to utilize
`source_chunk` for improved clarity and maintainability.
Simplify the process of sending code chunks by integrating R and
Python handling into a single function. Remove redundant internal
function and streamline chunk retrieval and filtering using the
quarto module.
…nction to improve code readability and maintainability
…ction to clarify behavior based on language type
…s and improve code readability by using range method in chunk operations
@jalvesaq jalvesaq force-pushed the better-code-chunk-extraction branch from a550666 to 5afcf17 Compare March 3, 2025 19:54
@jalvesaq
Copy link
Member

jalvesaq commented Mar 3, 2025

I rebased the pull request on the main branch.

@PMassicotte
Copy link
Collaborator Author

No rush to merge if you want to test it. I have used this branch for the last 2–3 days without issue. Do not hesitate to co-author some code if you have suggestion/comment

@jalvesaq
Copy link
Member

jalvesaq commented Mar 3, 2025

I can't review it carefully for the next days, and almost never someone comment on pull requests. So, we have to merge them to make people try the code. I think we can merge it now.

@PMassicotte
Copy link
Collaborator Author

Sounds good!

@jalvesaq jalvesaq merged commit 508dc92 into main Mar 4, 2025
2 checks passed
@jalvesaq jalvesaq deleted the better-code-chunk-extraction branch March 4, 2025 01:35
@jalvesaq
Copy link
Member

jalvesaq commented Mar 4, 2025

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants