Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/hir-expand/src/files.rs')
-rw-r--r--crates/hir-expand/src/files.rs135
1 files changed, 67 insertions, 68 deletions
diff --git a/crates/hir-expand/src/files.rs b/crates/hir-expand/src/files.rs
index f3bcc77268..321ee8feb9 100644
--- a/crates/hir-expand/src/files.rs
+++ b/crates/hir-expand/src/files.rs
@@ -2,15 +2,13 @@
use std::borrow::Borrow;
use either::Either;
-use span::{
- AstIdNode, EditionedFileId, ErasedFileAstId, FileAstId, HirFileId, HirFileIdRepr, MacroFileId,
- SyntaxContextId,
-};
+use span::{AstIdNode, ErasedFileAstId, FileAstId, FileId, SyntaxContext};
use syntax::{AstNode, AstPtr, SyntaxNode, SyntaxNodePtr, SyntaxToken, TextRange, TextSize};
use crate::{
+ EditionedFileId, HirFileId, MacroCallId, MacroKind,
db::{self, ExpandDatabase},
- map_node_range_up, map_node_range_up_rooted, span_for_offset, MacroFileIdExt, MacroKind,
+ map_node_range_up, map_node_range_up_rooted, span_for_offset,
};
/// `InFile<T>` stores a value of `T` inside a particular file/syntax tree.
@@ -26,7 +24,7 @@ pub struct InFileWrapper<FileKind, T> {
pub value: T,
}
pub type InFile<T> = InFileWrapper<HirFileId, T>;
-pub type InMacroFile<T> = InFileWrapper<MacroFileId, T>;
+pub type InMacroFile<T> = InFileWrapper<MacroCallId, T>;
pub type InRealFile<T> = InFileWrapper<EditionedFileId, T>;
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
@@ -35,12 +33,13 @@ pub struct FilePositionWrapper<FileKind> {
pub offset: TextSize,
}
pub type HirFilePosition = FilePositionWrapper<HirFileId>;
-pub type MacroFilePosition = FilePositionWrapper<MacroFileId>;
+pub type MacroFilePosition = FilePositionWrapper<MacroCallId>;
pub type FilePosition = FilePositionWrapper<EditionedFileId>;
-impl From<FilePositionWrapper<EditionedFileId>> for FilePositionWrapper<span::FileId> {
- fn from(value: FilePositionWrapper<EditionedFileId>) -> Self {
- FilePositionWrapper { file_id: value.file_id.into(), offset: value.offset }
+impl FilePosition {
+ #[inline]
+ pub fn into_file_id(self, db: &dyn ExpandDatabase) -> FilePositionWrapper<FileId> {
+ FilePositionWrapper { file_id: self.file_id.file_id(db), offset: self.offset }
}
}
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
@@ -49,12 +48,13 @@ pub struct FileRangeWrapper<FileKind> {
pub range: TextRange,
}
pub type HirFileRange = FileRangeWrapper<HirFileId>;
-pub type MacroFileRange = FileRangeWrapper<MacroFileId>;
+pub type MacroFileRange = FileRangeWrapper<MacroCallId>;
pub type FileRange = FileRangeWrapper<EditionedFileId>;
-impl From<FileRangeWrapper<EditionedFileId>> for FileRangeWrapper<span::FileId> {
- fn from(value: FileRangeWrapper<EditionedFileId>) -> Self {
- FileRangeWrapper { file_id: value.file_id.into(), range: value.range }
+impl FileRange {
+ #[inline]
+ pub fn into_file_id(self, db: &dyn ExpandDatabase) -> FileRangeWrapper<FileId> {
+ FileRangeWrapper { file_id: self.file_id.file_id(db), range: self.range }
}
}
@@ -76,6 +76,9 @@ impl<N: AstIdNode> AstId<N> {
pub fn to_ptr(&self, db: &dyn ExpandDatabase) -> AstPtr<N> {
db.ast_id_map(self.file_id).get(self.value)
}
+ pub fn erase(&self) -> ErasedAstId {
+ crate::InFile::new(self.file_id, self.value.erase())
+ }
}
pub type ErasedAstId = crate::InFile<ErasedFileAstId>;
@@ -162,7 +165,7 @@ impl FileIdToSyntax for EditionedFileId {
db.parse(self).syntax_node()
}
}
-impl FileIdToSyntax for MacroFileId {
+impl FileIdToSyntax for MacroCallId {
fn file_syntax(self, db: &dyn db::ExpandDatabase) -> SyntaxNode {
db.parse_macro_expansion(self).value.0.syntax_node()
}
@@ -215,7 +218,7 @@ impl<SN: Borrow<SyntaxNode>> InFile<SN> {
let succ = move |node: &InFile<SyntaxNode>| match node.value.parent() {
Some(parent) => Some(node.with_value(parent)),
None => db
- .lookup_intern_macro_call(node.file_id.macro_file()?.macro_call_id)
+ .lookup_intern_macro_call(node.file_id.macro_file()?)
.to_node_item(db)
.syntax()
.cloned()
@@ -232,7 +235,7 @@ impl<SN: Borrow<SyntaxNode>> InFile<SN> {
let succ = move |node: &InFile<SyntaxNode>| match node.value.parent() {
Some(parent) => Some(node.with_value(parent)),
None => db
- .lookup_intern_macro_call(node.file_id.macro_file()?.macro_call_id)
+ .lookup_intern_macro_call(node.file_id.macro_file()?)
.to_node_item(db)
.syntax()
.cloned()
@@ -272,11 +275,11 @@ impl<SN: Borrow<SyntaxNode>> InFile<SN> {
) -> Option<InRealFile<SyntaxNode>> {
// This kind of upmapping can only be achieved in attribute expanded files,
// as we don't have node inputs otherwise and therefore can't find an `N` node in the input
- let file_id = match self.file_id.repr() {
- HirFileIdRepr::FileId(file_id) => {
- return Some(InRealFile { file_id, value: self.value.borrow().clone() })
+ let file_id = match self.file_id {
+ HirFileId::FileId(file_id) => {
+ return Some(InRealFile { file_id, value: self.value.borrow().clone() });
}
- HirFileIdRepr::MacroFile(m)
+ HirFileId::MacroFile(m)
if matches!(m.kind(db), MacroKind::Attr | MacroKind::AttrBuiltIn) =>
{
m
@@ -284,7 +287,7 @@ impl<SN: Borrow<SyntaxNode>> InFile<SN> {
_ => return None,
};
- let FileRange { file_id, range } = map_node_range_up_rooted(
+ let FileRange { file_id: editioned_file_id, range } = map_node_range_up_rooted(
db,
&db.expansion_span_map(file_id),
self.value.borrow().text_range(),
@@ -292,13 +295,13 @@ impl<SN: Borrow<SyntaxNode>> InFile<SN> {
let kind = self.kind();
let value = db
- .parse(file_id)
+ .parse(editioned_file_id)
.syntax_node()
.covering_element(range)
.ancestors()
.take_while(|it| it.text_range() == range)
.find(|it| it.kind() == kind)?;
- Some(InRealFile::new(file_id, value))
+ Some(InRealFile::new(editioned_file_id, value))
}
}
@@ -307,7 +310,7 @@ impl InFile<&SyntaxNode> {
pub fn original_file_range_opt(
self,
db: &dyn db::ExpandDatabase,
- ) -> Option<(FileRange, SyntaxContextId)> {
+ ) -> Option<(FileRange, SyntaxContext)> {
self.borrow().map(SyntaxNode::text_range).original_node_file_range_opt(db)
}
}
@@ -324,9 +327,9 @@ impl InMacroFile<SyntaxToken> {
impl InFile<SyntaxToken> {
/// Falls back to the macro call range if the node cannot be mapped up fully.
pub fn original_file_range(self, db: &dyn db::ExpandDatabase) -> FileRange {
- match self.file_id.repr() {
- HirFileIdRepr::FileId(file_id) => FileRange { file_id, range: self.value.text_range() },
- HirFileIdRepr::MacroFile(mac_file) => {
+ match self.file_id {
+ HirFileId::FileId(file_id) => FileRange { file_id, range: self.value.text_range() },
+ HirFileId::MacroFile(mac_file) => {
let (range, ctxt) = span_for_offset(
db,
&db.expansion_span_map(mac_file),
@@ -340,7 +343,7 @@ impl InFile<SyntaxToken> {
}
// Fall back to whole macro call.
- let loc = db.lookup_intern_macro_call(mac_file.macro_call_id);
+ let loc = db.lookup_intern_macro_call(mac_file);
loc.kind.original_call_range(db)
}
}
@@ -348,11 +351,11 @@ impl InFile<SyntaxToken> {
/// Attempts to map the syntax node back up its macro calls.
pub fn original_file_range_opt(self, db: &dyn db::ExpandDatabase) -> Option<FileRange> {
- match self.file_id.repr() {
- HirFileIdRepr::FileId(file_id) => {
+ match self.file_id {
+ HirFileId::FileId(file_id) => {
Some(FileRange { file_id, range: self.value.text_range() })
}
- HirFileIdRepr::MacroFile(mac_file) => {
+ HirFileId::MacroFile(mac_file) => {
let (range, ctxt) = span_for_offset(
db,
&db.expansion_span_map(mac_file),
@@ -361,18 +364,14 @@ impl InFile<SyntaxToken> {
// FIXME: Figure out an API that makes proper use of ctx, this only exists to
// keep pre-token map rewrite behaviour.
- if ctxt.is_root() {
- Some(range)
- } else {
- None
- }
+ if ctxt.is_root() { Some(range) } else { None }
}
}
}
}
impl InMacroFile<TextSize> {
- pub fn original_file_range(self, db: &dyn db::ExpandDatabase) -> (FileRange, SyntaxContextId) {
+ pub fn original_file_range(self, db: &dyn db::ExpandDatabase) -> (FileRange, SyntaxContext) {
span_for_offset(db, &db.expansion_span_map(self.file_id), self.value)
}
}
@@ -381,17 +380,17 @@ impl InFile<TextRange> {
pub fn original_node_file_range(
self,
db: &dyn db::ExpandDatabase,
- ) -> (FileRange, SyntaxContextId) {
- match self.file_id.repr() {
- HirFileIdRepr::FileId(file_id) => {
- (FileRange { file_id, range: self.value }, SyntaxContextId::root(file_id.edition()))
+ ) -> (FileRange, SyntaxContext) {
+ match self.file_id {
+ HirFileId::FileId(file_id) => {
+ (FileRange { file_id, range: self.value }, SyntaxContext::root(file_id.edition(db)))
}
- HirFileIdRepr::MacroFile(mac_file) => {
+ HirFileId::MacroFile(mac_file) => {
match map_node_range_up(db, &db.expansion_span_map(mac_file), self.value) {
Some(it) => it,
None => {
- let loc = db.lookup_intern_macro_call(mac_file.macro_call_id);
- (loc.kind.original_call_range(db), SyntaxContextId::root(loc.def.edition))
+ let loc = db.lookup_intern_macro_call(mac_file);
+ (loc.kind.original_call_range(db), SyntaxContext::root(loc.def.edition))
}
}
}
@@ -399,13 +398,13 @@ impl InFile<TextRange> {
}
pub fn original_node_file_range_rooted(self, db: &dyn db::ExpandDatabase) -> FileRange {
- match self.file_id.repr() {
- HirFileIdRepr::FileId(file_id) => FileRange { file_id, range: self.value },
- HirFileIdRepr::MacroFile(mac_file) => {
+ match self.file_id {
+ HirFileId::FileId(file_id) => FileRange { file_id, range: self.value },
+ HirFileId::MacroFile(mac_file) => {
match map_node_range_up_rooted(db, &db.expansion_span_map(mac_file), self.value) {
Some(it) => it,
_ => {
- let loc = db.lookup_intern_macro_call(mac_file.macro_call_id);
+ let loc = db.lookup_intern_macro_call(mac_file);
loc.kind.original_call_range(db)
}
}
@@ -417,13 +416,13 @@ impl InFile<TextRange> {
self,
db: &dyn db::ExpandDatabase,
) -> FileRange {
- match self.file_id.repr() {
- HirFileIdRepr::FileId(file_id) => FileRange { file_id, range: self.value },
- HirFileIdRepr::MacroFile(mac_file) => {
+ match self.file_id {
+ HirFileId::FileId(file_id) => FileRange { file_id, range: self.value },
+ HirFileId::MacroFile(mac_file) => {
match map_node_range_up_rooted(db, &db.expansion_span_map(mac_file), self.value) {
Some(it) => it,
_ => {
- let loc = db.lookup_intern_macro_call(mac_file.macro_call_id);
+ let loc = db.lookup_intern_macro_call(mac_file);
loc.kind.original_call_range_with_body(db)
}
}
@@ -434,13 +433,13 @@ impl InFile<TextRange> {
pub fn original_node_file_range_opt(
self,
db: &dyn db::ExpandDatabase,
- ) -> Option<(FileRange, SyntaxContextId)> {
- match self.file_id.repr() {
- HirFileIdRepr::FileId(file_id) => Some((
+ ) -> Option<(FileRange, SyntaxContext)> {
+ match self.file_id {
+ HirFileId::FileId(file_id) => Some((
FileRange { file_id, range: self.value },
- SyntaxContextId::root(file_id.edition()),
+ SyntaxContext::root(file_id.edition(db)),
)),
- HirFileIdRepr::MacroFile(mac_file) => {
+ HirFileId::MacroFile(mac_file) => {
map_node_range_up(db, &db.expansion_span_map(mac_file), self.value)
}
}
@@ -451,34 +450,34 @@ impl<N: AstNode> InFile<N> {
pub fn original_ast_node_rooted(self, db: &dyn db::ExpandDatabase) -> Option<InRealFile<N>> {
// This kind of upmapping can only be achieved in attribute expanded files,
// as we don't have node inputs otherwise and therefore can't find an `N` node in the input
- let file_id = match self.file_id.repr() {
- HirFileIdRepr::FileId(file_id) => {
- return Some(InRealFile { file_id, value: self.value })
+ let file_id = match self.file_id {
+ HirFileId::FileId(file_id) => {
+ return Some(InRealFile { file_id, value: self.value });
}
- HirFileIdRepr::MacroFile(m) => m,
+ HirFileId::MacroFile(m) => m,
};
if !matches!(file_id.kind(db), MacroKind::Attr | MacroKind::AttrBuiltIn) {
return None;
}
- let FileRange { file_id, range } = map_node_range_up_rooted(
+ let FileRange { file_id: editioned_file_id, range } = map_node_range_up_rooted(
db,
&db.expansion_span_map(file_id),
self.value.syntax().text_range(),
)?;
// FIXME: This heuristic is brittle and with the right macro may select completely unrelated nodes?
- let anc = db.parse(file_id).syntax_node().covering_element(range);
+ let anc = db.parse(editioned_file_id).syntax_node().covering_element(range);
let value = anc.ancestors().find_map(N::cast)?;
- Some(InRealFile::new(file_id, value))
+ Some(InRealFile::new(editioned_file_id, value))
}
}
impl<T> InFile<T> {
pub fn into_real_file(self) -> Result<InRealFile<T>, InFile<T>> {
- match self.file_id.repr() {
- HirFileIdRepr::FileId(file_id) => Ok(InRealFile { file_id, value: self.value }),
- HirFileIdRepr::MacroFile(_) => Err(self),
+ match self.file_id {
+ HirFileId::FileId(file_id) => Ok(InRealFile { file_id, value: self.value }),
+ HirFileId::MacroFile(_) => Err(self),
}
}
}