Search in sources :

Example 1 with ChunkInlineOutput

use of org.rstudio.studio.client.workbench.views.source.editors.text.ChunkInlineOutput 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

HandlerRegistration (com.google.gwt.event.shared.HandlerRegistration)1 PopupPanel (com.google.gwt.user.client.ui.PopupPanel)1 Mutable (org.rstudio.core.client.Mutable)1 SendToChunkConsoleEvent (org.rstudio.studio.client.rmarkdown.events.SendToChunkConsoleEvent)1 ChunkInlineOutput (org.rstudio.studio.client.workbench.views.source.editors.text.ChunkInlineOutput)1 Scope (org.rstudio.studio.client.workbench.views.source.editors.text.Scope)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