Skip to content

Commit d655523

Browse files
authored
refactor(lsp): creation of context (#4704)
1 parent 3d50d79 commit d655523

File tree

1 file changed

+34
-43
lines changed

1 file changed

+34
-43
lines changed

sqlmesh/lsp/main.py

Lines changed: 34 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,22 @@ def __init__(
8585
# Register LSP features (e.g., formatting, hover, etc.)
8686
self._register_features()
8787

88+
def _create_lsp_context(self, paths: t.List[Path]) -> t.Optional[LSPContext]:
89+
"""Create a new LSPContext instance using the configured context class.
90+
91+
Args:
92+
paths: List of paths to pass to the context constructor
93+
94+
Returns:
95+
A new LSPContext instance wrapping the created context, or None if creation fails
96+
"""
97+
try:
98+
context = self.context_class(paths=paths)
99+
return LSPContext(context)
100+
except Exception as e:
101+
self.server.log_trace(f"Error creating context: {e}")
102+
return None
103+
88104
def _register_features(self) -> None:
89105
"""Register LSP features on the internal LanguageServer instance."""
90106

@@ -116,16 +132,11 @@ def initialize(ls: LanguageServer, params: types.InitializeParams) -> None:
116132
for ext in ("py", "yml", "yaml"):
117133
config_path = folder_path / f"config.{ext}"
118134
if config_path.exists():
119-
try:
120-
# Use user-provided instantiator to build the context
121-
created_context = self.context_class(paths=[folder_path])
122-
self.lsp_context = LSPContext(created_context)
135+
lsp_context = self._create_lsp_context([folder_path])
136+
if lsp_context:
137+
self.lsp_context = lsp_context
123138
loaded_sqlmesh_message(ls, folder_path)
124139
return # Exit after successfully loading any config
125-
except Exception as e:
126-
ls.log_trace(
127-
f"Error loading context from {config_path}: {e}",
128-
)
129140
except Exception as e:
130141
ls.log_trace(
131142
f"Error initializing SQLMesh context: {e}",
@@ -288,13 +299,10 @@ def did_save(ls: LanguageServer, params: types.DidSaveTextDocumentParams) -> Non
288299

289300
# Reload the entire context and create a new LSPContext
290301
if self.lsp_context is not None:
291-
try:
292-
new_context = Context(paths=list(self.lsp_context.context.configs))
293-
new_full_context = LSPContext(new_context)
294-
self.lsp_context = new_full_context
302+
new_lsp_context = self._create_lsp_context(list(self.lsp_context.context.configs))
303+
if new_lsp_context:
304+
self.lsp_context = new_lsp_context
295305
return
296-
except Exception as e:
297-
pass
298306

299307
context = self._context_get_or_load(uri)
300308
models = context.map[uri.to_path()]
@@ -664,29 +672,22 @@ def _ensure_context_in_folder(self, folder_uri: Path) -> None:
664672
for ext in ("py", "yml", "yaml"):
665673
config_path = folder_uri / f"config.{ext}"
666674
if config_path.exists():
667-
try:
668-
created_context = self.context_class(paths=[folder_uri])
669-
self.lsp_context = LSPContext(created_context)
675+
lsp_context = self._create_lsp_context([folder_uri])
676+
if lsp_context:
677+
self.lsp_context = lsp_context
670678
loaded_sqlmesh_message(self.server, folder_uri)
671679
return
672-
except Exception as e:
673-
self.server.show_message(f"Error loading context: {e}", types.MessageType.Error)
674680

675681
# If not found in the provided folder, search through all workspace folders
676682
for workspace_folder in self.workspace_folders:
677683
for ext in ("py", "yml", "yaml"):
678684
config_path = workspace_folder / f"config.{ext}"
679685
if config_path.exists():
680-
try:
681-
created_context = self.context_class(paths=[workspace_folder])
682-
self.lsp_context = LSPContext(created_context)
686+
lsp_context = self._create_lsp_context([workspace_folder])
687+
if lsp_context:
688+
self.lsp_context = lsp_context
683689
loaded_sqlmesh_message(self.server, workspace_folder)
684690
return
685-
except Exception as e:
686-
self.server.show_message(
687-
f"Error loading context from {config_path}: {e}",
688-
types.MessageType.Warning,
689-
)
690691

691692
def _ensure_context_for_document(
692693
self,
@@ -713,17 +714,12 @@ def _ensure_context_for_document(
713714
for ext in ("py", "yml", "yaml"):
714715
config_path = path / f"config.{ext}"
715716
if config_path.exists():
716-
try:
717-
# Use user-provided instantiator to build the context
718-
created_context = self.context_class(paths=[path])
719-
self.lsp_context = LSPContext(created_context)
717+
lsp_context = self._create_lsp_context([path])
718+
if lsp_context:
719+
self.lsp_context = lsp_context
720720
loaded = True
721721
# Re-check context for the document now that it's loaded
722722
return self._ensure_context_for_document(document_uri)
723-
except Exception as e:
724-
self.server.show_message(
725-
f"Error loading context: {e}", types.MessageType.Error
726-
)
727723
path = path.parent
728724

729725
# If still no context found, try the workspace folders
@@ -732,16 +728,11 @@ def _ensure_context_for_document(
732728
for ext in ("py", "yml", "yaml"):
733729
config_path = workspace_folder / f"config.{ext}"
734730
if config_path.exists():
735-
try:
736-
created_context = self.context_class(paths=[workspace_folder])
737-
self.lsp_context = LSPContext(created_context)
731+
lsp_context = self._create_lsp_context([workspace_folder])
732+
if lsp_context:
733+
self.lsp_context = lsp_context
738734
loaded_sqlmesh_message(self.server, workspace_folder)
739735
return
740-
except Exception as e:
741-
self.server.show_message(
742-
f"Error loading context from {config_path}: {e}",
743-
types.MessageType.Warning,
744-
)
745736

746737
return
747738

0 commit comments

Comments
 (0)