@@ -106,10 +106,13 @@ def _create_lsp_context(self, paths: t.List[Path]) -> t.Optional[LSPContext]:
106106 A new LSPContext instance wrapping the created context, or None if creation fails
107107 """
108108 try :
109- context = self .context_class (paths = paths )
110- lsp_context = LSPContext (context )
111- self .lsp_context = lsp_context
112- return lsp_context
109+ if self .lsp_context is None :
110+ context = self .context_class (paths = paths )
111+ else :
112+ self .lsp_context .context .load ()
113+ context = self .lsp_context .context
114+ self .lsp_context = LSPContext (context )
115+ return self .lsp_context
113116 except Exception as e :
114117 self .server .log_trace (f"Error creating context: { e } " )
115118 return None
@@ -293,8 +296,16 @@ def did_open(ls: LanguageServer, params: types.DidOpenTextDocumentParams) -> Non
293296
294297 @self .server .feature (types .TEXT_DOCUMENT_DID_CHANGE )
295298 def did_change (ls : LanguageServer , params : types .DidChangeTextDocumentParams ) -> None :
299+ if self .lsp_context is None :
300+ current_path = Path .cwd ()
301+ self ._ensure_context_in_folder (current_path )
302+ if self .lsp_context is None :
303+ ls .log_trace ("No context found after did_change" )
304+ return
305+
296306 uri = URI (params .text_document .uri )
297307 context = self ._context_get_or_load (uri )
308+
298309 models = context .map [uri .to_path ()]
299310 if models is None or not isinstance (models , ModelTarget ):
300311 return
@@ -731,9 +742,8 @@ def _ensure_context_for_document(
731742 for a config.py or config.yml file in the parent directories.
732743 """
733744 if self .lsp_context is not None :
734- context = self .lsp_context
735- context .context .load () # Reload or refresh context
736- self .lsp_context = LSPContext (context .context )
745+ self .lsp_context .context .load ()
746+ self .lsp_context = LSPContext (self .lsp_context .context )
737747 return
738748
739749 # No context yet: try to find config and load it
0 commit comments