Skip to content

Commit c160796

Browse files
committed
addressing comments
1 parent 38c0719 commit c160796

1 file changed

Lines changed: 24 additions & 12 deletions

File tree

sqlmesh/lsp/main.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@
1111
from pygls.server import LanguageServer
1212
from pygls.workspace import TextDocument
1313
from sqlmesh._version import __version__
14+
from sqlmesh.core.dialect import Audit
1415
from sqlmesh.core.model import Model
1516

1617
logger = logging.getLogger(__name__)
1718

18-
CONTEXT: t.Optional[
19+
GLOBAL_CONTEXT: t.Optional[
1920
t.Tuple[
2021
Context,
21-
t.Dict[str, t.Tuple[Context, Model]],
22+
t.Dict[str, t.Tuple[Context, t.Union[Model, Audit]]],
2223
]
2324
] = None
2425

@@ -29,20 +30,31 @@
2930
def ensure_context_for_document(document: TextDocument) -> TextDocument:
3031
"""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."""
3132
# If the context is already loaded, return the document, if it is part of the same context
32-
if CONTEXT is not None:
33-
CONTEXT[0].reload()
34-
if document.uri in CONTEXT[1]:
33+
global GLOBAL_CONTEXT
34+
if GLOBAL_CONTEXT is not None:
35+
GLOBAL_CONTEXT[0].load()
36+
if document.uri in GLOBAL_CONTEXT[1]:
3537
return document
3638
else:
37-
for model in CONTEXT[1]._models:
39+
for model in GLOBAL_CONTEXT[0]._models.values():
40+
if model._path is None:
41+
continue
3842
path = model._path.resolve()
3943
if path == document.path:
40-
CONTEXT = (CONTEXT[0], CONTEXT[1] | {document.uri: (CONTEXT[0], model)})
44+
GLOBAL_CONTEXT = (
45+
GLOBAL_CONTEXT[0],
46+
GLOBAL_CONTEXT[1] | {document.uri: (GLOBAL_CONTEXT[0], model)},
47+
)
4148
return document
42-
for audit in CONTEXT[1]._audits:
49+
for audit in GLOBAL_CONTEXT[0]._audits.values():
50+
if audit._path is None:
51+
continue
4352
path = audit._path.resolve()
4453
if path == document.path:
45-
CONTEXT = (CONTEXT[0], CONTEXT[1] | {document.uri: (CONTEXT[0], audit)})
54+
GLOBAL_CONTEXT = (
55+
GLOBAL_CONTEXT[0],
56+
GLOBAL_CONTEXT[1] | {document.uri: (GLOBAL_CONTEXT[0], audit)},
57+
)
4658
return document
4759
return document
4860

@@ -57,7 +69,7 @@ def ensure_context_for_document(document: TextDocument) -> TextDocument:
5769
if config_path.exists():
5870
with suppress(Exception):
5971
handle = Context(paths=[path])
60-
CONTEXT = (handle, {})
72+
GLOBAL_CONTEXT = (handle, {})
6173
server.show_message(f"Context loaded for: {path}")
6274
loaded = True
6375
return ensure_context_for_document(document)
@@ -73,10 +85,10 @@ def formatting(
7385
"""Format the document using SQLMesh format_model_expressions."""
7486
try:
7587
document = ensure_context_for_document(ls.workspace.get_document(params.text_document.uri))
76-
context = CONTEXT
88+
context = GLOBAL_CONTEXT
7789
if context is None:
7890
raise Exception(f"No context found for document: {document.path}")
79-
context.format(paths=(Path(document.path),))
91+
context[0].format(paths=(Path(document.path),))
8092
with open(document.path, "r+", encoding="utf-8") as file:
8193
return [
8294
types.TextEdit(

0 commit comments

Comments
 (0)