Unnamed repository; edit this file 'description' to name the repository.
migrate `ExpandDatabase::resolve_span`
| -rw-r--r-- | crates/hir-expand/src/db.rs | 15 | ||||
| -rw-r--r-- | crates/hir-expand/src/lib.rs | 18 | ||||
| -rw-r--r-- | crates/load-cargo/src/lib.rs | 6 |
3 files changed, 19 insertions, 20 deletions
diff --git a/crates/hir-expand/src/db.rs b/crates/hir-expand/src/db.rs index 5c9520d397..c0a73369a6 100644 --- a/crates/hir-expand/src/db.rs +++ b/crates/hir-expand/src/db.rs @@ -1,13 +1,12 @@ //! Defines database & queries for macro expansion. use base_db::{Crate, SourceDatabase}; -use span::Span; use syntax::ast; use crate::{ AstId, BuiltinAttrExpander, BuiltinDeriveExpander, BuiltinFnLikeExpander, EagerExpander, - EditionedFileId, FileRange, HirFileId, MacroDefId, MacroDefKind, - declarative::DeclarativeMacroExpander, proc_macro::CustomProcMacroExpander, + MacroDefId, MacroDefKind, declarative::DeclarativeMacroExpander, + proc_macro::CustomProcMacroExpander, }; #[derive(Debug, Clone, Copy, Eq, PartialEq)] @@ -29,9 +28,6 @@ pub enum TokenExpander<'db> { #[query_group::query_group] pub trait ExpandDatabase: SourceDatabase { - #[salsa::transparent] - fn resolve_span(&self, span: Span) -> FileRange; - /// Fetches the expander for this macro. #[salsa::transparent] #[salsa::invoke(TokenExpander::macro_expander)] @@ -47,13 +43,6 @@ pub trait ExpandDatabase: SourceDatabase { ) -> &DeclarativeMacroExpander; } -fn resolve_span(db: &dyn ExpandDatabase, Span { range, anchor, ctx: _ }: Span) -> FileRange { - let file_id = EditionedFileId::from_span_file_id(db, anchor.file_id); - let anchor_offset = - HirFileId::from(file_id).ast_id_map(db).get_erased(anchor.ast_id).text_range().start(); - FileRange { file_id, range: range + anchor_offset } -} - impl<'db> TokenExpander<'db> { fn macro_expander(db: &'db dyn ExpandDatabase, id: MacroDefId) -> TokenExpander<'db> { match id.kind { diff --git a/crates/hir-expand/src/lib.rs b/crates/hir-expand/src/lib.rs index 75ede0c719..9d648b12a2 100644 --- a/crates/hir-expand/src/lib.rs +++ b/crates/hir-expand/src/lib.rs @@ -1381,7 +1381,7 @@ impl<'db> ExpansionInfo<'db> { let span = self.exp_map.span_at(token.start()); match &self.arg_map { SpanMap::RealSpanMap(_) => { - let range = db.resolve_span(span); + let range = resolve_span(db, span); InFile { file_id: range.file_id.into(), value: smallvec::smallvec![range.range] } } SpanMap::ExpansionSpanMap(arg_map) => { @@ -1435,7 +1435,7 @@ pub fn map_node_range_up_rooted( start = start.min(span.range.start()); end = end.max(span.range.end()); } - Some(db.resolve_span(Span { range: TextRange::new(start, end), anchor, ctx })) + Some(resolve_span(db, Span { range: TextRange::new(start, end), anchor, ctx })) } /// Maps up the text range out of the expansion hierarchy back into the original file its from. @@ -1458,7 +1458,7 @@ pub fn map_node_range_up( start = start.min(span.range.start()); end = end.max(span.range.end()); } - Some((db.resolve_span(Span { range: TextRange::new(start, end), anchor, ctx }), ctx)) + Some((resolve_span(db, Span { range: TextRange::new(start, end), anchor, ctx }), ctx)) } /// Looks up the span at the given offset. @@ -1468,7 +1468,17 @@ pub fn span_for_offset( offset: TextSize, ) -> (FileRange, SyntaxContext) { let span = exp_map.span_at(offset); - (db.resolve_span(span), span.ctx) + (resolve_span(db, span), span.ctx) +} + +// FIXME: This is only public because of its use in `load_cargo` (which we should consider removing +// by moving the implementations of the subrequests to `hir_expand`, and calling within `load-cargo`). +// Avoid adding any more outside uses. +pub fn resolve_span(db: &dyn ExpandDatabase, Span { range, anchor, ctx: _ }: Span) -> FileRange { + let file_id = EditionedFileId::from_span_file_id(db, anchor.file_id); + let anchor_offset = + HirFileId::from(file_id).ast_id_map(db).get_erased(anchor.ast_id).text_range().start(); + FileRange { file_id, range: range + anchor_offset } } /// In Rust, macros expand token trees to token trees. When we want to turn a diff --git a/crates/load-cargo/src/lib.rs b/crates/load-cargo/src/lib.rs index 4a97a5f05e..b5860cb0ca 100644 --- a/crates/load-cargo/src/lib.rs +++ b/crates/load-cargo/src/lib.rs @@ -659,7 +659,7 @@ impl ProcMacroExpander for Expander { let call_site_file = macro_call_loc.kind.file_id(); - let resolved = db.resolve_span(current_span); + let resolved = hir_expand::resolve_span(db, current_span); current_ctx = macro_call_loc.ctxt; current_span = Span { @@ -676,7 +676,7 @@ impl ProcMacroExpander for Expander { } } - let resolved = db.resolve_span(current_span); + let resolved = hir_expand::resolve_span(db, current_span); Ok(SubResponse::SpanSourceResult { file_id: resolved.file_id.span_file_id(db).as_u32(), @@ -761,7 +761,7 @@ fn resolve_sub_span( anchor: SpanAnchor { file_id: editioned_file_id, ast_id }, ctx: SyntaxContext::root(editioned_file_id.edition()), }; - db.resolve_span(span) + hir_expand::resolve_span(db, span) } #[cfg(test)] |