2424from sqlmesh .lsp .context import (
2525 LSPContext ,
2626 ModelTarget ,
27- render_model as render_model_context ,
2827)
2928from sqlmesh .lsp .custom import (
3029 ALL_MODELS_FEATURE ,
@@ -58,10 +57,6 @@ def __init__(
5857 self .context_class = context_class
5958 self .lsp_context : t .Optional [LSPContext ] = None
6059
61- # Cache stores tuples of (diagnostics, diagnostic_version)
62- self .lint_cache : t .Dict [URI , t .Tuple [t .List [AnnotatedRuleViolation ], int ]] = {}
63- self ._diagnostic_version_counter : int = 0
64-
6560 self .client_supports_pull_diagnostics = False
6661 # Register LSP features (e.g., formatting, hover, etc.)
6762 self ._register_features ()
@@ -120,7 +115,7 @@ def all_models(ls: LanguageServer, params: AllModelsRequest) -> AllModelsRespons
120115 def render_model (ls : LanguageServer , params : RenderModelRequest ) -> RenderModelResponse :
121116 uri = URI (params .textDocumentUri )
122117 context = self ._context_get_or_load (uri )
123- return RenderModelResponse (models = list ( render_model_context ( context , uri ) ))
118+ return RenderModelResponse (models = context . render_model ( uri ))
124119
125120 @self .server .feature (API_FEATURE )
126121 def api (ls : LanguageServer , request : ApiRequest ) -> t .Dict [str , t .Any ]:
@@ -173,17 +168,11 @@ def did_open(ls: LanguageServer, params: types.DidOpenTextDocumentParams) -> Non
173168 if models is None or not isinstance (models , ModelTarget ):
174169 return
175170
176- if self .lint_cache .get (uri ) is None :
177- diagnostics = context .context .lint_models (
178- models .names ,
179- raise_on_error = False ,
180- )
181- self ._diagnostic_version_counter += 1
182- self .lint_cache [uri ] = (diagnostics , self ._diagnostic_version_counter )
171+ # Get diagnostics from context (which handles caching)
172+ diagnostics = context .lint_model (uri )
183173
184174 # Only publish diagnostics if client doesn't support pull diagnostics
185175 if not self .client_supports_pull_diagnostics :
186- diagnostics , _ = self .lint_cache [uri ]
187176 ls .publish_diagnostics (
188177 params .text_document .uri ,
189178 SQLMeshLanguageServer ._diagnostics_to_lsp_diagnostics (diagnostics ),
@@ -197,13 +186,8 @@ def did_change(ls: LanguageServer, params: types.DidChangeTextDocumentParams) ->
197186 if models is None or not isinstance (models , ModelTarget ):
198187 return
199188
200- # Always update the cache
201- diagnostics = context .context .lint_models (
202- models .names ,
203- raise_on_error = False ,
204- )
205- self ._diagnostic_version_counter += 1
206- self .lint_cache [uri ] = (diagnostics , self ._diagnostic_version_counter )
189+ # Get diagnostics from context (which handles caching)
190+ diagnostics = context .lint_model (uri )
207191
208192 # Only publish diagnostics if client doesn't support pull diagnostics
209193 if not self .client_supports_pull_diagnostics :
@@ -215,18 +199,20 @@ def did_change(ls: LanguageServer, params: types.DidChangeTextDocumentParams) ->
215199 @self .server .feature (types .TEXT_DOCUMENT_DID_SAVE )
216200 def did_save (ls : LanguageServer , params : types .DidSaveTextDocumentParams ) -> None :
217201 uri = URI (params .text_document .uri )
202+
203+ # Reload the entire context and create a new LSPContext
204+ if self .lsp_context is not None :
205+ old_context = self .lsp_context .context
206+ old_context .load () # Reload the context
207+ self .lsp_context = LSPContext (old_context )
208+
218209 context = self ._context_get_or_load (uri )
219210 models = context .map [uri .to_path ()]
220211 if models is None or not isinstance (models , ModelTarget ):
221212 return
222213
223- # Always update the cache
224- diagnostics = context .context .lint_models (
225- models .names ,
226- raise_on_error = False ,
227- )
228- self ._diagnostic_version_counter += 1
229- self .lint_cache [uri ] = (diagnostics , self ._diagnostic_version_counter )
214+ # Get diagnostics from context (which handles caching)
215+ diagnostics = context .lint_model (uri )
230216
231217 # Only publish diagnostics if client doesn't support pull diagnostics
232218 if not self .client_supports_pull_diagnostics :
@@ -445,29 +431,17 @@ def workspace_diagnostic(
445431 return types .WorkspaceDiagnosticReport (items = [])
446432
447433 def _get_diagnostics_for_uri (self , uri : URI ) -> t .Tuple [t .List [types .Diagnostic ], int ]:
448- """Get diagnostics for a specific URI, returning (diagnostics, result_id)."""
449- # Check if we have cached diagnostics
450- if uri in self .lint_cache :
451- diagnostics , result_id = self .lint_cache [uri ]
452- return SQLMeshLanguageServer ._diagnostics_to_lsp_diagnostics (diagnostics ), result_id
434+ """Get diagnostics for a specific URI, returning (diagnostics, result_id).
453435
436+ Since we no longer track version numbers, we always return 0 as the result_id.
437+ This means pull diagnostics will always fetch fresh results.
438+ """
454439 # Try to get diagnostics by loading context and linting
455440 try :
456441 context = self ._context_get_or_load (uri )
457- models = context .map [uri .to_path ()]
458- if models is None or not isinstance (models , ModelTarget ):
459- return [], 0
460-
461- # Lint the models and cache the results
462- diagnostics = context .context .lint_models (
463- models .names ,
464- raise_on_error = False ,
465- )
466- self ._diagnostic_version_counter += 1
467- self .lint_cache [uri ] = (diagnostics , self ._diagnostic_version_counter )
468- return SQLMeshLanguageServer ._diagnostics_to_lsp_diagnostics (
469- diagnostics
470- ), self ._diagnostic_version_counter
442+ # Get diagnostics from context (which handles caching)
443+ diagnostics = context .lint_model (uri )
444+ return SQLMeshLanguageServer ._diagnostics_to_lsp_diagnostics (diagnostics ), 0
471445 except Exception :
472446 # If we can't get diagnostics, return empty list with no result ID
473447 return [], 0
@@ -523,7 +497,7 @@ def _ensure_context_for_document(
523497 created_context = self .context_class (paths = [path ])
524498 self .lsp_context = LSPContext (created_context )
525499 loaded = True
526- # Re-check context for document now that it's loaded
500+ # Re-check context for the document now that it's loaded
527501 return self ._ensure_context_for_document (document_uri )
528502 except Exception as e :
529503 self .server .show_message (
0 commit comments