Extract Translator struct

This commit is contained in:
Cameron Steffen
2025-06-19 13:02:04 -05:00
parent 8de4c7234d
commit 07b9bb1855
18 changed files with 190 additions and 250 deletions

View File

@@ -15,17 +15,15 @@ use rustc_span::source_map::SourceMap;
use crate::emitter::FileWithAnnotatedLines;
use crate::registry::Registry;
use crate::snippet::Line;
use crate::translation::{Translate, to_fluent_args};
use crate::translation::{Translator, to_fluent_args};
use crate::{
CodeSuggestion, DiagInner, DiagMessage, Emitter, ErrCode, FluentBundle, LazyFallbackBundle,
Level, MultiSpan, Style, Subdiag,
CodeSuggestion, DiagInner, DiagMessage, Emitter, ErrCode, Level, MultiSpan, Style, Subdiag,
};
/// Generates diagnostics using annotate-snippet
pub struct AnnotateSnippetEmitter {
source_map: Option<Arc<SourceMap>>,
fluent_bundle: Option<Arc<FluentBundle>>,
fallback_bundle: LazyFallbackBundle,
translator: Translator,
/// If true, hides the longer explanation text
short_message: bool,
@@ -35,16 +33,6 @@ pub struct AnnotateSnippetEmitter {
macro_backtrace: bool,
}
impl Translate for AnnotateSnippetEmitter {
fn fluent_bundle(&self) -> Option<&FluentBundle> {
self.fluent_bundle.as_deref()
}
fn fallback_fluent_bundle(&self) -> &FluentBundle {
&self.fallback_bundle
}
}
impl Emitter for AnnotateSnippetEmitter {
/// The entry point for the diagnostics generation
fn emit_diagnostic(&mut self, mut diag: DiagInner, _registry: &Registry) {
@@ -78,6 +66,10 @@ impl Emitter for AnnotateSnippetEmitter {
fn should_show_explain(&self) -> bool {
!self.short_message
}
fn translator(&self) -> &Translator {
&self.translator
}
}
/// Provides the source string for the given `line` of `file`
@@ -104,19 +96,11 @@ fn annotation_level_for_level(level: Level) -> annotate_snippets::Level {
impl AnnotateSnippetEmitter {
pub fn new(
source_map: Option<Arc<SourceMap>>,
fluent_bundle: Option<Arc<FluentBundle>>,
fallback_bundle: LazyFallbackBundle,
translator: Translator,
short_message: bool,
macro_backtrace: bool,
) -> Self {
Self {
source_map,
fluent_bundle,
fallback_bundle,
short_message,
ui_testing: false,
macro_backtrace,
}
Self { source_map, translator, short_message, ui_testing: false, macro_backtrace }
}
/// Allows to modify `Self` to enable or disable the `ui_testing` flag.
@@ -137,7 +121,7 @@ impl AnnotateSnippetEmitter {
_children: &[Subdiag],
_suggestions: &[CodeSuggestion],
) {
let message = self.translate_messages(messages, args);
let message = self.translator.translate_messages(messages, args);
if let Some(source_map) = &self.source_map {
// Make sure our primary file comes first
let primary_lo = if let Some(primary_span) = msp.primary_span().as_ref() {