mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-30 13:51:31 +00:00
Remove MacroFileKind
This commit is contained in:
parent
9e551d5452
commit
509fedd9d2
7 changed files with 52 additions and 70 deletions
|
@ -208,7 +208,7 @@ fn partial_ord_expand(
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::{test_db::TestDB, AstId, MacroCallKind, MacroCallLoc, MacroFileKind};
|
||||
use crate::{test_db::TestDB, AstId, MacroCallKind, MacroCallLoc};
|
||||
use ra_db::{fixture::WithFixture, SourceDatabase};
|
||||
|
||||
fn expand_builtin_derive(s: &str, expander: BuiltinDeriveExpander) -> String {
|
||||
|
@ -229,7 +229,7 @@ mod tests {
|
|||
};
|
||||
|
||||
let id = db.intern_macro(loc);
|
||||
let parsed = db.parse_or_expand(id.as_file(MacroFileKind::Items)).unwrap();
|
||||
let parsed = db.parse_or_expand(id.as_file()).unwrap();
|
||||
|
||||
// FIXME text() for syntax nodes parsed from token tree looks weird
|
||||
// because there's no whitespace, see below
|
||||
|
|
|
@ -2,8 +2,7 @@
|
|||
use crate::db::AstDatabase;
|
||||
use crate::{
|
||||
ast::{self, AstNode},
|
||||
name, AstId, CrateId, HirFileId, MacroCallId, MacroDefId, MacroDefKind, MacroFileKind,
|
||||
TextUnit,
|
||||
name, AstId, CrateId, HirFileId, MacroCallId, MacroDefId, MacroDefKind, TextUnit,
|
||||
};
|
||||
|
||||
use crate::quote;
|
||||
|
@ -90,7 +89,7 @@ fn line_expand(
|
|||
let arg = loc.kind.arg(db).ok_or_else(|| mbe::ExpandError::UnexpectedToken)?;
|
||||
let arg_start = arg.text_range().start();
|
||||
|
||||
let file = id.as_file(MacroFileKind::Expr);
|
||||
let file = id.as_file();
|
||||
let line_num = to_line_number(db, file, arg_start);
|
||||
|
||||
let expanded = quote! {
|
||||
|
@ -158,7 +157,7 @@ fn column_expand(
|
|||
let _arg = macro_call.token_tree().ok_or_else(|| mbe::ExpandError::UnexpectedToken)?;
|
||||
let col_start = macro_call.syntax().text_range().start();
|
||||
|
||||
let file = id.as_file(MacroFileKind::Expr);
|
||||
let file = id.as_file();
|
||||
let col_num = to_col_number(db, file, col_start);
|
||||
|
||||
let expanded = quote! {
|
||||
|
@ -269,7 +268,7 @@ mod tests {
|
|||
};
|
||||
|
||||
let id = db.intern_macro(loc);
|
||||
let parsed = db.parse_or_expand(id.as_file(MacroFileKind::Expr)).unwrap();
|
||||
let parsed = db.parse_or_expand(id.as_file()).unwrap();
|
||||
|
||||
parsed.text().to_string()
|
||||
}
|
||||
|
|
|
@ -6,11 +6,11 @@ use mbe::MacroRules;
|
|||
use ra_db::{salsa, SourceDatabase};
|
||||
use ra_parser::FragmentKind;
|
||||
use ra_prof::profile;
|
||||
use ra_syntax::{AstNode, Parse, SyntaxNode};
|
||||
use ra_syntax::{AstNode, Parse, SyntaxKind::*, SyntaxNode};
|
||||
|
||||
use crate::{
|
||||
ast_id_map::AstIdMap, BuiltinDeriveExpander, BuiltinFnLikeExpander, HirFileId, HirFileIdRepr,
|
||||
MacroCallId, MacroCallLoc, MacroDefId, MacroDefKind, MacroFile, MacroFileKind,
|
||||
MacroCallId, MacroCallLoc, MacroDefId, MacroDefKind, MacroFile,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
||||
|
@ -155,11 +155,42 @@ pub(crate) fn parse_macro(
|
|||
})
|
||||
.ok()?;
|
||||
|
||||
let fragment_kind = match macro_file.macro_file_kind {
|
||||
MacroFileKind::Items => FragmentKind::Items,
|
||||
MacroFileKind::Expr => FragmentKind::Expr,
|
||||
MacroFileKind::Statements => FragmentKind::Statements,
|
||||
};
|
||||
let fragment_kind = to_fragment_kind(db, macro_call_id);
|
||||
|
||||
let (parse, rev_token_map) = mbe::token_tree_to_syntax_node(&tt, fragment_kind).ok()?;
|
||||
Some((parse, Arc::new(rev_token_map)))
|
||||
}
|
||||
|
||||
/// Given a `MacroCallId`, return what `FragmentKind` it belongs to.
|
||||
/// FIXME: Not completed
|
||||
fn to_fragment_kind(db: &dyn AstDatabase, macro_call_id: MacroCallId) -> FragmentKind {
|
||||
let syn = db.lookup_intern_macro(macro_call_id).kind.node(db).value;
|
||||
|
||||
let parent = match syn.parent() {
|
||||
Some(it) => it,
|
||||
None => {
|
||||
// FIXME:
|
||||
// If it is root, which means the parent HirFile
|
||||
// MacroKindFile must be non-items
|
||||
// return expr now.
|
||||
return FragmentKind::Expr;
|
||||
}
|
||||
};
|
||||
|
||||
match parent.kind() {
|
||||
MACRO_ITEMS | SOURCE_FILE => FragmentKind::Items,
|
||||
LET_STMT => {
|
||||
// FIXME: Handle Pattern
|
||||
FragmentKind::Expr
|
||||
}
|
||||
EXPR_STMT => FragmentKind::Statements,
|
||||
BLOCK => FragmentKind::Statements,
|
||||
ARG_LIST => FragmentKind::Expr,
|
||||
TRY_EXPR => FragmentKind::Expr,
|
||||
TUPLE_EXPR => FragmentKind::Expr,
|
||||
_ => {
|
||||
// Unknown , Just guess it is `Items`
|
||||
FragmentKind::Items
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -117,14 +117,6 @@ impl HirFileId {
|
|||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct MacroFile {
|
||||
macro_call_id: MacroCallId,
|
||||
macro_file_kind: MacroFileKind,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub enum MacroFileKind {
|
||||
Items,
|
||||
Expr,
|
||||
Statements,
|
||||
}
|
||||
|
||||
/// `MacroCallId` identifies a particular macro invocation, like
|
||||
|
@ -205,9 +197,8 @@ impl MacroCallKind {
|
|||
}
|
||||
|
||||
impl MacroCallId {
|
||||
pub fn as_file(self, kind: MacroFileKind) -> HirFileId {
|
||||
let macro_file = MacroFile { macro_call_id: self, macro_file_kind: kind };
|
||||
macro_file.into()
|
||||
pub fn as_file(self) -> HirFileId {
|
||||
MacroFile { macro_call_id: self }.into()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue