Skip to content

Commit b267cea

Browse files
committed
refactor: code structure cleanup
1 parent bfcd3c1 commit b267cea

File tree

8 files changed

+146
-139
lines changed

8 files changed

+146
-139
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"onNotebook:sql-notebook",
2222
"onView:sqlnotebook-connections"
2323
],
24-
"main": "./out/extension/extension.js",
24+
"main": "./out/extension/main.js",
2525
"capabilities": {
2626
"virtualWorkspaces": true,
2727
"untrustedWorkspaces": {
@@ -137,6 +137,7 @@
137137
}
138138
},
139139
"scripts": {
140+
"build": "vsce package",
140141
"vscode:prepublish": "npm run compile",
141142
"compile": "tsc -b ./src & webpack --config ./webview/webpack.config.js",
142143
"lint": "eslint src --ext ts",

src/commands.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
SQLNotebookConnections,
66
} from './connections';
77
import { getPool, PoolConfig } from './driver';
8-
import { storageKey, globalConnPool, globalLspClient } from './extension';
8+
import { storageKey, globalConnPool, globalLspClient } from './main';
99
import { getCompiledLSPBinaryPath, sqlsDriverFromDriver } from './lsp';
1010

1111
export function deleteConnectionConfiguration(

src/connections.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as vscode from 'vscode';
22
import * as path from 'path';
3-
import { storageKey } from './extension';
3+
import { storageKey } from './main';
44
import { DriverKey } from './driver';
55

66
export class SQLNotebookConnections
Lines changed: 27 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,8 @@
1-
import { TextDecoder, TextEncoder } from 'util';
21
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';
84

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 {
986
readonly controllerId = 'sql-notebook-executor';
997
readonly notebookType = notebookType;
1008
readonly label = 'SQL Notebook';
@@ -184,6 +92,30 @@ class SQLNotebookController {
18492
}
18593
}
18694

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+
187119
function resultToMarkdownTable(result: TabularResult): string {
188120
if (result.length < 1) {
189121
return '*Empty Results Table*';
@@ -229,42 +161,3 @@ function markdownHeader(obj: Row): string {
229161
.join(' | ');
230162
return `| ${keys} |\n| ${divider} |`;
231163
}
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-
}

src/form.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as vscode from 'vscode';
22
import { ConnData } from './connections';
3-
import { storageKey } from './extension';
3+
import { storageKey } from './main';
44

55
export function activateFormProvider(context: vscode.ExtensionContext) {
66
const provider = new SQLConfigurationViewProvider(

src/lsp.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
} from 'vscode-languageclient/node';
66
import * as vscode from 'vscode';
77
import { DriverKey } from './driver';
8-
import path = require('path');
8+
import * as path from 'path';
99

1010
export interface LspConfig {
1111
binPath: string;

src/main.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import * as vscode from 'vscode';
2+
import { SQLNotebookConnections } from './connections';
3+
import { connectToDatabase, deleteConnectionConfiguration } from './commands';
4+
import { Pool } from './driver';
5+
import { activateFormProvider } from './form';
6+
import { SqlLspClient } from './lsp';
7+
import { SQLSerializer } from './serializer';
8+
import { SQLNotebookController } from './controller';
9+
10+
export const notebookType = 'sql-notebook';
11+
export const storageKey = 'sqlnotebook-connections';
12+
13+
export const globalConnPool: { pool: Pool | null } = {
14+
pool: null,
15+
};
16+
17+
export const globalLspClient = new SqlLspClient();
18+
19+
export function activate(context: vscode.ExtensionContext) {
20+
context.subscriptions.push(
21+
vscode.workspace.registerNotebookSerializer(
22+
notebookType,
23+
new SQLSerializer()
24+
)
25+
);
26+
const connectionsSidepanel = new SQLNotebookConnections(context);
27+
vscode.window.registerTreeDataProvider(
28+
'sqlnotebook-connections',
29+
connectionsSidepanel
30+
);
31+
32+
activateFormProvider(context);
33+
34+
context.subscriptions.push(new SQLNotebookController());
35+
36+
vscode.commands.registerCommand(
37+
'sqlnotebook.deleteConnectionConfiguration',
38+
deleteConnectionConfiguration(context, connectionsSidepanel)
39+
);
40+
41+
vscode.commands.registerCommand('sqlnotebook.refreshConnectionPanel', () => {
42+
connectionsSidepanel.refresh();
43+
});
44+
vscode.commands.registerCommand(
45+
'sqlnotebook.connect',
46+
connectToDatabase(context, connectionsSidepanel)
47+
);
48+
}
49+
50+
export function deactivate() {}

src/serializer.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { TextDecoder, TextEncoder } from 'util';
2+
import * as vscode from 'vscode';
3+
4+
export class SQLSerializer implements vscode.NotebookSerializer {
5+
async deserializeNotebook(
6+
context: Uint8Array,
7+
_token: vscode.CancellationToken
8+
): Promise<vscode.NotebookData> {
9+
const str = new TextDecoder().decode(context);
10+
const blocks = splitSqlBlocks(str);
11+
12+
const cells = blocks.map((query) => {
13+
const isMarkdown = query.startsWith('/*markdown') && query.endsWith('*/');
14+
if (isMarkdown) {
15+
const lines = query.split('\n');
16+
const innerMarkdown =
17+
lines.length > 2 ? lines.slice(1, lines.length - 1).join('\n') : '';
18+
return new vscode.NotebookCellData(
19+
vscode.NotebookCellKind.Markup,
20+
innerMarkdown,
21+
'markdown'
22+
);
23+
}
24+
25+
return new vscode.NotebookCellData(
26+
vscode.NotebookCellKind.Code,
27+
query,
28+
'sql'
29+
);
30+
});
31+
return new vscode.NotebookData(cells);
32+
}
33+
34+
async serializeNotebook(
35+
data: vscode.NotebookData,
36+
_token: vscode.CancellationToken
37+
): Promise<Uint8Array> {
38+
return new TextEncoder().encode(
39+
data.cells
40+
.map(({ value, kind }) =>
41+
kind === vscode.NotebookCellKind.Code
42+
? value
43+
: `/*markdown\n${value}\n*/`
44+
)
45+
.join('\n\n')
46+
);
47+
}
48+
}
49+
50+
function splitSqlBlocks(raw: string): string[] {
51+
const blocks = [];
52+
for (const block of raw.split('\n\n')) {
53+
if (block.trim().length > 0) {
54+
blocks.push(block);
55+
continue;
56+
}
57+
if (blocks.length < 1) {
58+
continue;
59+
}
60+
blocks[blocks.length - 1] += '\n\n';
61+
}
62+
return blocks;
63+
}

0 commit comments

Comments
 (0)