Repository navigation
Implement R Notebook execution and output in visual mode - #7666
Conversation
| private maxContentWidth = 0; | ||
| private minContentPadding = 0; | ||
|
|
||
| // keep track of whether the last transaction was selection-only |
There was a problem hiding this comment.
Haven't yet seen the GWT side of this, but the original purpose of this was to leave the selection entirely alone if the user had been editing. This is b/c in this case the selection will already be in the perfect location (b/c we are replaying diffs unrolled to inserts/deletes/etc. into source mode as the user edits). It's possible that we've lost this behavior as a result of this change. Let's discuss before you do anything here though both to explore alternative ways to preserve the behavior as well as to evaluate the priority level of preserving it.
| public static final Pattern RE_SWEAVE_CHUNK_END = | ||
| Pattern.create("^\\s*@\\s*$", ""); | ||
|
|
||
| public static final Pattern RE_EMBEDDED_R_CHUNK_BEGIN = |
There was a problem hiding this comment.
Is this intended to capture all valid knitr chunks in a file? If so it may not get indented chunks or chunks inside a blockquote (see knitr regex patterns and/or patterns we use in source capsule detection).
There was a problem hiding this comment.
It isn't; it is only used inside the embedded Ace instances (EMBEDDED) where we thankfully don't have to worry about that.
| return; | ||
| } | ||
|
|
||
| // we are about to mutate the document, so create a single |
There was a problem hiding this comment.
You may have some pretty funky merge conflicts here as I took part of this function and wrapped it in a handler that does some prompting the first time visual mode is activated and/or when we may plow over column or sentence based line wrapping. Here's the gist of my change: 0c4edf4#diff-06b7d8ba9d7ace26762c2194678a2fd8
There was a problem hiding this comment.
Thanks, will attempt to resolve and get your review when complete!
Intent
The goal of this change to achieve feature parity between R Notebooks in code view and visual mode.
Towards this end, it adds support for many notebook features, such as:
etc.
Approach
Code Sharing
In order to make the behavior as similar as possible to traditional R notebooks, I've reused as much of the existing code as possible, refactoring where necessary. Though the UI is in the visual editor, most of the operations that involve code execution invisibly run an AST -> Markdown conversion if necessary and then run against the "parent" Ace instance. In addition to allowing us to re-use a great deal of existing code, this makes it possible for the editors to share a common execution state.
Widgets like the chunk toolbar and output UI wrappers now have two different versions that share most code but have separate implementations for code view and visual mode.
Matching and Tracking Chunks
In order to figure out which chunk in visual mode corresponds to the same chunk in source mode, I've added a couple of new tools. The first is a
positionfield in the R Markdown chunk capsule, which indicates the index in the original Markdown where the chunk was discovered; this is used when initially loading the NodeView hosting the chunk. The second is used once both editors (visual and code) are both warm; it compares the chunk lists from both editors and synchronizes positions from code view into visual mode.This is of particular interest since all of the R Notebook storage is row-based. In code view, a line widget conveniently tracks which row a chunk is on (and Ace updates it for us as the row moves around); in visual mode we can't rely on line widgets and instead rely on a synchronized scope tree to tell us where chunks begin and end so we can write the correct definitions to disk.
Mode Switches
For the most part, visual mode and code view each have their own UI widgets. The exception is the
ChunkOutputWidget; these often have a lot of content and it would be wasteful (and complicated) to try to maintain two copies of it. When switching modes, we do a bunch of work to detach all of the notebook's output from the old view and reattach it to the new view. This makes it possible for us to show the output identically across modes.QA Notes
Anything that works in an R Notebook in code view should generally work the same way in visual mode, so a good start for testing is to enumerate R Notebook features (see the R Notebooks section in the R Markdown Definitive Guide, for example) and sanity test them in visual mode if it makes sense to do so (happy to consult on gray areas of applicability or bits we don't implement).
The largest risks here are around the alignment of code view with visual mode; especially around synchronizing output between modes, saving the right outputs with the right code for the R Notebook preview file (.nb.html), and executing the right code (as the code being executed comes from a different source than what's actually visible to the user).
Draft Note
This PR is ready for feedback, but not for merge as there are a couple of quirks and known bugs that need to be addressed first. I'm going to be OOO from now until mid next week, and will address feedback when I return.