Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 34 additions & 43 deletions sqlmesh/lsp/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,22 @@ def __init__(
# Register LSP features (e.g., formatting, hover, etc.)
self._register_features()

def _create_lsp_context(self, paths: t.List[Path]) -> t.Optional[LSPContext]:
"""Create a new LSPContext instance using the configured context class.

Args:
paths: List of paths to pass to the context constructor

Returns:
A new LSPContext instance wrapping the created context, or None if creation fails
"""
try:
context = self.context_class(paths=paths)
return LSPContext(context)
except Exception as e:
self.server.log_trace(f"Error creating context: {e}")
return None

def _register_features(self) -> None:
"""Register LSP features on the internal LanguageServer instance."""

Expand Down Expand Up @@ -116,16 +132,11 @@ def initialize(ls: LanguageServer, params: types.InitializeParams) -> None:
for ext in ("py", "yml", "yaml"):
config_path = folder_path / f"config.{ext}"
if config_path.exists():
try:
# Use user-provided instantiator to build the context
created_context = self.context_class(paths=[folder_path])
self.lsp_context = LSPContext(created_context)
lsp_context = self._create_lsp_context([folder_path])
if lsp_context:
self.lsp_context = lsp_context
loaded_sqlmesh_message(ls, folder_path)
return # Exit after successfully loading any config
except Exception as e:
ls.log_trace(
f"Error loading context from {config_path}: {e}",
)
except Exception as e:
ls.log_trace(
f"Error initializing SQLMesh context: {e}",
Expand Down Expand Up @@ -288,13 +299,10 @@ def did_save(ls: LanguageServer, params: types.DidSaveTextDocumentParams) -> Non

# Reload the entire context and create a new LSPContext
if self.lsp_context is not None:
try:
new_context = Context(paths=list(self.lsp_context.context.configs))
new_full_context = LSPContext(new_context)
self.lsp_context = new_full_context
new_lsp_context = self._create_lsp_context(list(self.lsp_context.context.configs))
if new_lsp_context:
self.lsp_context = new_lsp_context
return
except Exception as e:
pass

context = self._context_get_or_load(uri)
models = context.map[uri.to_path()]
Expand Down Expand Up @@ -664,29 +672,22 @@ def _ensure_context_in_folder(self, folder_uri: Path) -> None:
for ext in ("py", "yml", "yaml"):
config_path = folder_uri / f"config.{ext}"
if config_path.exists():
try:
created_context = self.context_class(paths=[folder_uri])
self.lsp_context = LSPContext(created_context)
lsp_context = self._create_lsp_context([folder_uri])
Copy link

Copilot AI Jun 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

User-facing error messages were removed when context creation fails in ensure_context_in_folder. Failures now only log at trace level, so users may not know why loading didn't occur. Consider restoring server.show_message calls or adding a warning to inform users of load failures.

Copilot uses AI. Check for mistakes.
if lsp_context:
self.lsp_context = lsp_context
loaded_sqlmesh_message(self.server, folder_uri)
return
except Exception as e:
self.server.show_message(f"Error loading context: {e}", types.MessageType.Error)

# If not found in the provided folder, search through all workspace folders
for workspace_folder in self.workspace_folders:
for ext in ("py", "yml", "yaml"):
config_path = workspace_folder / f"config.{ext}"
if config_path.exists():
try:
created_context = self.context_class(paths=[workspace_folder])
self.lsp_context = LSPContext(created_context)
lsp_context = self._create_lsp_context([workspace_folder])
if lsp_context:
self.lsp_context = lsp_context
loaded_sqlmesh_message(self.server, workspace_folder)
return
except Exception as e:
self.server.show_message(
f"Error loading context from {config_path}: {e}",
types.MessageType.Warning,
)

def _ensure_context_for_document(
self,
Expand All @@ -713,17 +714,12 @@ def _ensure_context_for_document(
for ext in ("py", "yml", "yaml"):
config_path = path / f"config.{ext}"
if config_path.exists():
try:
# Use user-provided instantiator to build the context
created_context = self.context_class(paths=[path])
self.lsp_context = LSPContext(created_context)
lsp_context = self._create_lsp_context([path])
if lsp_context:
self.lsp_context = lsp_context
loaded = True
# Re-check context for the document now that it's loaded
return self._ensure_context_for_document(document_uri)
except Exception as e:
self.server.show_message(
f"Error loading context: {e}", types.MessageType.Error
)
path = path.parent

# If still no context found, try the workspace folders
Expand All @@ -732,16 +728,11 @@ def _ensure_context_for_document(
for ext in ("py", "yml", "yaml"):
config_path = workspace_folder / f"config.{ext}"
if config_path.exists():
try:
created_context = self.context_class(paths=[workspace_folder])
self.lsp_context = LSPContext(created_context)
lsp_context = self._create_lsp_context([workspace_folder])
if lsp_context:
self.lsp_context = lsp_context
loaded_sqlmesh_message(self.server, workspace_folder)
return
except Exception as e:
self.server.show_message(
f"Error loading context from {config_path}: {e}",
types.MessageType.Warning,
)

return

Expand Down