@@ -56,6 +56,7 @@ def __init__(
5656 self .server = LanguageServer (server_name , version )
5757 self .context_class = context_class
5858 self .lsp_context : t .Optional [LSPContext ] = None
59+ self .workspace_folders : t .List [Path ] = []
5960
6061 self .client_supports_pull_diagnostics = False
6162 # Register LSP features (e.g., formatting, hover, etc.)
@@ -68,7 +69,7 @@ def _register_features(self) -> None:
6869 def initialize (ls : LanguageServer , params : types .InitializeParams ) -> None :
6970 """Initialize the server when the client connects."""
7071 try :
71- # Check if client supports pull diagnostics
72+ # Check if the client supports pull diagnostics
7273 if params .capabilities and params .capabilities .text_document :
7374 diagnostics = getattr (params .capabilities .text_document , "diagnostic" , None )
7475 if diagnostics :
@@ -81,9 +82,13 @@ def initialize(ls: LanguageServer, params: types.InitializeParams) -> None:
8182 self .client_supports_pull_diagnostics = False
8283
8384 if params .workspace_folders :
85+ # Store all workspace folders for later use
86+ self .workspace_folders = [
87+ Path (self ._uri_to_path (folder .uri )) for folder in params .workspace_folders
88+ ]
89+
8490 # Try to find a SQLMesh config file in any workspace folder (only at the root level)
85- for folder in params .workspace_folders :
86- folder_path = Path (self ._uri_to_path (folder .uri ))
91+ for folder_path in self .workspace_folders :
8792 # Only check for config files directly in the workspace directory
8893 for ext in ("py" , "yml" , "yaml" ):
8994 config_path = folder_path / f"config.{ ext } "
@@ -104,8 +109,8 @@ def initialize(ls: LanguageServer, params: types.InitializeParams) -> None:
104109
105110 @self .server .feature (ALL_MODELS_FEATURE )
106111 def all_models (ls : LanguageServer , params : AllModelsRequest ) -> AllModelsResponse :
112+ uri = URI (params .textDocument .uri )
107113 try :
108- uri = URI (params .textDocument .uri )
109114 context = self ._context_get_or_load (uri )
110115 return get_sql_completions (context , uri )
111116 except Exception as e :
@@ -457,6 +462,8 @@ def _context_get_or_load(self, document_uri: URI) -> LSPContext:
457462 def _ensure_context_in_folder (self , folder_uri : Path ) -> None :
458463 if self .lsp_context is not None :
459464 return
465+
466+ # First, check the provided folder
460467 for ext in ("py" , "yml" , "yaml" ):
461468 config_path = folder_uri / f"config.{ ext } "
462469 if config_path .exists ():
@@ -468,6 +475,22 @@ def _ensure_context_in_folder(self, folder_uri: Path) -> None:
468475 except Exception as e :
469476 self .server .show_message (f"Error loading context: { e } " , types .MessageType .Error )
470477
478+ # If not found in the provided folder, search through all workspace folders
479+ for workspace_folder in self .workspace_folders :
480+ for ext in ("py" , "yml" , "yaml" ):
481+ config_path = workspace_folder / f"config.{ ext } "
482+ if config_path .exists ():
483+ try :
484+ created_context = self .context_class (paths = [workspace_folder ])
485+ self .lsp_context = LSPContext (created_context )
486+ loaded_sqlmesh_message (self .server , workspace_folder )
487+ return
488+ except Exception as e :
489+ self .server .show_message (
490+ f"Error loading context from { config_path } : { e } " ,
491+ types .MessageType .Warning ,
492+ )
493+
471494 def _ensure_context_for_document (
472495 self ,
473496 document_uri : URI ,
@@ -506,6 +529,23 @@ def _ensure_context_for_document(
506529 )
507530 path = path .parent
508531
532+ # If still no context found, try the workspace folders
533+ if not loaded :
534+ for workspace_folder in self .workspace_folders :
535+ for ext in ("py" , "yml" , "yaml" ):
536+ config_path = workspace_folder / f"config.{ ext } "
537+ if config_path .exists ():
538+ try :
539+ created_context = self .context_class (paths = [workspace_folder ])
540+ self .lsp_context = LSPContext (created_context )
541+ loaded_sqlmesh_message (self .server , workspace_folder )
542+ return
543+ except Exception as e :
544+ self .server .show_message (
545+ f"Error loading context from { config_path } : { e } " ,
546+ types .MessageType .Warning ,
547+ )
548+
509549 return
510550
511551 @staticmethod
0 commit comments