Search in sources :

Example 1 with SendToChunkConsoleEvent

use of org.rstudio.studio.client.rmarkdown.events.SendToChunkConsoleEvent in project rstudio by rstudio.

the class EditingTargetCodeExecution method executeRange.

private void executeRange(Range range, String functionWrapper, boolean onlyUseConsole) {
    String code = codeExtractor_.extractCode(docDisplay_, range);
    setLastExecuted(range.getStart(), range.getEnd());
    // trim intelligently
    code = code.trim();
    if (code.length() == 0)
        code = "\n";
    // strip roxygen off the beginning of lines
    if (isRoxygenExampleRange(range)) {
        code = code.replaceFirst("^[ \\t]*#'[ \\t]?", "");
        code = code.replaceAll("\n[ \\t]*#'[ \\t]?", "\n");
    }
    // if we're in a chunk with in-line output, execute it there instead
    if (!onlyUseConsole && docDisplay_.showChunkOutputInline()) {
        Scope scope = docDisplay_.getCurrentChunk(range.getStart());
        if (scope != null) {
            events_.fireEvent(new SendToChunkConsoleEvent(docId_, scope, range));
            return;
        }
    }
    if (functionWrapper != null) {
        code = functionWrapper + "({" + code + "})";
    }
    // send to console
    events_.fireEvent(new SendToConsoleEvent(code, true, prefs_.focusConsoleAfterExec().getValue()));
}
Also used : Scope(org.rstudio.studio.client.workbench.views.source.editors.text.Scope) SendToChunkConsoleEvent(org.rstudio.studio.client.rmarkdown.events.SendToChunkConsoleEvent) SendToConsoleEvent(org.rstudio.studio.client.workbench.views.console.events.SendToConsoleEvent)

Example 2 with SendToChunkConsoleEvent

use of org.rstudio.studio.client.rmarkdown.events.SendToChunkConsoleEvent in project rstudio by rstudio.

the class EditingTargetInlineChunkExecution method execute.

public void execute(Range range) {
    // synthesize an identifier for this chunk execution
    final String chunkId = "i" + StringUtil.makeRandomId(12);
    // are, remove it to make way for the new one
    for (ChunkInlineOutput output : outputs_.values()) {
        if (output.range().isEqualTo(range)) {
            if (output.state() == ChunkInlineOutput.State.Finished) {
                // remove old, completed output for this input
                output.hide();
                outputs_.remove(output.chunkId());
            } else {
                // unintended duplicate.
                return;
            }
        }
    }
    // create dummy scope for execution
    Scope scope = Scope.createRScopeNode(chunkId, range.getStart(), range.getEnd(), Scope.SCOPE_TYPE_CHUNK);
    // create popup panel to host output
    final ChunkInlineOutput output = new ChunkInlineOutput(chunkId, display_.createAnchoredSelection(range.getStart(), range.getEnd()));
    // auto dismiss the panel when the cursor leaves the inline chunk
    final Mutable<HandlerRegistration> cursorHandler = new Mutable<HandlerRegistration>();
    cursorHandler.set(display_.addCursorChangedHandler(new CursorChangedHandler() {

        @Override
        public void onCursorChanged(CursorChangedEvent event) {
            Position position = event.getPosition();
            if (!output.range().contains(position)) {
                output.hide();
            }
        }
    }));
    // when the popup is dismissed, clean up local state
    output.addCloseHandler(new CloseHandler<PopupPanel>() {

        @Override
        public void onClose(CloseEvent<PopupPanel> event) {
            outputs_.remove(chunkId);
            cursorHandler.get().removeHandler();
        }
    });
    // render offscreen until complete
    output.setPopupPosition(-100000, -100000);
    output.show();
    outputs_.put(chunkId, output);
    SendToChunkConsoleEvent event = new SendToChunkConsoleEvent(docId_, scope, range, NotebookQueueUnit.EXEC_SCOPE_INLINE);
    events_.fireEvent(event);
}
Also used : HandlerRegistration(com.google.gwt.event.shared.HandlerRegistration) Position(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position) PopupPanel(com.google.gwt.user.client.ui.PopupPanel) CursorChangedEvent(org.rstudio.studio.client.workbench.views.source.editors.text.events.CursorChangedEvent) ChunkInlineOutput(org.rstudio.studio.client.workbench.views.source.editors.text.ChunkInlineOutput) Mutable(org.rstudio.core.client.Mutable) Scope(org.rstudio.studio.client.workbench.views.source.editors.text.Scope) CursorChangedHandler(org.rstudio.studio.client.workbench.views.source.editors.text.events.CursorChangedHandler) SendToChunkConsoleEvent(org.rstudio.studio.client.rmarkdown.events.SendToChunkConsoleEvent)

Aggregations

SendToChunkConsoleEvent (org.rstudio.studio.client.rmarkdown.events.SendToChunkConsoleEvent)2 Scope (org.rstudio.studio.client.workbench.views.source.editors.text.Scope)2 HandlerRegistration (com.google.gwt.event.shared.HandlerRegistration)1 PopupPanel (com.google.gwt.user.client.ui.PopupPanel)1 Mutable (org.rstudio.core.client.Mutable)1 SendToConsoleEvent (org.rstudio.studio.client.workbench.views.console.events.SendToConsoleEvent)1 ChunkInlineOutput (org.rstudio.studio.client.workbench.views.source.editors.text.ChunkInlineOutput)1 Position (org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position)1 CursorChangedEvent (org.rstudio.studio.client.workbench.views.source.editors.text.events.CursorChangedEvent)1 CursorChangedHandler (org.rstudio.studio.client.workbench.views.source.editors.text.events.CursorChangedHandler)1