|
1 | | -from lsprotocol.types import Diagnostic |
2 | | -from sqlmesh.utils.errors import ConfigError, ModeBlockExtraFields, ModelBlockFieldValidationMissingFieldsError |
| 1 | +from lsprotocol.types import Diagnostic, DiagnosticSeverity, Range, Position |
| 2 | + |
| 3 | +from sqlmesh.core.linter.helpers import get_range_of_model_block |
| 4 | +from sqlmesh.lsp.helpers import to_lsp_range |
| 5 | +from sqlmesh.lsp.uri import URI |
| 6 | +from sqlmesh.utils.errors import ( |
| 7 | + ConfigError, |
| 8 | + ModeBlockExtraFields, |
| 9 | + ModelBlockFieldValidationMissingFieldsError, |
| 10 | +) |
3 | 11 | import typing as t |
4 | 12 |
|
5 | | -type ContextFailedError = str | ConfigError | ModeBlockExtraFields | ModelBlockFieldValidationMissingFieldsError |
| 13 | +type ContextFailedError = ( |
| 14 | + str | ConfigError | ModeBlockExtraFields | ModelBlockFieldValidationMissingFieldsError |
| 15 | +) |
| 16 | + |
6 | 17 |
|
7 | | -def contextErrorToDiagnostic(error: Exception,) -> t.Tuple[t.Optional[t.Tuple[str, Diagnostic]], ContextFailedError]: |
| 18 | +def contextErrorToDiagnostic( |
| 19 | + error: Exception, |
| 20 | +) -> t.Tuple[t.Optional[t.Tuple[str, Diagnostic]], ContextFailedError]: |
8 | 21 | if isinstance(error, ModelBlockFieldValidationMissingFieldsError): |
9 | 22 | return missingErrorToDiagnostic(error), error |
10 | | - if isinstance(error, ModeBlockExtraFields) |
| 23 | + if isinstance(error, ModeBlockExtraFields): |
11 | 24 | return extraFieldsErrorToDiagnostic(error), error |
12 | 25 | if isinstance(error, ConfigError): |
13 | 26 | return configErrorToDiagnostic(error), error |
14 | 27 | return None, str(error) |
15 | 28 |
|
16 | 29 |
|
17 | 30 | def missingErrorToDiagnostic( |
18 | | - error: ModelBlockFieldValidationMissingFieldsError, |
19 | | -) ->t.Optional[t.Tuple[str, Diagnostic]]: |
20 | | - raise NotImplementedError() |
| 31 | + error: ModelBlockFieldValidationMissingFieldsError, |
| 32 | +) -> t.Optional[t.Tuple[str, Diagnostic]]: |
| 33 | + if error.location is None: |
| 34 | + return None |
| 35 | + uri = URI.from_path(error.location).value |
| 36 | + with open(error.location, "r") as file: |
| 37 | + content = file.read() |
| 38 | + model_block = get_range_of_model_block(content) |
| 39 | + if model_block is None: |
| 40 | + return None |
| 41 | + return uri, Diagnostic( |
| 42 | + message=str(error), |
| 43 | + range=to_lsp_range(model_block), |
| 44 | + severity=DiagnosticSeverity.Error, |
| 45 | + source="SQLMesh", |
| 46 | + ) |
| 47 | + |
21 | 48 |
|
22 | 49 | def extraFieldsErrorToDiagnostic( |
23 | | - error: ModeBlockExtraFields, |
| 50 | + error: ModeBlockExtraFields, |
24 | 51 | ) -> t.Optional[t.Tuple[str, Diagnostic]]: |
25 | | - raise NotImplementedError() |
| 52 | + if error.location is None: |
| 53 | + return None |
| 54 | + uri = URI.from_path(error.location).value |
| 55 | + with open(error.location, "r") as file: |
| 56 | + content = file.read() |
| 57 | + model_block = get_range_of_model_block(content) |
| 58 | + if model_block is None: |
| 59 | + return None |
| 60 | + return uri, Diagnostic( |
| 61 | + message=str(error), |
| 62 | + range=to_lsp_range(model_block), |
| 63 | + severity=DiagnosticSeverity.Error, |
| 64 | + source="SQLMesh", |
| 65 | + ) |
26 | 66 |
|
27 | | -def configErrorToDiagnostic( |
28 | | - error: ConfigError, |
29 | | -) ->t.Optional[t.Tuple[str, Diagnostic]]: |
30 | | - return Diagnostic( |
31 | 67 |
|
| 68 | +def configErrorToDiagnostic( |
| 69 | + error: ConfigError, |
| 70 | +) -> t.Optional[t.Tuple[str, Diagnostic]]: |
| 71 | + if error.location is None: |
| 72 | + return None |
| 73 | + uri = URI.from_path(error.location).value |
| 74 | + return uri, Diagnostic( |
| 75 | + range=Range( |
| 76 | + start=Position( |
| 77 | + line=0, |
| 78 | + character=0, |
| 79 | + ), |
| 80 | + end=Position( |
| 81 | + line=0, |
| 82 | + character=0, |
| 83 | + ), |
| 84 | + ), |
| 85 | + message=str(error), |
| 86 | + severity=DiagnosticSeverity.Error, |
| 87 | + source="SQLMesh", |
32 | 88 | ) |
33 | | - raise NotImplementedError() |
34 | | - |
|
0 commit comments