Пример #1
0
  private boolean isSetupChunk(Element el, AceEditorNative editor) {
    int pageX = el.getAbsoluteLeft();
    int pageY = el.getAbsoluteTop();

    Position position = editor.getRenderer().screenToTextCoordinates(pageX, pageY);

    String line = editor.getSession().getLine(position.getRow());

    return line.contains("r setup");
  }
Пример #2
0
  private void manageChunkIcons(AceEditorNative editor) {
    Element container = editor.getContainer();
    if (container == null) return;

    Element[] icons =
        DomUtils.getElementsByClassName(container, ThemeStyles.INSTANCE.inlineChunkToolbar());

    for (Element icon : icons) icon.removeFromParent();

    if (!uiPrefs_.showInlineToolbarForRCodeChunks().getValue()) return;

    if (!shouldDisplayIcons(editor)) return;

    Element[] chunkStarts = DomUtils.getElementsByClassName("rstudio_chunk_start ace_start");

    for (int i = 0; i < chunkStarts.length; i++) {
      Element el = chunkStarts[i];

      if (isPseudoMarker(el)) continue;

      if (!isRunnableChunk(el, editor)) continue;

      if (!DomUtils.isVisibleVert(container, el)) continue;

      if (el.getChildCount() > 0) el.removeAllChildren();

      addToolbar(el, isSetupChunk(el, editor), editor);
    }
  }
Пример #3
0
  private void addToolbar(Element el, boolean isSetupChunk, AceEditorNative editor) {
    FlowPanel toolbarPanel = new FlowPanel();
    toolbarPanel.addStyleName(ThemeStyles.INSTANCE.inlineChunkToolbar());

    boolean isDark = themes_.isDark(themes_.getEffectiveThemeName(uiPrefs_.theme().getValue()));

    if (isSetupChunk) {
      Image optionsIcon = createOptionsIcon(isDark, true);
      toolbarPanel.add(optionsIcon);
    } else {
      Image optionsIcon = createOptionsIcon(isDark, false);
      optionsIcon.getElement().getStyle().setMarginRight(9, Unit.PX);
      toolbarPanel.add(optionsIcon);

      // Note that 'run current chunk' currently only operates within Rmd
      if (editor.getSession().getMode().getId().equals("mode/rmarkdown")) {
        Image runPreviousIcon = createRunPreviousIcon(isDark);
        runPreviousIcon.getElement().getStyle().setMarginRight(8, Unit.PX);
        toolbarPanel.add(runPreviousIcon);

        Image runIcon = createRunIcon();
        toolbarPanel.add(runIcon);
      }
    }

    display(toolbarPanel, el);
  }
Пример #4
0
  private boolean isRunnableChunk(Element el, AceEditorNative editor) {
    Position pos = toDocumentPosition(el, editor);
    String text = editor.getSession().getLine(pos.getRow());

    Pattern pattern = Pattern.create("engine\\s*=\\s*['\"]([^'\"]*)['\"]", "");
    Match match = pattern.match(text, 0);

    if (match == null) return true;

    String engine = match.getGroup(1).toLowerCase();

    return engine.equals("r") || engine.equals("rscript");
  }
Пример #5
0
  private void doSetCode(String code, boolean preserveCursorPosition) {
    // Filter out Escape characters that might have snuck in from an old
    // bug in 0.95. We can choose to remove this when 0.95 ships, hopefully
    // any documents that would be affected by this will be gone by then.
    code = code.replaceAll("\u001B", "");

    final AceEditorNative ed = widget_.getEditor();

    if (preserveCursorPosition) {
      final Position cursorPos;
      final int scrollTop, scrollLeft;

      cursorPos = ed.getSession().getSelection().getCursor();
      scrollTop = ed.getRenderer().getScrollTop();
      scrollLeft = ed.getRenderer().getScrollLeft();

      // Setting the value directly on the document prevents undo/redo
      // stack from being blown away
      widget_.getEditor().getSession().getDocument().setValue(code);

      ed.getSession().getSelection().moveCursorTo(cursorPos.getRow(), cursorPos.getColumn(), false);
      ed.getRenderer().scrollToY(scrollTop);
      ed.getRenderer().scrollToX(scrollLeft);
      Scheduler.get()
          .scheduleDeferred(
              new ScheduledCommand() {
                @Override
                public void execute() {
                  ed.getRenderer().scrollToY(scrollTop);
                  ed.getRenderer().scrollToX(scrollLeft);
                }
              });
    } else {
      ed.getSession().setValue(code);
      ed.getSession().getSelection().moveCursorTo(0, 0, false);
    }
  }
Пример #6
0
  private Position toDocumentPosition(Element el, AceEditorNative editor) {
    int pageX = el.getAbsoluteLeft();
    int pageY = el.getAbsoluteTop();

    return editor.getRenderer().screenToTextCoordinates(pageX, pageY);
  }
Пример #7
0
 private boolean shouldDisplayIcons(AceEditorNative editor) {
   String id = editor.getSession().getMode().getId();
   return id.equals("mode/rmarkdown"); // also Rpres
 }