|
1 | 1 | from dataclasses import dataclass |
2 | 2 | from pathlib import Path |
3 | | -from pygls.server import LanguageServer |
4 | 3 | from sqlmesh.core.context import Context |
5 | 4 | import typing as t |
| 5 | + |
6 | 6 | from sqlmesh.core.linter.rule import Range |
7 | | -from sqlmesh.core.model.definition import SqlModel, ExternalModel |
| 7 | +from sqlmesh.core.model.definition import SqlModel |
8 | 8 | from sqlmesh.core.linter.definition import AnnotatedRuleViolation |
9 | | -from sqlmesh.core.schema_loader import get_columns |
10 | | -from sqlmesh.lsp.commands import EXTERNAL_MODEL_UPDATE_COLUMNS |
11 | 9 | from sqlmesh.lsp.custom import ModelForRendering, TestEntry, RunTestResponse |
12 | 10 | from sqlmesh.lsp.custom import AllModelsResponse, RenderModelEntry |
13 | 11 | from sqlmesh.lsp.tests_ranges import get_test_ranges |
14 | | -from sqlmesh.lsp.helpers import to_lsp_range |
15 | 12 | from sqlmesh.lsp.uri import URI |
16 | 13 | from lsprotocol import types |
17 | | -from sqlmesh.utils import yaml |
18 | | -from sqlmesh.utils.lineage import get_yaml_model_name_ranges |
19 | 14 |
|
20 | 15 |
|
21 | 16 | @dataclass |
@@ -307,36 +302,6 @@ def get_code_actions( |
307 | 302 |
|
308 | 303 | return code_actions if code_actions else None |
309 | 304 |
|
310 | | - def get_code_lenses(self, uri: URI) -> t.Optional[t.List[types.CodeLens]]: |
311 | | - models_in_file = self.map.get(uri.to_path()) |
312 | | - if isinstance(models_in_file, ModelTarget): |
313 | | - models = [self.context.get_model(model) for model in models_in_file.names] |
314 | | - if any(isinstance(model, ExternalModel) for model in models): |
315 | | - code_lenses = self._get_external_model_code_lenses(uri) |
316 | | - if code_lenses: |
317 | | - return code_lenses |
318 | | - |
319 | | - return None |
320 | | - |
321 | | - def _get_external_model_code_lenses(self, uri: URI) -> t.List[types.CodeLens]: |
322 | | - """Get code lenses for external models YAML files.""" |
323 | | - ranges = get_yaml_model_name_ranges(uri.to_path()) |
324 | | - if ranges is None: |
325 | | - return [] |
326 | | - return [ |
327 | | - types.CodeLens( |
328 | | - range=to_lsp_range(range), |
329 | | - command=types.Command( |
330 | | - title="Update Columns", |
331 | | - command=EXTERNAL_MODEL_UPDATE_COLUMNS, |
332 | | - arguments=[ |
333 | | - name, |
334 | | - ], |
335 | | - ), |
336 | | - ) |
337 | | - for name, range in ranges.items() |
338 | | - ] |
339 | | - |
340 | 305 | def list_of_models_for_rendering(self) -> t.List[ModelForRendering]: |
341 | 306 | """Get a list of models for rendering. |
342 | 307 |
|
@@ -438,74 +403,3 @@ def diagnostic_to_lsp_diagnostic( |
438 | 403 | code=diagnostic.rule.name, |
439 | 404 | code_description=types.CodeDescription(href=rule_uri), |
440 | 405 | ) |
441 | | - |
442 | | - def update_external_model_columns(self, ls: LanguageServer, uri: URI, model_name: str) -> bool: |
443 | | - """ |
444 | | - Update the columns for an external model in the YAML file. Returns True if changed, False if didn't because |
445 | | - of the columns already being up to date. |
446 | | -
|
447 | | - In this case, the model name is the name of the external model as is defined in the YAML file, not any other version of it. |
448 | | -
|
449 | | - Errors still throw exceptions to be handled by the caller. |
450 | | - """ |
451 | | - models = yaml.load(uri.to_path()) |
452 | | - if not isinstance(models, list): |
453 | | - raise ValueError( |
454 | | - f"Expected a list of models in {uri.to_path()}, but got {type(models).__name__}" |
455 | | - ) |
456 | | - |
457 | | - existing_model = next((model for model in models if model.get("name") == model_name), None) |
458 | | - if existing_model is None: |
459 | | - raise ValueError(f"Could not find model {model_name} in {uri.to_path()}") |
460 | | - |
461 | | - existing_model_columns = existing_model.get("columns") |
462 | | - |
463 | | - # Get the adapter and fetch columns |
464 | | - adapter = self.context.engine_adapter |
465 | | - # Get columns for the model |
466 | | - new_columns = get_columns( |
467 | | - adapter=adapter, |
468 | | - dialect=self.context.config.model_defaults.dialect, |
469 | | - table=model_name, |
470 | | - strict=True, |
471 | | - ) |
472 | | - # Compare existing columns and matching types and if they are the same, do not update |
473 | | - if existing_model_columns is not None: |
474 | | - if existing_model_columns == new_columns: |
475 | | - return False |
476 | | - |
477 | | - # Model index to update |
478 | | - model_index = next( |
479 | | - (i for i, model in enumerate(models) if model.get("name") == model_name), None |
480 | | - ) |
481 | | - if model_index is None: |
482 | | - raise ValueError(f"Could not find model {model_name} in {uri.to_path()}") |
483 | | - |
484 | | - # Get end of the file to set the edit range |
485 | | - with open(uri.to_path(), "r", encoding="utf-8") as file: |
486 | | - read_file = file.read() |
487 | | - |
488 | | - end_line = read_file.count("\n") |
489 | | - end_character = len(read_file.splitlines()[-1]) if end_line > 0 else 0 |
490 | | - |
491 | | - models[model_index]["columns"] = new_columns |
492 | | - edit = types.TextDocumentEdit( |
493 | | - text_document=types.OptionalVersionedTextDocumentIdentifier( |
494 | | - uri=uri.value, |
495 | | - version=None, |
496 | | - ), |
497 | | - edits=[ |
498 | | - types.TextEdit( |
499 | | - range=types.Range( |
500 | | - start=types.Position(line=0, character=0), |
501 | | - end=types.Position( |
502 | | - line=end_line, |
503 | | - character=end_character, |
504 | | - ), |
505 | | - ), |
506 | | - new_text=yaml.dump(models), |
507 | | - ) |
508 | | - ], |
509 | | - ) |
510 | | - ls.apply_edit(types.WorkspaceEdit(document_changes=[edit])) |
511 | | - return True |
0 commit comments