Skip to content

Commit

Permalink
[red-knot] Make Diagnostic::file optional
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Jan 21, 2025
1 parent deba6f5 commit 3cc8c14
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 22 deletions.
4 changes: 2 additions & 2 deletions crates/red_knot_project/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,8 +402,8 @@ impl Diagnostic for IOErrorDiagnostic {
self.error.to_string().into()
}

fn file(&self) -> File {
self.file
fn file(&self) -> Option<File> {
Some(self.file)
}

fn range(&self) -> Option<TextRange> {
Expand Down
1 change: 1 addition & 0 deletions crates/red_knot_project/src/metadata/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use red_knot_python_semantic::{
use ruff_db::system::{System, SystemPath};
use ruff_macros::Combine;
use serde::{Deserialize, Serialize};
use std::fmt::Debug;
use thiserror::Error;

/// The options for the project.
Expand Down
4 changes: 2 additions & 2 deletions crates/red_knot_python_semantic/src/types/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -802,8 +802,8 @@ impl Diagnostic for TypeCheckDiagnostic {
TypeCheckDiagnostic::message(self).into()
}

fn file(&self) -> File {
TypeCheckDiagnostic::file(self)
fn file(&self) -> Option<File> {
Some(TypeCheckDiagnostic::file(self))
}

fn range(&self) -> Option<TextRange> {
Expand Down
6 changes: 3 additions & 3 deletions crates/red_knot_server/src/server/api/requests/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ fn to_lsp_diagnostic(
diagnostic: &dyn ruff_db::diagnostic::Diagnostic,
encoding: crate::PositionEncoding,
) -> Diagnostic {
let range = if let Some(range) = diagnostic.range() {
let index = line_index(db.upcast(), diagnostic.file());
let source = source_text(db.upcast(), diagnostic.file());
let range = if let (Some(file), Some(range)) = (diagnostic.file(), diagnostic.range()) {
let index = line_index(db.upcast(), file);
let source = source_text(db.upcast(), file);

range.to_range(&source, &index, encoding)
} else {
Expand Down
29 changes: 14 additions & 15 deletions crates/ruff_db/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ pub trait Diagnostic: Send + Sync + std::fmt::Debug {

fn message(&self) -> Cow<str>;

fn file(&self) -> File;
fn file(&self) -> Option<File>;

fn range(&self) -> Option<TextRange>;

Expand Down Expand Up @@ -197,16 +197,15 @@ impl std::fmt::Display for DisplayDiagnostic<'_> {
Severity::Fatal => f.write_str("fatal")?,
}

write!(
f,
"[{rule}] {path}",
rule = self.diagnostic.id(),
path = self.diagnostic.file().path(self.db)
)?;
write!(f, "[{rule}]", rule = self.diagnostic.id())?;

if let Some(range) = self.diagnostic.range() {
let index = line_index(self.db, self.diagnostic.file());
let source = source_text(self.db, self.diagnostic.file());
if let Some(file) = self.diagnostic.file() {
write!(f, " {path}", path = file.path(self.db))?;
}

if let (Some(file), Some(range)) = (self.diagnostic.file(), self.diagnostic.range()) {
let index = line_index(self.db, file);
let source = source_text(self.db, file);

let start = index.source_location(range.start(), &source);

Expand All @@ -229,7 +228,7 @@ where
(**self).message()
}

fn file(&self) -> File {
fn file(&self) -> Option<File> {
(**self).file()
}

Expand All @@ -254,7 +253,7 @@ where
(**self).message()
}

fn file(&self) -> File {
fn file(&self) -> Option<File> {
(**self).file()
}

Expand All @@ -276,7 +275,7 @@ impl Diagnostic for Box<dyn Diagnostic> {
(**self).message()
}

fn file(&self) -> File {
fn file(&self) -> Option<File> {
(**self).file()
}

Expand Down Expand Up @@ -310,8 +309,8 @@ impl Diagnostic for ParseDiagnostic {
self.error.error.to_string().into()
}

fn file(&self) -> File {
self.file
fn file(&self) -> Option<File> {
Some(self.file)
}

fn range(&self) -> Option<TextRange> {
Expand Down

0 comments on commit 3cc8c14

Please sign in to comment.