aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorLukas Wirth <[email protected]>2021-03-16 18:06:58 +0000
committerLukas Wirth <[email protected]>2021-03-16 18:06:58 +0000
commitacc6458390eb7ed5947fe8b6a0628b4a9018fad1 (patch)
tree038a673cd1e1dcd74ac7efed266c9419a38bd6c4 /crates
parent11e9bc60a2d9c22dbf51b7e1aa3d6e30a7006a35 (diff)
Replace trait object boxing with extra AttrsOwnerNode
Diffstat (limited to 'crates')
-rw-r--r--crates/ide/src/syntax_highlighting/inject.rs67
-rw-r--r--crates/syntax/src/ast/traits.rs2
2 files changed, 49 insertions, 20 deletions
diff --git a/crates/ide/src/syntax_highlighting/inject.rs b/crates/ide/src/syntax_highlighting/inject.rs
index 5b065c09f..086db40e5 100644
--- a/crates/ide/src/syntax_highlighting/inject.rs
+++ b/crates/ide/src/syntax_highlighting/inject.rs
@@ -4,7 +4,7 @@ use either::Either;
4use hir::{HasAttrs, Semantics}; 4use hir::{HasAttrs, Semantics};
5use ide_db::call_info::ActiveParameter; 5use ide_db::call_info::ActiveParameter;
6use syntax::{ 6use syntax::{
7 ast::{self, AstNode, AttrsOwner}, 7 ast::{self, AstNode, AttrsOwner, DocCommentsOwner},
8 match_ast, AstToken, SyntaxNode, SyntaxToken, TextRange, TextSize, 8 match_ast, AstToken, SyntaxNode, SyntaxToken, TextRange, TextSize,
9}; 9};
10 10
@@ -85,28 +85,57 @@ const RUSTDOC_FENCE_TOKENS: &[&'static str] = &[
85 "edition2021", 85 "edition2021",
86]; 86];
87 87
88// Basically an owned dyn AttrsOwner without extra Boxing
89struct AttrsOwnerNode {
90 node: SyntaxNode,
91}
92
93impl AttrsOwnerNode {
94 fn new<N: DocCommentsOwner>(node: N) -> Self {
95 AttrsOwnerNode { node: node.syntax().clone() }
96 }
97}
98
99impl AttrsOwner for AttrsOwnerNode {}
100impl AstNode for AttrsOwnerNode {
101 fn can_cast(_: syntax::SyntaxKind) -> bool
102 where
103 Self: Sized,
104 {
105 false
106 }
107 fn cast(_: SyntaxNode) -> Option<Self>
108 where
109 Self: Sized,
110 {
111 None
112 }
113 fn syntax(&self) -> &SyntaxNode {
114 &self.node
115 }
116}
117
88fn doc_attributes<'node>( 118fn doc_attributes<'node>(
89 sema: &Semantics<RootDatabase>, 119 sema: &Semantics<RootDatabase>,
90 node: &'node SyntaxNode, 120 node: &'node SyntaxNode,
91) -> Option<(Box<dyn AttrsOwner>, hir::Attrs)> { 121) -> Option<(AttrsOwnerNode, hir::Attrs)> {
92 match_ast! { 122 match_ast! {
93 match node { 123 match node {
94 ast::SourceFile(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))), 124 ast::SourceFile(it) => sema.to_def(&it).map(|def| (AttrsOwnerNode::new(it), def.attrs(sema.db))),
95 ast::Fn(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))), 125 ast::Fn(it) => sema.to_def(&it).map(|def| (AttrsOwnerNode::new(it), def.attrs(sema.db))),
96 ast::Struct(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))), 126 ast::Struct(it) => sema.to_def(&it).map(|def| (AttrsOwnerNode::new(it), def.attrs(sema.db))),
97 ast::Union(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))), 127 ast::Union(it) => sema.to_def(&it).map(|def| (AttrsOwnerNode::new(it), def.attrs(sema.db))),
98 ast::RecordField(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))), 128 ast::RecordField(it) => sema.to_def(&it).map(|def| (AttrsOwnerNode::new(it), def.attrs(sema.db))),
99 ast::TupleField(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))), 129 ast::TupleField(it) => sema.to_def(&it).map(|def| (AttrsOwnerNode::new(it), def.attrs(sema.db))),
100 ast::Enum(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))), 130 ast::Enum(it) => sema.to_def(&it).map(|def| (AttrsOwnerNode::new(it), def.attrs(sema.db))),
101 ast::Variant(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))), 131 ast::Variant(it) => sema.to_def(&it).map(|def| (AttrsOwnerNode::new(it), def.attrs(sema.db))),
102 ast::Trait(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))), 132 ast::Trait(it) => sema.to_def(&it).map(|def| (AttrsOwnerNode::new(it), def.attrs(sema.db))),
103 ast::Module(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))), 133 ast::Module(it) => sema.to_def(&it).map(|def| (AttrsOwnerNode::new(it), def.attrs(sema.db))),
104 ast::Static(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))), 134 ast::Static(it) => sema.to_def(&it).map(|def| (AttrsOwnerNode::new(it), def.attrs(sema.db))),
105 ast::Const(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))), 135 ast::Const(it) => sema.to_def(&it).map(|def| (AttrsOwnerNode::new(it), def.attrs(sema.db))),
106 ast::TypeAlias(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))), 136 ast::TypeAlias(it) => sema.to_def(&it).map(|def| (AttrsOwnerNode::new(it), def.attrs(sema.db))),
107 ast::Impl(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))), 137 ast::Impl(it) => sema.to_def(&it).map(|def| (AttrsOwnerNode::new(it), def.attrs(sema.db))),
108 ast::MacroRules(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))), 138 ast::MacroRules(it) => sema.to_def(&it).map(|def| (AttrsOwnerNode::new(it), def.attrs(sema.db))),
109 ast::MacroRules(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))),
110 // ast::MacroDef(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))), 139 // ast::MacroDef(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))),
111 // ast::Use(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))), 140 // ast::Use(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))),
112 _ => return None 141 _ => return None
@@ -124,7 +153,7 @@ pub(super) fn doc_comment(hl: &mut Highlights, sema: &Semantics<RootDatabase>, n
124 if attributes.docs().map_or(true, |docs| !String::from(docs).contains(RUSTDOC_FENCE)) { 153 if attributes.docs().map_or(true, |docs| !String::from(docs).contains(RUSTDOC_FENCE)) {
125 return; 154 return;
126 } 155 }
127 let doc_comments = attributes.by_key("doc").attrs().map(|attr| attr.to_src(&*owner)); 156 let doc_comments = attributes.by_key("doc").attrs().map(|attr| attr.to_src(&owner));
128 157
129 let mut inj = Injector::default(); 158 let mut inj = Injector::default();
130 inj.add_unmapped("fn doctest() {\n"); 159 inj.add_unmapped("fn doctest() {\n");
diff --git a/crates/syntax/src/ast/traits.rs b/crates/syntax/src/ast/traits.rs
index 13a769d51..96d4cc997 100644
--- a/crates/syntax/src/ast/traits.rs
+++ b/crates/syntax/src/ast/traits.rs
@@ -72,7 +72,7 @@ pub trait AttrsOwner: AstNode {
72 } 72 }
73} 73}
74 74
75pub trait DocCommentsOwner: AstNode { 75pub trait DocCommentsOwner: AttrsOwner {
76 fn doc_comments(&self) -> CommentIter { 76 fn doc_comments(&self) -> CommentIter {
77 CommentIter { iter: self.syntax().children_with_tokens() } 77 CommentIter { iter: self.syntax().children_with_tokens() }
78 } 78 }