Skip to content
Merged
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
14 changes: 13 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"activationEvents": [
"onLanguage:prql",
"onCommand:prql.openSqlPreview",
"onCommand:prql.generateSqlFile"
"onCommand:prql.generateSqlFile",
"onCommand:prql.copySqlToClipboard"
],
"contributes": {
"languages": [
Expand Down Expand Up @@ -57,6 +58,12 @@
"title": "Generate SQL File",
"category": "PRQL",
"icon": "$(database)"
},
{
"command": "prql.copySqlToClipboard",
"title": "Copy SQL to Clipboard",
"category": "PRQL",
"icon": "$(copy)"
}
],
"menus": {
Expand All @@ -70,6 +77,11 @@
"command": "prql.generateSqlFile",
"when": "resourceFilename =~ /\\.prql$/",
"group": "navigation"
},
{
"command": "prql.copySqlToClipboard",
"when": "prql.sqlPreviewActive",
"group": "navigation"
}
],
"editor/title/context": [
Expand Down
10 changes: 10 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
commands,
env,
window,
workspace,
Disposable,
Expand All @@ -20,6 +21,15 @@ import { TextEncoder } from "util";
*/
export function registerCommands(context: ExtensionContext) {
registerCommand(context, constants.GenerateSqlFile, generateSqlFile);

registerCommand(context, constants.CopySqlToClipboard, () => {
const sql: string | undefined = context.workspaceState.get("prql.sql");
if (sql) {
// write the last generated sql code to vscode clipboard
env.clipboard.writeText(sql);
window.showInformationMessage("PRQL: SQL copied to Clipboard.");
}
});
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ export const SqlPreviewTitle = "SQL Preview";
// PRQL extension command constants
export const OpenSqlPreview = `${ExtensionId}.openSqlPreview`;
export const GenerateSqlFile = `${ExtensionId}.generateSqlFile`;
export const CopySqlToClipboard = `${ExtensionId}.copySqlToClipboard`;

// PRQL context keys
export const SqlPreviewActive = `${ExtensionId}.sqlPreviewActive`;
24 changes: 20 additions & 4 deletions src/sql_output/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,18 @@ async function compilePrql(
return {
status: "ok",
html: highlighted,
sql: result,
};
}

function clearSqlContext(context: ExtensionContext) {
commands.executeCommand("setContext", constants.SqlPreviewActive, false);
context.workspaceState.update("prql.sql", undefined);
}

let lastOkHtml: string | undefined;

function sendText(panel: WebviewPanel) {
function sendText(context: ExtensionContext, panel: WebviewPanel) {
const editor = window.activeTextEditor;

if (panel.visible && editor && isPrqlDocument(editor)) {
Expand All @@ -106,8 +112,16 @@ function sendText(panel: WebviewPanel) {
lastOkHtml = result.html;
}
panel.webview.postMessage(result);

// set sql preview flag and update sql output
commands.executeCommand("setContext", constants.SqlPreviewActive, true);
context.workspaceState.update("prql.sql", result.sql);
});
}

if (!panel.visible || !panel.active) {
clearSqlContext(context);
}
}

function sendThemeChanged(panel: WebviewPanel) {
Expand Down Expand Up @@ -141,7 +155,7 @@ function createWebviewPanel(
disposables.push(
event(
debounce(() => {
sendText(panel);
sendText(context, panel);
}, 10)
)
);
Expand All @@ -154,7 +168,8 @@ function createWebviewPanel(
if (editor && editor !== lastEditor) {
lastEditor = editor;
lastOkHtml = undefined;
sendText(panel);
clearSqlContext(context);
sendText(context, panel);
}
})
);
Expand All @@ -169,14 +184,15 @@ function createWebviewPanel(

panel.onDidDispose(
() => {
clearSqlContext(context);
disposables.forEach((d) => d.dispose());
onDidDispose();
},
undefined,
context.subscriptions
);

sendText(panel);
sendText(context, panel);

return panel;
}
Expand Down
1 change: 1 addition & 0 deletions src/sql_output/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ExtensionContext, Uri } from "vscode";
export interface CompilationResult {
status: "ok" | "error";
html?: string;
sql?: string;
error?: {
message: string;
};
Expand Down