Revert "give up and pass down notebook_indexes"

This reverts commit 5d918e963a3fb052c9f3567dc85b6f41578bd022.
This commit is contained in:
Brent Westbrook 2025-07-04 10:44:54 -04:00
parent 2f730a5f0f
commit af7137de17
14 changed files with 35 additions and 103 deletions

View file

@ -1,12 +1,10 @@
use std::{fmt::Formatter, sync::Arc};
use ruff_diagnostics::Fix;
use ruff_notebook::NotebookIndex;
use ruff_source_file::{LineColumn, SourceCode, SourceFile};
use ruff_annotate_snippets::Level as AnnotateLevel;
use ruff_text_size::{Ranged, TextRange, TextSize};
use rustc_hash::FxHashMap;
pub use self::render::{DisplayDiagnostic, DisplayDiagnostics, FileResolver, Input};
use crate::{Db, files::File};
@ -145,9 +143,8 @@ impl Diagnostic {
&'a self,
resolver: &'a dyn FileResolver,
config: &'a DisplayDiagnosticConfig,
notebook_indexes: &'a FxHashMap<String, NotebookIndex>,
) -> DisplayDiagnostic<'a> {
DisplayDiagnostic::new(resolver, config, self, notebook_indexes)
DisplayDiagnostic::new(resolver, config, self)
}
/// Returns the identifier for this diagnostic.

View file

@ -1,13 +1,12 @@
use std::collections::BTreeMap;
use json::{diagnostics_to_json_value, message_to_json_value};
use ruff_annotate_snippets::{
Annotation as AnnotateAnnotation, Level as AnnotateLevel, Message as AnnotateMessage,
Renderer as AnnotateRenderer, Snippet as AnnotateSnippet,
};
use ruff_notebook::NotebookIndex;
use ruff_source_file::{LineIndex, OneIndexed, SourceCode};
use ruff_text_size::{TextRange, TextSize};
use rustc_hash::FxHashMap;
use crate::diagnostic::stylesheet::{DiagnosticStylesheet, fmt_styled};
use crate::{
@ -40,7 +39,6 @@ pub struct DisplayDiagnostic<'a> {
resolver: &'a dyn FileResolver,
annotate_renderer: AnnotateRenderer,
diag: &'a Diagnostic,
notebook_indexes: &'a FxHashMap<String, NotebookIndex>,
}
impl<'a> DisplayDiagnostic<'a> {
@ -48,7 +46,6 @@ impl<'a> DisplayDiagnostic<'a> {
resolver: &'a dyn FileResolver,
config: &'a DisplayDiagnosticConfig,
diag: &'a Diagnostic,
notebook_indexes: &'a FxHashMap<String, NotebookIndex>,
) -> DisplayDiagnostic<'a> {
let annotate_renderer = if config.color {
AnnotateRenderer::styled()
@ -61,7 +58,6 @@ impl<'a> DisplayDiagnostic<'a> {
resolver,
annotate_renderer,
diag,
notebook_indexes,
}
}
}
@ -77,7 +73,6 @@ pub struct DisplayDiagnostics<'a> {
config: &'a DisplayDiagnosticConfig,
resolver: &'a dyn FileResolver,
diagnostics: &'a [Diagnostic],
notebook_indexes: &'a FxHashMap<String, NotebookIndex>,
}
impl<'a> DisplayDiagnostics<'a> {
@ -85,13 +80,11 @@ impl<'a> DisplayDiagnostics<'a> {
resolver: &'a dyn FileResolver,
config: &'a DisplayDiagnosticConfig,
diagnostics: &'a [Diagnostic],
notebook_indexes: &'a FxHashMap<String, NotebookIndex>,
) -> DisplayDiagnostics<'a> {
DisplayDiagnostics {
config,
resolver,
diagnostics,
notebook_indexes,
}
}
}
@ -101,22 +94,14 @@ impl std::fmt::Display for DisplayDiagnostics<'_> {
match self.config.format {
DiagnosticFormat::Concise | DiagnosticFormat::Azure | DiagnosticFormat::Full => {
for diag in self.diagnostics {
write!(
f,
"{}",
diag.display(self.resolver, self.config, self.notebook_indexes)
)?;
}
}
DiagnosticFormat::Json => {
if let Some(value) = json::diagnostics_to_json_value(
self.diagnostics,
self.resolver,
self.notebook_indexes,
) {
write!(f, "{value:#}")?;
write!(f, "{}", diag.display(self.resolver, self.config))?;
}
}
DiagnosticFormat::Json => write!(
f,
"{:#}",
diagnostics_to_json_value(self.diagnostics, self.resolver)
)?,
}
Ok(())
@ -202,9 +187,7 @@ impl std::fmt::Display for DisplayDiagnostic<'_> {
)?;
}
DiagnosticFormat::Json => {
if let Some(value) =
json::message_to_json_value(self.diag, self.resolver, self.notebook_indexes)
{
if let Some(value) = message_to_json_value(self.diag, self.resolver) {
writeln!(f, "{value}")?;
}
}
@ -2343,8 +2326,7 @@ watermelon
///
/// (This will set the "printed" flag on `Diagnostic`.)
fn render(&self, diag: &Diagnostic) -> String {
diag.display(&self.db, &self.config, &FxHashMap::default())
.to_string()
diag.display(&self.db, &self.config).to_string()
}
}

View file

@ -1,4 +1,3 @@
use rustc_hash::FxHashMap;
use serde::{Serialize, Serializer, ser::SerializeSeq};
use serde_json::{Value, json};
@ -14,37 +13,32 @@ use super::FileResolver;
pub(super) fn diagnostics_to_json_value<'a>(
diagnostics: impl IntoIterator<Item = &'a Diagnostic>,
resolver: &dyn FileResolver,
notebook_indexes: &FxHashMap<String, NotebookIndex>,
) -> Option<Value> {
let messages: Option<Vec<_>> = diagnostics
.iter()
.map(|diag| message_to_json_value(diag, resolver, notebook_indexes))
) -> Value {
let messages: Vec<_> = diagnostics
.into_iter()
.filter_map(|diag| message_to_json_value(diag, resolver))
.collect();
Some(json!(messages?))
json!(messages)
}
pub(super) fn message_to_json_value(
message: &Diagnostic,
resolver: &dyn FileResolver,
notebook_indexes: &FxHashMap<String, NotebookIndex>,
) -> Option<Value> {
let span = message.primary_span()?;
let filename = span.file().path(resolver);
let range = span.range()?;
let diagnostic_source = span.file().diagnostic_source(resolver);
let source_code = diagnostic_source.as_source_code();
let notebook_index = notebook_indexes.get(filename);
// Input can be a notebook for ty, but we don't have a good way of retrieving the notebook
// index for Ruff. we might just need to pass it in
let notebook_index = None; // TODO
let fix = message.fix().map(|fix| {
json!({
"applicability": fix.applicability(),
"message": message.suggestion(),
"edits": &ExpandedEdits {
edits: fix.edits(),
source_code: &source_code,
notebook_index,
},
"edits": &ExpandedEdits { edits: fix.edits(), source_code: &source_code, notebook_index },
})
});

View file

@ -16,16 +16,12 @@ impl Emitter for AzureEmitter {
&mut self,
writer: &mut dyn Write,
diagnostics: &[Diagnostic],
context: &EmitterContext,
_context: &EmitterContext,
) -> anyhow::Result<()> {
let resolver = DummyFileResolver;
let config = DisplayDiagnosticConfig::default().format(DiagnosticFormat::Azure);
for diagnostic in diagnostics {
write!(
writer,
"{}",
diagnostic.display(&resolver, &config, context.notebook_indexes)
)?;
write!(writer, "{}", diagnostic.display(&resolver, &config))?;
}
Ok(())

View file

@ -14,14 +14,14 @@ impl Emitter for JsonEmitter {
&mut self,
writer: &mut dyn Write,
diagnostics: &[Diagnostic],
context: &EmitterContext,
_context: &EmitterContext,
) -> anyhow::Result<()> {
let resolver = DummyFileResolver;
let config = DisplayDiagnosticConfig::default().format(DiagnosticFormat::Json);
write!(
writer,
"{}",
DisplayDiagnostics::new(&resolver, &config, diagnostics, context.notebook_indexes)
DisplayDiagnostics::new(&resolver, &config, diagnostics)
)?)
}
}

View file

@ -12,16 +12,12 @@ impl Emitter for JsonLinesEmitter {
&mut self,
writer: &mut dyn Write,
diagnostics: &[Diagnostic],
context: &EmitterContext,
_context: &EmitterContext,
) -> anyhow::Result<()> {
let resolver = DummyFileResolver;
let config = DisplayDiagnosticConfig::default().format(DiagnosticFormat::Json);
for diagnostic in diagnostics {
write!(
writer,
"{}",
diagnostic.display(&resolver, &config, context.notebook_indexes)
)?;
write!(writer, "{}", diagnostic.display(&resolver, &config))?;
}
Ok(())

View file

@ -558,7 +558,6 @@ impl IndexKind {
/// memory optimizations
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "get-size", derive(get_size2::GetSize))]
pub struct OneIndexed(NonZeroUsize);
impl OneIndexed {

View file

@ -5,7 +5,6 @@ mod version;
pub use args::Cli;
use std::collections::HashMap;
use std::io::{self, BufWriter, Write, stdout};
use std::process::{ExitCode, Termination};
@ -314,11 +313,7 @@ impl MainLoop {
let diagnostics_count = result.len();
for diagnostic in result {
write!(
stdout,
"{}",
diagnostic.display(db, &display_config, &HashMap::default())
)?;
write!(stdout, "{}", diagnostic.display(db, &display_config))?;
max_severity = max_severity.max(diagnostic.severity());
}

View file

@ -134,7 +134,6 @@ mod tests {
Severity, Span,
};
use ruff_text_size::{Ranged, TextRange};
use rustc_hash::FxHashMap;
#[test]
fn hover_basic() {
@ -771,12 +770,7 @@ mod tests {
.message("Cursor offset"),
);
write!(
buf,
"{}",
diagnostic.display(&self.db, &config, &FxHashMap::default())
)
.unwrap();
write!(buf, "{}", diagnostic.display(&self.db, &config)).unwrap();
buf
}

View file

@ -206,7 +206,6 @@ mod tests {
use ruff_db::files::{File, system_path_to_file};
use ruff_db::system::{DbWithWritableSystem, SystemPath, SystemPathBuf};
use ruff_text_size::TextSize;
use rustc_hash::FxHashMap;
use ty_python_semantic::{
Program, ProgramSettings, PythonPlatform, PythonVersionWithSource, SearchPathSettings,
};
@ -252,12 +251,7 @@ mod tests {
.format(DiagnosticFormat::Full);
for diagnostic in diagnostics {
let diag = diagnostic.into_diagnostic();
write!(
buf,
"{}",
diag.display(&self.db, &config, &FxHashMap::default())
)
.unwrap();
write!(buf, "{}", diag.display(&self.db, &config)).unwrap();
}
buf

View file

@ -20,7 +20,7 @@ use ruff_db::vendored::VendoredFileSystem;
use ruff_macros::{Combine, OptionsMetadata, RustDoc};
use ruff_options_metadata::{OptionSet, OptionsMetadata, Visit};
use ruff_python_ast::PythonVersion;
use rustc_hash::{FxHashMap, FxHasher};
use rustc_hash::FxHasher;
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::fmt::{self, Debug, Display};
@ -1314,11 +1314,10 @@ impl ToSettingsError {
write!(
f,
"{}",
self.error.diagnostic.to_diagnostic().display(
&self.db,
&display_config,
&FxHashMap::default()
)
self.error
.diagnostic
.to_diagnostic()
.display(&self.db, &display_config)
)
}
}

View file

@ -13,7 +13,6 @@ use ruff_db::parsed::parsed_module;
use ruff_db::system::{DbWithWritableSystem as _, SystemPath, SystemPathBuf};
use ruff_db::testing::{setup_logging, setup_logging_with_filter};
use ruff_source_file::{LineIndex, OneIndexed};
use rustc_hash::FxHashMap;
use std::backtrace::BacktraceStatus;
use std::fmt::Write;
use ty_python_semantic::pull_types::pull_types;
@ -467,12 +466,7 @@ fn create_diagnostic_snapshot(
writeln!(snapshot).unwrap();
}
writeln!(snapshot, "```").unwrap();
write!(
snapshot,
"{}",
diag.display(db, &display_config, &FxHashMap::default())
)
.unwrap();
write!(snapshot, "{}", diag.display(db, &display_config)).unwrap();
writeln!(snapshot, "```").unwrap();
}
snapshot

View file

@ -1,5 +1,4 @@
use std::any::Any;
use std::collections::HashMap;
use js_sys::{Error, JsString};
use ruff_db::Db as _;
@ -413,7 +412,7 @@ impl Diagnostic {
pub fn display(&self, workspace: &Workspace) -> JsString {
let config = DisplayDiagnosticConfig::default().color(false);
self.inner
.display(&workspace.db, &config, &HashMap::default())
.display(&workspace.db, &config)
.to_string()
.into()
}

7
ty.schema.json generated
View file

@ -81,13 +81,6 @@
"enum": [
"azure"
]
},
{
"description": "Print diagnostics in JSON format.",
"type": "string",
"enum": [
"json"
]
}
]
},