Skip to content

Commit bdf080c

Browse files
committed
fix(vscode): on sign in, restart lsp
1 parent 05c793c commit bdf080c

6 files changed

Lines changed: 467 additions & 311 deletions

File tree

vscode/extension/src/commands/format.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,18 @@ export const format =
1111
(
1212
authProvider: AuthenticationProviderTobikoCloud,
1313
lsp: LSPClient | undefined,
14+
restartLSP: () => Promise<void>,
1415
) =>
1516
async (): Promise<void> => {
1617
traceLog('Calling format')
1718
const out = await internalFormat(lsp)
1819
if (isErr(out)) {
19-
return handleError(authProvider, out.error, 'Project format failed')
20+
return handleError(
21+
authProvider,
22+
restartLSP,
23+
out.error,
24+
'Project format failed',
25+
)
2026
}
2127
vscode.window.showInformationMessage('Project formatted successfully')
2228
}
Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
11
import { AuthenticationProviderTobikoCloud } from '../auth/auth'
22
import * as vscode from 'vscode'
33
import { isCodespaces } from '../utilities/isCodespaces'
4+
import { traceInfo } from '../utilities/common/log'
45

56
export const signIn =
6-
(authenticationProvider: AuthenticationProviderTobikoCloud) => async () => {
7+
(
8+
authenticationProvider: AuthenticationProviderTobikoCloud,
9+
onSignInSuccess: () => Promise<void>,
10+
) =>
11+
async () => {
712
if (isCodespaces()) {
813
await authenticationProvider.sign_in_device_flow()
914
} else {
1015
await authenticationProvider.createSession()
1116
}
12-
await vscode.window.showInformationMessage('Signed in successfully')
17+
18+
// Do not await this, as this will block the thread, you just need to show the message, but not block
19+
vscode.window.showInformationMessage('Signed in successfully')
20+
21+
// Execute callback after successful sign-in
22+
if (onSignInSuccess) {
23+
traceInfo('Executing post sign-in callback')
24+
try {
25+
await onSignInSuccess()
26+
} catch (error) {
27+
traceInfo(`Error in post sign-in callback: ${error}`)
28+
}
29+
}
1330
}

vscode/extension/src/commands/signinSpecifyFlow.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import { traceInfo } from '../utilities/common/log'
33
import { window } from 'vscode'
44

55
export const signInSpecifyFlow =
6-
(authenticationProvider: AuthenticationProviderTobikoCloud) => async () => {
6+
(
7+
authenticationProvider: AuthenticationProviderTobikoCloud,
8+
onSignInSuccess?: () => Promise<void>,
9+
) =>
10+
async () => {
711
traceInfo('Sign in specify flow')
812
const flowOptions = [
913
{
@@ -24,11 +28,31 @@ export const signInSpecifyFlow =
2428
await authenticationProvider.sign_in_oauth_flow()
2529
await authenticationProvider.getSessions()
2630
await window.showInformationMessage('Sign in success')
31+
32+
// Execute callback after successful sign-in
33+
if (onSignInSuccess) {
34+
traceInfo('Executing post sign-in callback')
35+
try {
36+
await onSignInSuccess()
37+
} catch (error) {
38+
traceInfo(`Error in post sign-in callback: ${error}`)
39+
}
40+
}
2741
return
2842
} else if (selectedFlow.label === 'Device Flow') {
2943
await authenticationProvider.sign_in_device_flow()
3044
await authenticationProvider.getSessions()
3145
await window.showInformationMessage('Sign in success')
46+
47+
// Execute callback after successful sign-in
48+
if (onSignInSuccess) {
49+
traceInfo('Executing post sign-in callback')
50+
try {
51+
await onSignInSuccess()
52+
} catch (error) {
53+
traceInfo(`Error in post sign-in callback: ${error}`)
54+
}
55+
}
3256
return
3357
} else {
3458
traceInfo('Invalid flow selected')

vscode/extension/src/extension.ts

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,21 @@ export async function activate(context: vscode.ExtensionContext) {
4545
traceInfo('Authentication provider registered')
4646

4747
context.subscriptions.push(
48-
vscode.commands.registerCommand('sqlmesh.signin', signIn(authProvider)),
48+
vscode.commands.registerCommand(
49+
'sqlmesh.signin',
50+
signIn(authProvider, async () => {
51+
traceInfo('Restarting LSP after sign-in')
52+
await restart()
53+
}),
54+
),
4955
)
5056
context.subscriptions.push(
5157
vscode.commands.registerCommand(
5258
'sqlmesh.signinSpecifyFlow',
53-
signInSpecifyFlow(authProvider),
59+
signInSpecifyFlow(authProvider, async () => {
60+
traceInfo('Restarting LSP after sign-in')
61+
await restart()
62+
}),
5463
),
5564
)
5665
context.subscriptions.push(
@@ -76,13 +85,6 @@ export async function activate(context: vscode.ExtensionContext) {
7685
),
7786
)
7887

79-
context.subscriptions.push(
80-
vscode.commands.registerCommand(
81-
'sqlmesh.format',
82-
format(authProvider, lspClient),
83-
),
84-
)
85-
8688
// Register the webview
8789
const lineagePanel = new LineagePanel(context.extensionUri, lspClient)
8890
context.subscriptions.push(
@@ -118,14 +120,35 @@ export async function activate(context: vscode.ExtensionContext) {
118120
if (isErr(restartResult)) {
119121
return handleError(
120122
authProvider,
123+
restart,
121124
restartResult.error,
122125
'LSP restart failed',
123126
)
124127
}
125128
context.subscriptions.push(lspClient)
129+
} else {
130+
lspClient = new LSPClient()
131+
const result = await lspClient.start()
132+
if (isErr(result)) {
133+
return handleError(
134+
authProvider,
135+
restart,
136+
result.error,
137+
'Failed to start LSP',
138+
)
139+
} else {
140+
context.subscriptions.push(lspClient)
141+
}
126142
}
127143
}
128144

145+
context.subscriptions.push(
146+
vscode.commands.registerCommand(
147+
'sqlmesh.format',
148+
format(authProvider, lspClient, restart),
149+
),
150+
)
151+
129152
context.subscriptions.push(
130153
onDidChangePythonInterpreter(async () => {
131154
await restart()
@@ -140,7 +163,12 @@ export async function activate(context: vscode.ExtensionContext) {
140163

141164
const result = await lspClient.start()
142165
if (isErr(result)) {
143-
return handleError(authProvider, result.error, 'Failed to start LSP')
166+
return handleError(
167+
authProvider,
168+
restart,
169+
result.error,
170+
'Failed to start LSP',
171+
)
144172
} else {
145173
context.subscriptions.push(lspClient)
146174
}

vscode/extension/src/utilities/errors.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ interface SqlmeshLspDependenciesMissingError {
6969

7070
export async function handleError(
7171
authProvider: AuthenticationProviderTobikoCloud,
72+
restartLsp: () => Promise<void>,
7273
error: ErrorType,
7374
genericErrorPrefix?: string,
7475
): Promise<void> {
@@ -85,7 +86,7 @@ export async function handleError(
8586
)
8687
return
8788
case 'not_signed_in':
88-
return handleNotSignedInError(authProvider)
89+
return handleNotSignedInError(authProvider, restartLsp)
8990
case 'sqlmesh_not_found':
9091
return handleSqlmeshNotFoundError()
9192
case 'sqlmesh_lsp_not_found':
@@ -110,14 +111,15 @@ export async function handleError(
110111
*/
111112
const handleNotSignedInError = async (
112113
authProvider: AuthenticationProviderTobikoCloud,
114+
restartLsp: () => Promise<void>,
113115
): Promise<void> => {
114116
traceInfo('handleNotSginedInError')
115117
const result = await window.showInformationMessage(
116118
'Please sign in to Tobiko Cloud to use SQLMesh',
117119
'Sign In',
118120
)
119121
if (result === 'Sign In') {
120-
await signIn(authProvider)()
122+
await signIn(authProvider, restartLsp)()
121123
}
122124
}
123125

0 commit comments

Comments
 (0)