Skip to content

Commit

Permalink
Merge pull request #8 from Vulpesx/diagnostic-levels
Browse files Browse the repository at this point in the history
Add diagnostic levels
  • Loading branch information
MathiasPius authored Jun 3, 2024
2 parents a00d214 + 12cf69c commit f69d6fc
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 5 deletions.
34 changes: 30 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,34 +59,60 @@ impl Backend {
};

// Produce diagnostic hints for each crate where we might be helpful.
let nu_sev = self.settings.needs_update_severity().await;
let utd_sev = self.settings.up_to_date_severity().await;
let ud_sev = self.settings.unknown_dep_severity().await;
let diagnostics: Vec<_> = dependency_with_versions
.into_iter()
.map(|dependency| {
if let Some(Some(newest_version)) = newest_packages.get(&dependency.name) {
match &dependency.version {
DependencyVersion::Complete { range, version } => {
if !version.matches(newest_version) {
Diagnostic::new_simple(
Diagnostic::new(
*range,
Some(nu_sev),
None,
None,
format!("{}: {newest_version}", &dependency.name),
None,
None,
)
} else {
let range = Range {
start: Position::new(range.start.line, 0),
end: Position::new(range.start.line, 0),
};
Diagnostic::new_simple(range, "✓".to_string())
Diagnostic::new(
range,
Some(utd_sev),
None,
None,
"✓".to_string(),
None,
None,
)
}
}
DependencyVersion::Partial { range, .. } => Diagnostic::new_simple(
DependencyVersion::Partial { range, .. } => Diagnostic::new(
*range,
Some(nu_sev),
None,
None,
format!("{}: {newest_version}", &dependency.name),
None,
None,
),
}
} else {
Diagnostic::new_simple(
Diagnostic::new(
dependency.version.range(),
Some(ud_sev),
None,
None,
format!("{}: Unknown crate", &dependency.name),
None,
None,
)
}
})
Expand Down
45 changes: 44 additions & 1 deletion src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::sync::Arc;

use serde::Deserialize;
use tokio::sync::RwLock;
use tower_lsp::lsp_types::DiagnosticSeverity;

#[derive(Default, Debug, Clone)]
pub struct Settings {
Expand All @@ -19,12 +20,54 @@ impl Settings {
pub async fn use_api(&self) -> bool {
self.inner.read().await.lsp.use_api.unwrap_or_default()
}

pub async fn needs_update_severity(&self) -> DiagnosticSeverity {
self.inner
.read()
.await
.lsp
.needs_update_severity
.filter(verify_severity)
.unwrap_or(DiagnosticSeverity::INFORMATION)
}

pub async fn up_to_date_severity(&self) -> DiagnosticSeverity {
self.inner
.read()
.await
.lsp
.up_to_date_severity
.filter(verify_severity)
.unwrap_or(DiagnosticSeverity::HINT)
}

pub async fn unknown_dep_severity(&self) -> DiagnosticSeverity {
self.inner
.read()
.await
.lsp
.unknown_dep_severity
.filter(verify_severity)
.unwrap_or(DiagnosticSeverity::WARNING)
}
}

// verify the config is a valid severity level
fn verify_severity(d: &DiagnosticSeverity) -> bool {
*d >= DiagnosticSeverity::ERROR && *d <= DiagnosticSeverity::HINT
}

#[derive(Default, Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LspSettings {
#[serde(rename = "useApi", default)]
#[serde(default)]
pub use_api: Option<bool>,
#[serde(default)]
pub needs_update_severity: Option<DiagnosticSeverity>,
#[serde(default)]
pub up_to_date_severity: Option<DiagnosticSeverity>,
#[serde(default)]
pub unknown_dep_severity: Option<DiagnosticSeverity>,
}

#[derive(Default, Debug, Clone, Deserialize)]
Expand Down

0 comments on commit f69d6fc

Please sign in to comment.