From 0346dbe0af17750cb038158dab787c8401c40ddb Mon Sep 17 00:00:00 2001 From: Joe Miller Date: Tue, 10 Nov 2020 11:51:33 -0600 Subject: [PATCH 1/2] Added format-selection command --- README.md | 6 ++++++ package.json | 9 ++++++++- src/extension.js | 24 +++++++++++++++++++++++- 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c498df1..243263a 100644 --- a/README.md +++ b/README.md @@ -9,3 +9,9 @@ Format SQL files using the [sql-formatter-plus](https://github.com/kufii/sql-for **`sql-formatter.uppercase`**: Convert keywords to uppercase. Defaults to false. **`sql-formatter.linesBetweenQueries`**: Number of linebreaks between queries. Defaults to 2. + +## Commands +The following command is also added to VS Code: + +`vscode-sql-formatter.format-selection (Format the selected SQL using the configured settings)` +- Formats the selected text based on the current settings, regardless of the editor's Language Mode. diff --git a/package.json b/package.json index 6273ca5..70f813c 100644 --- a/package.json +++ b/package.json @@ -22,9 +22,16 @@ "pl/sql" ], "activationEvents": [ - "onLanguage:sql" + "onLanguage:sql", + "onCommand:vscode-sql-formatter.format-selection" ], "contributes": { + "commands": [ + { + "command": "vscode-sql-formatter.format-selection", + "title": "SQL Formatter: Format the selected SQL using the configured settings" + } + ], "configuration": { "type": "object", "title": "SQL Formatter", diff --git a/src/extension.js b/src/extension.js index c3196fd..233513f 100644 --- a/src/extension.js +++ b/src/extension.js @@ -21,11 +21,33 @@ const getConfig = ({ insertSpaces, tabSize }) => ({ linesBetweenQueries: getSetting('sql-formatter', 'linesBetweenQueries', 2) }); +const formatSelection = () => { + const editor = vscode.window.activeTextEditor; + + if (editor) { + const document = editor.document; + const selection = editor.selection; + const text = document.getText(selection); + + if(text) { + const formatted = format(text); + editor.edit(editBuilder => { + editBuilder.replace(selection, formatted); + }); + } + } +}; + const format = (text, config) => sqlFormatter.format(text, config); -module.exports.activate = () => +module.exports.activate = (context) => { + const formatSelectionCommand = vscode.commands.registerCommand('vscode-sql-formatter.format-selection', formatSelection); + vscode.languages.registerDocumentRangeFormattingEditProvider('sql', { provideDocumentRangeFormattingEdits: (document, range, options) => [ vscode.TextEdit.replace(range, format(document.getText(range), getConfig(options))) ] }); + + context.subscriptions.push(formatSelectionCommand); +} From 6dbc550af9ecd4c74f69e498de64f242c2042237 Mon Sep 17 00:00:00 2001 From: Joe Miller Date: Tue, 10 Nov 2020 17:19:59 -0600 Subject: [PATCH 2/2] Actually use the configured settings... --- src/extension.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/extension.js b/src/extension.js index 233513f..6ad37ab 100644 --- a/src/extension.js +++ b/src/extension.js @@ -28,9 +28,13 @@ const formatSelection = () => { const document = editor.document; const selection = editor.selection; const text = document.getText(selection); + const options = { + insertSpaces: editor.insertSpaces, + tabSize: editor.tabSize, + }; if(text) { - const formatted = format(text); + const formatted = format(text, getConfig(options)); editor.edit(editBuilder => { editBuilder.replace(selection, formatted); });