1616
1717logger = logging .getLogger (__name__ )
1818
19- GLOBAL_CONTEXT : t .Optional [
20- t .Tuple [
21- Context ,
22- t .Dict [str , t .Tuple [Context , t .Union [Model , ModelAudit ]]],
23- ]
24- ] = None
19+ GLOBAL_CONTEXT : t .Optional [Context ,] = None
20+ FILE_MAP : t .Dict [str , t .Union [Model , ModelAudit ]] = {}
2521
2622
2723server = LanguageServer ("sqlmesh_lsp" , __version__ )
3026def ensure_context_for_document (document : TextDocument ) -> TextDocument :
3127 """Ensure that a context exists for the given document if applicable by searching for a config.py or config.yml file in the parent directories."""
3228 # If the context is already loaded, return the document, if it is part of the same context
33- global GLOBAL_CONTEXT
29+ global GLOBAL_CONTEXT , FILE_MAP
3430 if GLOBAL_CONTEXT is not None :
35- GLOBAL_CONTEXT [ 0 ] .load ()
36- if document .uri in GLOBAL_CONTEXT [ 1 ] :
31+ GLOBAL_CONTEXT .load ()
32+ if document .uri in FILE_MAP :
3733 return document
3834 else :
39- for model in GLOBAL_CONTEXT [ 0 ] ._models .values ():
35+ for model in GLOBAL_CONTEXT ._models .values ():
4036 if model ._path is None :
4137 continue
4238 path = model ._path .resolve ()
4339 if path == document .path :
44- GLOBAL_CONTEXT = (
45- GLOBAL_CONTEXT [0 ],
46- GLOBAL_CONTEXT [1 ] | {document .uri : (GLOBAL_CONTEXT [0 ], model )},
47- )
40+ FILE_MAP [document .uri ] = model
4841 return document
49- for audit in GLOBAL_CONTEXT [ 0 ] ._audits .values ():
42+ for audit in GLOBAL_CONTEXT ._audits .values ():
5043 if audit ._path is None :
5144 continue
5245 path = audit ._path .resolve ()
5346 if path == document .path :
54- GLOBAL_CONTEXT = (
55- GLOBAL_CONTEXT [0 ],
56- GLOBAL_CONTEXT [1 ] | {document .uri : (GLOBAL_CONTEXT [0 ], audit )},
57- )
47+ FILE_MAP [document .uri ] = audit
5848 return document
5949 return document
6050
@@ -68,8 +58,7 @@ def ensure_context_for_document(document: TextDocument) -> TextDocument:
6858 config_path = path / f"config.{ ext } "
6959 if config_path .exists ():
7060 with suppress (Exception ):
71- handle = Context (paths = [path ])
72- GLOBAL_CONTEXT = (handle , {})
61+ GLOBAL_CONTEXT = Context (paths = [path ])
7362 server .show_message (f"Context loaded for: { path } " )
7463 loaded = True
7564 return ensure_context_for_document (document )
@@ -88,7 +77,7 @@ def formatting(
8877 context = GLOBAL_CONTEXT
8978 if context is None :
9079 raise Exception (f"No context found for document: { document .path } " )
91- context [ 0 ] .format (paths = (Path (document .path ),))
80+ context .format (paths = (Path (document .path ),))
9281 with open (document .path , "r+" , encoding = "utf-8" ) as file :
9382 return [
9483 types .TextEdit (
0 commit comments