From 4c119a588b735b41b8204c90a48053d21f19b220 Mon Sep 17 00:00:00 2001 From: RandomFractals Date: Mon, 20 Feb 2023 10:13:18 -0600 Subject: [PATCH 1/5] add prql.openSqlPreview command to file explorer context menu --- package.json | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/package.json b/package.json index a0818b7..8caf2db 100644 --- a/package.json +++ b/package.json @@ -81,6 +81,13 @@ } ], "menus": { + "explorer/context": [ + { + "when": "resourceLangId == prql", + "command": "prql.openSqlPreview", + "group": "prql" + } + ], "editor/title": [ { "command": "prql.openSqlPreview", From 4f2a1518ba91dff3423e6d55f04d460f93a39a44 Mon Sep 17 00:00:00 2001 From: RandomFractals Date: Mon, 20 Feb 2023 10:18:31 -0600 Subject: [PATCH 2/5] use resourceLangId == prql when clause for prql commands to simplify that when clause, and use prql group for the prql commands in editor/title/context menu to show them at the end of that menu list --- package.json | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 8caf2db..70beb6b 100644 --- a/package.json +++ b/package.json @@ -83,25 +83,25 @@ "menus": { "explorer/context": [ { - "when": "resourceLangId == prql", "command": "prql.openSqlPreview", + "when": "resourceLangId == prql", "group": "prql" } ], "editor/title": [ { "command": "prql.openSqlPreview", - "when": "resourceFilename =~ /\\.prql$/", + "when": "resourceLangId == prql", "group": "navigation" }, { "command": "prql.generateSqlFile", - "when": "resourceFilename =~ /\\.prql$/", + "when": "resourceLangId == prql", "group": "navigation" }, { "command": "prql.viewSettings", - "when": "resourceFilename =~ /\\.prql$/", + "when": "resourceLangId == prql", "group": "navigation" }, { @@ -113,13 +113,13 @@ "editor/title/context": [ { "command": "prql.openSqlPreview", - "when": "resourceFilename =~ /\\.prql$/", - "group": "navigation" + "when": "resourceLangId == prql", + "group": "prql" }, { "command": "prql.generateSqlFile", - "when": "resourceFilename =~ /\\.prql$/", - "group": "navigation" + "when": "resourceLangId == prql", + "group": "prql" } ] }, From 45311fdb6cbb6090566ef19b3bec1378538ae74c Mon Sep 17 00:00:00 2001 From: RandomFractals Date: Mon, 20 Feb 2023 10:53:22 -0600 Subject: [PATCH 3/5] update prql.generateSqlFile command to work with the file explorer context menu and generate sql from a prql file without opening the prql source document --- package.json | 5 +++ src/commands.ts | 93 ++++++++++++++++++++++++++++--------------------- 2 files changed, 59 insertions(+), 39 deletions(-) diff --git a/package.json b/package.json index 70beb6b..f7cbc54 100644 --- a/package.json +++ b/package.json @@ -86,6 +86,11 @@ "command": "prql.openSqlPreview", "when": "resourceLangId == prql", "group": "prql" + }, + { + "command": "prql.generateSqlFile", + "when": "resourceLangId == prql", + "group": "prql" } ], "editor/title": [ diff --git a/src/commands.ts b/src/commands.ts index eb00256..15b21fe 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -8,12 +8,13 @@ import { Uri, } from 'vscode'; +import * as fs from 'fs'; import * as path from 'path'; -import * as constants from './constants'; -import { compile } from './compiler'; -import { TextEncoder } from 'util'; import { SqlPreview } from './views/sqlPreview'; +import { TextEncoder } from 'util'; +import { compile } from './compiler'; +import * as constants from './constants'; /** * Registers PRQL extension commands. @@ -21,9 +22,25 @@ import { SqlPreview } from './views/sqlPreview'; * @param context Extension context. */ export function registerCommands(context: ExtensionContext) { - registerCommand(context, constants.GenerateSqlFile, generateSqlFile); + registerCommand(context, constants.ViewSettings, viewPrqlSettings); + registerCommand(context, constants.GenerateSqlFile, (documentUri: Uri) => { + + const editor = window.activeTextEditor; + if (!documentUri && editor && editor.document.languageId === 'prql') { + // use prql from the active prql text editor + generateSqlFile(editor.document.uri, editor.document.getText()); + } + else if (documentUri && + fs.existsSync(documentUri.fsPath) && + documentUri.fsPath.endsWith('.prql')) { + // load prql code from the local prql file + const prqlCode: string = fs.readFileSync(documentUri.fsPath, 'utf8'); + generateSqlFile(documentUri, prqlCode); + } + }); + registerCommand(context, constants.OpenSqlPreview, (documentUri: Uri) => { if (!documentUri && window.activeTextEditor) { // use active text editor document Uri @@ -92,50 +109,48 @@ async function viewPrqlSettings() { } /** - * Compiles PRQL text from the active text editor, - * and creates or updates the corresponding SQL file - * with PRQL compiler output. + * Compiles PRQL text for the given PRQL document uri, and creates + * or updates the corresponding SQL file with PRQL compiler output. * * Opens generated SQL file in text editor for code formatting, * or running generated SQL statements with available * vscode database extensions and sql tools. + * + * @param prqlDocumentUri PRQL source document Uri. + * @param prqlCode PRQL source code. */ -async function generateSqlFile() { - const editor = window.activeTextEditor; +async function generateSqlFile(prqlDocumentUri: Uri, prqlCode: string) { - if (editor && editor.document.languageId === 'prql') { - // compile PRQL - const prql = editor.document.getText(); - const result = compile(prql); + // compile given prql source code + const sqlCode = compile(prqlCode); - if (Array.isArray(result)) { - window.showErrorMessage(`PRQL Compile \ - ${result[0].display ?? result[0].reason}`); + if (Array.isArray(sqlCode)) { + // display prql compilation errors + window.showErrorMessage(`PRQL Compile \ + ${sqlCode[0].display ?? sqlCode[0].reason}`); + } + else { + // get sql file generation prql settings + const prqlSettings = workspace.getConfiguration('prql'); + const target = prqlSettings.get('target'); + const addTargetDialectToSqlFilenames = + prqlSettings.get(constants.AddTargetDialectToSqlFilenames); + + // create sql filename based on prql file path, name, and current settings + const prqlFilePath = path.parse(prqlDocumentUri.fsPath); + let sqlFilenameSuffix = ''; + if (addTargetDialectToSqlFilenames && target !== 'Generic' && target !== 'None') { + sqlFilenameSuffix = `.${target.toLowerCase()}`; } - else { - const prqlDocumentUri: Uri = editor.document.uri; - const prqlFilePath = path.parse(prqlDocumentUri.fsPath); - const prqlSettings = workspace.getConfiguration('prql'); - const target = prqlSettings.get('target'); - const addTargetDialectToSqlFilenames = - prqlSettings.get(constants.AddTargetDialectToSqlFilenames); - - let sqlFilenameSuffix = ''; - if (addTargetDialectToSqlFilenames && target !== 'Generic' && target !== 'None') { - sqlFilenameSuffix = `.${target.toLowerCase()}`; - } + const sqlFilePath = path.join(prqlFilePath.dir, `${prqlFilePath.name}${sqlFilenameSuffix}.sql`); - // create sql filename based on prql file path, name, and current settings - const sqlFilePath = path.join(prqlFilePath.dir, `${prqlFilePath.name}${sqlFilenameSuffix}.sql`); + // create sql file + const sqlFileUri: Uri = Uri.file(sqlFilePath); + const textEncoder: TextEncoder = new TextEncoder(); + const sqlContent: Uint8Array = textEncoder.encode(sqlCode); + await workspace.fs.writeFile(sqlFileUri, sqlContent); - // create sql file - const sqlFileUri: Uri = Uri.file(sqlFilePath); - const textEncoder: TextEncoder = new TextEncoder(); - const sqlContent: Uint8Array = textEncoder.encode(result); - await workspace.fs.writeFile(sqlFileUri, sqlContent); - - // show generated sql file - await window.showTextDocument(sqlFileUri); - } + // show generated sql file + await window.showTextDocument(sqlFileUri); } } From 419f77c24c5157c5c9588bf64be43abf82ff4990 Mon Sep 17 00:00:00 2001 From: RandomFractals Date: Mon, 20 Feb 2023 11:04:29 -0600 Subject: [PATCH 4/5] enable copy to clipboard on prql docs and in editor/title/context menu --- package.json | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index f7cbc54..52c7b77 100644 --- a/package.json +++ b/package.json @@ -111,7 +111,7 @@ }, { "command": "prql.copySqlToClipboard", - "when": "prql.sqlPreviewActive", + "when": "prql.sqlPreviewActive || resourceLangId == prql", "group": "navigation" } ], @@ -125,6 +125,11 @@ "command": "prql.generateSqlFile", "when": "resourceLangId == prql", "group": "prql" + }, + { + "command": "prql.copySqlToClipboard", + "when": "prql.sqlPreviewActive || resourceLangId == prql", + "group": "prql" } ] }, From 7ab9d7ff1be75ced22155563f1ebf1e84411ee8f Mon Sep 17 00:00:00 2001 From: RandomFractals Date: Mon, 20 Feb 2023 11:16:37 -0600 Subject: [PATCH 5/5] change PRQL Settings command title to View PRQL Settings and show it in PRQL editor/title/context menus too --- package.json | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 52c7b77..f4547c9 100644 --- a/package.json +++ b/package.json @@ -75,7 +75,7 @@ }, { "command": "prql.viewSettings", - "title": "Settings", + "title": "View PRQL Settings", "category": "PRQL", "icon": "$(gear)" } @@ -126,6 +126,11 @@ "when": "resourceLangId == prql", "group": "prql" }, + { + "command": "prql.viewSettings", + "when": "resourceLangId == prql", + "group": "prql" + }, { "command": "prql.copySqlToClipboard", "when": "prql.sqlPreviewActive || resourceLangId == prql",