diff --git a/crates/hir-def/src/macro_expansion_tests/mod.rs b/crates/hir-def/src/macro_expansion_tests/mod.rs index 408d03ff718e..a2d0ba3deb84 100644 --- a/crates/hir-def/src/macro_expansion_tests/mod.rs +++ b/crates/hir-def/src/macro_expansion_tests/mod.rs @@ -22,7 +22,7 @@ use hir_expand::{ db::ExpandDatabase, proc_macro::{ProcMacro, ProcMacroExpander, ProcMacroExpansionError, ProcMacroKind}, span_map::SpanMapRef, - InFile, MacroCallKind, MacroFileId, MacroFileIdExt, + InFile, MacroCallKind, MacroFileId, MacroFileIdExt, MacroKind, }; use intern::Symbol; use itertools::Itertools; @@ -211,7 +211,11 @@ pub fn identity_when_valid(_attr: TokenStream, item: TokenStream) -> TokenStream if let Some(src) = src { if let Some(file_id) = src.file_id.macro_file() { - if file_id.is_attr_macro(&db) || file_id.is_custom_derive(&db) { + if let MacroKind::Derive + | MacroKind::DeriveBuiltIn + | MacroKind::Attr + | MacroKind::AttrBuiltIn = file_id.kind(&db) + { let call = file_id.call_node(&db); let mut show_spans = false; let mut show_ctxt = false; @@ -236,7 +240,7 @@ pub fn identity_when_valid(_attr: TokenStream, item: TokenStream) -> TokenStream for impl_id in def_map[local_id].scope.impls() { let src = impl_id.lookup(&db).source(&db); if let Some(macro_file) = src.file_id.macro_file() { - if macro_file.is_builtin_derive(&db) { + if let MacroKind::DeriveBuiltIn | MacroKind::Derive = macro_file.kind(&db) { let pp = pretty_print_macro_expansion( src.value.syntax().clone(), db.span_map(macro_file.into()).as_ref(), diff --git a/crates/hir-expand/src/files.rs b/crates/hir-expand/src/files.rs index 13ddb0d4acce..f3bcc7726822 100644 --- a/crates/hir-expand/src/files.rs +++ b/crates/hir-expand/src/files.rs @@ -10,7 +10,7 @@ use syntax::{AstNode, AstPtr, SyntaxNode, SyntaxNodePtr, SyntaxToken, TextRange, use crate::{ db::{self, ExpandDatabase}, - map_node_range_up, map_node_range_up_rooted, span_for_offset, MacroFileIdExt, + map_node_range_up, map_node_range_up_rooted, span_for_offset, MacroFileIdExt, MacroKind, }; /// `InFile` stores a value of `T` inside a particular file/syntax tree. @@ -276,7 +276,11 @@ impl> InFile { HirFileIdRepr::FileId(file_id) => { return Some(InRealFile { file_id, value: self.value.borrow().clone() }) } - HirFileIdRepr::MacroFile(m) if m.is_attr_macro(db) => m, + HirFileIdRepr::MacroFile(m) + if matches!(m.kind(db), MacroKind::Attr | MacroKind::AttrBuiltIn) => + { + m + } _ => return None, }; @@ -453,7 +457,7 @@ impl InFile { } HirFileIdRepr::MacroFile(m) => m, }; - if !file_id.is_attr_macro(db) { + if !matches!(file_id.kind(db), MacroKind::Attr | MacroKind::AttrBuiltIn) { return None; } diff --git a/crates/hir-expand/src/lib.rs b/crates/hir-expand/src/lib.rs index 2c664029f615..41603e3c92e3 100644 --- a/crates/hir-expand/src/lib.rs +++ b/crates/hir-expand/src/lib.rs @@ -416,6 +416,24 @@ impl HirFileIdExt for HirFileId { } } +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum MacroKind { + /// `macro_rules!` or Macros 2.0 macro. + Declarative, + /// A built-in function-like macro. + DeclarativeBuiltIn, + /// A custom derive. + Derive, + /// A builtin-in derive. + DeriveBuiltIn, + /// A procedural attribute macro. + Attr, + /// A built-in attribute macro. + AttrBuiltIn, + /// A function-like procedural macro. + ProcMacro, +} + pub trait MacroFileIdExt { fn is_env_or_option_env(&self, db: &dyn ExpandDatabase) -> bool; fn is_include_like_macro(&self, db: &dyn ExpandDatabase) -> bool; @@ -427,15 +445,12 @@ pub trait MacroFileIdExt { fn expansion_info(self, db: &dyn ExpandDatabase) -> ExpansionInfo; - fn is_builtin_derive(&self, db: &dyn ExpandDatabase) -> bool; - fn is_custom_derive(&self, db: &dyn ExpandDatabase) -> bool; + fn kind(&self, db: &dyn ExpandDatabase) -> MacroKind; /// Return whether this file is an include macro fn is_include_macro(&self, db: &dyn ExpandDatabase) -> bool; fn is_eager(&self, db: &dyn ExpandDatabase) -> bool; - /// Return whether this file is an attr macro - fn is_attr_macro(&self, db: &dyn ExpandDatabase) -> bool; /// Return whether this file is the pseudo expansion of the derive attribute. /// See [`crate::builtin_attr_macro::derive_attr_expand`]. @@ -468,18 +483,18 @@ impl MacroFileIdExt for MacroFileId { ExpansionInfo::new(db, self) } - fn is_custom_derive(&self, db: &dyn ExpandDatabase) -> bool { - matches!( - db.lookup_intern_macro_call(self.macro_call_id).def.kind, - MacroDefKind::ProcMacro(_, _, ProcMacroKind::CustomDerive) - ) - } - - fn is_builtin_derive(&self, db: &dyn ExpandDatabase) -> bool { - matches!( - db.lookup_intern_macro_call(self.macro_call_id).def.kind, - MacroDefKind::BuiltInDerive(..) - ) + fn kind(&self, db: &dyn ExpandDatabase) -> MacroKind { + match db.lookup_intern_macro_call(self.macro_call_id).def.kind { + MacroDefKind::Declarative(..) => MacroKind::Declarative, + MacroDefKind::BuiltIn(..) | MacroDefKind::BuiltInEager(..) => { + MacroKind::DeclarativeBuiltIn + } + MacroDefKind::BuiltInDerive(..) => MacroKind::DeriveBuiltIn, + MacroDefKind::ProcMacro(_, _, ProcMacroKind::CustomDerive) => MacroKind::Derive, + MacroDefKind::ProcMacro(_, _, ProcMacroKind::Attr) => MacroKind::Attr, + MacroDefKind::ProcMacro(_, _, ProcMacroKind::Bang) => MacroKind::ProcMacro, + MacroDefKind::BuiltInAttr(..) => MacroKind::AttrBuiltIn, + } } fn is_include_macro(&self, db: &dyn ExpandDatabase) -> bool { @@ -507,13 +522,6 @@ impl MacroFileIdExt for MacroFileId { } } - fn is_attr_macro(&self, db: &dyn ExpandDatabase) -> bool { - matches!( - db.lookup_intern_macro_call(self.macro_call_id).def.kind, - MacroDefKind::BuiltInAttr(..) | MacroDefKind::ProcMacro(_, _, ProcMacroKind::Attr) - ) - } - fn is_derive_attr_pseudo_expansion(&self, db: &dyn ExpandDatabase) -> bool { let loc = db.lookup_intern_macro_call(self.macro_call_id); loc.def.is_attribute_derive() diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 727d31cffb51..55448d4ae868 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -142,7 +142,7 @@ pub use { name::Name, prettify_macro_expansion, proc_macro::{ProcMacros, ProcMacrosBuilder}, - tt, ExpandResult, HirFileId, HirFileIdExt, MacroFileId, MacroFileIdExt, + tt, ExpandResult, HirFileId, HirFileIdExt, MacroFileId, MacroFileIdExt, MacroKind, }, hir_ty::{ consteval::ConstEvalError, @@ -699,7 +699,10 @@ impl Module { let source_map = tree_source_maps.impl_(loc.id.value).item(); let node = &tree[loc.id.value]; let file_id = loc.id.file_id(); - if file_id.macro_file().is_some_and(|it| it.is_builtin_derive(db.upcast())) { + if file_id + .macro_file() + .is_some_and(|it| it.kind(db.upcast()) == MacroKind::DeriveBuiltIn) + { // these expansion come from us, diagnosing them is a waste of resources // FIXME: Once we diagnose the inputs to builtin derives, we should at least extract those diagnostics somehow continue; @@ -3049,20 +3052,6 @@ impl BuiltinType { } } -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub enum MacroKind { - /// `macro_rules!` or Macros 2.0 macro. - Declarative, - /// A built-in or custom derive. - Derive, - /// A built-in function-like macro. - BuiltIn, - /// A procedural attribute macro. - Attr, - /// A function-like procedural macro. - ProcMacro, -} - #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct Macro { pub(crate) id: MacroId, @@ -3093,15 +3082,19 @@ impl Macro { match self.id { MacroId::Macro2Id(it) => match it.lookup(db.upcast()).expander { MacroExpander::Declarative => MacroKind::Declarative, - MacroExpander::BuiltIn(_) | MacroExpander::BuiltInEager(_) => MacroKind::BuiltIn, - MacroExpander::BuiltInAttr(_) => MacroKind::Attr, - MacroExpander::BuiltInDerive(_) => MacroKind::Derive, + MacroExpander::BuiltIn(_) | MacroExpander::BuiltInEager(_) => { + MacroKind::DeclarativeBuiltIn + } + MacroExpander::BuiltInAttr(_) => MacroKind::AttrBuiltIn, + MacroExpander::BuiltInDerive(_) => MacroKind::DeriveBuiltIn, }, MacroId::MacroRulesId(it) => match it.lookup(db.upcast()).expander { MacroExpander::Declarative => MacroKind::Declarative, - MacroExpander::BuiltIn(_) | MacroExpander::BuiltInEager(_) => MacroKind::BuiltIn, - MacroExpander::BuiltInAttr(_) => MacroKind::Attr, - MacroExpander::BuiltInDerive(_) => MacroKind::Derive, + MacroExpander::BuiltIn(_) | MacroExpander::BuiltInEager(_) => { + MacroKind::DeclarativeBuiltIn + } + MacroExpander::BuiltInAttr(_) => MacroKind::AttrBuiltIn, + MacroExpander::BuiltInDerive(_) => MacroKind::DeriveBuiltIn, }, MacroId::ProcMacroId(it) => match it.lookup(db.upcast()).kind { ProcMacroKind::CustomDerive => MacroKind::Derive, @@ -3112,10 +3105,10 @@ impl Macro { } pub fn is_fn_like(&self, db: &dyn HirDatabase) -> bool { - match self.kind(db) { - MacroKind::Declarative | MacroKind::BuiltIn | MacroKind::ProcMacro => true, - MacroKind::Attr | MacroKind::Derive => false, - } + matches!( + self.kind(db), + MacroKind::Declarative | MacroKind::DeclarativeBuiltIn | MacroKind::ProcMacro + ) } pub fn is_builtin_derive(&self, db: &dyn HirDatabase) -> bool { @@ -3155,11 +3148,11 @@ impl Macro { } pub fn is_attr(&self, db: &dyn HirDatabase) -> bool { - matches!(self.kind(db), MacroKind::Attr) + matches!(self.kind(db), MacroKind::Attr | MacroKind::AttrBuiltIn) } pub fn is_derive(&self, db: &dyn HirDatabase) -> bool { - matches!(self.kind(db), MacroKind::Derive) + matches!(self.kind(db), MacroKind::Derive | MacroKind::DeriveBuiltIn) } } diff --git a/crates/hir/src/semantics.rs b/crates/hir/src/semantics.rs index c9145f7d212d..bb67fa63a1d1 100644 --- a/crates/hir/src/semantics.rs +++ b/crates/hir/src/semantics.rs @@ -508,9 +508,7 @@ impl<'db> SemanticsImpl<'db> { }) } - pub fn is_derive_annotated(&self, adt: &ast::Adt) -> bool { - let file_id = self.find_file(adt.syntax()).file_id; - let adt = InFile::new(file_id, adt); + pub fn is_derive_annotated(&self, adt: InFile<&ast::Adt>) -> bool { self.with_ctx(|ctx| ctx.has_derives(adt)) } @@ -551,10 +549,8 @@ impl<'db> SemanticsImpl<'db> { res.is_empty().not().then_some(res) } - pub fn is_attr_macro_call(&self, item: &ast::Item) -> bool { - let file_id = self.find_file(item.syntax()).file_id; - let src = InFile::new(file_id, item); - self.with_ctx(|ctx| ctx.item_to_macro_call(src).is_some()) + pub fn is_attr_macro_call(&self, item: InFile<&ast::Item>) -> bool { + self.with_ctx(|ctx| ctx.item_to_macro_call(item).is_some()) } /// Expand the macro call with a different token tree, mapping the `token_to_map` down into the @@ -1526,8 +1522,13 @@ impl<'db> SemanticsImpl<'db> { self.analyze(field.syntax())?.resolve_record_pat_field(self.db, field) } + // FIXME: Replace this with `resolve_macro_call2` pub fn resolve_macro_call(&self, macro_call: &ast::MacroCall) -> Option { let macro_call = self.find_file(macro_call.syntax()).with_value(macro_call); + self.resolve_macro_call2(macro_call) + } + + pub fn resolve_macro_call2(&self, macro_call: InFile<&ast::MacroCall>) -> Option { self.with_ctx(|ctx| { ctx.macro_call_to_macro_call(macro_call) .and_then(|call| macro_call_to_macro_id(ctx, call)) @@ -1538,8 +1539,8 @@ impl<'db> SemanticsImpl<'db> { }) } - pub fn is_proc_macro_call(&self, macro_call: &ast::MacroCall) -> bool { - self.resolve_macro_call(macro_call) + pub fn is_proc_macro_call(&self, macro_call: InFile<&ast::MacroCall>) -> bool { + self.resolve_macro_call2(macro_call) .is_some_and(|m| matches!(m.id, MacroId::ProcMacroId(..))) } diff --git a/crates/ide-db/src/lib.rs b/crates/ide-db/src/lib.rs index 3a29232d331f..96115eee6dc2 100644 --- a/crates/ide-db/src/lib.rs +++ b/crates/ide-db/src/lib.rs @@ -252,10 +252,10 @@ pub enum SymbolKind { impl From for SymbolKind { fn from(it: hir::MacroKind) -> Self { match it { - hir::MacroKind::Declarative | hir::MacroKind::BuiltIn => SymbolKind::Macro, + hir::MacroKind::Declarative | hir::MacroKind::DeclarativeBuiltIn => SymbolKind::Macro, hir::MacroKind::ProcMacro => SymbolKind::ProcMacro, - hir::MacroKind::Derive => SymbolKind::Derive, - hir::MacroKind::Attr => SymbolKind::Attribute, + hir::MacroKind::Derive | hir::MacroKind::DeriveBuiltIn => SymbolKind::Derive, + hir::MacroKind::Attr | hir::MacroKind::AttrBuiltIn => SymbolKind::Attribute, } } } diff --git a/crates/ide-db/src/search.rs b/crates/ide-db/src/search.rs index 7963e8ae4f78..02cd8b8bdf51 100644 --- a/crates/ide-db/src/search.rs +++ b/crates/ide-db/src/search.rs @@ -373,7 +373,9 @@ impl Definition { SearchScope::krate(db, module.krate()) } } - hir::MacroKind::BuiltIn => SearchScope::crate_graph(db), + hir::MacroKind::AttrBuiltIn + | hir::MacroKind::DeriveBuiltIn + | hir::MacroKind::DeclarativeBuiltIn => SearchScope::crate_graph(db), hir::MacroKind::Derive | hir::MacroKind::Attr | hir::MacroKind::ProcMacro => { SearchScope::reverse_dependencies(db, module.krate()) } diff --git a/crates/ide/src/doc_links.rs b/crates/ide/src/doc_links.rs index d88f7c5033e0..8d2ca33bf254 100644 --- a/crates/ide/src/doc_links.rs +++ b/crates/ide/src/doc_links.rs @@ -635,12 +635,13 @@ fn filename_and_frag_for_def( } Definition::Macro(mac) => match mac.kind(db) { hir::MacroKind::Declarative - | hir::MacroKind::BuiltIn + | hir::MacroKind::AttrBuiltIn + | hir::MacroKind::DeclarativeBuiltIn | hir::MacroKind::Attr | hir::MacroKind::ProcMacro => { format!("macro.{}.html", mac.name(db).as_str()) } - hir::MacroKind::Derive => { + hir::MacroKind::Derive | hir::MacroKind::DeriveBuiltIn => { format!("derive.{}.html", mac.name(db).as_str()) } }, diff --git a/crates/ide/src/moniker.rs b/crates/ide/src/moniker.rs index 66ea49a98a08..25d12a4c0b4f 100644 --- a/crates/ide/src/moniker.rs +++ b/crates/ide/src/moniker.rs @@ -184,11 +184,11 @@ pub(crate) fn def_to_kind(db: &RootDatabase, def: Definition) -> SymbolInformati match def { Definition::Macro(it) => match it.kind(db) { - MacroKind::Declarative => Macro, - MacroKind::Derive => Attribute, - MacroKind::BuiltIn => Macro, - MacroKind::Attr => Attribute, - MacroKind::ProcMacro => Macro, + MacroKind::Derive + | MacroKind::DeriveBuiltIn + | MacroKind::AttrBuiltIn + | MacroKind::Attr => Attribute, + MacroKind::Declarative | MacroKind::DeclarativeBuiltIn | MacroKind::ProcMacro => Macro, }, Definition::Field(..) | Definition::TupleField(..) => Field, Definition::Module(..) | Definition::Crate(..) => Module, diff --git a/crates/ide/src/syntax_highlighting.rs b/crates/ide/src/syntax_highlighting.rs index 1853e3a34074..519133e3ad18 100644 --- a/crates/ide/src/syntax_highlighting.rs +++ b/crates/ide/src/syntax_highlighting.rs @@ -7,7 +7,6 @@ mod escape; mod format; mod highlight; mod inject; -mod macro_; mod html; #[cfg(test)] @@ -15,14 +14,14 @@ mod tests; use std::ops::ControlFlow; -use hir::{InRealFile, Name, Semantics}; +use hir::{HirFileIdExt, InFile, InRealFile, MacroFileIdExt, MacroKind, Name, Semantics}; use ide_db::{FxHashMap, Ranker, RootDatabase, SymbolKind}; use span::EditionedFileId; use syntax::{ ast::{self, IsString}, AstNode, AstToken, NodeOrToken, SyntaxKind::*, - SyntaxNode, TextRange, WalkEvent, T, + SyntaxNode, SyntaxToken, TextRange, WalkEvent, T, }; use crate::{ @@ -30,7 +29,6 @@ use crate::{ escape::{highlight_escape_byte, highlight_escape_char, highlight_escape_string}, format::highlight_format_string, highlights::Highlights, - macro_::MacroHighlighter, tags::Highlight, }, FileId, HlMod, HlOperator, HlPunct, HlTag, @@ -221,7 +219,7 @@ pub(crate) fn highlight( Some(it) => it.krate(), None => return hl.to_vec(), }; - traverse(&mut hl, &sema, config, file_id, &root, krate, range_to_highlight); + traverse(&mut hl, &sema, config, InRealFile::new(file_id, &root), krate, range_to_highlight); hl.to_vec() } @@ -229,8 +227,7 @@ fn traverse( hl: &mut Highlights, sema: &Semantics<'_, RootDatabase>, config: HighlightConfig, - file_id: EditionedFileId, - root: &SyntaxNode, + InRealFile { file_id, value: root }: InRealFile<&SyntaxNode>, krate: hir::Crate, range_to_highlight: TextRange, ) { @@ -252,18 +249,15 @@ fn traverse( let mut tt_level = 0; let mut attr_or_derive_item = None; - let mut current_macro: Option = None; - let mut macro_highlighter = MacroHighlighter::default(); // FIXME: these are not perfectly accurate, we determine them by the real file's syntax tree // an attribute nested in a macro call will not emit `inside_attribute` let mut inside_attribute = false; - let mut inside_macro_call = false; - let mut inside_proc_macro_call = false; // Walk all nodes, keeping track of whether we are inside a macro or not. // If in macro, expand it first and highlight the expanded code. - for event in root.preorder_with_tokens() { + let mut preorder = root.preorder_with_tokens(); + while let Some(event) = preorder.next() { use WalkEvent::{Enter, Leave}; let range = match &event { @@ -275,16 +269,11 @@ fn traverse( continue; } - // set macro and attribute highlighting states match event.clone() { - Enter(NodeOrToken::Node(node)) - if current_macro.is_none() && ast::TokenTree::can_cast(node.kind()) => - { + Enter(NodeOrToken::Node(node)) if ast::TokenTree::can_cast(node.kind()) => { tt_level += 1; } - Leave(NodeOrToken::Node(node)) - if current_macro.is_none() && ast::TokenTree::can_cast(node.kind()) => - { + Leave(NodeOrToken::Node(node)) if ast::TokenTree::can_cast(node.kind()) => { tt_level -= 1; } Enter(NodeOrToken::Node(node)) if ast::Attr::can_cast(node.kind()) => { @@ -297,28 +286,14 @@ fn traverse( Enter(NodeOrToken::Node(node)) => { if let Some(item) = ast::Item::cast(node.clone()) { match item { - ast::Item::MacroRules(mac) => { - macro_highlighter.init(); - current_macro = Some(mac.into()); - continue; - } - ast::Item::MacroDef(mac) => { - macro_highlighter.init(); - current_macro = Some(mac.into()); - continue; - } ast::Item::Fn(_) | ast::Item::Const(_) | ast::Item::Static(_) => { bindings_shadow_count.clear() } - ast::Item::MacroCall(ref macro_call) => { - inside_macro_call = true; - inside_proc_macro_call = sema.is_proc_macro_call(macro_call); - } _ => (), } if attr_or_derive_item.is_none() { - if sema.is_attr_macro_call(&item) { + if sema.is_attr_macro_call(InFile::new(file_id.into(), &item)) { attr_or_derive_item = Some(AttrOrDerive::Attr(item)); } else { let adt = match item { @@ -328,7 +303,10 @@ fn traverse( _ => None, }; match adt { - Some(adt) if sema.is_derive_annotated(&adt) => { + Some(adt) + if sema + .is_derive_annotated(InFile::new(file_id.into(), &adt)) => + { attr_or_derive_item = Some(AttrOrDerive::Derive(ast::Item::from(adt))); } @@ -340,25 +318,11 @@ fn traverse( } Leave(NodeOrToken::Node(node)) if ast::Item::can_cast(node.kind()) => { match ast::Item::cast(node.clone()) { - Some(ast::Item::MacroRules(mac)) => { - assert_eq!(current_macro, Some(mac.into())); - current_macro = None; - macro_highlighter = MacroHighlighter::default(); - } - Some(ast::Item::MacroDef(mac)) => { - assert_eq!(current_macro, Some(mac.into())); - current_macro = None; - macro_highlighter = MacroHighlighter::default(); - } Some(item) if attr_or_derive_item.as_ref().is_some_and(|it| *it.item() == item) => { attr_or_derive_item = None; } - Some(ast::Item::MacroCall(_)) => { - inside_macro_call = false; - inside_proc_macro_call = false; - } _ => (), } } @@ -379,12 +343,6 @@ fn traverse( } }; - if current_macro.is_some() { - if let Some(tok) = element.as_token() { - macro_highlighter.advance(tok); - } - } - let element = match element.clone() { NodeOrToken::Node(n) => match ast::NameLike::cast(n) { Some(n) => NodeOrToken::Node(n), @@ -392,7 +350,7 @@ fn traverse( }, NodeOrToken::Token(t) => NodeOrToken::Token(t), }; - let token = element.as_token().cloned(); + let original_token = element.as_token().cloned(); // Descending tokens into macros is expensive even if no descending occurs, so make sure // that we actually are in a position where descending is possible. @@ -405,144 +363,52 @@ fn traverse( let descended_element = if in_macro { // Attempt to descend tokens into macro-calls. - let res = match element { - NodeOrToken::Token(token) if token.kind() != COMMENT => { - let ranker = Ranker::from_token(&token); - - let mut t = None; - let mut r = 0; - sema.descend_into_macros_breakable( - InRealFile::new(file_id, token.clone()), - |tok, _ctx| { - // FIXME: Consider checking ctx transparency for being opaque? - let tok = tok.value; - let my_rank = ranker.rank_token(&tok); - - if my_rank >= Ranker::MAX_RANK { - // a rank of 0b1110 means that we have found a maximally interesting - // token so stop early. - t = Some(tok); - return ControlFlow::Break(()); - } - - // r = r.max(my_rank); - // t = Some(t.take_if(|_| r < my_rank).unwrap_or(tok)); - match &mut t { - Some(prev) if r < my_rank => { - *prev = tok; - r = my_rank; - } - Some(_) => (), - None => { - r = my_rank; - t = Some(tok) - } - } - ControlFlow::Continue(()) - }, - ); - - let token = t.unwrap_or(token); - match token.parent().and_then(ast::NameLike::cast) { - // Remap the token into the wrapping single token nodes - Some(parent) => match (token.kind(), parent.syntax().kind()) { - (T![self] | T![ident], NAME | NAME_REF) => NodeOrToken::Node(parent), - (T![self] | T![super] | T![crate] | T![Self], NAME_REF) => { - NodeOrToken::Node(parent) - } - (INT_NUMBER, NAME_REF) => NodeOrToken::Node(parent), - (LIFETIME_IDENT, LIFETIME) => NodeOrToken::Node(parent), - _ => NodeOrToken::Token(token), - }, - None => NodeOrToken::Token(token), - } - } - e => e, - }; - res + match element { + NodeOrToken::Token(token) => descend_token(sema, InRealFile::new(file_id, token)), + n => InFile::new(file_id.into(), n), + } } else { - element + InFile::new(file_id.into(), element) }; - // FIXME: do proper macro def highlighting https://github.com/rust-lang/rust-analyzer/issues/6232 - // Skip metavariables from being highlighted to prevent keyword highlighting in them - if descended_element.as_token().and_then(|t| macro_highlighter.highlight(t)).is_some() { - continue; + // string highlight injections + if let (Some(original_token), Some(descended_token)) = + (original_token, descended_element.value.as_token()) + { + let control_flow = string_injections( + hl, + sema, + config, + file_id, + krate, + original_token, + descended_token, + ); + if control_flow.is_break() { + continue; + } } - // string highlight injections, note this does not use the descended element as proc-macros - // can rewrite string literals which invalidates our indices - if let (Some(token), Some(descended_token)) = (token, descended_element.as_token()) { - if ast::String::can_cast(token.kind()) && ast::String::can_cast(descended_token.kind()) - { - let string = ast::String::cast(token); - let string_to_highlight = ast::String::cast(descended_token.clone()); - if let Some((string, expanded_string)) = string.zip(string_to_highlight) { - if string.is_raw() - && inject::ra_fixture(hl, sema, config, &string, &expanded_string).is_some() - { - continue; - } - highlight_format_string( - hl, - sema, - krate, - &string, - &expanded_string, - range, - file_id.edition(), - ); - - if !string.is_raw() { - highlight_escape_string(hl, &string, range.start()); - } - } - } else if ast::ByteString::can_cast(token.kind()) - && ast::ByteString::can_cast(descended_token.kind()) - { - if let Some(byte_string) = ast::ByteString::cast(token) { - if !byte_string.is_raw() { - highlight_escape_string(hl, &byte_string, range.start()); - } + let edition = descended_element.file_id.edition(sema.db); + let element = match descended_element.value { + NodeOrToken::Node(name_like) => { + let hl = highlight::name_like( + sema, + krate, + &mut bindings_shadow_count, + config.syntactic_name_ref_highlighting, + name_like, + edition, + ); + if hl.is_some() && !in_macro { + // skip highlighting the contained token of our name-like node + // as that would potentially overwrite our result + preorder.skip_subtree(); } - } else if ast::CString::can_cast(token.kind()) - && ast::CString::can_cast(descended_token.kind()) - { - if let Some(c_string) = ast::CString::cast(token) { - if !c_string.is_raw() { - highlight_escape_string(hl, &c_string, range.start()); - } - } - } else if ast::Char::can_cast(token.kind()) - && ast::Char::can_cast(descended_token.kind()) - { - let Some(char) = ast::Char::cast(token) else { - continue; - }; - - highlight_escape_char(hl, &char, range.start()) - } else if ast::Byte::can_cast(token.kind()) - && ast::Byte::can_cast(descended_token.kind()) - { - let Some(byte) = ast::Byte::cast(token) else { - continue; - }; - - highlight_escape_byte(hl, &byte, range.start()) + hl } - } - - let element = match descended_element { - NodeOrToken::Node(name_like) => highlight::name_like( - sema, - krate, - &mut bindings_shadow_count, - config.syntactic_name_ref_highlighting, - name_like, - file_id.edition(), - ), NodeOrToken::Token(token) => { - highlight::token(sema, token, file_id.edition()).zip(Some(None)) + highlight::token(sema, token, edition, tt_level > 0).zip(Some(None)) } }; if let Some((mut highlight, binding_hash)) = element { @@ -551,13 +417,6 @@ fn traverse( // let the editor do its highlighting for these tokens instead continue; } - if highlight.tag == HlTag::UnresolvedReference - && matches!(attr_or_derive_item, Some(AttrOrDerive::Derive(_)) if inside_attribute) - { - // do not emit unresolved references in derive helpers if the token mapping maps to - // something unresolvable. FIXME: There should be a way to prevent that - continue; - } // apply config filtering if !filter_by_config(&mut highlight, config) { @@ -567,8 +426,9 @@ fn traverse( if inside_attribute { highlight |= HlMod::Attribute } - if inside_macro_call && tt_level > 0 { - if inside_proc_macro_call { + if let Some(m) = descended_element.file_id.macro_file() { + if let MacroKind::ProcMacro | MacroKind::Attr | MacroKind::Derive = m.kind(sema.db) + { highlight |= HlMod::ProcMacro } highlight |= HlMod::Macro @@ -579,6 +439,99 @@ fn traverse( } } +fn string_injections( + hl: &mut Highlights, + sema: &Semantics<'_, RootDatabase>, + config: HighlightConfig, + file_id: EditionedFileId, + krate: hir::Crate, + token: SyntaxToken, + descended_token: &SyntaxToken, +) -> ControlFlow<()> { + if !matches!(token.kind(), STRING | BYTE_STRING | BYTE | CHAR | C_STRING) { + return ControlFlow::Continue(()); + } + if let Some(string) = ast::String::cast(token.clone()) { + if let Some(descended_string) = ast::String::cast(descended_token.clone()) { + if string.is_raw() + && inject::ra_fixture(hl, sema, config, &string, &descended_string).is_some() + { + return ControlFlow::Break(()); + } + highlight_format_string(hl, sema, krate, &string, &descended_string, file_id.edition()); + + if !string.is_raw() { + highlight_escape_string(hl, &string); + } + } + } else if let Some(byte_string) = ast::ByteString::cast(token.clone()) { + if !byte_string.is_raw() { + highlight_escape_string(hl, &byte_string); + } + } else if let Some(c_string) = ast::CString::cast(token.clone()) { + if !c_string.is_raw() { + highlight_escape_string(hl, &c_string); + } + } else if let Some(char) = ast::Char::cast(token.clone()) { + highlight_escape_char(hl, &char) + } else if let Some(byte) = ast::Byte::cast(token) { + highlight_escape_byte(hl, &byte) + } + ControlFlow::Continue(()) +} + +fn descend_token( + sema: &Semantics<'_, RootDatabase>, + token: InRealFile, +) -> InFile> { + if token.value.kind() == COMMENT { + return token.map(NodeOrToken::Token).into(); + } + let ranker = Ranker::from_token(&token.value); + + let mut t = None; + let mut r = 0; + sema.descend_into_macros_breakable(token.clone(), |tok, _ctx| { + // FIXME: Consider checking ctx transparency for being opaque? + let my_rank = ranker.rank_token(&tok.value); + + if my_rank >= Ranker::MAX_RANK { + // a rank of 0b1110 means that we have found a maximally interesting + // token so stop early. + t = Some(tok); + return ControlFlow::Break(()); + } + + // r = r.max(my_rank); + // t = Some(t.take_if(|_| r < my_rank).unwrap_or(tok)); + match &mut t { + Some(prev) if r < my_rank => { + *prev = tok; + r = my_rank; + } + Some(_) => (), + None => { + r = my_rank; + t = Some(tok) + } + } + ControlFlow::Continue(()) + }); + + let token = t.unwrap_or_else(|| token.into()); + token.map(|token| match token.parent().and_then(ast::NameLike::cast) { + // Remap the token into the wrapping single token nodes + Some(parent) => match (token.kind(), parent.syntax().kind()) { + (T![ident] | T![self], NAME) + | (T![ident] | T![self] | T![super] | T![crate] | T![Self], NAME_REF) + | (INT_NUMBER, NAME_REF) + | (LIFETIME_IDENT, LIFETIME) => NodeOrToken::Node(parent), + _ => NodeOrToken::Token(token), + }, + None => NodeOrToken::Token(token), + }) +} + fn filter_by_config(highlight: &mut Highlight, config: HighlightConfig) -> bool { match &mut highlight.tag { HlTag::StringLiteral if !config.strings => return false, diff --git a/crates/ide/src/syntax_highlighting/escape.rs b/crates/ide/src/syntax_highlighting/escape.rs index 552ce9cd8c3a..094f88f3a864 100644 --- a/crates/ide/src/syntax_highlighting/escape.rs +++ b/crates/ide/src/syntax_highlighting/escape.rs @@ -4,12 +4,9 @@ use crate::{HlRange, HlTag}; use syntax::ast::{Byte, Char, IsString}; use syntax::{AstToken, TextRange, TextSize}; -pub(super) fn highlight_escape_string( - stack: &mut Highlights, - string: &T, - start: TextSize, -) { +pub(super) fn highlight_escape_string(stack: &mut Highlights, string: &T) { let text = string.text(); + let start = string.syntax().text_range().start(); string.escaped_char_ranges(&mut |piece_range, char| { if text[piece_range.start().into()..].starts_with('\\') { let highlight = match char { @@ -25,7 +22,7 @@ pub(super) fn highlight_escape_string( }); } -pub(super) fn highlight_escape_char(stack: &mut Highlights, char: &Char, start: TextSize) { +pub(super) fn highlight_escape_char(stack: &mut Highlights, char: &Char) { if char.value().is_err() { // We do not emit invalid escapes highlighting here. The lexer would likely be in a bad // state and this token contains junk, since `'` is not a reliable delimiter (consider @@ -42,11 +39,14 @@ pub(super) fn highlight_escape_char(stack: &mut Highlights, char: &Char, start: return; }; - let range = TextRange::at(start + TextSize::from(1), TextSize::from(text.len() as u32)); + let range = TextRange::at( + char.syntax().text_range().start() + TextSize::from(1), + TextSize::from(text.len() as u32), + ); stack.add(HlRange { range, highlight: HlTag::EscapeSequence.into(), binding_hash: None }) } -pub(super) fn highlight_escape_byte(stack: &mut Highlights, byte: &Byte, start: TextSize) { +pub(super) fn highlight_escape_byte(stack: &mut Highlights, byte: &Byte) { if byte.value().is_err() { // See `highlight_escape_char` for why no error highlighting here. return; @@ -61,6 +61,9 @@ pub(super) fn highlight_escape_byte(stack: &mut Highlights, byte: &Byte, start: return; }; - let range = TextRange::at(start + TextSize::from(2), TextSize::from(text.len() as u32)); + let range = TextRange::at( + byte.syntax().text_range().start() + TextSize::from(2), + TextSize::from(text.len() as u32), + ); stack.add(HlRange { range, highlight: HlTag::EscapeSequence.into(), binding_hash: None }) } diff --git a/crates/ide/src/syntax_highlighting/format.rs b/crates/ide/src/syntax_highlighting/format.rs index 43a6bdad7e9b..c63043621c27 100644 --- a/crates/ide/src/syntax_highlighting/format.rs +++ b/crates/ide/src/syntax_highlighting/format.rs @@ -5,7 +5,7 @@ use ide_db::{ SymbolKind, }; use span::Edition; -use syntax::{ast, TextRange}; +use syntax::{ast, AstToken}; use crate::{ syntax_highlighting::{highlight::highlight_def, highlights::Highlights}, @@ -18,15 +18,15 @@ pub(super) fn highlight_format_string( krate: hir::Crate, string: &ast::String, expanded_string: &ast::String, - range: TextRange, edition: Edition, ) { if is_format_string(expanded_string) { + let start = string.syntax().text_range().start(); // FIXME: Replace this with the HIR info we have now. lex_format_specifiers(string, &mut |piece_range, kind| { if let Some(highlight) = highlight_format_specifier(kind) { stack.add(HlRange { - range: piece_range + range.start(), + range: piece_range + start, highlight: highlight.into(), binding_hash: None, }); diff --git a/crates/ide/src/syntax_highlighting/highlight.rs b/crates/ide/src/syntax_highlighting/highlight.rs index 194fde116011..127861a04bd8 100644 --- a/crates/ide/src/syntax_highlighting/highlight.rs +++ b/crates/ide/src/syntax_highlighting/highlight.rs @@ -23,6 +23,7 @@ pub(super) fn token( sema: &Semantics<'_, RootDatabase>, token: SyntaxToken, edition: Edition, + in_tt: bool, ) -> Option { if let Some(comment) = ast::Comment::cast(token.clone()) { let h = HlTag::Comment; @@ -40,13 +41,20 @@ pub(super) fn token( INT_NUMBER | FLOAT_NUMBER => HlTag::NumericLiteral.into(), BYTE => HlTag::ByteLiteral.into(), CHAR => HlTag::CharLiteral.into(), - IDENT if token.parent().and_then(ast::TokenTree::cast).is_some() => { + IDENT if in_tt => { // from this point on we are inside a token tree, this only happens for identifiers // that were not mapped down into macro invocations HlTag::None.into() } p if p.is_punct() => punctuation(sema, token, p), - k if k.is_keyword(edition) => keyword(sema, token, k)?, + k if k.is_keyword(edition) => { + if in_tt && token.prev_token().is_some_and(|t| t.kind() == T![$]) { + // we are likely within a macro definition where our keyword is a fragment name + HlTag::None.into() + } else { + keyword(sema, token, k)? + } + } _ => return None, }; Some(highlight) @@ -81,7 +89,7 @@ pub(super) fn name_like( Some(IdentClass::NameRefClass(NameRefClass::Definition(def, _))) => { highlight_def(sema, krate, def, edition) } - // FIXME: Fallback for 'static and '_, as we do not resolve these yet + // FIXME: Fallback for '_, as we do not resolve these yet _ => SymbolKind::LifetimeParam.into(), }, }; @@ -214,12 +222,6 @@ fn keyword( T![true] | T![false] => HlTag::BoolLiteral.into(), // crate is handled just as a token if it's in an `extern crate` T![crate] if parent_matches::(&token) => h, - // self, crate, super and `Self` are handled as either a Name or NameRef already, unless they - // are inside unmapped token trees - T![self] | T![crate] | T![super] | T![Self] if parent_matches::(&token) => { - return None - } - T![self] if parent_matches::(&token) => return None, T![ref] => match token.parent().and_then(ast::IdentPat::cast) { Some(ident) if sema.is_unsafe_ident_pat(&ident) => h | HlMod::Unsafe, _ => h, diff --git a/crates/ide/src/syntax_highlighting/macro_.rs b/crates/ide/src/syntax_highlighting/macro_.rs deleted file mode 100644 index b441b4cc90eb..000000000000 --- a/crates/ide/src/syntax_highlighting/macro_.rs +++ /dev/null @@ -1,128 +0,0 @@ -//! Syntax highlighting for macro_rules!. -use syntax::{SyntaxKind, SyntaxToken, TextRange, T}; - -use crate::{HlRange, HlTag}; - -#[derive(Default)] -pub(super) struct MacroHighlighter { - state: Option, -} - -impl MacroHighlighter { - pub(super) fn init(&mut self) { - self.state = Some(MacroMatcherParseState::default()); - } - - pub(super) fn advance(&mut self, token: &SyntaxToken) { - if let Some(state) = self.state.as_mut() { - update_macro_state(state, token); - } - } - - pub(super) fn highlight(&self, token: &SyntaxToken) -> Option { - if let Some(state) = self.state.as_ref() { - if matches!(state.rule_state, RuleState::Matcher | RuleState::Expander) { - if let Some(range) = is_metavariable(token) { - return Some(HlRange { - range, - highlight: HlTag::UnresolvedReference.into(), - binding_hash: None, - }); - } - } - } - None - } -} - -struct MacroMatcherParseState { - /// Opening and corresponding closing bracket of the matcher or expander of the current rule - paren_ty: Option<(SyntaxKind, SyntaxKind)>, - paren_level: usize, - rule_state: RuleState, - /// Whether we are inside the outer `{` `}` macro block that holds the rules - in_invoc_body: bool, -} - -impl Default for MacroMatcherParseState { - fn default() -> Self { - MacroMatcherParseState { - paren_ty: None, - paren_level: 0, - in_invoc_body: false, - rule_state: RuleState::None, - } - } -} - -#[derive(Copy, Clone, Debug, PartialEq)] -enum RuleState { - Matcher, - Expander, - Between, - None, -} - -impl RuleState { - fn transition(&mut self) { - *self = match self { - RuleState::Matcher => RuleState::Between, - RuleState::Expander => RuleState::None, - RuleState::Between => RuleState::Expander, - RuleState::None => RuleState::Matcher, - }; - } -} - -fn update_macro_state(state: &mut MacroMatcherParseState, tok: &SyntaxToken) { - if !state.in_invoc_body { - if tok.kind() == T!['{'] || tok.kind() == T!['('] { - state.in_invoc_body = true; - } - return; - } - - match state.paren_ty { - Some((open, close)) => { - if tok.kind() == open { - state.paren_level += 1; - } else if tok.kind() == close { - state.paren_level -= 1; - if state.paren_level == 0 { - state.rule_state.transition(); - state.paren_ty = None; - } - } - } - None => { - match tok.kind() { - T!['('] => { - state.paren_ty = Some((T!['('], T![')'])); - } - T!['{'] => { - state.paren_ty = Some((T!['{'], T!['}'])); - } - T!['['] => { - state.paren_ty = Some((T!['['], T![']'])); - } - _ => (), - } - if state.paren_ty.is_some() { - state.paren_level = 1; - state.rule_state.transition(); - } - } - } -} - -fn is_metavariable(token: &SyntaxToken) -> Option { - match token.kind() { - kind if kind.is_any_identifier() => { - if let Some(_dollar) = token.prev_token().filter(|t| t.kind() == T![$]) { - return Some(token.text_range()); - } - } - _ => (), - }; - None -} diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_asm.html b/crates/ide/src/syntax_highlighting/test_data/highlight_asm.html index 15a6386aa3c8..2bc22f960bd5 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_asm.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_asm.html @@ -49,26 +49,26 @@ unsafe { let foo = 1; let mut o = 0; - core::arch::asm!( + core::arch::asm!( "%input = OpLoad _ {0}", concat!("%result = ", "bar", " _ %input"), "OpStore {1} %result", in(reg) &foo, in(reg) &mut o, - ); + ); let thread_id: usize; - core::arch::asm!(" + core::arch::asm!(" mov {0}, gs:[0x30] mov {0}, [{0}+0x48] - ", out(reg) thread_id, options(pure, readonly, nostack)); + ", out(reg) thread_id, options(pure, readonly, nostack)); static UNMAP_BASE: usize; const MEM_RELEASE: usize; static VirtualFree: usize; const OffPtr: usize; const OffFn: usize; - core::arch::asm!(" + core::arch::asm!(" push {free_type} push {free_size} push {base} @@ -92,7 +92,7 @@ base = sym UNMAP_BASE, options(noreturn), - ); + ); } } // taken from https://github.com/rust-embedded/cortex-m/blob/47921b51f8b960344fcfa1255a50a0d19efcde6d/cortex-m/src/asm.rs#L254-L274 @@ -101,19 +101,19 @@ // Ensure thumb mode is set. let rv = (rv as u32) | 1; let msp = msp as u32; - core::arch::asm!( + core::arch::asm!( "mrs {tmp}, CONTROL", "bics {tmp}, {spsel}", "msr CONTROL, {tmp}", "isb", "msr MSP, {msp}", "bx {rv}", - // `out(reg) _` is not permitted in a `noreturn` asm! call, - // so instead use `in(reg) 0` and don't restore it afterwards. + // `out(reg) _` is not permitted in a `noreturn` asm! call, + // so instead use `in(reg) 0` and don't restore it afterwards. tmp = in(reg) 0, spsel = in(reg) 2, msp = in(reg) msp, rv = in(reg) rv, options(noreturn, nomem, nostack), - ); + ); } \ No newline at end of file diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_attributes.html b/crates/ide/src/syntax_highlighting/test_data/highlight_attributes.html index 485d44f97e16..e1d51dc0b71e 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_attributes.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_attributes.html @@ -45,20 +45,20 @@ .invalid_escape_sequence { color: #FC5555; text-decoration: wavy underline; } .unresolved_reference { color: #FC5555; text-decoration: wavy underline; } -
#[allow(dead_code)]
-#[rustfmt::skip]
+
#[allow(dead_code)]
+#[rustfmt::skip]
 #[proc_macros::identity]
-#[derive(Default)]
+#[derive(Default)]
 /// This is a doc comment
 // This is a normal comment
 /// This is a doc comment
-#[derive(Copy)]
+#[derive(Copy)]
 // This is another normal comment
 /// This is another doc comment
 // This is another normal comment
-#[derive(Copy, Unresolved)]
+#[derive(Copy, Unresolved)]
 // The reason for these being here is to test AttrIds
-enum Foo {
-    #[default]
-    Bar
-}
\ No newline at end of file +enum Foo { + #[default] + Bar +}
\ No newline at end of file diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_block_mod_items.html b/crates/ide/src/syntax_highlighting/test_data/highlight_block_mod_items.html index c6eab90e42b0..af29af3f03ca 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_block_mod_items.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_block_mod_items.html @@ -53,9 +53,9 @@ }; } fn main() { - foo!(Foo); + foo!(Foo); mod module { - foo!(Bar); + foo!(Bar); fn func(_: y::Bar) { mod inner { struct Innerest<const C: usize> { field: [(); {C}] } diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_const.html b/crates/ide/src/syntax_highlighting/test_data/highlight_const.html index 96cdb532dd54..6d8f6b3c6e32 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_const.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_const.html @@ -57,7 +57,7 @@ const { const || {} } - id!( + id!( CONST_ITEM; CONST_PARAM; const { @@ -65,7 +65,7 @@ }; &raw const (); const - ); + ); ().assoc_const_method(); } trait ConstTrait { diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html b/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html index eb77c14c2a57..263e4545fb5a 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html @@ -105,7 +105,7 @@ /// let foo = Foo::new(); /// /// // calls bar on foo - /// assert!(foo.bar()); + /// assert!(foo.bar()); /// /// let bar = foo.bar || Foo::bar; /// @@ -156,8 +156,8 @@ } /// ``` -/// macro_rules! noop { ($expr:expr) => { $expr }} -/// noop!(1); +/// macro_rules! noop { ($expr:expr) => { $expr }} +/// noop!(1); /// ``` macro_rules! noop { ($expr:expr) => { @@ -177,7 +177,7 @@ /// #[cfg_attr(feature = "alloc", doc = "```rust")] #[cfg_attr(not(feature = "alloc"), doc = "```ignore")] -/// let _ = example(&alloc::vec![1, 2, 3]); +/// let _ = example(&alloc::vec![1, 2, 3]); /// ``` pub fn mix_and_match() {} diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_general.html b/crates/ide/src/syntax_highlighting/test_data/highlight_general.html index 9477d0d1b877..eb532a5639d4 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_general.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_general.html @@ -78,7 +78,7 @@ use self::FooCopy::{self as BarCopy}; -#[derive(Copy)] +#[derive(Copy)] struct FooCopy { x: u32, } @@ -151,7 +151,7 @@ let bar = Foo::baz; let baz = (-42,); - let baz = -baz.0; + let baz = -baz.0; let _ = !true; @@ -170,7 +170,7 @@ impl<T> Option<T> { fn and<U>(self, other: Option<U>) -> Option<(T, U)> { match other { - None => unimplemented!(), + None => unimplemented!(), Nope => Nope, } } @@ -184,7 +184,7 @@ async fn async_main() { let f1 = learn_and_sing(); let f2 = dance(); - futures::join!(f1, f2); + futures::join!(f1, f2); } fn use_foo_items() { @@ -196,7 +196,7 @@ let control_flow = foo::identity(foo::ControlFlow::Continue); if control_flow.should_die() { - foo::die!(); + foo::die!(); } } diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_injection.html b/crates/ide/src/syntax_highlighting/test_data/highlight_injection.html index 5fbed35192bb..1f9422161de4 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_injection.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_injection.html @@ -51,7 +51,7 @@ fixture(r#" trait Foo { fn foo() { - println!("2 + 2 = {}", 4); + println!("2 + 2 = {}", 4); } }"# ); diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_issue_18089.html b/crates/ide/src/syntax_highlighting/test_data/highlight_issue_18089.html index 06817af1b1f2..a846addba3f2 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_issue_18089.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_issue_18089.html @@ -46,8 +46,8 @@ .unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
fn main() {
-    template!(template);
+    template!(template);
 }
 
 #[proc_macros::issue_18089]
-fn template() {}
\ No newline at end of file +fn template() {} \ No newline at end of file diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_keywords_2015.html b/crates/ide/src/syntax_highlighting/test_data/highlight_keywords_2015.html index 2d3407dbcda0..c3377614d729 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_keywords_2015.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_keywords_2015.html @@ -54,21 +54,21 @@ } macro_rules! void { - ($($tt:tt)*) => {} + ($($tt:tt)*) => {discard!($($tt:tt)*)} } struct __ where Self:; fn __(_: Self) {} -void!(Self); +void!(Self); // edition dependent -void!(try async await gen); +void!(try async await gen); // edition and context dependent -void!(dyn); +void!(dyn); // builtin custom syntax -void!(builtin offset_of format_args asm); +void!(builtin offset_of format_args asm); // contextual -void!(macro_rules, union, default, raw, auto, yeet); +void!(macro_rules, union, default, raw, auto, yeet); // reserved -void!(abstract become box do final macro override priv typeof unsized virtual yield); -void!('static 'self 'unsafe) \ No newline at end of file +void!(abstract become box do final macro override priv typeof unsized virtual yield); +void!('static 'self 'unsafe) \ No newline at end of file diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_keywords_2018.html b/crates/ide/src/syntax_highlighting/test_data/highlight_keywords_2018.html index f8eb5d068a81..9b22500396bd 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_keywords_2018.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_keywords_2018.html @@ -54,21 +54,21 @@ } macro_rules! void { - ($($tt:tt)*) => {} + ($($tt:tt)*) => {discard!($($tt:tt)*)} } struct __ where Self:; fn __(_: Self) {} -void!(Self); +void!(Self); // edition dependent -void!(try async await gen); +void!(try async await gen); // edition and context dependent -void!(dyn); +void!(dyn); // builtin custom syntax -void!(builtin offset_of format_args asm); +void!(builtin offset_of format_args asm); // contextual -void!(macro_rules, union, default, raw, auto, yeet); +void!(macro_rules, union, default, raw, auto, yeet); // reserved -void!(abstract become box do final macro override priv typeof unsized virtual yield); -void!('static 'self 'unsafe) \ No newline at end of file +void!(abstract become box do final macro override priv typeof unsized virtual yield); +void!('static 'self 'unsafe) \ No newline at end of file diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_keywords_2021.html b/crates/ide/src/syntax_highlighting/test_data/highlight_keywords_2021.html index f8eb5d068a81..9b22500396bd 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_keywords_2021.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_keywords_2021.html @@ -54,21 +54,21 @@ } macro_rules! void { - ($($tt:tt)*) => {} + ($($tt:tt)*) => {discard!($($tt:tt)*)} } struct __ where Self:; fn __(_: Self) {} -void!(Self); +void!(Self); // edition dependent -void!(try async await gen); +void!(try async await gen); // edition and context dependent -void!(dyn); +void!(dyn); // builtin custom syntax -void!(builtin offset_of format_args asm); +void!(builtin offset_of format_args asm); // contextual -void!(macro_rules, union, default, raw, auto, yeet); +void!(macro_rules, union, default, raw, auto, yeet); // reserved -void!(abstract become box do final macro override priv typeof unsized virtual yield); -void!('static 'self 'unsafe) \ No newline at end of file +void!(abstract become box do final macro override priv typeof unsized virtual yield); +void!('static 'self 'unsafe) \ No newline at end of file diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_keywords_2024.html b/crates/ide/src/syntax_highlighting/test_data/highlight_keywords_2024.html index fca840170696..ac8353120e87 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_keywords_2024.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_keywords_2024.html @@ -54,21 +54,21 @@ } macro_rules! void { - ($($tt:tt)*) => {} + ($($tt:tt)*) => {discard!($($tt:tt)*)} } struct __ where Self:; fn __(_: Self) {} -void!(Self); +void!(Self); // edition dependent -void!(try async await gen); +void!(try async await gen); // edition and context dependent -void!(dyn); +void!(dyn); // builtin custom syntax -void!(builtin offset_of format_args asm); +void!(builtin offset_of format_args asm); // contextual -void!(macro_rules, union, default, raw, auto, yeet); +void!(macro_rules, union, default, raw, auto, yeet); // reserved -void!(abstract become box do final macro override priv typeof unsized virtual yield); -void!('static 'self 'unsafe) \ No newline at end of file +void!(abstract become box do final macro override priv typeof unsized virtual yield); +void!('static 'self 'unsafe) \ No newline at end of file diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_keywords_macros.html b/crates/ide/src/syntax_highlighting/test_data/highlight_keywords_macros.html new file mode 100644 index 000000000000..694e54d2fa8c --- /dev/null +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_keywords_macros.html @@ -0,0 +1,50 @@ + + +
lib2015::void_2015!(try async await gen);
+lib2024::void_2024!(try async await gen);
+
\ No newline at end of file diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_macros.html b/crates/ide/src/syntax_highlighting/test_data/highlight_macros.html index f640a5e6ca7f..f224435e9618 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_macros.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_macros.html @@ -47,21 +47,21 @@
use proc_macros::{mirror, identity, DeriveIdentity};
 
-mirror! {
+mirror! {
     {
         ,i32 :x pub
         ,i32 :y pub
     } Foo struct
-}
+}
 macro_rules! def_fn {
     ($($tt:tt)*) => {$($tt)*}
 }
 
-def_fn! {
+def_fn! {
     fn bar() -> u32 {
         100
     }
-}
+}
 
 macro_rules! dont_color_me_braces {
     () => {0}
@@ -100,16 +100,16 @@
     };
 }
 
-include!(concat!("foo/", "foo.rs"));
+include!(concat!("foo/", "foo.rs"));
 
 struct S<T>(T);
 fn main() {
     struct TestLocal;
     // regression test, TestLocal here used to not resolve
-    let _: S<id![TestLocal]>;
+    let _: S<id![TestLocal]>;
 
-    format_args!("Hello, {}!", (92,).0);
-    dont_color_me_braces!();
-    noop!(noop!(1));
+    format_args!("Hello, {}!", (92,).0);
+    dont_color_me_braces!();
+    noop!(noop!(1));
 }
 
\ No newline at end of file diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html b/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html index 1794d7dbfe29..539c74f6b57e 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html @@ -99,86 +99,86 @@ let a = b'\xFF'; - println!("Hello {{Hello}}"); + println!("Hello {{Hello}}"); // from https://doc.rust-lang.org/std/fmt/index.html - println!("Hello"); // => "Hello" - println!("Hello, {}!", "world"); // => "Hello, world!" - println!("The number is {}", 1); // => "The number is 1" - println!("{:?}", (3, 4)); // => "(3, 4)" - println!("{value}", value=4); // => "4" - println!("{} {}", 1, 2); // => "1 2" - println!("{:04}", 42); // => "0042" with leading zerosV - println!("{1} {} {0} {}", 1, 2); // => "2 1 1 2" - println!("{argument}", argument = "test"); // => "test" - println!("{name} {}", 1, name = 2); // => "2 1" - println!("{a} {c} {b}", a="a", b='b', c=3); // => "a 3 b" - println!("{{{}}}", 2); // => "{2}" - println!("Hello {:5}!", "x"); - println!("Hello {:1$}!", "x", 5); - println!("Hello {1:0$}!", 5, "x"); - println!("Hello {:width$}!", "x", width = 5); - println!("Hello {:<5}!", "x"); - println!("Hello {:-<5}!", "x"); - println!("Hello {:^5}!", "x"); - println!("Hello {:>5}!", "x"); - println!("Hello {:+}!", 5); - println!("{:#x}!", 27); - println!("Hello {:05}!", 5); - println!("Hello {:05}!", -5); - println!("{:#010x}!", 27); - println!("Hello {0} is {1:.5}", "x", 0.01); - println!("Hello {1} is {2:.0$}", 5, "x", 0.01); - println!("Hello {0} is {2:.1$}", "x", 5, 0.01); - println!("Hello {} is {:.*}", "x", 5, 0.01); - println!("Hello {} is {2:.*}", "x", 5, 0.01); - println!("Hello {} is {number:.prec$}", "x", prec = 5, number = 0.01); - println!("{}, `{name:.*}` has 3 fractional digits", "Hello", 3, name=1234.56); - println!("{}, `{name:.*}` has 3 characters", "Hello", 3, name="1234.56"); - println!("{}, `{name:>8.*}` has 3 right-aligned characters", "Hello", 3, name="1234.56"); + println!("Hello"); // => "Hello" + println!("Hello, {}!", "world"); // => "Hello, world!" + println!("The number is {}", 1); // => "The number is 1" + println!("{:?}", (3, 4)); // => "(3, 4)" + println!("{value}", value=4); // => "4" + println!("{} {}", 1, 2); // => "1 2" + println!("{:04}", 42); // => "0042" with leading zerosV + println!("{1} {} {0} {}", 1, 2); // => "2 1 1 2" + println!("{argument}", argument = "test"); // => "test" + println!("{name} {}", 1, name = 2); // => "2 1" + println!("{a} {c} {b}", a="a", b='b', c=3); // => "a 3 b" + println!("{{{}}}", 2); // => "{2}" + println!("Hello {:5}!", "x"); + println!("Hello {:1$}!", "x", 5); + println!("Hello {1:0$}!", 5, "x"); + println!("Hello {:width$}!", "x", width = 5); + println!("Hello {:<5}!", "x"); + println!("Hello {:-<5}!", "x"); + println!("Hello {:^5}!", "x"); + println!("Hello {:>5}!", "x"); + println!("Hello {:+}!", 5); + println!("{:#x}!", 27); + println!("Hello {:05}!", 5); + println!("Hello {:05}!", -5); + println!("{:#010x}!", 27); + println!("Hello {0} is {1:.5}", "x", 0.01); + println!("Hello {1} is {2:.0$}", 5, "x", 0.01); + println!("Hello {0} is {2:.1$}", "x", 5, 0.01); + println!("Hello {} is {:.*}", "x", 5, 0.01); + println!("Hello {} is {2:.*}", "x", 5, 0.01); + println!("Hello {} is {number:.prec$}", "x", prec = 5, number = 0.01); + println!("{}, `{name:.*}` has 3 fractional digits", "Hello", 3, name=1234.56); + println!("{}, `{name:.*}` has 3 characters", "Hello", 3, name="1234.56"); + println!("{}, `{name:>8.*}` has 3 right-aligned characters", "Hello", 3, name="1234.56"); let _ = "{}" let _ = "{{}}"; - println!("Hello {{}}"); - println!("{{ Hello"); - println!("Hello }}"); - println!("{{Hello}}"); - println!("{{ Hello }}"); - println!("{{Hello }}"); - println!("{{ Hello}}"); + println!("Hello {{}}"); + println!("{{ Hello"); + println!("Hello }}"); + println!("{{Hello}}"); + println!("{{ Hello }}"); + println!("{{Hello }}"); + println!("{{ Hello}}"); - println!(r"Hello, {}!", "world"); + println!(r"Hello, {}!", "world"); // escape sequences - println!("Hello\nWorld"); - println!("\u{48}\x65\x6C\x6C\x6F World"); + println!("Hello\nWorld"); + println!("\u{48}\x65\x6C\x6C\x6F World"); let _ = "\x28\x28\x00\x63\xFF\u{FF}\n"; // invalid non-UTF8 escape sequences let _ = b"\x28\x28\x00\x63\xFF\u{FF}\n"; // valid bytes, invalid unicodes let _ = c"\u{FF}\xFF"; // valid bytes, valid unicodes let backslash = r"\\"; - println!("{\x41}", A = 92); - println!("{ничоси}", ничоси = 92); + println!("{\x41}", A = 92); + println!("{ничоси}", ничоси = 92); - println!("{:x?} {} ", thingy, n2); - panic!("{}", 0); - panic!("more {}", 1); - assert!(true, "{}", 1); - assert!(true, "{} asdasd", 1); - toho!("{}fmt", 0); + println!("{:x?} {} ", thingy, n2); + panic!("{}", 0); + panic!("more {}", 1); + assert!(true, "{}", 1); + assert!(true, "{} asdasd", 1); + toho!("{}fmt", 0); let i: u64 = 3; let o: u64; - core::arch::asm!( + core::arch::asm!( "mov {0}, {1}", "add {0}, 5", out(reg) o, in(reg) i, - ); + ); const CONSTANT: () = (): let mut m = (); - format_args!(concat!("{}"), "{}"); - format_args!("{} {} {} {} {} {} {backslash} {CONSTANT} {m}", backslash, format_args!("{}", 0), foo, "bar", toho!(), backslash); - reuse_twice!("{backslash}"); + format_args!(concat!("{}"), "{}"); + format_args!("{} {} {} {} {} {} {backslash} {CONSTANT} {m}", backslash, format_args!("{}", 0), foo, "bar", toho!(), backslash); + reuse_twice!("{backslash}"); } \ No newline at end of file diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_unsafe.html b/crates/ide/src/syntax_highlighting/test_data/highlight_unsafe.html index d9beac308982..9a46d9f4025d 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_unsafe.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_unsafe.html @@ -92,13 +92,13 @@ let x = &5 as *const _ as *const usize; let u = Union { b: 0 }; - id! { + id! { unsafe { unsafe_deref!() } - }; + }; unsafe { - unsafe_deref!(); - id! { unsafe_deref!() }; + unsafe_deref!(); + id! { unsafe_deref!() }; // unsafe fn and method calls unsafe_fn(); diff --git a/crates/ide/src/syntax_highlighting/tests.rs b/crates/ide/src/syntax_highlighting/tests.rs index 3775265f234d..e48ca86c46b5 100644 --- a/crates/ide/src/syntax_highlighting/tests.rs +++ b/crates/ide/src/syntax_highlighting/tests.rs @@ -386,7 +386,7 @@ mod __ { } macro_rules! void { - ($($tt:tt)*) => {} + ($($tt:tt)*) => {discard!($($tt:tt)*)} } struct __ where Self:; @@ -411,6 +411,31 @@ void!('static 'self 'unsafe) } } +#[test] +fn test_keyword_macro_edition_highlighting() { + check_highlighting( + r#" +//- /main.rs crate:main edition:2018 deps:lib2015,lib2024 +lib2015::void_2015!(try async await gen); +lib2024::void_2024!(try async await gen); +//- /lib2015.rs crate:lib2015 edition:2015 +#[macro_export] +macro_rules! void_2015 { + ($($tt:tt)*) => {discard!($($tt:tt)*)} +} + +//- /lib2024.rs crate:lib2024 edition:2024 +#[macro_export] +macro_rules! void_2024 { + ($($tt:tt)*) => {discard!($($tt:tt)*)} +} + +"#, + expect_file![format!("./test_data/highlight_keywords_macros.html")], + false, + ); +} + #[test] fn test_string_highlighting() { // The format string detection is based on macro-expansion, diff --git a/crates/parser/src/syntax_kind/generated.rs b/crates/parser/src/syntax_kind/generated.rs index 79900425a17c..e56e09eeb66b 100644 --- a/crates/parser/src/syntax_kind/generated.rs +++ b/crates/parser/src/syntax_kind/generated.rs @@ -147,9 +147,6 @@ pub enum SyntaxKind { C_STRING, FLOAT_NUMBER, INT_NUMBER, - RAW_BYTE_STRING, - RAW_C_STRING, - RAW_STRING, STRING, COMMENT, ERROR, @@ -343,9 +340,6 @@ impl SyntaxKind { | C_STRING | FLOAT_NUMBER | INT_NUMBER - | RAW_BYTE_STRING - | RAW_C_STRING - | RAW_STRING | STRING | ABI | ADT @@ -898,18 +892,7 @@ impl SyntaxKind { ) } pub fn is_literal(self) -> bool { - matches!( - self, - BYTE | BYTE_STRING - | CHAR - | C_STRING - | FLOAT_NUMBER - | INT_NUMBER - | RAW_BYTE_STRING - | RAW_C_STRING - | RAW_STRING - | STRING - ) + matches!(self, BYTE | BYTE_STRING | CHAR | C_STRING | FLOAT_NUMBER | INT_NUMBER | STRING) } pub fn from_keyword(ident: &str, edition: Edition) -> Option { let kw = match ident { diff --git a/crates/syntax/rust.ungram b/crates/syntax/rust.ungram index bbb8413cbc08..88d7beb897e0 100644 --- a/crates/syntax/rust.ungram +++ b/crates/syntax/rust.ungram @@ -438,9 +438,9 @@ MacroExpr = Literal = Attr* value:( '@int_number' | '@float_number' - | '@string' | '@raw_string' - | '@byte_string' | '@raw_byte_string' - | '@c_string' | '@raw_c_string' + | '@string' + | '@byte_string' + | '@c_string' | '@char' | '@byte' | 'true' | 'false' ) diff --git a/crates/test-utils/src/fixture.rs b/crates/test-utils/src/fixture.rs index daeb56c5835c..7240069753e8 100644 --- a/crates/test-utils/src/fixture.rs +++ b/crates/test-utils/src/fixture.rs @@ -218,16 +218,11 @@ impl FixtureWithProjectMeta { ); } - if line.starts_with("//-") { + if let Some(line) = line.strip_prefix("//-") { let meta = Self::parse_meta_line(line); res.push(meta); } else { - if line.starts_with("// ") - && line.contains(':') - && !line.contains("::") - && !line.contains('.') - && line.chars().all(|it| !it.is_uppercase()) - { + if matches!(line.strip_prefix("// "), Some(l) if l.trim().starts_with('/')) { panic!("looks like invalid metadata line: {line:?}"); } @@ -242,8 +237,7 @@ impl FixtureWithProjectMeta { //- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b env:OUTDIR=path/to,OTHER=foo fn parse_meta_line(meta: &str) -> Fixture { - assert!(meta.starts_with("//-")); - let meta = meta["//-".len()..].trim(); + let meta = meta.trim(); let mut components = meta.split_ascii_whitespace(); let path = components.next().expect("fixture meta must start with a path").to_owned();