@@ -40,6 +40,35 @@ def __init__(
4040 def _register_features (self ) -> None :
4141 """Register LSP features on the internal LanguageServer instance."""
4242
43+ @self .server .feature (types .INITIALIZE )
44+ def initialize (ls : LanguageServer , params : types .InitializeParams ) -> None :
45+ """Initialize the server when the client connects."""
46+ try :
47+ if params .workspace_folders :
48+ # Try to find a SQLMesh config file in any workspace folder (only at the root level)
49+ for folder in params .workspace_folders :
50+ folder_path = Path (self ._uri_to_path (folder .uri ))
51+ # Only check for config files directly in the workspace directory
52+ for ext in ("py" , "yml" , "yaml" ):
53+ config_path = folder_path / f"config.{ ext } "
54+ if config_path .exists ():
55+ try :
56+ # Use user-provided instantiator to build the context
57+ created_context = self .context_class (paths = [folder_path ])
58+ self .lsp_context = LSPContext (created_context )
59+ ls .show_message (
60+ f"Loaded SQLMesh context from { config_path } " ,
61+ types .MessageType .Info ,
62+ )
63+ return # Exit after successfully loading any config
64+ except Exception as e :
65+ ls .show_message (
66+ f"Error loading context from { config_path } : { e } " ,
67+ types .MessageType .Warning ,
68+ )
69+ except Exception as e :
70+ ls .show_message (f"Error initializing SQLMesh context: { e } " , types .MessageType .Error )
71+
4372 @self .server .feature (ALL_MODELS_FEATURE )
4473 def all_models (ls : LanguageServer , params : AllModelsRequest ) -> AllModelsResponse :
4574 try :
0 commit comments