|
1 | | -import { TextDecoder, TextEncoder } from 'util'; |
2 | 1 | import * as vscode from 'vscode'; |
3 | | -import { SQLNotebookConnections } from './connections'; |
4 | | -import { connectToDatabase, deleteConnectionConfiguration } from './commands'; |
5 | | -import { Pool, ExecutionResult, TabularResult, Row } from './driver'; |
6 | | -import { activateFormProvider } from './form'; |
7 | | -import { SqlLspClient } from './lsp'; |
| 2 | +import { ExecutionResult, Row, TabularResult } from './driver'; |
| 3 | +import { globalConnPool, notebookType } from './main'; |
8 | 4 |
|
9 | | -const notebookType = 'sql-notebook'; |
10 | | -export const storageKey = 'sqlnotebook-connections'; |
11 | | - |
12 | | -export const globalConnPool: { pool: Pool | null } = { |
13 | | - pool: null, |
14 | | -}; |
15 | | - |
16 | | -export const globalLspClient = new SqlLspClient(); |
17 | | - |
18 | | -export function activate(context: vscode.ExtensionContext) { |
19 | | - context.subscriptions.push( |
20 | | - vscode.workspace.registerNotebookSerializer( |
21 | | - notebookType, |
22 | | - new SQLSerializer() |
23 | | - ) |
24 | | - ); |
25 | | - const connectionsSidepanel = new SQLNotebookConnections(context); |
26 | | - vscode.window.registerTreeDataProvider( |
27 | | - 'sqlnotebook-connections', |
28 | | - connectionsSidepanel |
29 | | - ); |
30 | | - |
31 | | - activateFormProvider(context); |
32 | | - |
33 | | - context.subscriptions.push(new SQLNotebookController()); |
34 | | - |
35 | | - vscode.commands.registerCommand( |
36 | | - 'sqlnotebook.deleteConnectionConfiguration', |
37 | | - deleteConnectionConfiguration(context, connectionsSidepanel) |
38 | | - ); |
39 | | - |
40 | | - vscode.commands.registerCommand('sqlnotebook.refreshConnectionPanel', () => { |
41 | | - connectionsSidepanel.refresh(); |
42 | | - }); |
43 | | - vscode.commands.registerCommand( |
44 | | - 'sqlnotebook.connect', |
45 | | - connectToDatabase(context, connectionsSidepanel) |
46 | | - ); |
47 | | -} |
48 | | - |
49 | | -export function deactivate() {} |
50 | | - |
51 | | -class SQLSerializer implements vscode.NotebookSerializer { |
52 | | - async deserializeNotebook( |
53 | | - context: Uint8Array, |
54 | | - _token: vscode.CancellationToken |
55 | | - ): Promise<vscode.NotebookData> { |
56 | | - const str = new TextDecoder().decode(context); |
57 | | - const blocks = splitSqlBlocks(str); |
58 | | - |
59 | | - const cells = blocks.map((query) => { |
60 | | - const isMarkdown = query.startsWith('/*markdown') && query.endsWith('*/'); |
61 | | - if (isMarkdown) { |
62 | | - const lines = query.split('\n'); |
63 | | - const innerMarkdown = |
64 | | - lines.length > 2 ? lines.slice(1, lines.length - 1).join('\n') : ''; |
65 | | - return new vscode.NotebookCellData( |
66 | | - vscode.NotebookCellKind.Markup, |
67 | | - innerMarkdown, |
68 | | - 'markdown' |
69 | | - ); |
70 | | - } |
71 | | - |
72 | | - return new vscode.NotebookCellData( |
73 | | - vscode.NotebookCellKind.Code, |
74 | | - query, |
75 | | - 'sql' |
76 | | - ); |
77 | | - }); |
78 | | - return new vscode.NotebookData(cells); |
79 | | - } |
80 | | - |
81 | | - async serializeNotebook( |
82 | | - data: vscode.NotebookData, |
83 | | - _token: vscode.CancellationToken |
84 | | - ): Promise<Uint8Array> { |
85 | | - return new TextEncoder().encode( |
86 | | - data.cells |
87 | | - .map(({ value, kind }) => |
88 | | - kind === vscode.NotebookCellKind.Code |
89 | | - ? value |
90 | | - : `/*markdown\n${value}\n*/` |
91 | | - ) |
92 | | - .join('\n\n') |
93 | | - ); |
94 | | - } |
95 | | -} |
96 | | - |
97 | | -class SQLNotebookController { |
| 5 | +export class SQLNotebookController { |
98 | 6 | readonly controllerId = 'sql-notebook-executor'; |
99 | 7 | readonly notebookType = notebookType; |
100 | 8 | readonly label = 'SQL Notebook'; |
@@ -184,6 +92,30 @@ class SQLNotebookController { |
184 | 92 | } |
185 | 93 | } |
186 | 94 |
|
| 95 | +function writeErr(execution: vscode.NotebookCellExecution, err: string) { |
| 96 | + execution.replaceOutput([ |
| 97 | + new vscode.NotebookCellOutput([vscode.NotebookCellOutputItem.text(err)]), |
| 98 | + ]); |
| 99 | + execution.end(false, Date.now()); |
| 100 | +} |
| 101 | + |
| 102 | +function writeSuccess( |
| 103 | + execution: vscode.NotebookCellExecution, |
| 104 | + text: string | string[], |
| 105 | + mimeType?: string |
| 106 | +) { |
| 107 | + const items = typeof text === 'string' ? [text] : text; |
| 108 | + execution.replaceOutput( |
| 109 | + items.map( |
| 110 | + (item) => |
| 111 | + new vscode.NotebookCellOutput([ |
| 112 | + vscode.NotebookCellOutputItem.text(item, mimeType), |
| 113 | + ]) |
| 114 | + ) |
| 115 | + ); |
| 116 | + execution.end(true, Date.now()); |
| 117 | +} |
| 118 | + |
187 | 119 | function resultToMarkdownTable(result: TabularResult): string { |
188 | 120 | if (result.length < 1) { |
189 | 121 | return '*Empty Results Table*'; |
@@ -229,42 +161,3 @@ function markdownHeader(obj: Row): string { |
229 | 161 | .join(' | '); |
230 | 162 | return `| ${keys} |\n| ${divider} |`; |
231 | 163 | } |
232 | | - |
233 | | -function writeErr(execution: vscode.NotebookCellExecution, err: string) { |
234 | | - execution.replaceOutput([ |
235 | | - new vscode.NotebookCellOutput([vscode.NotebookCellOutputItem.text(err)]), |
236 | | - ]); |
237 | | - execution.end(false, Date.now()); |
238 | | -} |
239 | | - |
240 | | -function writeSuccess( |
241 | | - execution: vscode.NotebookCellExecution, |
242 | | - text: string | string[], |
243 | | - mimeType?: string |
244 | | -) { |
245 | | - const items = typeof text === 'string' ? [text] : text; |
246 | | - execution.replaceOutput( |
247 | | - items.map( |
248 | | - (item) => |
249 | | - new vscode.NotebookCellOutput([ |
250 | | - vscode.NotebookCellOutputItem.text(item, mimeType), |
251 | | - ]) |
252 | | - ) |
253 | | - ); |
254 | | - execution.end(true, Date.now()); |
255 | | -} |
256 | | - |
257 | | -function splitSqlBlocks(raw: string): string[] { |
258 | | - const blocks = []; |
259 | | - for (const block of raw.split('\n\n')) { |
260 | | - if (block.trim().length > 0) { |
261 | | - blocks.push(block); |
262 | | - continue; |
263 | | - } |
264 | | - if (blocks.length < 1) { |
265 | | - continue; |
266 | | - } |
267 | | - blocks[blocks.length - 1] += '\n\n'; |
268 | | - } |
269 | | - return blocks; |
270 | | -} |
0 commit comments