Skip to content

Commit

Permalink
Render output in a temp directory, when in an R package
Browse files Browse the repository at this point in the history
  • Loading branch information
juliasilge committed May 8, 2024
1 parent 0feb78c commit 0fdd78e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
7 changes: 7 additions & 0 deletions apps/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1073,6 +1073,13 @@
"default": true,
"markdownDescription": "Reveal the preview panel after document render."
},
"quarto.render.rPackageOutputDirectory": {
"order": 15,
"scope": "window",
"type": "boolean",
"default": true,
"markdownDescription": "Render output files in a temporary directory, when in an R package."
},
"quarto.visualEditor.fontSize": {
"order": 31,
"scope": "resource",
Expand Down
24 changes: 24 additions & 0 deletions apps/vscode/src/providers/preview/preview-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,30 @@ export function isQuartoShinyKnitrDoc(

}

export async function isRPackage() {
const descriptionLines = await parseRPackageDescription();
const packageLines = descriptionLines.filter(line => line.startsWith('Package:'));
const typeLines = descriptionLines.filter(line => line.startsWith('Type:'));
const typeIsPackage = (typeLines.length > 0
? typeLines[0].toLowerCase().includes('package')
: false);
const typeIsPackageOrMissing = typeLines.length === 0 || typeIsPackage;
return packageLines.length > 0 && typeIsPackageOrMissing;
}

async function parseRPackageDescription(): Promise<string[]> {
if (vscode.workspace.workspaceFolders !== undefined) {
const folderUri = vscode.workspace.workspaceFolders[0].uri;
const fileUri = vscode.Uri.joinPath(folderUri, 'DESCRIPTION');
try {
const bytes = await vscode.workspace.fs.readFile(fileUri);
const descriptionText = Buffer.from(bytes).toString('utf8');
const descriptionLines = descriptionText.split(/(\r?\n)/);
return descriptionLines;
} catch { }
}
return [''];
}

export async function renderOnSave(engine: MarkdownEngine, document: TextDocument) {
// if its a notebook and we don't have a save hook for notebooks then don't
Expand Down
13 changes: 13 additions & 0 deletions apps/vscode/src/providers/preview/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ import {
haveNotebookSaveEvents,
isQuartoShinyDoc,
isQuartoShinyKnitrDoc,
isRPackage,
renderOnSave,
} from "./preview-util";

Expand Down Expand Up @@ -448,6 +449,9 @@ class PreviewManager {
// terminal options
const options = terminalOptions(kPreviewWindowTitle, target, this.previewEnv_);

// is this workspace an R package?
const isRPackageWorkspace = await isRPackage();

// is this is a shiny doc?
const isShiny = isQuartoShinyDoc(this.engine_, doc);
const useServeCommand = this.usesQuartoServeCommand(doc);
Expand Down Expand Up @@ -477,6 +481,11 @@ class PreviewManager {
cmd.push("--no-watch-inputs");
}

// use temp output-dir for R package
if (isRPackageWorkspace && this.previewRPackageDirConfig()) {
cmd.push("--output-dir", tmp.dirSync().name);
}

// send terminal command
await sendTerminalCommand(this.terminal_, this.previewEnv_, this.quartoContext_, cmd);

Expand Down Expand Up @@ -711,6 +720,10 @@ class PreviewManager {
return this.quartoConfig().get("render.previewReveal", true);
}

private previewRPackageDirConfig(): boolean {
return this.quartoConfig().get("render.rPackageOutputDirectory", true);
}

private quartoConfig() {
return vscode.workspace.getConfiguration("quarto");
}
Expand Down

0 comments on commit 0fdd78e

Please sign in to comment.