Skip to content

Commit 38c0719

Browse files
Update sqlmesh/lsp/main.py
Co-authored-by: Jo <46752250+georgesittas@users.noreply.github.com>
1 parent 675d9a7 commit 38c0719

1 file changed

Lines changed: 31 additions & 27 deletions

File tree

sqlmesh/lsp/main.py

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,61 +15,65 @@
1515

1616
logger = logging.getLogger(__name__)
1717

18-
CONTEXTS: t.Dict[str, Context] = {}
19-
"""A mapping of workspace paths to SQLMesh contexts."""
18+
CONTEXT: t.Optional[
19+
t.Tuple[
20+
Context,
21+
t.Dict[str, t.Tuple[Context, Model]],
22+
]
23+
] = None
2024

21-
PATHS_TO_MODELS: t.Dict[str, t.Tuple[Context, Model]] = {}
22-
"""A mapping of file paths to SQLMesh (context, model) tuples."""
2325

2426
server = LanguageServer("sqlmesh_lsp", __version__)
2527

26-
_CACHE: t.Set[str] = set()
27-
"""A cache of URIs for which we have already ensured a context exists."""
28-
2928

3029
def ensure_context_for_document(document: TextDocument) -> TextDocument:
3130
"""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."""
32-
if document.uri in _CACHE:
33-
return document
34-
_CACHE.add(document.uri)
31+
# 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]:
35+
return document
36+
else:
37+
for model in CONTEXT[1]._models:
38+
path = model._path.resolve()
39+
if path == document.path:
40+
CONTEXT = (CONTEXT[0], CONTEXT[1] | {document.uri: (CONTEXT[0], model)})
41+
return document
42+
for audit in CONTEXT[1]._audits:
43+
path = audit._path.resolve()
44+
if path == document.path:
45+
CONTEXT = (CONTEXT[0], CONTEXT[1] | {document.uri: (CONTEXT[0], audit)})
46+
return document
47+
return document
48+
49+
# If there is no context, load the context and then call this function again
3550
path = Path(document.path).resolve()
3651
if path.suffix not in (".sql", ".py"):
3752
return document
38-
initial_path = path
39-
while path.parents:
40-
if str(path) in CONTEXTS:
41-
return document
42-
path = path.parent
43-
path = initial_path
4453
loaded = False
4554
while path.parents and not loaded:
4655
for ext in ("py", "yml", "yaml"):
4756
config_path = path / f"config.{ext}"
4857
if config_path.exists():
4958
with suppress(Exception):
50-
handle = Context(paths=[f"{path}"])
51-
CONTEXTS[str(path)] = handle
52-
PATHS_TO_MODELS.update(
53-
{
54-
str(model._path.resolve()): (handle, model)
55-
for model in handle.models.values()
56-
}
57-
)
59+
handle = Context(paths=[path])
60+
CONTEXT = (handle, {})
5861
server.show_message(f"Context loaded for: {path}")
5962
loaded = True
60-
break
63+
return ensure_context_for_document(document)
6164
path = path.parent
65+
6266
return document
6367

6468

6569
@server.feature(types.TEXT_DOCUMENT_FORMATTING)
6670
def formatting(
6771
ls: LanguageServer, params: types.DocumentFormattingParams
6872
) -> t.List[types.TextEdit]:
69-
"""Format the document based using SQLMesh format_model_expressions."""
73+
"""Format the document using SQLMesh format_model_expressions."""
7074
try:
7175
document = ensure_context_for_document(ls.workspace.get_document(params.text_document.uri))
72-
context, _ = PATHS_TO_MODELS.get(document.path, (None, None))
76+
context = CONTEXT
7377
if context is None:
7478
raise Exception(f"No context found for document: {document.path}")
7579
context.format(paths=(Path(document.path),))

0 commit comments

Comments
 (0)