@@ -5,19 +5,45 @@ import * as vscode from 'vscode'
55import { ErrorType , handleError } from '../utilities/errors'
66import { AuthenticationProviderTobikoCloud } from '../auth/auth'
77import { execAsync } from '../utilities/exec'
8+ import { LSPClient } from '../lsp/lsp'
89
910export const format =
10- ( authProvider : AuthenticationProviderTobikoCloud ) =>
11+ (
12+ authProvider : AuthenticationProviderTobikoCloud ,
13+ lsp : LSPClient | undefined ,
14+ ) =>
1115 async ( ) : Promise < void > => {
1216 traceLog ( 'Calling format' )
13- const out = await internalFormat ( )
17+ const out = await internalFormat ( lsp )
1418 if ( isErr ( out ) ) {
1519 return handleError ( authProvider , out . error , 'Project format failed' )
1620 }
1721 vscode . window . showInformationMessage ( 'Project formatted successfully' )
1822 }
1923
20- const internalFormat = async ( ) : Promise < Result < undefined , ErrorType > > => {
24+ const internalFormat = async (
25+ lsp : LSPClient | undefined ,
26+ ) : Promise < Result < undefined , ErrorType > > => {
27+ try {
28+ // Try LSP method first
29+ if ( lsp ) {
30+ const response = await lsp . call_custom_method (
31+ 'sqlmesh/format_project' ,
32+ { } ,
33+ )
34+ if ( isErr ( response ) ) {
35+ return response
36+ }
37+ return ok ( undefined )
38+ }
39+ } catch ( error ) {
40+ traceLog ( `LSP format failed, falling back to CLI: ${ JSON . stringify ( error ) } ` )
41+ }
42+
43+ // Fallback to CLI method if LSP is not available
44+ // TODO This is a solution in order to be backwards compatible in the cases
45+ // where the LSP method is not implemented yet. This should be removed at
46+ // some point in the future.
2147 const exec = await sqlmeshExec ( )
2248 if ( isErr ( exec ) ) {
2349 return exec
0 commit comments