aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Cargo.lock68
-rw-r--r--crates/hir/src/lib.rs15
-rw-r--r--crates/ide/src/call_hierarchy.rs11
-rw-r--r--crates/ide/src/doc_links.rs260
-rw-r--r--crates/ide/src/goto_definition.rs66
-rw-r--r--crates/ide/src/hover.rs63
-rw-r--r--crates/ide/src/references/rename.rs81
-rw-r--r--crates/ide/src/runnables.rs28
-rw-r--r--crates/ide/src/syntax_highlighting/inject.rs6
-rw-r--r--crates/ide_assists/src/handlers/reorder_fields.rs112
-rw-r--r--crates/proc_macro_api/src/msg.rs35
-rw-r--r--crates/proc_macro_api/src/process.rs7
-rw-r--r--crates/proc_macro_api/src/rpc.rs30
-rw-r--r--crates/proc_macro_srv/src/cli.rs7
-rw-r--r--crates/rust-analyzer/tests/rust-analyzer/main.rs4
15 files changed, 418 insertions, 375 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 19ee689cb..7f666ca61 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -35,9 +35,9 @@ dependencies = [
35 35
36[[package]] 36[[package]]
37name = "anyhow" 37name = "anyhow"
38version = "1.0.38" 38version = "1.0.39"
39source = "registry+https://github.com/rust-lang/crates.io-index" 39source = "registry+https://github.com/rust-lang/crates.io-index"
40checksum = "afddf7f520a80dbf76e6f50a35bca42a2331ef227a28b3b6dc5c2e2338d114b1" 40checksum = "81cddc5f91628367664cc7c69714ff08deee8a3efc54623011c772544d7b2767"
41 41
42[[package]] 42[[package]]
43name = "anymap" 43name = "anymap"
@@ -111,9 +111,9 @@ checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610"
111 111
112[[package]] 112[[package]]
113name = "camino" 113name = "camino"
114version = "1.0.2" 114version = "1.0.4"
115source = "registry+https://github.com/rust-lang/crates.io-index" 115source = "registry+https://github.com/rust-lang/crates.io-index"
116checksum = "cd065703998b183ed0b348a22555691373a9345a1431141e5778b48bb17e4703" 116checksum = "d4648c6d00a709aa069a236adcaae4f605a6241c72bf5bee79331a4b625921a9"
117dependencies = [ 117dependencies = [
118 "serde", 118 "serde",
119] 119]
@@ -794,9 +794,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
794 794
795[[package]] 795[[package]]
796name = "libc" 796name = "libc"
797version = "0.2.88" 797version = "0.2.91"
798source = "registry+https://github.com/rust-lang/crates.io-index" 798source = "registry+https://github.com/rust-lang/crates.io-index"
799checksum = "03b07a082330a35e43f63177cc01689da34fbffa0105e1246cf0311472cac73a" 799checksum = "8916b1f6ca17130ec6568feccee27c156ad12037880833a3b842a823236502e7"
800 800
801[[package]] 801[[package]]
802name = "libloading" 802name = "libloading"
@@ -946,9 +946,9 @@ dependencies = [
946 946
947[[package]] 947[[package]]
948name = "mio" 948name = "mio"
949version = "0.7.9" 949version = "0.7.11"
950source = "registry+https://github.com/rust-lang/crates.io-index" 950source = "registry+https://github.com/rust-lang/crates.io-index"
951checksum = "a5dede4e2065b3842b8b0af444119f3aa331cc7cc2dd20388bfb0f5d5a38823a" 951checksum = "cf80d3e903b34e0bd7282b218398aec54e082c840d9baf8339e0080a0c542956"
952dependencies = [ 952dependencies = [
953 "libc", 953 "libc",
954 "log", 954 "log",
@@ -959,11 +959,10 @@ dependencies = [
959 959
960[[package]] 960[[package]]
961name = "miow" 961name = "miow"
962version = "0.3.6" 962version = "0.3.7"
963source = "registry+https://github.com/rust-lang/crates.io-index" 963source = "registry+https://github.com/rust-lang/crates.io-index"
964checksum = "5a33c1b55807fbed163481b5ba66db4b2fa6cde694a5027be10fb724206c5897" 964checksum = "b9f1c5b025cda876f66ef43a113f91ebc9f4ccef34843000e0adf6ebbab84e21"
965dependencies = [ 965dependencies = [
966 "socket2",
967 "winapi", 966 "winapi",
968] 967]
969 968
@@ -1299,9 +1298,9 @@ dependencies = [
1299 1298
1300[[package]] 1299[[package]]
1301name = "regex" 1300name = "regex"
1302version = "1.4.3" 1301version = "1.4.5"
1303source = "registry+https://github.com/rust-lang/crates.io-index" 1302source = "registry+https://github.com/rust-lang/crates.io-index"
1304checksum = "d9251239e129e16308e70d853559389de218ac275b515068abc96829d05b948a" 1303checksum = "957056ecddbeba1b26965114e191d2e8589ce74db242b6ea25fc4062427a5c19"
1305dependencies = [ 1304dependencies = [
1306 "regex-syntax", 1305 "regex-syntax",
1307] 1306]
@@ -1318,9 +1317,9 @@ dependencies = [
1318 1317
1319[[package]] 1318[[package]]
1320name = "regex-syntax" 1319name = "regex-syntax"
1321version = "0.6.22" 1320version = "0.6.23"
1322source = "registry+https://github.com/rust-lang/crates.io-index" 1321source = "registry+https://github.com/rust-lang/crates.io-index"
1323checksum = "b5eb417147ba9860a96cfe72a0b93bf88fee1744b5636ec99ab20c1aa9376581" 1322checksum = "24d5f089152e60f62d28b835fbff2cd2e8dc0baf1ac13343bef92ab7eed84548"
1324 1323
1325[[package]] 1324[[package]]
1326name = "rowan" 1325name = "rowan"
@@ -1485,18 +1484,18 @@ dependencies = [
1485 1484
1486[[package]] 1485[[package]]
1487name = "serde" 1486name = "serde"
1488version = "1.0.124" 1487version = "1.0.125"
1489source = "registry+https://github.com/rust-lang/crates.io-index" 1488source = "registry+https://github.com/rust-lang/crates.io-index"
1490checksum = "bd761ff957cb2a45fbb9ab3da6512de9de55872866160b23c25f1a841e99d29f" 1489checksum = "558dc50e1a5a5fa7112ca2ce4effcb321b0300c0d4ccf0776a9f60cd89031171"
1491dependencies = [ 1490dependencies = [
1492 "serde_derive", 1491 "serde_derive",
1493] 1492]
1494 1493
1495[[package]] 1494[[package]]
1496name = "serde_derive" 1495name = "serde_derive"
1497version = "1.0.124" 1496version = "1.0.125"
1498source = "registry+https://github.com/rust-lang/crates.io-index" 1497source = "registry+https://github.com/rust-lang/crates.io-index"
1499checksum = "1800f7693e94e186f5e25a28291ae1570da908aff7d97a095dec1e56ff99069b" 1498checksum = "b093b7a2bb58203b5da3056c05b4ec1fed827dcfdb37347a8841695263b3d06d"
1500dependencies = [ 1499dependencies = [
1501 "proc-macro2", 1500 "proc-macro2",
1502 "quote", 1501 "quote",
@@ -1566,17 +1565,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
1566checksum = "dc725476a1398f0480d56cd0ad381f6f32acf2642704456f8f59a35df464b59a" 1565checksum = "dc725476a1398f0480d56cd0ad381f6f32acf2642704456f8f59a35df464b59a"
1567 1566
1568[[package]] 1567[[package]]
1569name = "socket2"
1570version = "0.3.19"
1571source = "registry+https://github.com/rust-lang/crates.io-index"
1572checksum = "122e570113d28d773067fab24266b66753f6ea915758651696b6e35e49f88d6e"
1573dependencies = [
1574 "cfg-if",
1575 "libc",
1576 "winapi",
1577]
1578
1579[[package]]
1580name = "stdx" 1568name = "stdx"
1581version = "0.0.0" 1569version = "0.0.0"
1582dependencies = [ 1570dependencies = [
@@ -1586,9 +1574,9 @@ dependencies = [
1586 1574
1587[[package]] 1575[[package]]
1588name = "syn" 1576name = "syn"
1589version = "1.0.63" 1577version = "1.0.64"
1590source = "registry+https://github.com/rust-lang/crates.io-index" 1578source = "registry+https://github.com/rust-lang/crates.io-index"
1591checksum = "8fd9bc7ccc2688b3344c2f48b9b546648b25ce0b20fc717ee7fa7981a8ca9717" 1579checksum = "3fd9d1e9976102a03c542daa2eff1b43f9d72306342f3f8b3ed5fb8908195d6f"
1592dependencies = [ 1580dependencies = [
1593 "proc-macro2", 1581 "proc-macro2",
1594 "quote", 1582 "quote",
@@ -1718,9 +1706,9 @@ dependencies = [
1718 1706
1719[[package]] 1707[[package]]
1720name = "tracing-attributes" 1708name = "tracing-attributes"
1721version = "0.1.13" 1709version = "0.1.15"
1722source = "registry+https://github.com/rust-lang/crates.io-index" 1710source = "registry+https://github.com/rust-lang/crates.io-index"
1723checksum = "a8a9bd1db7706f2373a190b0d067146caa39350c486f3d455b0e33b431f94c07" 1711checksum = "c42e6fa53307c8a17e4ccd4dc81cf5ec38db9209f59b222210375b54ee40d1e2"
1724dependencies = [ 1712dependencies = [
1725 "proc-macro2", 1713 "proc-macro2",
1726 "quote", 1714 "quote",
@@ -1759,9 +1747,9 @@ dependencies = [
1759 1747
1760[[package]] 1748[[package]]
1761name = "tracing-subscriber" 1749name = "tracing-subscriber"
1762version = "0.2.16" 1750version = "0.2.17"
1763source = "registry+https://github.com/rust-lang/crates.io-index" 1751source = "registry+https://github.com/rust-lang/crates.io-index"
1764checksum = "8ab8966ac3ca27126141f7999361cc97dd6fb4b71da04c02044fa9045d98bb96" 1752checksum = "705096c6f83bf68ea5d357a6aa01829ddbdac531b357b45abeca842938085baa"
1765dependencies = [ 1753dependencies = [
1766 "ansi_term", 1754 "ansi_term",
1767 "chrono", 1755 "chrono",
@@ -1867,9 +1855,9 @@ dependencies = [
1867 1855
1868[[package]] 1856[[package]]
1869name = "version_check" 1857name = "version_check"
1870version = "0.9.2" 1858version = "0.9.3"
1871source = "registry+https://github.com/rust-lang/crates.io-index" 1859source = "registry+https://github.com/rust-lang/crates.io-index"
1872checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed" 1860checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe"
1873 1861
1874[[package]] 1862[[package]]
1875name = "vfs" 1863name = "vfs"
@@ -1896,9 +1884,9 @@ dependencies = [
1896 1884
1897[[package]] 1885[[package]]
1898name = "walkdir" 1886name = "walkdir"
1899version = "2.3.1" 1887version = "2.3.2"
1900source = "registry+https://github.com/rust-lang/crates.io-index" 1888source = "registry+https://github.com/rust-lang/crates.io-index"
1901checksum = "777182bc735b6424e1a57516d35ed72cb8019d85c8c9bf536dccb3445c1a2f7d" 1889checksum = "808cf2735cd4b6866113f648b791c6adc5714537bc222d9347bb203386ffda56"
1902dependencies = [ 1890dependencies = [
1903 "same-file", 1891 "same-file",
1904 "winapi", 1892 "winapi",
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs
index bdc1ad852..eb7865c84 100644
--- a/crates/hir/src/lib.rs
+++ b/crates/hir/src/lib.rs
@@ -973,6 +973,14 @@ impl SelfParam {
973 Access::Owned => "self", 973 Access::Owned => "self",
974 } 974 }
975 } 975 }
976
977 pub fn source(&self, db: &dyn HirDatabase) -> Option<InFile<ast::SelfParam>> {
978 let InFile { file_id, value } = Function::from(self.func).source(db)?;
979 value
980 .param_list()
981 .and_then(|params| params.self_param())
982 .map(|value| InFile { file_id, value })
983 }
976} 984}
977 985
978impl HasVisibility for Function { 986impl HasVisibility for Function {
@@ -1348,6 +1356,13 @@ impl Local {
1348 } 1356 }
1349 } 1357 }
1350 1358
1359 pub fn as_self_param(self, db: &dyn HirDatabase) -> Option<SelfParam> {
1360 match self.parent {
1361 DefWithBodyId::FunctionId(func) if self.is_self(db) => Some(SelfParam { func }),
1362 _ => None,
1363 }
1364 }
1365
1351 // FIXME: why is this an option? It shouldn't be? 1366 // FIXME: why is this an option? It shouldn't be?
1352 pub fn name(self, db: &dyn HirDatabase) -> Option<Name> { 1367 pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
1353 let body = db.body(self.parent); 1368 let body = db.body(self.parent);
diff --git a/crates/ide/src/call_hierarchy.rs b/crates/ide/src/call_hierarchy.rs
index 96021f677..5cd186565 100644
--- a/crates/ide/src/call_hierarchy.rs
+++ b/crates/ide/src/call_hierarchy.rs
@@ -50,16 +50,16 @@ pub(crate) fn incoming_calls(db: &RootDatabase, position: FilePosition) -> Optio
50 for (file_id, references) in refs.references { 50 for (file_id, references) in refs.references {
51 let file = sema.parse(file_id); 51 let file = sema.parse(file_id);
52 let file = file.syntax(); 52 let file = file.syntax();
53 for (r_range, _) in references { 53 for (relative_range, token) in references
54 let token = file.token_at_offset(r_range.start()).next()?; 54 .into_iter()
55 .filter_map(|(range, _)| Some(range).zip(file.token_at_offset(range.start()).next()))
56 {
55 let token = sema.descend_into_macros(token); 57 let token = sema.descend_into_macros(token);
56 // This target is the containing function 58 // This target is the containing function
57 if let Some(nav) = token.ancestors().find_map(|node| { 59 if let Some(nav) = token.ancestors().find_map(|node| {
58 let fn_ = ast::Fn::cast(node)?; 60 let def = ast::Fn::cast(node).and_then(|fn_| sema.to_def(&fn_))?;
59 let def = sema.to_def(&fn_)?;
60 def.try_to_nav(sema.db) 61 def.try_to_nav(sema.db)
61 }) { 62 }) {
62 let relative_range = r_range;
63 calls.add(&nav, relative_range); 63 calls.add(&nav, relative_range);
64 } 64 }
65 } 65 }
@@ -87,7 +87,6 @@ pub(crate) fn outgoing_calls(db: &RootDatabase, position: FilePosition) -> Optio
87 let name_ref = call_node.name_ref()?; 87 let name_ref = call_node.name_ref()?;
88 let func_target = match call_node { 88 let func_target = match call_node {
89 FnCallNode::CallExpr(expr) => { 89 FnCallNode::CallExpr(expr) => {
90 //FIXME: Type::as_callable is broken
91 let callable = sema.type_of_expr(&expr.expr()?)?.as_callable(db)?; 90 let callable = sema.type_of_expr(&expr.expr()?)?.as_callable(db)?;
92 match callable.kind() { 91 match callable.kind() {
93 hir::CallableKind::Function(it) => it.try_to_nav(db), 92 hir::CallableKind::Function(it) => it.try_to_nav(db),
diff --git a/crates/ide/src/doc_links.rs b/crates/ide/src/doc_links.rs
index c7c1f4fee..0cee741ac 100644
--- a/crates/ide/src/doc_links.rs
+++ b/crates/ide/src/doc_links.rs
@@ -1,4 +1,4 @@
1//! Resolves and rewrites links in markdown documentation. 1//! Extracts, resolves and rewrites links and intra-doc links in markdown documentation.
2 2
3use std::{convert::TryFrom, iter::once, ops::Range}; 3use std::{convert::TryFrom, iter::once, ops::Range};
4 4
@@ -15,7 +15,10 @@ use ide_db::{
15 defs::{Definition, NameClass, NameRefClass}, 15 defs::{Definition, NameClass, NameRefClass},
16 RootDatabase, 16 RootDatabase,
17}; 17};
18use syntax::{ast, match_ast, AstNode, SyntaxKind::*, SyntaxToken, TokenAtOffset, T}; 18use syntax::{
19 ast, match_ast, AstNode, AstToken, SyntaxKind::*, SyntaxNode, SyntaxToken, TextRange, TextSize,
20 TokenAtOffset, T,
21};
19 22
20use crate::{FilePosition, Semantics}; 23use crate::{FilePosition, Semantics};
21 24
@@ -60,29 +63,6 @@ pub(crate) fn rewrite_links(db: &RootDatabase, markdown: &str, definition: &Defi
60 out 63 out
61} 64}
62 65
63pub(crate) fn extract_definitions_from_markdown(
64 markdown: &str,
65) -> Vec<(String, Option<hir::Namespace>, Range<usize>)> {
66 let mut res = vec![];
67 let mut cb = |link: BrokenLink| {
68 // These allocations are actually unnecessary but the lifetimes on BrokenLinkCallback are wrong
69 // this is fixed in the repo but not on the crates.io release yet
70 Some((
71 /*url*/ link.reference.to_owned().into(),
72 /*title*/ link.reference.to_owned().into(),
73 ))
74 };
75 let doc = Parser::new_with_broken_link_callback(markdown, Options::empty(), Some(&mut cb));
76 for (event, range) in doc.into_offset_iter() {
77 if let Event::Start(Tag::Link(_, target, title)) = event {
78 let link = if target.is_empty() { title } else { target };
79 let (link, ns) = parse_link(&link);
80 res.push((link.to_string(), ns, range));
81 }
82 }
83 res
84}
85
86/// Remove all links in markdown documentation. 66/// Remove all links in markdown documentation.
87pub(crate) fn remove_links(markdown: &str) -> String { 67pub(crate) fn remove_links(markdown: &str) -> String {
88 let mut drop_link = false; 68 let mut drop_link = false;
@@ -118,6 +98,105 @@ pub(crate) fn remove_links(markdown: &str) -> String {
118 out 98 out
119} 99}
120 100
101pub(crate) fn extract_definitions_from_markdown(
102 markdown: &str,
103) -> Vec<(Range<usize>, String, Option<hir::Namespace>)> {
104 let mut res = vec![];
105 let mut cb = |link: BrokenLink| {
106 // These allocations are actually unnecessary but the lifetimes on BrokenLinkCallback are wrong
107 // this is fixed in the repo but not on the crates.io release yet
108 Some((
109 /*url*/ link.reference.to_owned().into(),
110 /*title*/ link.reference.to_owned().into(),
111 ))
112 };
113 let doc = Parser::new_with_broken_link_callback(markdown, Options::empty(), Some(&mut cb));
114 for (event, range) in doc.into_offset_iter() {
115 if let Event::Start(Tag::Link(_, target, title)) = event {
116 let link = if target.is_empty() { title } else { target };
117 let (link, ns) = parse_intra_doc_link(&link);
118 res.push((range, link.to_string(), ns));
119 }
120 }
121 res
122}
123
124/// Extracts a link from a comment at the given position returning the spanning range, link and
125/// optionally it's namespace.
126pub(crate) fn extract_positioned_link_from_comment(
127 position: TextSize,
128 comment: &ast::Comment,
129) -> Option<(TextRange, String, Option<hir::Namespace>)> {
130 let doc_comment = comment.doc_comment()?;
131 let comment_start =
132 comment.syntax().text_range().start() + TextSize::from(comment.prefix().len() as u32);
133 let def_links = extract_definitions_from_markdown(doc_comment);
134 let (range, def_link, ns) =
135 def_links.into_iter().find_map(|(Range { start, end }, def_link, ns)| {
136 let range = TextRange::at(
137 comment_start + TextSize::from(start as u32),
138 TextSize::from((end - start) as u32),
139 );
140 range.contains(position).then(|| (range, def_link, ns))
141 })?;
142 Some((range, def_link, ns))
143}
144
145/// Turns a syntax node into it's [`Definition`] if it can hold docs.
146pub(crate) fn doc_owner_to_def(
147 sema: &Semantics<RootDatabase>,
148 item: &SyntaxNode,
149) -> Option<Definition> {
150 let res: hir::ModuleDef = match_ast! {
151 match item {
152 ast::SourceFile(_it) => sema.scope(item).module()?.into(),
153 ast::Fn(it) => sema.to_def(&it)?.into(),
154 ast::Struct(it) => sema.to_def(&it)?.into(),
155 ast::Enum(it) => sema.to_def(&it)?.into(),
156 ast::Union(it) => sema.to_def(&it)?.into(),
157 ast::Trait(it) => sema.to_def(&it)?.into(),
158 ast::Const(it) => sema.to_def(&it)?.into(),
159 ast::Static(it) => sema.to_def(&it)?.into(),
160 ast::TypeAlias(it) => sema.to_def(&it)?.into(),
161 ast::Variant(it) => sema.to_def(&it)?.into(),
162 ast::Trait(it) => sema.to_def(&it)?.into(),
163 ast::Impl(it) => return sema.to_def(&it).map(Definition::SelfType),
164 ast::MacroRules(it) => return sema.to_def(&it).map(Definition::Macro),
165 ast::TupleField(it) => return sema.to_def(&it).map(Definition::Field),
166 ast::RecordField(it) => return sema.to_def(&it).map(Definition::Field),
167 _ => return None,
168 }
169 };
170 Some(Definition::ModuleDef(res))
171}
172
173pub(crate) fn resolve_doc_path_for_def(
174 db: &dyn HirDatabase,
175 def: Definition,
176 link: &str,
177 ns: Option<hir::Namespace>,
178) -> Option<hir::ModuleDef> {
179 match def {
180 Definition::ModuleDef(def) => match def {
181 ModuleDef::Module(it) => it.resolve_doc_path(db, &link, ns),
182 ModuleDef::Function(it) => it.resolve_doc_path(db, &link, ns),
183 ModuleDef::Adt(it) => it.resolve_doc_path(db, &link, ns),
184 ModuleDef::Variant(it) => it.resolve_doc_path(db, &link, ns),
185 ModuleDef::Const(it) => it.resolve_doc_path(db, &link, ns),
186 ModuleDef::Static(it) => it.resolve_doc_path(db, &link, ns),
187 ModuleDef::Trait(it) => it.resolve_doc_path(db, &link, ns),
188 ModuleDef::TypeAlias(it) => it.resolve_doc_path(db, &link, ns),
189 ModuleDef::BuiltinType(_) => None,
190 },
191 Definition::Macro(it) => it.resolve_doc_path(db, &link, ns),
192 Definition::Field(it) => it.resolve_doc_path(db, &link, ns),
193 Definition::SelfType(_)
194 | Definition::Local(_)
195 | Definition::GenericParam(_)
196 | Definition::Label(_) => None,
197 }
198}
199
121// FIXME: 200// FIXME:
122// BUG: For Option::Some 201// BUG: For Option::Some
123// Returns https://doc.rust-lang.org/nightly/core/prelude/v1/enum.Option.html#variant.Some 202// Returns https://doc.rust-lang.org/nightly/core/prelude/v1/enum.Option.html#variant.Some
@@ -197,26 +276,8 @@ fn rewrite_intra_doc_link(
197 title: &str, 276 title: &str,
198) -> Option<(String, String)> { 277) -> Option<(String, String)> {
199 let link = if target.is_empty() { title } else { target }; 278 let link = if target.is_empty() { title } else { target };
200 let (link, ns) = parse_link(link); 279 let (link, ns) = parse_intra_doc_link(link);
201 let resolved = match def { 280 let resolved = resolve_doc_path_for_def(db, def, link, ns)?;
202 Definition::ModuleDef(def) => match def {
203 ModuleDef::Module(it) => it.resolve_doc_path(db, link, ns),
204 ModuleDef::Function(it) => it.resolve_doc_path(db, link, ns),
205 ModuleDef::Adt(it) => it.resolve_doc_path(db, link, ns),
206 ModuleDef::Variant(it) => it.resolve_doc_path(db, link, ns),
207 ModuleDef::Const(it) => it.resolve_doc_path(db, link, ns),
208 ModuleDef::Static(it) => it.resolve_doc_path(db, link, ns),
209 ModuleDef::Trait(it) => it.resolve_doc_path(db, link, ns),
210 ModuleDef::TypeAlias(it) => it.resolve_doc_path(db, link, ns),
211 ModuleDef::BuiltinType(_) => return None,
212 },
213 Definition::Macro(it) => it.resolve_doc_path(db, link, ns),
214 Definition::Field(it) => it.resolve_doc_path(db, link, ns),
215 Definition::SelfType(_)
216 | Definition::Local(_)
217 | Definition::GenericParam(_)
218 | Definition::Label(_) => return None,
219 }?;
220 let krate = resolved.module(db)?.krate(); 281 let krate = resolved.module(db)?.krate();
221 let canonical_path = resolved.canonical_path(db)?; 282 let canonical_path = resolved.canonical_path(db)?;
222 let mut new_url = get_doc_url(db, &krate)? 283 let mut new_url = get_doc_url(db, &krate)?
@@ -228,24 +289,23 @@ fn rewrite_intra_doc_link(
228 .ok()?; 289 .ok()?;
229 290
230 if let ModuleDef::Trait(t) = resolved { 291 if let ModuleDef::Trait(t) = resolved {
231 let items = t.items(db); 292 if let Some(assoc_item) = t.items(db).into_iter().find_map(|assoc_item| {
232 if let Some(field_or_assoc_item) = items.iter().find_map(|assoc_item| {
233 if let Some(name) = assoc_item.name(db) { 293 if let Some(name) = assoc_item.name(db) {
234 if *link == format!("{}::{}", canonical_path, name) { 294 if *link == format!("{}::{}", canonical_path, name) {
235 return Some(FieldOrAssocItem::AssocItem(*assoc_item)); 295 return Some(assoc_item);
236 } 296 }
237 } 297 }
238 None 298 None
239 }) { 299 }) {
240 if let Some(fragment) = get_symbol_fragment(db, &field_or_assoc_item) { 300 if let Some(fragment) =
301 get_symbol_fragment(db, &FieldOrAssocItem::AssocItem(assoc_item))
302 {
241 new_url = new_url.join(&fragment).ok()?; 303 new_url = new_url.join(&fragment).ok()?;
242 } 304 }
243 }; 305 };
244 } 306 }
245 307
246 let new_target = new_url.into_string(); 308 Some((new_url.into_string(), strip_prefixes_suffixes(title).to_string()))
247 let new_title = strip_prefixes_suffixes(title);
248 Some((new_target, new_title.to_string()))
249} 309}
250 310
251/// Try to resolve path to local documentation via path-based links (i.e. `../gateway/struct.Shard.html`). 311/// Try to resolve path to local documentation via path-based links (i.e. `../gateway/struct.Shard.html`).
@@ -322,73 +382,61 @@ fn map_links<'e>(
322 }) 382 })
323} 383}
324 384
325fn parse_link(s: &str) -> (&str, Option<hir::Namespace>) { 385const TYPES: ([&str; 9], [&str; 0]) =
326 let path = strip_prefixes_suffixes(s); 386 (["type", "struct", "enum", "mod", "trait", "union", "module", "prim", "primitive"], []);
327 let ns = ns_from_intra_spec(s); 387const VALUES: ([&str; 8], [&str; 1]) =
328 (path, ns)
329}
330
331/// Strip prefixes, suffixes, and inline code marks from the given string.
332fn strip_prefixes_suffixes(mut s: &str) -> &str {
333 s = s.trim_matches('`');
334
335 [
336 (TYPES.0.iter(), TYPES.1.iter()),
337 (VALUES.0.iter(), VALUES.1.iter()),
338 (MACROS.0.iter(), MACROS.1.iter()),
339 ]
340 .iter()
341 .for_each(|(prefixes, suffixes)| {
342 prefixes.clone().for_each(|prefix| s = s.trim_start_matches(*prefix));
343 suffixes.clone().for_each(|suffix| s = s.trim_end_matches(*suffix));
344 });
345 s.trim_start_matches('@').trim()
346}
347
348static TYPES: ([&str; 7], [&str; 0]) =
349 (["type", "struct", "enum", "mod", "trait", "union", "module"], []);
350static VALUES: ([&str; 8], [&str; 1]) =
351 (["value", "function", "fn", "method", "const", "static", "mod", "module"], ["()"]); 388 (["value", "function", "fn", "method", "const", "static", "mod", "module"], ["()"]);
352static MACROS: ([&str; 1], [&str; 1]) = (["macro"], ["!"]); 389const MACROS: ([&str; 2], [&str; 1]) = (["macro", "derive"], ["!"]);
353 390
354/// Extract the specified namespace from an intra-doc-link if one exists. 391/// Extract the specified namespace from an intra-doc-link if one exists.
355/// 392///
356/// # Examples 393/// # Examples
357/// 394///
358/// * `struct MyStruct` -> `Namespace::Types` 395/// * `struct MyStruct` -> ("MyStruct", `Namespace::Types`)
359/// * `panic!` -> `Namespace::Macros` 396/// * `panic!` -> ("panic", `Namespace::Macros`)
360/// * `fn@from_intra_spec` -> `Namespace::Values` 397/// * `fn@from_intra_spec` -> ("from_intra_spec", `Namespace::Values`)
361fn ns_from_intra_spec(s: &str) -> Option<hir::Namespace> { 398fn parse_intra_doc_link(s: &str) -> (&str, Option<hir::Namespace>) {
399 let s = s.trim_matches('`');
400
362 [ 401 [
363 (hir::Namespace::Types, (TYPES.0.iter(), TYPES.1.iter())), 402 (hir::Namespace::Types, (TYPES.0.iter(), TYPES.1.iter())),
364 (hir::Namespace::Values, (VALUES.0.iter(), VALUES.1.iter())), 403 (hir::Namespace::Values, (VALUES.0.iter(), VALUES.1.iter())),
365 (hir::Namespace::Macros, (MACROS.0.iter(), MACROS.1.iter())), 404 (hir::Namespace::Macros, (MACROS.0.iter(), MACROS.1.iter())),
366 ] 405 ]
367 .iter() 406 .iter()
368 .filter(|(_ns, (prefixes, suffixes))| { 407 .cloned()
369 prefixes 408 .find_map(|(ns, (mut prefixes, mut suffixes))| {
370 .clone() 409 if let Some(prefix) = prefixes.find(|&&prefix| {
371 .map(|prefix| { 410 s.starts_with(prefix)
372 s.starts_with(*prefix) 411 && s.chars().nth(prefix.len()).map_or(false, |c| c == '@' || c == ' ')
373 && s.chars() 412 }) {
374 .nth(prefix.len() + 1) 413 Some((&s[prefix.len() + 1..], ns))
375 .map(|c| c == '@' || c == ' ') 414 } else {
376 .unwrap_or(false) 415 suffixes.find_map(|&suffix| s.strip_suffix(suffix).zip(Some(ns)))
377 }) 416 }
378 .any(|cond| cond) 417 })
379 || suffixes 418 .map_or((s, None), |(s, ns)| (s, Some(ns)))
380 .clone() 419}
381 .map(|suffix| { 420
382 s.starts_with(*suffix) 421fn strip_prefixes_suffixes(s: &str) -> &str {
383 && s.chars() 422 [
384 .nth(suffix.len() + 1) 423 (TYPES.0.iter(), TYPES.1.iter()),
385 .map(|c| c == '@' || c == ' ') 424 (VALUES.0.iter(), VALUES.1.iter()),
386 .unwrap_or(false) 425 (MACROS.0.iter(), MACROS.1.iter()),
387 }) 426 ]
388 .any(|cond| cond) 427 .iter()
428 .cloned()
429 .find_map(|(mut prefixes, mut suffixes)| {
430 if let Some(prefix) = prefixes.find(|&&prefix| {
431 s.starts_with(prefix)
432 && s.chars().nth(prefix.len()).map_or(false, |c| c == '@' || c == ' ')
433 }) {
434 Some(&s[prefix.len() + 1..])
435 } else {
436 suffixes.find_map(|&suffix| s.strip_suffix(suffix))
437 }
389 }) 438 })
390 .map(|(ns, (_, _))| *ns) 439 .unwrap_or(s)
391 .next()
392} 440}
393 441
394/// Get the root URL for the documentation of a crate. 442/// Get the root URL for the documentation of a crate.
diff --git a/crates/ide/src/goto_definition.rs b/crates/ide/src/goto_definition.rs
index 473d48c2f..a2c97061f 100644
--- a/crates/ide/src/goto_definition.rs
+++ b/crates/ide/src/goto_definition.rs
@@ -1,18 +1,14 @@
1use std::ops::Range;
2
3use either::Either; 1use either::Either;
4use hir::{HasAttrs, ModuleDef, Semantics}; 2use hir::Semantics;
5use ide_db::{ 3use ide_db::{
6 defs::{Definition, NameClass, NameRefClass}, 4 defs::{NameClass, NameRefClass},
7 RootDatabase, 5 RootDatabase,
8}; 6};
9use syntax::{ 7use syntax::{ast, match_ast, AstNode, AstToken, SyntaxKind::*, SyntaxToken, TokenAtOffset, T};
10 ast, match_ast, AstNode, AstToken, SyntaxKind::*, SyntaxToken, TextRange, TextSize,
11 TokenAtOffset, T,
12};
13 8
14use crate::{ 9use crate::{
15 display::TryToNav, doc_links::extract_definitions_from_markdown, runnables::doc_owner_to_def, 10 display::TryToNav,
11 doc_links::{doc_owner_to_def, extract_positioned_link_from_comment, resolve_doc_path_for_def},
16 FilePosition, NavigationTarget, RangeInfo, 12 FilePosition, NavigationTarget, RangeInfo,
17}; 13};
18 14
@@ -35,7 +31,9 @@ pub(crate) fn goto_definition(
35 let token = sema.descend_into_macros(original_token.clone()); 31 let token = sema.descend_into_macros(original_token.clone());
36 let parent = token.parent()?; 32 let parent = token.parent()?;
37 if let Some(comment) = ast::Comment::cast(token) { 33 if let Some(comment) = ast::Comment::cast(token) {
38 let nav = def_for_doc_comment(&sema, position, &comment)?.try_to_nav(db)?; 34 let (_, link, ns) = extract_positioned_link_from_comment(position.offset, &comment)?;
35 let def = doc_owner_to_def(&sema, &parent)?;
36 let nav = resolve_doc_path_for_def(db, def, &link, ns)?.try_to_nav(db)?;
39 return Some(RangeInfo::new(original_token.text_range(), vec![nav])); 37 return Some(RangeInfo::new(original_token.text_range(), vec![nav]));
40 } 38 }
41 39
@@ -61,54 +59,6 @@ pub(crate) fn goto_definition(
61 Some(RangeInfo::new(original_token.text_range(), nav.into_iter().collect())) 59 Some(RangeInfo::new(original_token.text_range(), nav.into_iter().collect()))
62} 60}
63 61
64fn def_for_doc_comment(
65 sema: &Semantics<RootDatabase>,
66 position: FilePosition,
67 doc_comment: &ast::Comment,
68) -> Option<hir::ModuleDef> {
69 let parent = doc_comment.syntax().parent()?;
70 let (link, ns) = extract_positioned_link_from_comment(position, doc_comment)?;
71
72 let def = doc_owner_to_def(sema, parent)?;
73 match def {
74 Definition::ModuleDef(def) => match def {
75 ModuleDef::Module(it) => it.resolve_doc_path(sema.db, &link, ns),
76 ModuleDef::Function(it) => it.resolve_doc_path(sema.db, &link, ns),
77 ModuleDef::Adt(it) => it.resolve_doc_path(sema.db, &link, ns),
78 ModuleDef::Variant(it) => it.resolve_doc_path(sema.db, &link, ns),
79 ModuleDef::Const(it) => it.resolve_doc_path(sema.db, &link, ns),
80 ModuleDef::Static(it) => it.resolve_doc_path(sema.db, &link, ns),
81 ModuleDef::Trait(it) => it.resolve_doc_path(sema.db, &link, ns),
82 ModuleDef::TypeAlias(it) => it.resolve_doc_path(sema.db, &link, ns),
83 ModuleDef::BuiltinType(_) => return None,
84 },
85 Definition::Macro(it) => it.resolve_doc_path(sema.db, &link, ns),
86 Definition::Field(it) => it.resolve_doc_path(sema.db, &link, ns),
87 Definition::SelfType(_)
88 | Definition::Local(_)
89 | Definition::GenericParam(_)
90 | Definition::Label(_) => return None,
91 }
92}
93
94fn extract_positioned_link_from_comment(
95 position: FilePosition,
96 comment: &ast::Comment,
97) -> Option<(String, Option<hir::Namespace>)> {
98 let doc_comment = comment.doc_comment()?;
99 let comment_start =
100 comment.syntax().text_range().start() + TextSize::from(comment.prefix().len() as u32);
101 let def_links = extract_definitions_from_markdown(doc_comment);
102 let (def_link, ns, _) = def_links.into_iter().find(|&(_, _, Range { start, end })| {
103 TextRange::at(
104 comment_start + TextSize::from(start as u32),
105 TextSize::from((end - start) as u32),
106 )
107 .contains(position.offset)
108 })?;
109 Some((def_link, ns))
110}
111
112fn pick_best(tokens: TokenAtOffset<SyntaxToken>) -> Option<SyntaxToken> { 62fn pick_best(tokens: TokenAtOffset<SyntaxToken>) -> Option<SyntaxToken> {
113 return tokens.max_by_key(priority); 63 return tokens.max_by_key(priority);
114 fn priority(n: &SyntaxToken) -> usize { 64 fn priority(n: &SyntaxToken) -> usize {
diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs
index a3fb17c0a..c43089476 100644
--- a/crates/ide/src/hover.rs
+++ b/crates/ide/src/hover.rs
@@ -11,11 +11,14 @@ use ide_db::{
11}; 11};
12use itertools::Itertools; 12use itertools::Itertools;
13use stdx::format_to; 13use stdx::format_to;
14use syntax::{ast, match_ast, AstNode, SyntaxKind::*, SyntaxToken, TokenAtOffset, T}; 14use syntax::{ast, match_ast, AstNode, AstToken, SyntaxKind::*, SyntaxToken, TokenAtOffset, T};
15 15
16use crate::{ 16use crate::{
17 display::{macro_label, TryToNav}, 17 display::{macro_label, TryToNav},
18 doc_links::{remove_links, rewrite_links}, 18 doc_links::{
19 doc_owner_to_def, extract_positioned_link_from_comment, remove_links,
20 resolve_doc_path_for_def, rewrite_links,
21 },
19 markdown_remove::remove_markdown, 22 markdown_remove::remove_markdown,
20 markup::Markup, 23 markup::Markup,
21 runnables::{runnable_fn, runnable_mod}, 24 runnables::{runnable_fn, runnable_mod},
@@ -93,20 +96,35 @@ pub(crate) fn hover(
93 let mut res = HoverResult::default(); 96 let mut res = HoverResult::default();
94 97
95 let node = token.parent()?; 98 let node = token.parent()?;
99 let mut range = None;
96 let definition = match_ast! { 100 let definition = match_ast! {
97 match node { 101 match node {
98 // we don't use NameClass::referenced_or_defined here as we do not want to resolve 102 // we don't use NameClass::referenced_or_defined here as we do not want to resolve
99 // field pattern shorthands to their definition 103 // field pattern shorthands to their definition
100 ast::Name(name) => NameClass::classify(&sema, &name).and_then(|class| match class { 104 ast::Name(name) => NameClass::classify(&sema, &name).and_then(|class| match class {
101 NameClass::ConstReference(def) => Some(def), 105 NameClass::ConstReference(def) => Some(def),
102 def => def.defined(sema.db), 106 def => def.defined(db),
103 }), 107 }),
104 ast::NameRef(name_ref) => NameRefClass::classify(&sema, &name_ref).map(|d| d.referenced(sema.db)), 108 ast::NameRef(name_ref) => {
105 ast::Lifetime(lifetime) => NameClass::classify_lifetime(&sema, &lifetime) 109 NameRefClass::classify(&sema, &name_ref).map(|d| d.referenced(db))
106 .map_or_else(|| NameRefClass::classify_lifetime(&sema, &lifetime).map(|d| d.referenced(sema.db)), |d| d.defined(sema.db)), 110 },
107 _ => None, 111 ast::Lifetime(lifetime) => NameClass::classify_lifetime(&sema, &lifetime).map_or_else(
112 || NameRefClass::classify_lifetime(&sema, &lifetime).map(|d| d.referenced(db)),
113 |d| d.defined(db),
114 ),
115
116 _ => ast::Comment::cast(token.clone())
117 .and_then(|comment| {
118 let (idl_range, link, ns) =
119 extract_positioned_link_from_comment(position.offset, &comment)?;
120 range = Some(idl_range);
121 let def = doc_owner_to_def(&sema, &node)?;
122 resolve_doc_path_for_def(db, def, &link, ns)
123 })
124 .map(Definition::ModuleDef),
108 } 125 }
109 }; 126 };
127
110 if let Some(definition) = definition { 128 if let Some(definition) = definition {
111 let famous_defs = match &definition { 129 let famous_defs = match &definition {
112 Definition::ModuleDef(ModuleDef::BuiltinType(_)) => { 130 Definition::ModuleDef(ModuleDef::BuiltinType(_)) => {
@@ -128,15 +146,16 @@ pub(crate) fn hover(
128 res.actions.push(action); 146 res.actions.push(action);
129 } 147 }
130 148
131 let range = sema.original_range(&node).range; 149 let range = range.unwrap_or_else(|| sema.original_range(&node).range);
132 return Some(RangeInfo::new(range, res)); 150 return Some(RangeInfo::new(range, res));
133 } 151 }
134 } 152 }
135 153
136 if token.kind() == syntax::SyntaxKind::COMMENT { 154 if token.kind() == syntax::SyntaxKind::COMMENT {
137 // don't highlight the entire parent node on comment hover 155 cov_mark::hit!(no_highlight_on_comment_hover);
138 return None; 156 return None;
139 } 157 }
158
140 if let res @ Some(_) = hover_for_keyword(&sema, links_in_hover, markdown, &token) { 159 if let res @ Some(_) = hover_for_keyword(&sema, links_in_hover, markdown, &token) {
141 return res; 160 return res;
142 } 161 }
@@ -3483,6 +3502,7 @@ fn foo$0() {}
3483 3502
3484 #[test] 3503 #[test]
3485 fn hover_comments_dont_highlight_parent() { 3504 fn hover_comments_dont_highlight_parent() {
3505 cov_mark::check!(no_highlight_on_comment_hover);
3486 check_hover_no_result( 3506 check_hover_no_result(
3487 r#" 3507 r#"
3488fn no_hover() { 3508fn no_hover() {
@@ -3755,4 +3775,29 @@ fn main() {
3755 "#]], 3775 "#]],
3756 ) 3776 )
3757 } 3777 }
3778
3779 #[test]
3780 fn hover_intra_doc_links() {
3781 check(
3782 r#"
3783/// This is the [`foo`](foo$0) function.
3784fn foo() {}
3785"#,
3786 expect![[r#"
3787 *[`foo`](foo)*
3788
3789 ```rust
3790 test
3791 ```
3792
3793 ```rust
3794 fn foo()
3795 ```
3796
3797 ---
3798
3799 This is the [`foo`](https://docs.rs/test/*/test/fn.foo.html) function.
3800 "#]],
3801 );
3802 }
3758} 3803}
diff --git a/crates/ide/src/references/rename.rs b/crates/ide/src/references/rename.rs
index b1ca6d50f..26d6dc9c9 100644
--- a/crates/ide/src/references/rename.rs
+++ b/crates/ide/src/references/rename.rs
@@ -219,40 +219,44 @@ fn rename_reference(
219) -> RenameResult<SourceChange> { 219) -> RenameResult<SourceChange> {
220 let ident_kind = check_identifier(new_name)?; 220 let ident_kind = check_identifier(new_name)?;
221 221
222 let def_is_lbl_or_lt = matches!( 222 if matches!(
223 def, 223 def, // is target a lifetime?
224 Definition::GenericParam(hir::GenericParam::LifetimeParam(_)) | Definition::Label(_) 224 Definition::GenericParam(hir::GenericParam::LifetimeParam(_)) | Definition::Label(_)
225 ); 225 ) {
226 match (ident_kind, def) { 226 match ident_kind {
227 (IdentifierKind::ToSelf, _) 227 IdentifierKind::Ident | IdentifierKind::ToSelf | IdentifierKind::Underscore => {
228 | (IdentifierKind::Underscore, _) 228 cov_mark::hit!(rename_not_a_lifetime_ident_ref);
229 | (IdentifierKind::Ident, _) 229 bail!("Invalid name `{}`: not a lifetime identifier", new_name);
230 if def_is_lbl_or_lt => 230 }
231 { 231 IdentifierKind::Lifetime => cov_mark::hit!(rename_lifetime),
232 cov_mark::hit!(rename_not_a_lifetime_ident_ref);
233 bail!("Invalid name `{}`: not a lifetime identifier", new_name)
234 }
235 (IdentifierKind::Lifetime, _) if def_is_lbl_or_lt => cov_mark::hit!(rename_lifetime),
236 (IdentifierKind::Lifetime, _) => {
237 cov_mark::hit!(rename_not_an_ident_ref);
238 bail!("Invalid name `{}`: not an identifier", new_name)
239 }
240 (IdentifierKind::ToSelf, Definition::Local(local)) if local.is_self(sema.db) => {
241 // no-op
242 cov_mark::hit!(rename_self_to_self);
243 return Ok(SourceChange::default());
244 }
245 (ident_kind, Definition::Local(local)) if local.is_self(sema.db) => {
246 cov_mark::hit!(rename_self_to_param);
247 return rename_self_to_param(sema, local, new_name, ident_kind);
248 }
249 (IdentifierKind::ToSelf, Definition::Local(local)) => {
250 cov_mark::hit!(rename_to_self);
251 return rename_to_self(sema, local);
252 } 232 }
253 (IdentifierKind::ToSelf, _) => bail!("Invalid name `{}`: not an identifier", new_name), 233 } else {
254 (IdentifierKind::Ident, _) | (IdentifierKind::Underscore, _) => { 234 match (ident_kind, def) {
255 cov_mark::hit!(rename_ident) 235 (IdentifierKind::Lifetime, _) => {
236 cov_mark::hit!(rename_not_an_ident_ref);
237 bail!("Invalid name `{}`: not an identifier", new_name);
238 }
239 (IdentifierKind::ToSelf, Definition::Local(local)) => {
240 if local.is_self(sema.db) {
241 // no-op
242 cov_mark::hit!(rename_self_to_self);
243 return Ok(SourceChange::default());
244 } else {
245 cov_mark::hit!(rename_to_self);
246 return rename_to_self(sema, local);
247 }
248 }
249 (ident_kind, Definition::Local(local)) => {
250 if let Some(self_param) = local.as_self_param(sema.db) {
251 cov_mark::hit!(rename_self_to_param);
252 return rename_self_to_param(sema, local, self_param, new_name, ident_kind);
253 } else {
254 cov_mark::hit!(rename_local);
255 }
256 }
257 (IdentifierKind::ToSelf, _) => bail!("Invalid name `{}`: not an identifier", new_name),
258 (IdentifierKind::Ident, _) => cov_mark::hit!(rename_non_local),
259 (IdentifierKind::Underscore, _) => (),
256 } 260 }
257 } 261 }
258 262
@@ -336,16 +340,12 @@ fn rename_to_self(sema: &Semantics<RootDatabase>, local: hir::Local) -> RenameRe
336fn rename_self_to_param( 340fn rename_self_to_param(
337 sema: &Semantics<RootDatabase>, 341 sema: &Semantics<RootDatabase>,
338 local: hir::Local, 342 local: hir::Local,
343 self_param: hir::SelfParam,
339 new_name: &str, 344 new_name: &str,
340 identifier_kind: IdentifierKind, 345 identifier_kind: IdentifierKind,
341) -> RenameResult<SourceChange> { 346) -> RenameResult<SourceChange> {
342 let (file_id, self_param) = match local.source(sema.db) { 347 let InFile { file_id, value: self_param } =
343 InFile { file_id, value: Either::Right(self_param) } => (file_id, self_param), 348 self_param.source(sema.db).ok_or_else(|| format_err!("cannot find function source"))?;
344 _ => {
345 never!(true, "rename_self_to_param invoked on a non-self local");
346 bail!("rename_self_to_param invoked on a non-self local");
347 }
348 };
349 349
350 let def = Definition::Local(local); 350 let def = Definition::Local(local);
351 let usages = def.usages(sema).all(); 351 let usages = def.usages(sema).all();
@@ -701,7 +701,7 @@ foo!(Foo$0);",
701 701
702 #[test] 702 #[test]
703 fn test_rename_for_local() { 703 fn test_rename_for_local() {
704 cov_mark::check!(rename_ident); 704 cov_mark::check!(rename_local);
705 check( 705 check(
706 "k", 706 "k",
707 r#" 707 r#"
@@ -1242,6 +1242,7 @@ pub mod foo$0;
1242 1242
1243 #[test] 1243 #[test]
1244 fn test_enum_variant_from_module_1() { 1244 fn test_enum_variant_from_module_1() {
1245 cov_mark::check!(rename_non_local);
1245 check( 1246 check(
1246 "Baz", 1247 "Baz",
1247 r#" 1248 r#"
diff --git a/crates/ide/src/runnables.rs b/crates/ide/src/runnables.rs
index bea020b06..5b488e2c5 100644
--- a/crates/ide/src/runnables.rs
+++ b/crates/ide/src/runnables.rs
@@ -7,17 +7,13 @@ use hir::{AsAssocItem, HasAttrs, HasSource, HirDisplay, Semantics};
7use ide_assists::utils::test_related_attribute; 7use ide_assists::utils::test_related_attribute;
8use ide_db::{ 8use ide_db::{
9 base_db::{FilePosition, FileRange}, 9 base_db::{FilePosition, FileRange},
10 defs::Definition,
11 helpers::visit_file_defs, 10 helpers::visit_file_defs,
12 search::SearchScope, 11 search::SearchScope,
13 RootDatabase, SymbolKind, 12 RootDatabase, SymbolKind,
14}; 13};
15use itertools::Itertools; 14use itertools::Itertools;
16use rustc_hash::FxHashSet; 15use rustc_hash::FxHashSet;
17use syntax::{ 16use syntax::ast::{self, AstNode, AttrsOwner};
18 ast::{self, AstNode, AttrsOwner},
19 match_ast, SyntaxNode,
20};
21 17
22use crate::{ 18use crate::{
23 display::{ToNav, TryToNav}, 19 display::{ToNav, TryToNav},
@@ -271,28 +267,6 @@ pub(crate) fn runnable_mod(sema: &Semantics<RootDatabase>, def: hir::Module) ->
271 Some(Runnable { nav, kind: RunnableKind::TestMod { path }, cfg }) 267 Some(Runnable { nav, kind: RunnableKind::TestMod { path }, cfg })
272} 268}
273 269
274// FIXME: figure out a proper API here.
275pub(crate) fn doc_owner_to_def(
276 sema: &Semantics<RootDatabase>,
277 item: SyntaxNode,
278) -> Option<Definition> {
279 let res: hir::ModuleDef = match_ast! {
280 match item {
281 ast::SourceFile(_it) => sema.scope(&item).module()?.into(),
282 ast::Fn(it) => sema.to_def(&it)?.into(),
283 ast::Struct(it) => sema.to_def(&it)?.into(),
284 ast::Enum(it) => sema.to_def(&it)?.into(),
285 ast::Union(it) => sema.to_def(&it)?.into(),
286 ast::Trait(it) => sema.to_def(&it)?.into(),
287 ast::Const(it) => sema.to_def(&it)?.into(),
288 ast::Static(it) => sema.to_def(&it)?.into(),
289 ast::TypeAlias(it) => sema.to_def(&it)?.into(),
290 _ => return None,
291 }
292 };
293 Some(Definition::ModuleDef(res))
294}
295
296fn module_def_doctest(sema: &Semantics<RootDatabase>, def: hir::ModuleDef) -> Option<Runnable> { 270fn module_def_doctest(sema: &Semantics<RootDatabase>, def: hir::ModuleDef) -> Option<Runnable> {
297 let attrs = match def { 271 let attrs = match def {
298 hir::ModuleDef::Module(it) => it.attrs(sema.db), 272 hir::ModuleDef::Module(it) => it.attrs(sema.db),
diff --git a/crates/ide/src/syntax_highlighting/inject.rs b/crates/ide/src/syntax_highlighting/inject.rs
index 8e0940184..38bf49348 100644
--- a/crates/ide/src/syntax_highlighting/inject.rs
+++ b/crates/ide/src/syntax_highlighting/inject.rs
@@ -190,10 +190,10 @@ pub(super) fn doc_comment(
190 intra_doc_links.extend( 190 intra_doc_links.extend(
191 extract_definitions_from_markdown(line) 191 extract_definitions_from_markdown(line)
192 .into_iter() 192 .into_iter()
193 .filter_map(|(link, ns, range)| { 193 .filter_map(|(range, link, ns)| {
194 validate_intra_doc_link(sema.db, &def, &link, ns).zip(Some(range)) 194 Some(range).zip(validate_intra_doc_link(sema.db, &def, &link, ns))
195 }) 195 })
196 .map(|(def, Range { start, end })| { 196 .map(|(Range { start, end }, def)| {
197 ( 197 (
198 def, 198 def,
199 TextRange::at( 199 TextRange::at(
diff --git a/crates/ide_assists/src/handlers/reorder_fields.rs b/crates/ide_assists/src/handlers/reorder_fields.rs
index 794c89323..383ca6c47 100644
--- a/crates/ide_assists/src/handlers/reorder_fields.rs
+++ b/crates/ide_assists/src/handlers/reorder_fields.rs
@@ -1,9 +1,6 @@
1use itertools::Itertools;
2use rustc_hash::FxHashMap; 1use rustc_hash::FxHashMap;
3 2
4use hir::{Adt, ModuleDef, PathResolution, Semantics, Struct}; 3use syntax::{algo, ast, match_ast, AstNode, SyntaxKind::*, SyntaxNode};
5use ide_db::RootDatabase;
6use syntax::{algo, ast, match_ast, AstNode, SyntaxKind, SyntaxKind::*, SyntaxNode};
7 4
8use crate::{AssistContext, AssistId, AssistKind, Assists}; 5use crate::{AssistContext, AssistId, AssistKind, Assists};
9 6
@@ -23,26 +20,39 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
23// ``` 20// ```
24// 21//
25pub(crate) fn reorder_fields(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { 22pub(crate) fn reorder_fields(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
26 reorder::<ast::RecordExpr>(acc, ctx).or_else(|| reorder::<ast::RecordPat>(acc, ctx)) 23 let record = ctx
27} 24 .find_node_at_offset::<ast::RecordExpr>()
25 .map(|it| it.syntax().clone())
26 .or_else(|| ctx.find_node_at_offset::<ast::RecordPat>().map(|it| it.syntax().clone()))?;
28 27
29fn reorder<R: AstNode>(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { 28 let path = record.children().find_map(ast::Path::cast)?;
30 let record = ctx.find_node_at_offset::<R>()?;
31 let path = record.syntax().children().find_map(ast::Path::cast)?;
32 29
33 let ranks = compute_fields_ranks(&path, &ctx)?; 30 let ranks = compute_fields_ranks(&path, &ctx)?;
34 31
35 let fields = get_fields(&record.syntax()); 32 let fields: Vec<SyntaxNode> = {
36 let sorted_fields = sorted_by_rank(&fields, |node| { 33 let field_kind = match record.kind() {
37 *ranks.get(&get_field_name(node)).unwrap_or(&usize::max_value()) 34 RECORD_EXPR => RECORD_EXPR_FIELD,
38 }); 35 RECORD_PAT => RECORD_PAT_FIELD,
36 _ => {
37 stdx::never!();
38 return None;
39 }
40 };
41 record.children().flat_map(|n| n.children()).filter(|n| n.kind() == field_kind).collect()
42 };
43
44 let sorted_fields = {
45 let mut fields = fields.clone();
46 fields.sort_by_key(|node| *ranks.get(&get_field_name(node)).unwrap_or(&usize::max_value()));
47 fields
48 };
39 49
40 if sorted_fields == fields { 50 if sorted_fields == fields {
41 cov_mark::hit!(reorder_sorted_fields); 51 cov_mark::hit!(reorder_sorted_fields);
42 return None; 52 return None;
43 } 53 }
44 54
45 let target = record.syntax().text_range(); 55 let target = record.text_range();
46 acc.add( 56 acc.add(
47 AssistId("reorder_fields", AssistKind::RefactorRewrite), 57 AssistId("reorder_fields", AssistKind::RefactorRewrite),
48 "Reorder record fields", 58 "Reorder record fields",
@@ -57,14 +67,6 @@ fn reorder<R: AstNode>(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
57 ) 67 )
58} 68}
59 69
60fn get_fields_kind(node: &SyntaxNode) -> Vec<SyntaxKind> {
61 match node.kind() {
62 RECORD_EXPR => vec![RECORD_EXPR_FIELD],
63 RECORD_PAT => vec![RECORD_PAT_FIELD, IDENT_PAT],
64 _ => vec![],
65 }
66}
67
68fn get_field_name(node: &SyntaxNode) -> String { 70fn get_field_name(node: &SyntaxNode) -> String {
69 let res = match_ast! { 71 let res = match_ast! {
70 match node { 72 match node {
@@ -76,34 +78,20 @@ fn get_field_name(node: &SyntaxNode) -> String {
76 res.unwrap_or_default() 78 res.unwrap_or_default()
77} 79}
78 80
79fn get_fields(record: &SyntaxNode) -> Vec<SyntaxNode> { 81fn compute_fields_ranks(path: &ast::Path, ctx: &AssistContext) -> Option<FxHashMap<String, usize>> {
80 let kinds = get_fields_kind(record); 82 let strukt = match ctx.sema.resolve_path(path) {
81 record.children().flat_map(|n| n.children()).filter(|n| kinds.contains(&n.kind())).collect() 83 Some(hir::PathResolution::Def(hir::ModuleDef::Adt(hir::Adt::Struct(it)))) => it,
82} 84 _ => return None,
83 85 };
84fn sorted_by_rank(
85 fields: &[SyntaxNode],
86 get_rank: impl Fn(&SyntaxNode) -> usize,
87) -> Vec<SyntaxNode> {
88 fields.iter().cloned().sorted_by_key(get_rank).collect()
89}
90 86
91fn struct_definition(path: &ast::Path, sema: &Semantics<RootDatabase>) -> Option<Struct> { 87 let res = strukt
92 match sema.resolve_path(path) { 88 .fields(ctx.db())
93 Some(PathResolution::Def(ModuleDef::Adt(Adt::Struct(s)))) => Some(s), 89 .iter()
94 _ => None, 90 .enumerate()
95 } 91 .map(|(idx, field)| (field.name(ctx.db()).to_string(), idx))
96} 92 .collect();
97 93
98fn compute_fields_ranks(path: &ast::Path, ctx: &AssistContext) -> Option<FxHashMap<String, usize>> { 94 Some(res)
99 Some(
100 struct_definition(path, &ctx.sema)?
101 .fields(ctx.db())
102 .iter()
103 .enumerate()
104 .map(|(idx, field)| (field.name(ctx.db()).to_string(), idx))
105 .collect(),
106 )
107} 95}
108 96
109#[cfg(test)] 97#[cfg(test)]
@@ -118,11 +106,7 @@ mod tests {
118 check_assist_not_applicable( 106 check_assist_not_applicable(
119 reorder_fields, 107 reorder_fields,
120 r#" 108 r#"
121struct Foo { 109struct Foo { foo: i32, bar: i32 }
122 foo: i32,
123 bar: i32,
124}
125
126const test: Foo = $0Foo { foo: 0, bar: 0 }; 110const test: Foo = $0Foo { foo: 0, bar: 0 };
127"#, 111"#,
128 ) 112 )
@@ -133,8 +117,8 @@ const test: Foo = $0Foo { foo: 0, bar: 0 };
133 check_assist_not_applicable( 117 check_assist_not_applicable(
134 reorder_fields, 118 reorder_fields,
135 r#" 119 r#"
136struct Foo {}; 120struct Foo {}
137const test: Foo = $0Foo {} 121const test: Foo = $0Foo {};
138"#, 122"#,
139 ) 123 )
140 } 124 }
@@ -144,12 +128,12 @@ const test: Foo = $0Foo {}
144 check_assist( 128 check_assist(
145 reorder_fields, 129 reorder_fields,
146 r#" 130 r#"
147struct Foo {foo: i32, bar: i32}; 131struct Foo { foo: i32, bar: i32 }
148const test: Foo = $0Foo {bar: 0, foo: 1} 132const test: Foo = $0Foo { bar: 0, foo: 1 };
149"#, 133"#,
150 r#" 134 r#"
151struct Foo {foo: i32, bar: i32}; 135struct Foo { foo: i32, bar: i32 }
152const test: Foo = Foo {foo: 1, bar: 0} 136const test: Foo = Foo { foo: 1, bar: 0 };
153"#, 137"#,
154 ) 138 )
155 } 139 }
@@ -186,10 +170,7 @@ fn f(f: Foo) -> {
186 check_assist( 170 check_assist(
187 reorder_fields, 171 reorder_fields,
188 r#" 172 r#"
189struct Foo { 173struct Foo { foo: String, bar: String }
190 foo: String,
191 bar: String,
192}
193 174
194impl Foo { 175impl Foo {
195 fn new() -> Foo { 176 fn new() -> Foo {
@@ -203,10 +184,7 @@ impl Foo {
203} 184}
204"#, 185"#,
205 r#" 186 r#"
206struct Foo { 187struct Foo { foo: String, bar: String }
207 foo: String,
208 bar: String,
209}
210 188
211impl Foo { 189impl Foo {
212 fn new() -> Foo { 190 fn new() -> Foo {
diff --git a/crates/proc_macro_api/src/msg.rs b/crates/proc_macro_api/src/msg.rs
index 970f165ed..f525df152 100644
--- a/crates/proc_macro_api/src/msg.rs
+++ b/crates/proc_macro_api/src/msg.rs
@@ -55,8 +55,8 @@ pub enum ErrorCode {
55} 55}
56 56
57pub trait Message: Serialize + DeserializeOwned { 57pub trait Message: Serialize + DeserializeOwned {
58 fn read(inp: &mut impl BufRead) -> io::Result<Option<Self>> { 58 fn read(inp: &mut impl BufRead, buf: &mut String) -> io::Result<Option<Self>> {
59 Ok(match read_json(inp)? { 59 Ok(match read_json(inp, buf)? {
60 None => None, 60 None => None,
61 Some(text) => { 61 Some(text) => {
62 let mut deserializer = serde_json::Deserializer::from_str(&text); 62 let mut deserializer = serde_json::Deserializer::from_str(&text);
@@ -76,14 +76,29 @@ pub trait Message: Serialize + DeserializeOwned {
76impl Message for Request {} 76impl Message for Request {}
77impl Message for Response {} 77impl Message for Response {}
78 78
79fn read_json(inp: &mut impl BufRead) -> io::Result<Option<String>> { 79fn read_json<'a>(
80 let mut buf = String::new(); 80 inp: &mut impl BufRead,
81 inp.read_line(&mut buf)?; 81 mut buf: &'a mut String,
82 buf.pop(); // Remove trailing '\n' 82) -> io::Result<Option<&'a String>> {
83 Ok(match buf.len() { 83 loop {
84 0 => None, 84 buf.clear();
85 _ => Some(buf), 85
86 }) 86 inp.read_line(&mut buf)?;
87 buf.pop(); // Remove trailing '\n'
88
89 if buf.is_empty() {
90 return Ok(None);
91 }
92
93 // Some ill behaved macro try to use stdout for debugging
94 // We ignore it here
95 if !buf.starts_with("{") {
96 log::error!("proc-macro tried to print : {}", buf);
97 continue;
98 }
99
100 return Ok(Some(buf));
101 }
87} 102}
88 103
89fn write_json(out: &mut impl Write, msg: &str) -> io::Result<()> { 104fn write_json(out: &mut impl Write, msg: &str) -> io::Result<()> {
diff --git a/crates/proc_macro_api/src/process.rs b/crates/proc_macro_api/src/process.rs
index 30bb1b687..99d05aef3 100644
--- a/crates/proc_macro_api/src/process.rs
+++ b/crates/proc_macro_api/src/process.rs
@@ -90,8 +90,10 @@ impl ProcMacroProcessSrv {
90fn client_loop(task_rx: Receiver<Task>, mut process: Process) { 90fn client_loop(task_rx: Receiver<Task>, mut process: Process) {
91 let (mut stdin, mut stdout) = process.stdio().expect("couldn't access child stdio"); 91 let (mut stdin, mut stdout) = process.stdio().expect("couldn't access child stdio");
92 92
93 let mut buf = String::new();
94
93 for Task { req, result_tx } in task_rx { 95 for Task { req, result_tx } in task_rx {
94 match send_request(&mut stdin, &mut stdout, req) { 96 match send_request(&mut stdin, &mut stdout, req, &mut buf) {
95 Ok(res) => result_tx.send(res).unwrap(), 97 Ok(res) => result_tx.send(res).unwrap(),
96 Err(err) => { 98 Err(err) => {
97 log::error!( 99 log::error!(
@@ -152,7 +154,8 @@ fn send_request(
152 mut writer: &mut impl Write, 154 mut writer: &mut impl Write,
153 mut reader: &mut impl BufRead, 155 mut reader: &mut impl BufRead,
154 req: Request, 156 req: Request,
157 buf: &mut String,
155) -> io::Result<Option<Response>> { 158) -> io::Result<Option<Response>> {
156 req.write(&mut writer)?; 159 req.write(&mut writer)?;
157 Response::read(&mut reader) 160 Response::read(&mut reader, buf)
158} 161}
diff --git a/crates/proc_macro_api/src/rpc.rs b/crates/proc_macro_api/src/rpc.rs
index 9a68e2cc5..8f7270afe 100644
--- a/crates/proc_macro_api/src/rpc.rs
+++ b/crates/proc_macro_api/src/rpc.rs
@@ -77,7 +77,11 @@ struct TokenIdDef(u32);
77#[derive(Serialize, Deserialize)] 77#[derive(Serialize, Deserialize)]
78#[serde(remote = "Delimiter")] 78#[serde(remote = "Delimiter")]
79struct DelimiterDef { 79struct DelimiterDef {
80 #[serde(with = "TokenIdDef")] 80 #[serde(
81 with = "TokenIdDef",
82 default = "tt::TokenId::unspecified",
83 skip_serializing_if = "token_id_def::skip_if"
84 )]
81 id: TokenId, 85 id: TokenId,
82 #[serde(with = "DelimiterKindDef")] 86 #[serde(with = "DelimiterKindDef")]
83 kind: DelimiterKind, 87 kind: DelimiterKind,
@@ -116,7 +120,11 @@ enum LeafDef {
116#[serde(remote = "Literal")] 120#[serde(remote = "Literal")]
117struct LiteralDef { 121struct LiteralDef {
118 text: SmolStr, 122 text: SmolStr,
119 #[serde(with = "TokenIdDef")] 123 #[serde(
124 with = "TokenIdDef",
125 default = "tt::TokenId::unspecified",
126 skip_serializing_if = "token_id_def::skip_if"
127 )]
120 id: TokenId, 128 id: TokenId,
121} 129}
122 130
@@ -126,7 +134,11 @@ struct PunctDef {
126 char: char, 134 char: char,
127 #[serde(with = "SpacingDef")] 135 #[serde(with = "SpacingDef")]
128 spacing: Spacing, 136 spacing: Spacing,
129 #[serde(with = "TokenIdDef")] 137 #[serde(
138 with = "TokenIdDef",
139 default = "tt::TokenId::unspecified",
140 skip_serializing_if = "token_id_def::skip_if"
141 )]
130 id: TokenId, 142 id: TokenId,
131} 143}
132 144
@@ -141,10 +153,20 @@ enum SpacingDef {
141#[serde(remote = "Ident")] 153#[serde(remote = "Ident")]
142struct IdentDef { 154struct IdentDef {
143 text: SmolStr, 155 text: SmolStr,
144 #[serde(with = "TokenIdDef")] 156 #[serde(
157 with = "TokenIdDef",
158 default = "tt::TokenId::unspecified",
159 skip_serializing_if = "token_id_def::skip_if"
160 )]
145 id: TokenId, 161 id: TokenId,
146} 162}
147 163
164mod token_id_def {
165 pub(super) fn skip_if(value: &tt::TokenId) -> bool {
166 *value == tt::TokenId::unspecified()
167 }
168}
169
148mod opt_delimiter_def { 170mod opt_delimiter_def {
149 use super::{Delimiter, DelimiterDef}; 171 use super::{Delimiter, DelimiterDef};
150 use serde::{Deserialize, Deserializer, Serialize, Serializer}; 172 use serde::{Deserialize, Deserializer, Serialize, Serializer};
diff --git a/crates/proc_macro_srv/src/cli.rs b/crates/proc_macro_srv/src/cli.rs
index d428b9567..bc48f1c43 100644
--- a/crates/proc_macro_srv/src/cli.rs
+++ b/crates/proc_macro_srv/src/cli.rs
@@ -6,8 +6,9 @@ use std::io;
6 6
7pub fn run() -> io::Result<()> { 7pub fn run() -> io::Result<()> {
8 let mut srv = ProcMacroSrv::default(); 8 let mut srv = ProcMacroSrv::default();
9 let mut buf = String::new();
9 10
10 while let Some(req) = read_request()? { 11 while let Some(req) = read_request(&mut buf)? {
11 let res = match req { 12 let res = match req {
12 msg::Request::ListMacro(task) => srv.list_macros(&task).map(msg::Response::ListMacro), 13 msg::Request::ListMacro(task) => srv.list_macros(&task).map(msg::Response::ListMacro),
13 msg::Request::ExpansionMacro(task) => { 14 msg::Request::ExpansionMacro(task) => {
@@ -30,8 +31,8 @@ pub fn run() -> io::Result<()> {
30 Ok(()) 31 Ok(())
31} 32}
32 33
33fn read_request() -> io::Result<Option<msg::Request>> { 34fn read_request(buf: &mut String) -> io::Result<Option<msg::Request>> {
34 msg::Request::read(&mut io::stdin().lock()) 35 msg::Request::read(&mut io::stdin().lock(), buf)
35} 36}
36 37
37fn write_response(msg: msg::Response) -> io::Result<()> { 38fn write_response(msg: msg::Response) -> io::Result<()> {
diff --git a/crates/rust-analyzer/tests/rust-analyzer/main.rs b/crates/rust-analyzer/tests/rust-analyzer/main.rs
index 19516de7b..4442cbff6 100644
--- a/crates/rust-analyzer/tests/rust-analyzer/main.rs
+++ b/crates/rust-analyzer/tests/rust-analyzer/main.rs
@@ -712,6 +712,10 @@ pub fn foo(_input: TokenStream) -> TokenStream {
712 // We hard code the output here for preventing to use any deps 712 // We hard code the output here for preventing to use any deps
713 let mut res = TokenStream::new(); 713 let mut res = TokenStream::new();
714 714
715 // ill behaved proc-macro will use the stdout
716 // we should ignore it
717 println!("I am bad guy");
718
715 // impl Bar for Foo { fn bar() {} } 719 // impl Bar for Foo { fn bar() {} }
716 let mut tokens = vec![t!("impl"), t!("Bar"), t!("for"), t!("Foo")]; 720 let mut tokens = vec![t!("impl"), t!("Bar"), t!("for"), t!("Foo")];
717 let mut fn_stream = TokenStream::new(); 721 let mut fn_stream = TokenStream::new();