11#!/usr/bin/env python
22"""A Language Server Protocol (LSP) server for SQL with SQLMesh integration."""
33
4+ import itertools
45import logging
56import typing as t
67from contextlib import suppress
1617
1718logger = logging .getLogger (__name__ )
1819
19- GLOBAL_CONTEXT : t .Optional [Context , ] = None
20+ GLOBAL_CONTEXT : t .Optional [Context ] = None
2021FILE_MAP : t .Dict [str , t .Union [Model , ModelAudit ]] = {}
2122
2223
@@ -31,22 +32,16 @@ def ensure_context_for_document(document: TextDocument) -> TextDocument:
3132 GLOBAL_CONTEXT .load ()
3233 if document .uri in FILE_MAP :
3334 return document
34- else :
35- for model in GLOBAL_CONTEXT ._models .values ():
36- if model ._path is None :
37- continue
38- path = model ._path .resolve ()
39- if path == document .path :
40- FILE_MAP [document .uri ] = model
41- return document
42- for audit in GLOBAL_CONTEXT ._audits .values ():
43- if audit ._path is None :
44- continue
45- path = audit ._path .resolve ()
46- if path == document .path :
47- FILE_MAP [document .uri ] = audit
48- return document
49- return document
35+ for model in itertools .chain (
36+ GLOBAL_CONTEXT ._models .values (), GLOBAL_CONTEXT ._audits .values ()
37+ ):
38+ if model ._path is None :
39+ continue
40+ path = model ._path .resolve ()
41+ if path == document .path :
42+ FILE_MAP [document .uri ] = model
43+ return document
44+ return document
5045
5146 # If there is no context, load the context and then call this function again
5247 path = Path (document .path ).resolve ()
0 commit comments