Search in sources :

Example 1 with RnwWeave

use of org.rstudio.studio.client.common.rnw.RnwWeave in project rstudio by rstudio.

the class TextEditingTargetCompilePdfHelper method checkCompilers.

public void checkCompilers(final WarningBarDisplay display, TextFileType fileType) {
    // for all tex files we need to parse magic comments and validate
    // any explict latex proram directive
    ArrayList<TexMagicComment> magicComments = null;
    if (fileType.canCompilePDF()) {
        magicComments = TexMagicComment.parseComments(docDisplay_.getCode());
        String latexProgramDirective = detectLatexProgramDirective(magicComments);
        if (latexProgramDirective != null) {
            if (latexProgramRegistry_.findTypeIgnoreCase(latexProgramDirective) == null) {
                // show warning and bail 
                display.showWarningBar("Unknown LaTeX program type '" + latexProgramDirective + "' specified (valid types are " + latexProgramRegistry_.getPrintableTypeNames() + ")");
                return;
            }
        }
    }
    // for Rnw we first determine the RnwWeave type
    RnwWeave rnwWeave = null;
    RnwWeaveDirective rnwWeaveDirective = null;
    if (fileType.isRnw()) {
        rnwWeaveDirective = detectRnwWeaveDirective(magicComments);
        if (rnwWeaveDirective != null) {
            rnwWeave = rnwWeaveDirective.getRnwWeave();
            if (rnwWeave == null) {
                // show warning and bail 
                display.showWarningBar("Unknown Rnw weave method '" + rnwWeaveDirective.getName() + "' specified (valid types are " + rnwWeaveRegistry_.getPrintableTypeNames() + ")");
                return;
            }
        } else {
            rnwWeave = rnwWeaveRegistry_.findTypeIgnoreCase(prefs_.defaultSweaveEngine().getValue());
        }
    }
    final SessionInfo sessionInfo = session_.getSessionInfo();
    TexCapabilities texCap = sessionInfo.getTexCapabilities();
    final boolean checkForTeX = fileType.canCompilePDF() && !texCap.isTexInstalled();
    final boolean checkForRnwWeave = (rnwWeave != null) && !texCap.isRnwWeaveAvailable(rnwWeave);
    if (checkForTeX || checkForRnwWeave) {
        // alias variables to finals
        final boolean hasRnwWeaveDirective = rnwWeaveDirective != null;
        final RnwWeave fRnwWeave = rnwWeave;
        server_.getTexCapabilities(new ServerRequestCallback<TexCapabilities>() {

            @Override
            public void onResponseReceived(TexCapabilities response) {
                if (checkForTeX && !response.isTexInstalled()) {
                    String warning;
                    if (Desktop.isDesktop())
                        warning = "No TeX installation detected. Please install " + "TeX before compiling.";
                    else
                        warning = "This server does not have TeX installed. You " + "may not be able to compile.";
                    display.showWarningBar(warning);
                } else if (checkForRnwWeave && !response.isRnwWeaveAvailable(fRnwWeave)) {
                    String forContext = "";
                    if (hasRnwWeaveDirective)
                        forContext = "this file";
                    else if (sessionInfo.getActiveProjectFile() != null)
                        forContext = "Rnw files for this project";
                    else
                        forContext = "Rnw files";
                    display.showWarningBar(fRnwWeave.getName() + " is configured to weave " + forContext + " " + "however the " + fRnwWeave.getPackageName() + " package is not installed.");
                } else {
                    display.hideWarningBar();
                }
            }

            @Override
            public void onError(ServerError error) {
                Debug.logError(error);
            }
        });
    } else {
        display.hideWarningBar();
    }
}
Also used : RnwWeave(org.rstudio.studio.client.common.rnw.RnwWeave) RnwWeaveDirective(org.rstudio.studio.client.common.rnw.RnwWeaveDirective) ServerError(org.rstudio.studio.client.server.ServerError) TexCapabilities(org.rstudio.studio.client.workbench.model.TexCapabilities) TexMagicComment(org.rstudio.core.client.tex.TexMagicComment) SessionInfo(org.rstudio.studio.client.workbench.model.SessionInfo)

Example 2 with RnwWeave

use of org.rstudio.studio.client.common.rnw.RnwWeave in project rstudio by rstudio.

the class Source method onNewSweaveDoc.

@Handler
public void onNewSweaveDoc() {
    // set concordance value if we need to
    String concordance = new String();
    if (uiPrefs_.alwaysEnableRnwConcordance().getValue()) {
        RnwWeave activeWeave = rnwWeaveRegistry_.findTypeIgnoreCase(uiPrefs_.defaultSweaveEngine().getValue());
        if (activeWeave.getInjectConcordance())
            concordance = "\\SweaveOpts{concordance=TRUE}\n";
    }
    final String concordanceValue = concordance;
    // show progress
    final ProgressIndicator indicator = new GlobalProgressDelayer(globalDisplay_, 500, "Creating new document...").getIndicator();
    // get the template
    server_.getSourceTemplate("", "sweave.Rnw", new ServerRequestCallback<String>() {

        @Override
        public void onResponseReceived(String templateContents) {
            indicator.onCompleted();
            // add in concordance if necessary
            final boolean hasConcordance = concordanceValue.length() > 0;
            if (hasConcordance) {
                String beginDoc = "\\begin{document}\n";
                templateContents = templateContents.replace(beginDoc, beginDoc + concordanceValue);
            }
            newDoc(FileTypeRegistry.SWEAVE, templateContents, new ResultCallback<EditingTarget, ServerError>() {

                @Override
                public void onSuccess(EditingTarget target) {
                    int startRow = 4 + (hasConcordance ? 1 : 0);
                    target.setCursorPosition(Position.create(startRow, 0));
                }
            });
        }

        @Override
        public void onError(ServerError error) {
            indicator.onError(error.getUserMessage());
        }
    });
}
Also used : RnwWeave(org.rstudio.studio.client.common.rnw.RnwWeave) ProgressIndicator(org.rstudio.core.client.widget.ProgressIndicator) ServerError(org.rstudio.studio.client.server.ServerError) GlobalProgressDelayer(org.rstudio.studio.client.common.GlobalProgressDelayer) TextEditingTarget(org.rstudio.studio.client.workbench.views.source.editors.text.TextEditingTarget) EditingTarget(org.rstudio.studio.client.workbench.views.source.editors.EditingTarget) DataEditingTarget(org.rstudio.studio.client.workbench.views.source.editors.data.DataEditingTarget) CodeBrowserEditingTarget(org.rstudio.studio.client.workbench.views.source.editors.codebrowser.CodeBrowserEditingTarget) JSONString(com.google.gwt.json.client.JSONString) JsArrayString(com.google.gwt.core.client.JsArrayString) NativePreviewHandler(com.google.gwt.user.client.Event.NativePreviewHandler) FileTypeChangedHandler(org.rstudio.studio.client.workbench.views.source.editors.text.events.FileTypeChangedHandler) Handler(org.rstudio.core.client.command.Handler) ValueChangeHandler(com.google.gwt.event.logical.shared.ValueChangeHandler) SelectionHandler(com.google.gwt.event.logical.shared.SelectionHandler) CloseHandler(com.google.gwt.event.logical.shared.CloseHandler) ViewDataHandler(org.rstudio.studio.client.workbench.views.data.events.ViewDataHandler) SourceOnSaveChangedHandler(org.rstudio.studio.client.workbench.views.source.editors.text.events.SourceOnSaveChangedHandler) ChangeHandler(com.google.gwt.event.dom.client.ChangeHandler) OpenSourceFileHandler(org.rstudio.studio.client.common.filetypes.events.OpenSourceFileHandler)

Example 3 with RnwWeave

use of org.rstudio.studio.client.common.rnw.RnwWeave in project rstudio by rstudio.

the class TextEditingTarget method sourceActiveDocument.

private void sourceActiveDocument(final boolean echo) {
    docDisplay_.focus();
    // If the document being sourced is a Shiny file, run the app instead.
    if (fileType_.isR() && extendedType_.startsWith(SourceDocument.XT_SHINY_PREFIX)) {
        runShinyApp();
        return;
    }
    // if the document is an R Markdown notebook, run all its chunks instead
    if (fileType_.isRmd() && isRmdNotebook()) {
        onExecuteAllCode();
        return;
    }
    // If the document being sourced is a script then use that codepath
    if (fileType_.isScript()) {
        runScript();
        return;
    }
    // If the document is previewable
    if (fileType_.canPreviewFromR()) {
        previewFromR();
        return;
    }
    String code = docDisplay_.getCode();
    if (code != null && code.trim().length() > 0) {
        // R 2.14 prints a warning when sourcing a file with no trailing \n
        if (!code.endsWith("\n"))
            code = code + "\n";
        boolean sweave = fileType_.canCompilePDF() || fileType_.canKnitToHTML() || fileType_.isRpres();
        RnwWeave rnwWeave = compilePdfHelper_.getActiveRnwWeave();
        final boolean forceEcho = sweave && (rnwWeave != null) ? rnwWeave.forceEchoOnExec() : false;
        // NOTE: we always set echo to true for knitr because knitr doesn't
        // require print statements so if you don't echo to the console
        // then you don't see any of the output
        boolean saveWhenSourcing = fileType_.isCpp() || docDisplay_.hasBreakpoints() || (prefs_.saveBeforeSourcing().getValue() && (getPath() != null) && !sweave);
        if ((dirtyState_.getValue() || sweave) && !saveWhenSourcing) {
            server_.saveActiveDocument(code, sweave, compilePdfHelper_.getActiveRnwWeaveName(), new SimpleRequestCallback<Void>() {

                @Override
                public void onResponseReceived(Void response) {
                    consoleDispatcher_.executeSourceCommand("~/.active-rstudio-document", fileType_, "UTF-8", activeCodeIsAscii(), forceEcho ? true : echo, prefs_.focusConsoleAfterExec().getValue(), docDisplay_.hasBreakpoints());
                }
            });
        } else {
            Command sourceCommand = new Command() {

                @Override
                public void execute() {
                    if (docDisplay_.hasBreakpoints()) {
                        hideBreakpointWarningBar();
                    }
                    consoleDispatcher_.executeSourceCommand(getPath(), fileType_, docUpdateSentinel_.getEncoding(), activeCodeIsAscii(), forceEcho ? true : echo, prefs_.focusConsoleAfterExec().getValue(), docDisplay_.hasBreakpoints());
                }
            };
            if (saveWhenSourcing && (dirtyState_.getValue() || (getPath() == null)))
                saveThenExecute(null, sourceCommand);
            else
                sourceCommand.execute();
        }
    }
    // update pref if necessary
    if (prefs_.sourceWithEcho().getValue() != echo) {
        prefs_.sourceWithEcho().setGlobalValue(echo, true);
        prefs_.writeUIPrefs();
    }
}
Also used : RnwWeave(org.rstudio.studio.client.common.rnw.RnwWeave) ScheduledCommand(com.google.gwt.core.client.Scheduler.ScheduledCommand) RepeatingCommand(com.google.gwt.core.client.Scheduler.RepeatingCommand) Command(com.google.gwt.user.client.Command) AppCommand(org.rstudio.core.client.command.AppCommand) JsArrayString(com.google.gwt.core.client.JsArrayString) Void(org.rstudio.studio.client.server.Void)

Example 4 with RnwWeave

use of org.rstudio.studio.client.common.rnw.RnwWeave in project rstudio by rstudio.

the class TextEditingTargetCompilePdfHelper method getActiveRnwWeave.

// get the currently active rnw weave method -- note this can return
// null in the case that there is an embedded directive which is invalid
public RnwWeave getActiveRnwWeave() {
    if (docDisplay_.getFileType().canKnitToHTML())
        return rnwWeaveRegistry_.findTypeIgnoreCase("knitr");
    RnwWeave weave = null;
    ArrayList<TexMagicComment> magicComments = TexMagicComment.parseComments(docDisplay_.getCode());
    RnwWeaveDirective rnwWeaveDirective = detectRnwWeaveDirective(magicComments);
    if (rnwWeaveDirective != null) {
        weave = rnwWeaveDirective.getRnwWeave();
    } else {
        weave = rnwWeaveRegistry_.findTypeIgnoreCase(prefs_.defaultSweaveEngine().getValue());
    }
    // look for the 'driver' directive and don't inject 
    // concocordance if there is a custom driver
    String driver = detectRnwDriverDirective(magicComments);
    if (driver != null)
        return RnwWeave.withNoConcordance(weave);
    else
        return weave;
}
Also used : RnwWeave(org.rstudio.studio.client.common.rnw.RnwWeave) RnwWeaveDirective(org.rstudio.studio.client.common.rnw.RnwWeaveDirective) TexMagicComment(org.rstudio.core.client.tex.TexMagicComment)

Example 5 with RnwWeave

use of org.rstudio.studio.client.common.rnw.RnwWeave in project rstudio by rstudio.

the class TextEditingTargetCompilePdfHelper method ensureRnwConcordance.

public void ensureRnwConcordance() {
    RnwWeave rnwWeave = getActiveRnwWeave();
    if ((rnwWeave != null) && rnwWeave.getInjectConcordance()) {
        if (!hasConcordanceDirective(docDisplay_.getCode())) {
            InputEditorSelection doc = docDisplay_.search("\\\\begin{document}", // backwards
            false, // wrap
            true, // case sensitive
            false, // whole word
            false, // from selection
            null, // range (search all)
            null, // regexp mode
            true);
            if (doc != null) {
                InputEditorPosition pos = doc.getEnd().moveToNextLine();
                docDisplay_.insertCode(pos, "\\SweaveOpts{concordance=TRUE}\n");
            }
        }
    }
}
Also used : RnwWeave(org.rstudio.studio.client.common.rnw.RnwWeave) InputEditorSelection(org.rstudio.studio.client.workbench.views.console.shell.editor.InputEditorSelection) InputEditorPosition(org.rstudio.studio.client.workbench.views.console.shell.editor.InputEditorPosition)

Aggregations

RnwWeave (org.rstudio.studio.client.common.rnw.RnwWeave)5 JsArrayString (com.google.gwt.core.client.JsArrayString)2 TexMagicComment (org.rstudio.core.client.tex.TexMagicComment)2 RnwWeaveDirective (org.rstudio.studio.client.common.rnw.RnwWeaveDirective)2 ServerError (org.rstudio.studio.client.server.ServerError)2 RepeatingCommand (com.google.gwt.core.client.Scheduler.RepeatingCommand)1 ScheduledCommand (com.google.gwt.core.client.Scheduler.ScheduledCommand)1 ChangeHandler (com.google.gwt.event.dom.client.ChangeHandler)1 CloseHandler (com.google.gwt.event.logical.shared.CloseHandler)1 SelectionHandler (com.google.gwt.event.logical.shared.SelectionHandler)1 ValueChangeHandler (com.google.gwt.event.logical.shared.ValueChangeHandler)1 JSONString (com.google.gwt.json.client.JSONString)1 Command (com.google.gwt.user.client.Command)1 NativePreviewHandler (com.google.gwt.user.client.Event.NativePreviewHandler)1 AppCommand (org.rstudio.core.client.command.AppCommand)1 Handler (org.rstudio.core.client.command.Handler)1 ProgressIndicator (org.rstudio.core.client.widget.ProgressIndicator)1 GlobalProgressDelayer (org.rstudio.studio.client.common.GlobalProgressDelayer)1 OpenSourceFileHandler (org.rstudio.studio.client.common.filetypes.events.OpenSourceFileHandler)1 Void (org.rstudio.studio.client.server.Void)1