aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Cargo.lock16
-rw-r--r--crates/hir_def/src/attr.rs2
-rw-r--r--crates/ide/src/display/navigation_target.rs52
-rw-r--r--crates/mbe/src/mbe_expander/matcher.rs32
-rw-r--r--crates/mbe/src/tests.rs13
5 files changed, 76 insertions, 39 deletions
diff --git a/Cargo.lock b/Cargo.lock
index d3aec4c99..8cb5fd7ad 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -261,9 +261,9 @@ dependencies = [
261 261
262[[package]] 262[[package]]
263name = "const_fn" 263name = "const_fn"
264version = "0.4.3" 264version = "0.4.4"
265source = "registry+https://github.com/rust-lang/crates.io-index" 265source = "registry+https://github.com/rust-lang/crates.io-index"
266checksum = "c478836e029dcef17fb47c89023448c64f781a046e0300e257ad8225ae59afab" 266checksum = "cd51eab21ab4fd6a3bf889e2d0958c0a6e3a61ad04260325e919e652a2a62826"
267 267
268[[package]] 268[[package]]
269name = "crc32fast" 269name = "crc32fast"
@@ -990,9 +990,9 @@ dependencies = [
990 990
991[[package]] 991[[package]]
992name = "net2" 992name = "net2"
993version = "0.2.36" 993version = "0.2.37"
994source = "registry+https://github.com/rust-lang/crates.io-index" 994source = "registry+https://github.com/rust-lang/crates.io-index"
995checksum = "d7cf75f38f16cb05ea017784dc6dbfd354f76c223dba37701734c4f5a9337d02" 995checksum = "391630d12b68002ae1e25e8f974306474966550ad82dac6886fb8910c19568ae"
996dependencies = [ 996dependencies = [
997 "cfg-if 0.1.10", 997 "cfg-if 0.1.10",
998 "libc", 998 "libc",
@@ -2021,18 +2021,18 @@ dependencies = [
2021 2021
2022[[package]] 2022[[package]]
2023name = "xshell" 2023name = "xshell"
2024version = "0.1.6" 2024version = "0.1.7"
2025source = "registry+https://github.com/rust-lang/crates.io-index" 2025source = "registry+https://github.com/rust-lang/crates.io-index"
2026checksum = "3e9bbfccbb2233e6b0473b7870d4b0811a402e9e249a5e8394e768e5a5c9c37d" 2026checksum = "ed0728d188f2ae530490b7d92435728aba53c6aed06d07e1951da9bd4c1d0798"
2027dependencies = [ 2027dependencies = [
2028 "xshell-macros", 2028 "xshell-macros",
2029] 2029]
2030 2030
2031[[package]] 2031[[package]]
2032name = "xshell-macros" 2032name = "xshell-macros"
2033version = "0.1.6" 2033version = "0.1.7"
2034source = "registry+https://github.com/rust-lang/crates.io-index" 2034source = "registry+https://github.com/rust-lang/crates.io-index"
2035checksum = "b94f1c632d730a1704b21dc551a4c74fbed713cfa59593708f94943548206134" 2035checksum = "367f903cc3f8bc4f4b2400d47dfa6c9e3e121ffb51a30cf0fb67a72c0a0f9617"
2036 2036
2037[[package]] 2037[[package]]
2038name = "xtask" 2038name = "xtask"
diff --git a/crates/hir_def/src/attr.rs b/crates/hir_def/src/attr.rs
index ff8fb7483..c64b78445 100644
--- a/crates/hir_def/src/attr.rs
+++ b/crates/hir_def/src/attr.rs
@@ -23,7 +23,7 @@ use crate::{
23}; 23};
24 24
25/// Holds documentation 25/// Holds documentation
26#[derive(Debug, Clone, PartialEq, Eq)] 26#[derive(Debug, Clone, PartialEq, Eq, Hash)]
27pub struct Documentation(String); 27pub struct Documentation(String);
28 28
29impl Documentation { 29impl Documentation {
diff --git a/crates/ide/src/display/navigation_target.rs b/crates/ide/src/display/navigation_target.rs
index 4790d648a..234f80a3a 100644
--- a/crates/ide/src/display/navigation_target.rs
+++ b/crates/ide/src/display/navigation_target.rs
@@ -1,11 +1,13 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2 2
3use either::Either; 3use either::Either;
4use hir::{AssocItem, FieldSource, HasSource, InFile, ModuleSource}; 4use hir::{
5 AssocItem, Documentation, FieldSource, HasAttrs, HasSource, HirFileId, InFile, ModuleSource,
6};
5use ide_db::base_db::{FileId, SourceDatabase}; 7use ide_db::base_db::{FileId, SourceDatabase};
6use ide_db::{defs::Definition, RootDatabase}; 8use ide_db::{defs::Definition, RootDatabase};
7use syntax::{ 9use syntax::{
8 ast::{self, DocCommentsOwner, NameOwner}, 10 ast::{self, NameOwner},
9 match_ast, AstNode, SmolStr, 11 match_ast, AstNode, SmolStr,
10 SyntaxKind::{self, IDENT_PAT, TYPE_PARAM}, 12 SyntaxKind::{self, IDENT_PAT, TYPE_PARAM},
11 TextRange, 13 TextRange,
@@ -43,7 +45,7 @@ pub struct NavigationTarget {
43 pub kind: SyntaxKind, 45 pub kind: SyntaxKind,
44 pub container_name: Option<SmolStr>, 46 pub container_name: Option<SmolStr>,
45 pub description: Option<String>, 47 pub description: Option<String>,
46 pub docs: Option<String>, 48 pub docs: Option<Documentation>,
47} 49}
48 50
49pub(crate) trait ToNav { 51pub(crate) trait ToNav {
@@ -71,7 +73,7 @@ impl NavigationTarget {
71 frange.range, 73 frange.range,
72 src.value.syntax().kind(), 74 src.value.syntax().kind(),
73 ); 75 );
74 res.docs = src.value.doc_comment_text(); 76 res.docs = module.attrs(db).docs();
75 res.description = src.value.short_label(); 77 res.description = src.value.short_label();
76 return res; 78 return res;
77 } 79 }
@@ -214,14 +216,14 @@ impl ToNavFromAst for hir::Trait {}
214 216
215impl<D> ToNav for D 217impl<D> ToNav for D
216where 218where
217 D: HasSource + ToNavFromAst + Copy, 219 D: HasSource + ToNavFromAst + Copy + HasAttrs,
218 D::Ast: ast::DocCommentsOwner + ast::NameOwner + ShortLabel, 220 D::Ast: ast::NameOwner + ShortLabel,
219{ 221{
220 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { 222 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
221 let src = self.source(db); 223 let src = self.source(db);
222 let mut res = 224 let mut res =
223 NavigationTarget::from_named(db, src.as_ref().map(|it| it as &dyn ast::NameOwner)); 225 NavigationTarget::from_named(db, src.as_ref().map(|it| it as &dyn ast::NameOwner));
224 res.docs = src.value.doc_comment_text(); 226 res.docs = self.docs(db);
225 res.description = src.value.short_label(); 227 res.description = src.value.short_label();
226 res 228 res
227 } 229 }
@@ -274,7 +276,7 @@ impl ToNav for hir::Field {
274 match &src.value { 276 match &src.value {
275 FieldSource::Named(it) => { 277 FieldSource::Named(it) => {
276 let mut res = NavigationTarget::from_named(db, src.with_value(it)); 278 let mut res = NavigationTarget::from_named(db, src.with_value(it));
277 res.docs = it.doc_comment_text(); 279 res.docs = self.docs(db);
278 res.description = it.short_label(); 280 res.description = it.short_label();
279 res 281 res
280 } 282 }
@@ -298,7 +300,7 @@ impl ToNav for hir::MacroDef {
298 log::debug!("nav target {:#?}", src.value.syntax()); 300 log::debug!("nav target {:#?}", src.value.syntax());
299 let mut res = 301 let mut res =
300 NavigationTarget::from_named(db, src.as_ref().map(|it| it as &dyn ast::NameOwner)); 302 NavigationTarget::from_named(db, src.as_ref().map(|it| it as &dyn ast::NameOwner));
301 res.docs = src.value.doc_comment_text(); 303 res.docs = self.docs(db);
302 res 304 res
303 } 305 }
304} 306}
@@ -374,26 +376,28 @@ impl ToNav for hir::TypeParam {
374 } 376 }
375} 377}
376 378
377pub(crate) fn docs_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> Option<String> { 379pub(crate) fn docs_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> Option<Documentation> {
378 let parse = db.parse(symbol.file_id); 380 let parse = db.parse(symbol.file_id);
379 let node = symbol.ptr.to_node(parse.tree().syntax()); 381 let node = symbol.ptr.to_node(parse.tree().syntax());
382 let file_id = HirFileId::from(symbol.file_id);
380 383
381 match_ast! { 384 let it = match_ast! {
382 match node { 385 match node {
383 ast::Fn(it) => it.doc_comment_text(), 386 ast::Fn(it) => hir::Attrs::from_attrs_owner(db, InFile::new(file_id, &it)),
384 ast::Struct(it) => it.doc_comment_text(), 387 ast::Struct(it) => hir::Attrs::from_attrs_owner(db, InFile::new(file_id, &it)),
385 ast::Enum(it) => it.doc_comment_text(), 388 ast::Enum(it) => hir::Attrs::from_attrs_owner(db, InFile::new(file_id, &it)),
386 ast::Trait(it) => it.doc_comment_text(), 389 ast::Trait(it) => hir::Attrs::from_attrs_owner(db, InFile::new(file_id, &it)),
387 ast::Module(it) => it.doc_comment_text(), 390 ast::Module(it) => hir::Attrs::from_attrs_owner(db, InFile::new(file_id, &it)),
388 ast::TypeAlias(it) => it.doc_comment_text(), 391 ast::TypeAlias(it) => hir::Attrs::from_attrs_owner(db, InFile::new(file_id, &it)),
389 ast::Const(it) => it.doc_comment_text(), 392 ast::Const(it) => hir::Attrs::from_attrs_owner(db, InFile::new(file_id, &it)),
390 ast::Static(it) => it.doc_comment_text(), 393 ast::Static(it) => hir::Attrs::from_attrs_owner(db, InFile::new(file_id, &it)),
391 ast::RecordField(it) => it.doc_comment_text(), 394 ast::RecordField(it) => hir::Attrs::from_attrs_owner(db, InFile::new(file_id, &it)),
392 ast::Variant(it) => it.doc_comment_text(), 395 ast::Variant(it) => hir::Attrs::from_attrs_owner(db, InFile::new(file_id, &it)),
393 ast::MacroCall(it) => it.doc_comment_text(), 396 ast::MacroCall(it) => hir::Attrs::from_attrs_owner(db, InFile::new(file_id, &it)),
394 _ => None, 397 _ => return None,
395 } 398 }
396 } 399 };
400 it.docs()
397} 401}
398 402
399/// Get a description of a symbol. 403/// Get a description of a symbol.
diff --git a/crates/mbe/src/mbe_expander/matcher.rs b/crates/mbe/src/mbe_expander/matcher.rs
index 3f8445897..93ee77908 100644
--- a/crates/mbe/src/mbe_expander/matcher.rs
+++ b/crates/mbe/src/mbe_expander/matcher.rs
@@ -356,6 +356,18 @@ impl<'a> TtIter<'a> {
356 ExpandResult { value: _, err: Some(_) } => None, 356 ExpandResult { value: _, err: Some(_) } => None,
357 } 357 }
358 } 358 }
359
360 pub(crate) fn eat_char(&mut self, c: char) -> Option<tt::TokenTree> {
361 let mut fork = self.clone();
362 match fork.expect_char(c) {
363 Ok(_) => {
364 let tt = self.next().cloned();
365 *self = fork;
366 tt
367 }
368 Err(_) => None,
369 }
370 }
359} 371}
360 372
361pub(super) fn match_repeat( 373pub(super) fn match_repeat(
@@ -447,10 +459,22 @@ fn match_meta_var(kind: &str, input: &mut TtIter) -> ExpandResult<Option<Fragmen
447 .expect_lifetime() 459 .expect_lifetime()
448 .map(|tt| Some(tt)) 460 .map(|tt| Some(tt))
449 .map_err(|()| err!("expected lifetime")), 461 .map_err(|()| err!("expected lifetime")),
450 "literal" => input 462 "literal" => {
451 .expect_literal() 463 let neg = input.eat_char('-');
452 .map(|literal| Some(tt::Leaf::from(literal.clone()).into())) 464 input
453 .map_err(|()| err!()), 465 .expect_literal()
466 .map(|literal| {
467 let lit = tt::Leaf::from(literal.clone());
468 match neg {
469 None => Some(lit.into()),
470 Some(neg) => Some(tt::TokenTree::Subtree(tt::Subtree {
471 delimiter: None,
472 token_trees: vec![neg, lit.into()],
473 })),
474 }
475 })
476 .map_err(|()| err!())
477 }
454 // `vis` is optional 478 // `vis` is optional
455 "vis" => match input.eat_vis() { 479 "vis" => match input.eat_vis() {
456 Some(vis) => Ok(Some(vis)), 480 Some(vis) => Ok(Some(vis)),
diff --git a/crates/mbe/src/tests.rs b/crates/mbe/src/tests.rs
index 0796ceee1..843054fe8 100644
--- a/crates/mbe/src/tests.rs
+++ b/crates/mbe/src/tests.rs
@@ -1008,11 +1008,20 @@ fn test_literal() {
1008 parse_macro( 1008 parse_macro(
1009 r#" 1009 r#"
1010 macro_rules! foo { 1010 macro_rules! foo {
1011 ($ type:ty $ lit:literal) => { const VALUE: $ type = $ lit;}; 1011 ($ type:ty , $ lit:literal) => { const VALUE: $ type = $ lit;};
1012 } 1012 }
1013"#, 1013"#,
1014 ) 1014 )
1015 .assert_expand_items(r#"foo!(u8 0);"#, r#"const VALUE : u8 = 0 ;"#); 1015 .assert_expand_items(r#"foo!(u8,0);"#, r#"const VALUE : u8 = 0 ;"#);
1016
1017 parse_macro(
1018 r#"
1019 macro_rules! foo {
1020 ($ type:ty , $ lit:literal) => { const VALUE: $ type = $ lit;};
1021 }
1022"#,
1023 )
1024 .assert_expand_items(r#"foo!(i32,-1);"#, r#"const VALUE : i32 = - 1 ;"#);
1016} 1025}
1017 1026
1018#[test] 1027#[test]