Skip to content

Commit 83a6f47

Browse files
authored
Merge pull request #7 from belian-earth:format-on-save
Format-on-save
2 parents 6eae244 + 864f215 commit 83a6f47

11 files changed

Lines changed: 856 additions & 261 deletions

README.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Write DuckDB SQL with full IDE support right inside R strings. Take full advanta
2828
- 🔌 **R Connection Picker** - Select specific connection objects (supports `:memory:`)
2929
- 🔄 **Auto-Refresh** - Detects schema changes automatically
3030
- 🌈 **Visual Distinction** - Themed background colors for SQL strings
31+
-**SQL Auto-Format** - Format SQL with glue interpolation support
3132
- ✈️ **Air Formatter Support** - Works with multi-line SQL
3233

3334
------------------------------------------------------------------------
@@ -105,15 +106,19 @@ Optional settings (`.vscode/settings.json`):
105106
{
106107
"duckdb-r-editor.defaultExtensions": ["spatial", "httpfs"],
107108
"duckdb-r-editor.autoRefreshSchema": true,
108-
"duckdb-r-editor.useSemanticHighlighting": true
109+
"duckdb-r-editor.useSemanticHighlighting": true,
110+
"duckdb-r-editor.sqlFormattingStyle": "standard",
111+
"duckdb-r-editor.sqlKeywordCase": "upper"
109112
}
110113
```
111114

112-
**Available Settings:**
115+
**Available Settings:**
113116
- `enableAutoComplete` - Enable autocomplete (default: true)
114-
- `useSemanticHighlighting` - syntax highlighting (default: true)
115-
- `defaultExtensions` - Extensions to auto-load (default: \[\])
116-
- `autoRefreshSchema` - Auto-detect schema changes (default: true)
117+
- `useSemanticHighlighting` - Syntax highlighting (default: true)
118+
- `defaultExtensions` - Extensions to auto-load (default: \[\])
119+
- `autoRefreshSchema` - Auto-detect schema changes (default: true)
120+
- `sqlFormattingStyle` - Format style: `standard`, `tabularLeft`, `tabularRight` (default: standard)
121+
- `sqlKeywordCase` - Keyword case: `preserve`, `upper`, `lower` (default: preserve)
117122

118123
------------------------------------------------------------------------
119124

@@ -124,6 +129,7 @@ Access via Command Palette (`Cmd/Ctrl + Shift + P`):
124129
- **Connect to DuckDB Database** - Select R connection for schema
125130
- **Disconnect from Database** - Clear connection
126131
- **Refresh DuckDB Schema** - Manually update schema
132+
- **Format SQL String** - Format SQL at cursor (preserves glue interpolations)
127133
- **Load DuckDB Extension (One-Time)** - Load official extension until restart
128134

129135
------------------------------------------------------------------------

package-lock.json

Lines changed: 85 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "duckdb-r-editor",
33
"displayName": "DuckDB R Editor (Positron)",
44
"description": "Positron-exclusive SQL editor for R. Queries your active R DuckDB connection for schema - no file locking! 900+ DuckDB functions with intelligent autocomplete.",
5-
"version": "0.6.4",
5+
"version": "0.7.0",
66
"publisher": "belian-earth",
77
"license": "MIT",
88
"repository": {
@@ -67,6 +67,36 @@
6767
"type": "boolean",
6868
"default": true,
6969
"description": "Automatically refresh schema when R code references the connection object. Keeps autocomplete in sync with database changes without manual refresh commands."
70+
},
71+
"duckdb-r-editor.sqlFormattingStyle": {
72+
"type": "string",
73+
"enum": [
74+
"standard",
75+
"tabularLeft",
76+
"tabularRight"
77+
],
78+
"default": "standard",
79+
"description": "SQL formatting style. 'standard': traditional formatting, 'tabularLeft': align keywords to the left, 'tabularRight': align keywords to the right.",
80+
"enumDescriptions": [
81+
"Traditional SQL formatting with standard indentation",
82+
"Tabular format with keywords aligned to the left",
83+
"Tabular format with keywords aligned to the right"
84+
]
85+
},
86+
"duckdb-r-editor.sqlKeywordCase": {
87+
"type": "string",
88+
"enum": [
89+
"preserve",
90+
"upper",
91+
"lower"
92+
],
93+
"default": "preserve",
94+
"description": "Transform SQL keyword casing. 'preserve': keep original case, 'upper': convert to UPPERCASE, 'lower': convert to lowercase.",
95+
"enumDescriptions": [
96+
"Keep original keyword casing",
97+
"Convert keywords to UPPERCASE",
98+
"Convert keywords to lowercase"
99+
]
70100
}
71101
}
72102
},
@@ -86,6 +116,10 @@
86116
{
87117
"command": "duckdb-r-editor.loadExtension",
88118
"title": "DuckDB R Editor: Load DuckDB Extension (One-Time)"
119+
},
120+
{
121+
"command": "duckdb-r-editor.formatSQL",
122+
"title": "DuckDB R Editor: Format SQL in R String"
89123
}
90124
]
91125
},
@@ -114,6 +148,7 @@
114148
},
115149
"dependencies": {
116150
"@posit-dev/positron": "^0.2.2",
117-
"duckdb": "^0.10.0"
151+
"duckdb": "^0.10.0",
152+
"sql-formatter": "^15.7.0"
118153
}
119-
}
154+
}

src/extension.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { RCodeExecutor } from './utils/rCodeExecutor';
1515
import { EXTENSION_ID, OUTPUT_CHANNEL_NAME, TIMING } from './constants';
1616
import { getErrorMessage, isErrorType } from './utils/errorHandler';
1717
import { RCodeTemplates } from './utils/rCodeTemplates';
18+
import { SQLFormatter } from './utils/sqlFormatter';
1819

1920
// Module-level state
2021
let schemaProvider: PositronSchemaProvider | undefined;
@@ -278,6 +279,24 @@ export async function activate(context: vscode.ExtensionContext) {
278279
}
279280
);
280281

282+
const formatSQLCommand = vscode.commands.registerCommand(
283+
'duckdb-r-editor.formatSQL',
284+
async () => {
285+
const editor = vscode.window.activeTextEditor;
286+
if (!editor) {
287+
vscode.window.showWarningMessage('No active text editor');
288+
return;
289+
}
290+
291+
if (editor.document.languageId !== 'r') {
292+
vscode.window.showWarningMessage('SQL formatting is only available in R files');
293+
return;
294+
}
295+
296+
await SQLFormatter.formatSQLAtCursor(editor.document, editor.selection.active);
297+
}
298+
);
299+
281300
// Register diagnostic provider
282301
context.subscriptions.push(
283302
vscode.languages.registerCodeActionsProvider(
@@ -315,7 +334,8 @@ export async function activate(context: vscode.ExtensionContext) {
315334
connectCommand,
316335
disconnectCommand,
317336
refreshSchemaCommand,
318-
loadExtensionCommand
337+
loadExtensionCommand,
338+
formatSQLCommand
319339
);
320340

321341
// Setup auto-refresh on code execution

0 commit comments

Comments
 (0)