Skip to content

Display command titles on editor action bar in Positron #692

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion apps/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,11 @@
"command": "quarto.preview",
"title": "Preview",
"icon": "$(preview)",
"category": "Quarto"
"category": "Quarto",
"actionBarOptions": {
"controlType": "button",
"displayTitle": true
}
},
{
"command": "quarto.previewScript",
Expand Down Expand Up @@ -425,6 +429,15 @@
"category": "Quarto",
"enablement": "editorTextFocus && !editorReadonly && editorLangId == quarto"
},
{
"command": "quarto.toggleRenderOnSave",
"title": "Render on Save",
"enablement": "isPositron",
"actionBarOptions": {
"controlType": "checkbox",
"checked": "quarto.renderOnSave == true"
}
},
{
"command": "quarto.zoteroConfigureLibrary",
"category": "Quarto",
Expand Down Expand Up @@ -678,6 +691,12 @@
"when": "editorLangId == mermaid || editorLangId == dot"
}
],
"editor/actions/right": [
{
"command": "quarto.toggleRenderOnSave",
"group": "navigation@101"
}
],
"notebook/toolbar": [
{
"command": "quarto.preview",
Expand Down
49 changes: 36 additions & 13 deletions apps/vscode/src/providers/preview/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@
import * as path from "path";
import * as fs from "fs";

import { TextDocument, window, Uri, workspace, commands, QuickPickItem } from "vscode";
import {
TextDocument,
window,
Uri,
workspace,
commands,
QuickPickItemKind,
ExtensionContext,
} from "vscode";
import { QuartoContext, QuartoFormatInfo, quartoDocumentFormats } from "quarto-core";

import { Command } from "../../core/command";
Expand All @@ -29,18 +37,19 @@ import { canPreviewDoc, findQuartoEditor, isNotebook } from "../../core/doc";
import { renderOnSave } from "./preview-util";
import { documentFrontMatterYaml } from "../../markdown/document";
import { FormatQuickPickItem, RenderCommand } from "../render";
import { QuickPickItemKind } from "vscode";

export function previewCommands(
quartoContext: QuartoContext,
context: ExtensionContext,
engine: MarkdownEngine
): Command[] {
return [
new PreviewCommand(quartoContext, engine),
new PreviewScriptCommand(quartoContext, engine),
new PreviewFormatCommand(quartoContext, engine),
new WalkthroughPreviewCommand(quartoContext, engine),
new PreviewCommand(quartoContext, context, engine),
new PreviewScriptCommand(quartoContext, context, engine),
new PreviewFormatCommand(quartoContext, context, engine),
new WalkthroughPreviewCommand(quartoContext, context, engine),
new ClearCacheCommand(quartoContext, engine),
new ToggleRenderOnSaveCommand(context),
];
}
const kChooseFormat = "EB451697-D09E-48F5-AA40-4DAE7E1D31B8";
Expand All @@ -49,14 +58,15 @@ const kChooseFormat = "EB451697-D09E-48F5-AA40-4DAE7E1D31B8";
abstract class PreviewDocumentCommandBase extends RenderCommand {
constructor(
quartoContext: QuartoContext,
private readonly context_: ExtensionContext,
private readonly engine_: MarkdownEngine
) {
super(quartoContext);
}
protected async renderFormat(format?: string | null, onShow?: () => void) {
const targetEditor = findQuartoEditor(this.engine_, this.quartoContext(), canPreviewDoc);
if (targetEditor) {
const hasRenderOnSave = await renderOnSave(this.engine_, targetEditor.document);
const hasRenderOnSave = await renderOnSave(this.engine_, targetEditor.document, this.context_);
const render =
!hasRenderOnSave ||
(hasRenderOnSave && format) ||
Expand Down Expand Up @@ -127,8 +137,8 @@ abstract class PreviewDocumentCommandBase extends RenderCommand {
class PreviewCommand
extends PreviewDocumentCommandBase
implements Command {
constructor(quartoContext: QuartoContext, engine: MarkdownEngine) {
super(quartoContext, engine);
constructor(quartoContext: QuartoContext, context: ExtensionContext, engine: MarkdownEngine) {
super(quartoContext, context, engine);
}
private static readonly id = "quarto.preview";
public readonly id = PreviewCommand.id;
Expand All @@ -141,8 +151,8 @@ class PreviewCommand
class PreviewScriptCommand
extends PreviewDocumentCommandBase
implements Command {
constructor(quartoContext: QuartoContext, engine: MarkdownEngine) {
super(quartoContext, engine);
constructor(quartoContext: QuartoContext, context: ExtensionContext, engine: MarkdownEngine) {
super(quartoContext, context, engine);
}
private static readonly id = "quarto.previewScript";
public readonly id = PreviewScriptCommand.id;
Expand All @@ -155,8 +165,8 @@ class PreviewScriptCommand
class PreviewFormatCommand
extends PreviewDocumentCommandBase
implements Command {
constructor(quartoContext: QuartoContext, engine: MarkdownEngine) {
super(quartoContext, engine);
constructor(quartoContext: QuartoContext, context: ExtensionContext, engine: MarkdownEngine) {
super(quartoContext, context, engine);
}
private static readonly id = "quarto.previewFormat";
public readonly id = PreviewFormatCommand.id;
Expand Down Expand Up @@ -238,3 +248,16 @@ function cacheDirForDocument(doc: TextDocument) {

return undefined;
}

class ToggleRenderOnSaveCommand implements Command {
constructor(private readonly context_: ExtensionContext) { }
private static readonly id = "quarto.toggleRenderOnSave";
public readonly id = ToggleRenderOnSaveCommand.id;

async execute() {
let renderOnSave = this.context_.workspaceState.get<boolean>('positron.quarto.toggleRenderOnSave') === true;
renderOnSave = !renderOnSave;
this.context_.workspaceState.update('positron.quarto.toggleRenderOnSave', renderOnSave);
commands.executeCommand<boolean>('setContext', 'quarto.renderOnSave', renderOnSave);
}
}
12 changes: 9 additions & 3 deletions apps/vscode/src/providers/preview/preview-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import semver from "semver";

import vscode from "vscode";
import { TextDocument, Uri, workspace } from "vscode";
import { TextDocument, ExtensionContext, workspace } from "vscode";

import {
projectDirForDocument,
Expand Down Expand Up @@ -87,7 +87,7 @@ async function parseRPackageDescription(): Promise<string[]> {
return [''];
}

export async function renderOnSave(engine: MarkdownEngine, document: TextDocument) {
export async function renderOnSave(engine: MarkdownEngine, document: TextDocument, context: ExtensionContext) {
// if its a notebook and we don't have a save hook for notebooks then don't
// allow renderOnSave (b/c we can't detect the saves)
if (isNotebook(document) && !haveNotebookSaveEvents()) {
Expand All @@ -99,7 +99,13 @@ export async function renderOnSave(engine: MarkdownEngine, document: TextDocumen
return true;
}

// first look for document level editor setting
// first look at workspace state for toggle value in Positron
const toggleRenderOnSave = context.workspaceState.get<boolean>('positron.quarto.toggleRenderOnSave') === true;
if (toggleRenderOnSave) {
return toggleRenderOnSave;
}

// next look for document level editor setting
const docYaml = documentFrontMatter(engine, document);
const docSetting = readRenderOnSave(docYaml);
if (docSetting !== undefined) {
Expand Down
14 changes: 12 additions & 2 deletions apps/vscode/src/providers/preview/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import * as semver from "semver";
import vscode, {
commands,
env,
workspace,
ExtensionContext,
MessageItem,
Terminal,
Expand Down Expand Up @@ -88,6 +89,7 @@ import {
yamlErrorLocation,
} from "./preview-errors";
import { ExtensionHost } from "../../host";
import { hasHooks } from "../../host/hooks";

tmp.setGracefulCleanup();

Expand All @@ -110,6 +112,14 @@ export function activatePreview(
context.subscriptions.push(previewManager);
}

// initialize Positron toggle from setting, store in workspace storage
if (hasHooks()) {
const config = workspace.getConfiguration("quarto");
const renderOnSave = config.get<boolean>("render.renderOnSave", false);
commands.executeCommand<boolean>("setContext", "quarto.renderOnSave", renderOnSave);
context.workspaceState.update('positron.quarto.toggleRenderOnSave', renderOnSave);
}

// render on save
const onSave = async (docUri: Uri) => {
const editor = findQuartoEditor(
Expand All @@ -120,7 +130,7 @@ export function activatePreview(
if (editor) {
if (
canPreviewDoc(editor.document) &&
(await renderOnSave(engine, editor.document)) &&
(await renderOnSave(engine, editor.document, context)) &&
(await previewManager.isPreviewRunningForDoc(editor.document))
) {
await previewDoc(editor, undefined, true, engine, quartoContext);
Expand Down Expand Up @@ -172,7 +182,7 @@ export function activatePreview(
);

// preview commands
return previewCommands(quartoContext, engine);
return previewCommands(quartoContext, context, engine);
}


Expand Down