Skip to content

Commit 6559a00

Browse files
authored
refactor(vscode): moving format code into function (#4204)
1 parent d49e1f3 commit 6559a00

2 files changed

Lines changed: 34 additions & 37 deletions

File tree

vscode/extension/src/commands/format.ts

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,36 @@
11
import { traceError, traceLog } from "../utilities/common/log"
2-
import { execSync } from 'child_process'
2+
import { execSync } from "child_process"
33
import { sqlmesh_exec } from "../utilities/sqlmesh/sqlmesh"
44
import { isErr } from "../utilities/functional/result"
5+
import * as vscode from "vscode"
56

6-
interface CLICallout {
7-
format: () => Promise<number>
7+
export const format = async () => {
8+
traceLog("Calling format")
9+
const out = await internalFormat()
10+
if (out === 0) {
11+
vscode.window.showInformationMessage("Project formatted successfully")
12+
} else {
13+
vscode.window.showErrorMessage("Project format failed")
14+
}
815
}
916

10-
export const actual_callout: CLICallout = {
11-
format: async () => {
12-
traceLog("Calling format")
13-
try {
14-
const exec = await sqlmesh_exec()
15-
if (isErr(exec)) {
16-
traceError(exec.error)
17-
return 1
18-
}
19-
execSync(`${exec.value.bin} format`, { encoding: 'utf-8', cwd: exec.value.workspacePath, env: exec.value.env })
20-
return 0
21-
} catch (error: any) {
22-
traceError('Error executing sqlmesh format:', error.message)
23-
traceError(error.stdout)
24-
traceError(error.stderr)
25-
return error.status || 1
26-
}
17+
const internalFormat = async (): Promise<number> => {
18+
try {
19+
const exec = await sqlmesh_exec()
20+
if (isErr(exec)) {
21+
traceError(exec.error)
22+
return 1
2723
}
24+
execSync(`${exec.value.bin} format`, {
25+
encoding: "utf-8",
26+
cwd: exec.value.workspacePath,
27+
env: exec.value.env,
28+
})
29+
return 0
30+
} catch (error: any) {
31+
traceError("Error executing sqlmesh format:", error.message)
32+
traceError(error.stdout)
33+
traceError(error.stderr)
34+
return error.status || 1
35+
}
2836
}
29-

vscode/extension/src/extension.ts

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
// The module 'vscode' contains the VS Code extensibility API
2-
// Import the module and reference it with the alias vscode in your code below
31
import * as vscode from "vscode"
4-
import { actual_callout } from "./commands/format"
2+
import { format } from "./commands/format"
53
import {
64
createOutputChannel,
75
onDidChangeConfiguration,
@@ -48,23 +46,16 @@ export async function activate(context: vscode.ExtensionContext) {
4846
)
4947

5048
context.subscriptions.push(
51-
vscode.commands.registerCommand("sqlmesh.signinSpecifyFlow", signInSpecifyFlow(authProvider)
49+
vscode.commands.registerCommand(
50+
"sqlmesh.signinSpecifyFlow",
51+
signInSpecifyFlow(authProvider)
5252
)
5353
)
54-
5554
context.subscriptions.push(
5655
vscode.commands.registerCommand("sqlmesh.signout", signOut(authProvider))
5756
)
58-
5957
context.subscriptions.push(
60-
vscode.commands.registerCommand("sqlmesh.format", async () => {
61-
const out = await actual_callout.format()
62-
if (out === 0) {
63-
vscode.window.showInformationMessage("Project formatted successfully")
64-
} else {
65-
vscode.window.showErrorMessage("Project format failed")
66-
}
67-
})
58+
vscode.commands.registerCommand("sqlmesh.format", format)
6859
)
6960

7061
lspClient = new LSPClient()
@@ -77,12 +68,11 @@ export async function activate(context: vscode.ExtensionContext) {
7768
await lspClient.restart()
7869
}
7970
}
80-
8171
context.subscriptions.push(
8272
onDidChangePythonInterpreter(async () => {
8373
await restart()
8474
}),
85-
onDidChangeConfiguration(async (_: vscode.ConfigurationChangeEvent) => {
75+
onDidChangeConfiguration(async () => {
8676
await restart()
8777
}),
8878
registerCommand(`sqlmesh.restart`, async () => {

0 commit comments

Comments
 (0)