|
| 1 | +use std::io; |
| 2 | + |
| 3 | +use pgt_console::markup; |
| 4 | +use pgt_diagnostics::{Advices, Diagnostic, LogCategory, MessageAndDescription, Severity, Visit}; |
| 5 | +use pgt_text_size::TextRange; |
| 6 | + |
| 7 | +use crate::{PlpgSqlCheckIssue, PlpgSqlCheckResult}; |
| 8 | + |
| 9 | +/// A specialized diagnostic for plpgsql_check. |
| 10 | +#[derive(Clone, Debug, Diagnostic)] |
| 11 | +#[diagnostic(category = "plpgsql_check")] |
| 12 | +pub struct PlPgSqlCheckDiagnostic { |
| 13 | + #[location(span)] |
| 14 | + pub span: Option<TextRange>, |
| 15 | + #[description] |
| 16 | + #[message] |
| 17 | + pub message: MessageAndDescription, |
| 18 | + #[advice] |
| 19 | + pub advices: PlPgSqlCheckAdvices, |
| 20 | + #[severity] |
| 21 | + pub severity: Severity, |
| 22 | +} |
| 23 | + |
| 24 | +#[derive(Debug, Clone)] |
| 25 | +pub struct PlPgSqlCheckAdvices { |
| 26 | + pub code: Option<String>, |
| 27 | + pub statement: Option<String>, |
| 28 | + pub query: Option<String>, |
| 29 | + pub line_number: Option<String>, |
| 30 | + pub query_position: Option<String>, |
| 31 | +} |
| 32 | + |
| 33 | +impl Advices for PlPgSqlCheckAdvices { |
| 34 | + fn record(&self, visitor: &mut dyn Visit) -> io::Result<()> { |
| 35 | + // Show the error code if available |
| 36 | + if let Some(code) = &self.code { |
| 37 | + visitor.record_log( |
| 38 | + LogCategory::Error, |
| 39 | + &markup! { "SQL State: " <Emphasis>{code}</Emphasis> }, |
| 40 | + )?; |
| 41 | + } |
| 42 | + |
| 43 | + // Show statement information if available |
| 44 | + if let Some(statement) = &self.statement { |
| 45 | + if let Some(line_number) = &self.line_number { |
| 46 | + visitor.record_log( |
| 47 | + LogCategory::Info, |
| 48 | + &markup! { "At line " <Emphasis>{line_number}</Emphasis> ": "{statement}"" }, |
| 49 | + )?; |
| 50 | + } else { |
| 51 | + visitor.record_log(LogCategory::Info, &markup! { "Statement: "{statement}"" })?; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + // Show query information if available |
| 56 | + if let Some(query) = &self.query { |
| 57 | + if let Some(pos) = &self.query_position { |
| 58 | + visitor.record_log( |
| 59 | + LogCategory::Info, |
| 60 | + &markup! { "In query at position " <Emphasis>{pos}</Emphasis> ":\n"{query}"" }, |
| 61 | + )?; |
| 62 | + } else { |
| 63 | + visitor.record_log(LogCategory::Info, &markup! { "Query:\n"{query}"" })?; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + Ok(()) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +/// Convert plpgsql_check results into diagnostics |
| 72 | +pub fn create_diagnostics_from_check_result( |
| 73 | + result: &PlpgSqlCheckResult, |
| 74 | + fn_body: &str, |
| 75 | + start: usize, |
| 76 | +) -> Vec<PlPgSqlCheckDiagnostic> { |
| 77 | + result |
| 78 | + .issues |
| 79 | + .iter() |
| 80 | + .map(|issue| create_diagnostic_from_issue(issue, fn_body, start)) |
| 81 | + .collect() |
| 82 | +} |
| 83 | + |
| 84 | +fn create_diagnostic_from_issue( |
| 85 | + issue: &PlpgSqlCheckIssue, |
| 86 | + fn_body: &str, |
| 87 | + start: usize, |
| 88 | +) -> PlPgSqlCheckDiagnostic { |
| 89 | + let severity = match issue.level.as_str() { |
| 90 | + "error" => Severity::Error, |
| 91 | + "warning" => Severity::Warning, |
| 92 | + "notice" => Severity::Hint, |
| 93 | + _ => Severity::Information, |
| 94 | + }; |
| 95 | + |
| 96 | + let span = if let Some(s) = &issue.statement { |
| 97 | + let line_number = s.line_number.parse::<usize>().unwrap_or(0); |
| 98 | + if line_number > 0 { |
| 99 | + let mut current_offset = 0; |
| 100 | + let mut result = None; |
| 101 | + for (i, line) in fn_body.lines().enumerate() { |
| 102 | + if i + 1 == line_number { |
| 103 | + if let Some(stmt_pos) = line.to_lowercase().find(&s.text.to_lowercase()) { |
| 104 | + let line_start = start + current_offset + stmt_pos; |
| 105 | + let line_end = line_start + s.text.len(); |
| 106 | + result = Some(TextRange::new( |
| 107 | + (line_start as u32).into(), |
| 108 | + (line_end as u32).into(), |
| 109 | + )); |
| 110 | + } else { |
| 111 | + let line_start = start + current_offset; |
| 112 | + let line_end = line_start + line.len(); |
| 113 | + result = Some(TextRange::new( |
| 114 | + (line_start as u32).into(), |
| 115 | + (line_end as u32).into(), |
| 116 | + )); |
| 117 | + } |
| 118 | + break; |
| 119 | + } |
| 120 | + current_offset += line.len() + 1; |
| 121 | + } |
| 122 | + result |
| 123 | + } else { |
| 124 | + None |
| 125 | + } |
| 126 | + } else { |
| 127 | + None |
| 128 | + }; |
| 129 | + |
| 130 | + PlPgSqlCheckDiagnostic { |
| 131 | + message: issue.message.clone().into(), |
| 132 | + severity, |
| 133 | + span, |
| 134 | + advices: PlPgSqlCheckAdvices { |
| 135 | + code: issue.sql_state.clone(), |
| 136 | + statement: issue.statement.as_ref().map(|s| s.text.clone()), |
| 137 | + query: issue.query.as_ref().map(|q| q.text.clone()), |
| 138 | + line_number: issue.statement.as_ref().map(|s| s.line_number.clone()), |
| 139 | + query_position: issue.query.as_ref().map(|q| q.position.clone()), |
| 140 | + }, |
| 141 | + } |
| 142 | +} |
0 commit comments