@@ -88,6 +88,8 @@ def __init__(
8888 def _create_lsp_context (self , paths : t .List [Path ]) -> t .Optional [LSPContext ]:
8989 """Create a new LSPContext instance using the configured context class.
9090
91+ On success, sets self.lsp_context and returns the created context.
92+
9193 Args:
9294 paths: List of paths to pass to the context constructor
9395
@@ -96,7 +98,9 @@ def _create_lsp_context(self, paths: t.List[Path]) -> t.Optional[LSPContext]:
9698 """
9799 try :
98100 context = self .context_class (paths = paths )
99- return LSPContext (context )
101+ lsp_context = LSPContext (context )
102+ self .lsp_context = lsp_context
103+ return lsp_context
100104 except Exception as e :
101105 self .server .log_trace (f"Error creating context: { e } " )
102106 return None
@@ -132,9 +136,7 @@ def initialize(ls: LanguageServer, params: types.InitializeParams) -> None:
132136 for ext in ("py" , "yml" , "yaml" ):
133137 config_path = folder_path / f"config.{ ext } "
134138 if config_path .exists ():
135- lsp_context = self ._create_lsp_context ([folder_path ])
136- if lsp_context :
137- self .lsp_context = lsp_context
139+ if self ._create_lsp_context ([folder_path ]):
138140 loaded_sqlmesh_message (ls , folder_path )
139141 return # Exit after successfully loading any config
140142 except Exception as e :
@@ -299,9 +301,7 @@ def did_save(ls: LanguageServer, params: types.DidSaveTextDocumentParams) -> Non
299301
300302 # Reload the entire context and create a new LSPContext
301303 if self .lsp_context is not None :
302- new_lsp_context = self ._create_lsp_context (list (self .lsp_context .context .configs ))
303- if new_lsp_context :
304- self .lsp_context = new_lsp_context
304+ if self ._create_lsp_context (list (self .lsp_context .context .configs )):
305305 return
306306
307307 context = self ._context_get_or_load (uri )
@@ -672,9 +672,7 @@ def _ensure_context_in_folder(self, folder_uri: Path) -> None:
672672 for ext in ("py" , "yml" , "yaml" ):
673673 config_path = folder_uri / f"config.{ ext } "
674674 if config_path .exists ():
675- lsp_context = self ._create_lsp_context ([folder_uri ])
676- if lsp_context :
677- self .lsp_context = lsp_context
675+ if self ._create_lsp_context ([folder_uri ]):
678676 loaded_sqlmesh_message (self .server , folder_uri )
679677 return
680678
@@ -683,9 +681,7 @@ def _ensure_context_in_folder(self, folder_uri: Path) -> None:
683681 for ext in ("py" , "yml" , "yaml" ):
684682 config_path = workspace_folder / f"config.{ ext } "
685683 if config_path .exists ():
686- lsp_context = self ._create_lsp_context ([workspace_folder ])
687- if lsp_context :
688- self .lsp_context = lsp_context
684+ if self ._create_lsp_context ([workspace_folder ]):
689685 loaded_sqlmesh_message (self .server , workspace_folder )
690686 return
691687
@@ -714,9 +710,7 @@ def _ensure_context_for_document(
714710 for ext in ("py" , "yml" , "yaml" ):
715711 config_path = path / f"config.{ ext } "
716712 if config_path .exists ():
717- lsp_context = self ._create_lsp_context ([path ])
718- if lsp_context :
719- self .lsp_context = lsp_context
713+ if self ._create_lsp_context ([path ]):
720714 loaded = True
721715 # Re-check context for the document now that it's loaded
722716 return self ._ensure_context_for_document (document_uri )
@@ -728,9 +722,7 @@ def _ensure_context_for_document(
728722 for ext in ("py" , "yml" , "yaml" ):
729723 config_path = workspace_folder / f"config.{ ext } "
730724 if config_path .exists ():
731- lsp_context = self ._create_lsp_context ([workspace_folder ])
732- if lsp_context :
733- self .lsp_context = lsp_context
725+ if self ._create_lsp_context ([workspace_folder ]):
734726 loaded_sqlmesh_message (self .server , workspace_folder )
735727 return
736728
0 commit comments