use crate::error::Result;
use crate::lsp::LanguageServerManager;
use crate::tools::hover::RangeInfo;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
#[derive(Debug, Serialize, Deserialize, Clone, Copy)]
#[serde(rename_all = "lowercase")]
pub enum DiagnosticSeverity {
Error,
Warning,
Information,
Hint,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct DiagnosticInfo {
pub message: String,
pub severity: DiagnosticSeverity,
pub range: RangeInfo,
pub source: Option<String>,
pub code: Option<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct DiagnosticsResult {
pub file: String,
pub diagnostics: Vec<DiagnosticInfo>,
pub error_count: usize,
pub warning_count: usize,
}
pub fn get_diagnostics(
manager: &LanguageServerManager,
file_path: &str,
) -> Result<DiagnosticsResult> {
let path = PathBuf::from(file_path);
let client = manager.get_client_for_file(&path)?;
let uri = crate::tools::file_to_url(&path)?;
let content = crate::tools::read_file(&path)?;
let language_id = client.language().language_id();
client.open_document(&uri, &content, language_id)?;
Ok(DiagnosticsResult {
file: file_path.to_string(),
diagnostics: vec![],
error_count: 0,
warning_count: 0,
})
}
#[allow(dead_code)]
fn convert_severity(severity: Option<lsp_types::DiagnosticSeverity>) -> DiagnosticSeverity {
match severity {
Some(lsp_types::DiagnosticSeverity::ERROR) => DiagnosticSeverity::Error,
Some(lsp_types::DiagnosticSeverity::WARNING) => DiagnosticSeverity::Warning,
Some(lsp_types::DiagnosticSeverity::INFORMATION) => DiagnosticSeverity::Information,
Some(lsp_types::DiagnosticSeverity::HINT) => DiagnosticSeverity::Hint,
_ => DiagnosticSeverity::Information,
}
}