Skip to content
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

add new prql file and text editor context menus #118

Merged
merged 5 commits into from
Feb 20, 2023
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
40 changes: 31 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,44 +75,66 @@
},
{
"command": "prql.viewSettings",
"title": "Settings",
"title": "View PRQL Settings",
"category": "PRQL",
"icon": "$(gear)"
}
],
"menus": {
"explorer/context": [
{
"command": "prql.openSqlPreview",
"when": "resourceLangId == prql",
"group": "prql"
},
{
"command": "prql.generateSqlFile",
"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"
},
{
"command": "prql.copySqlToClipboard",
"when": "prql.sqlPreviewActive",
"when": "prql.sqlPreviewActive || resourceLangId == prql",
"group": "navigation"
}
],
"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"
},
{
"command": "prql.viewSettings",
"when": "resourceLangId == prql",
"group": "prql"
},
{
"command": "prql.copySqlToClipboard",
"when": "prql.sqlPreviewActive || resourceLangId == prql",
"group": "prql"
}
]
},
Expand Down
93 changes: 54 additions & 39 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,39 @@ 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.
*
* @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
Expand Down Expand Up @@ -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 = <string>prqlSettings.get('target');
const addTargetDialectToSqlFilenames =
<boolean>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 = <string>prqlSettings.get('target');
const addTargetDialectToSqlFilenames =
<boolean>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);
}
}