diff options
24 files changed, 562 insertions, 68 deletions
diff --git a/Cargo.lock b/Cargo.lock index 4cfa4c519..fdb62e6ea 100644 --- a/Cargo.lock +++ b/Cargo.lock | |||
@@ -82,12 +82,12 @@ checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" | |||
82 | 82 | ||
83 | [[package]] | 83 | [[package]] |
84 | name = "backtrace" | 84 | name = "backtrace" |
85 | version = "0.3.51" | 85 | version = "0.3.53" |
86 | source = "registry+https://github.com/rust-lang/crates.io-index" | 86 | source = "registry+https://github.com/rust-lang/crates.io-index" |
87 | checksum = "ec1931848a574faa8f7c71a12ea00453ff5effbb5f51afe7f77d7a48cace6ac1" | 87 | checksum = "707b586e0e2f247cbde68cdd2c3ce69ea7b7be43e1c5b426e37c9319c4b9838e" |
88 | dependencies = [ | 88 | dependencies = [ |
89 | "addr2line", | 89 | "addr2line", |
90 | "cfg-if 0.1.10", | 90 | "cfg-if 1.0.0", |
91 | "libc", | 91 | "libc", |
92 | "miniz_oxide", | 92 | "miniz_oxide", |
93 | "object", | 93 | "object", |
@@ -140,9 +140,9 @@ dependencies = [ | |||
140 | 140 | ||
141 | [[package]] | 141 | [[package]] |
142 | name = "cc" | 142 | name = "cc" |
143 | version = "1.0.60" | 143 | version = "1.0.61" |
144 | source = "registry+https://github.com/rust-lang/crates.io-index" | 144 | source = "registry+https://github.com/rust-lang/crates.io-index" |
145 | checksum = "ef611cc68ff783f18535d77ddd080185275713d852c4f5cbb6122c462a7a825c" | 145 | checksum = "ed67cbde08356238e75fc4656be4749481eeffb09e19f320a25237d5221c985d" |
146 | 146 | ||
147 | [[package]] | 147 | [[package]] |
148 | name = "cfg" | 148 | name = "cfg" |
@@ -168,9 +168,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" | |||
168 | 168 | ||
169 | [[package]] | 169 | [[package]] |
170 | name = "chalk-derive" | 170 | name = "chalk-derive" |
171 | version = "0.32.0" | 171 | version = "0.33.0" |
172 | source = "registry+https://github.com/rust-lang/crates.io-index" | 172 | source = "registry+https://github.com/rust-lang/crates.io-index" |
173 | checksum = "2d072b2ba723f0bada7c515d8b3725224bc4f5052d2a92dcbeb0b118ff37084a" | 173 | checksum = "569014cab9084a6b826fe2507cc6d08f7897ba144fb1bc74e71b593dc8a0b952" |
174 | dependencies = [ | 174 | dependencies = [ |
175 | "proc-macro2", | 175 | "proc-macro2", |
176 | "quote", | 176 | "quote", |
@@ -180,9 +180,9 @@ dependencies = [ | |||
180 | 180 | ||
181 | [[package]] | 181 | [[package]] |
182 | name = "chalk-ir" | 182 | name = "chalk-ir" |
183 | version = "0.32.0" | 183 | version = "0.33.0" |
184 | source = "registry+https://github.com/rust-lang/crates.io-index" | 184 | source = "registry+https://github.com/rust-lang/crates.io-index" |
185 | checksum = "f60cdb0e18c5455cb6a85e8464aad3622b70476018edfa8845691df66f7e9a05" | 185 | checksum = "8d9eab2a6590b696419f89c9ca3616fe8e8266ef676e6a6da8818c94963c9541" |
186 | dependencies = [ | 186 | dependencies = [ |
187 | "chalk-derive", | 187 | "chalk-derive", |
188 | "lazy_static", | 188 | "lazy_static", |
@@ -190,9 +190,9 @@ dependencies = [ | |||
190 | 190 | ||
191 | [[package]] | 191 | [[package]] |
192 | name = "chalk-recursive" | 192 | name = "chalk-recursive" |
193 | version = "0.32.0" | 193 | version = "0.33.0" |
194 | source = "registry+https://github.com/rust-lang/crates.io-index" | 194 | source = "registry+https://github.com/rust-lang/crates.io-index" |
195 | checksum = "b14f40242102e7c0e2791a2cc86dbbc213a1d7b7acc0e22b7da329f4957d1722" | 195 | checksum = "4a4671bcc70aa2d7e12ff4fe03f91d0c3c9ce387de915915e57fdf0c91dc5abd" |
196 | dependencies = [ | 196 | dependencies = [ |
197 | "chalk-derive", | 197 | "chalk-derive", |
198 | "chalk-ir", | 198 | "chalk-ir", |
@@ -203,9 +203,9 @@ dependencies = [ | |||
203 | 203 | ||
204 | [[package]] | 204 | [[package]] |
205 | name = "chalk-solve" | 205 | name = "chalk-solve" |
206 | version = "0.32.0" | 206 | version = "0.33.0" |
207 | source = "registry+https://github.com/rust-lang/crates.io-index" | 207 | source = "registry+https://github.com/rust-lang/crates.io-index" |
208 | checksum = "981534d499a8476ecc0b520be4d3864757f96211826a75360fbf2cb6fae362ab" | 208 | checksum = "45f75cc603f2fd302576c8b2976437f334e159e26d0afbb108a565b96c52184e" |
209 | dependencies = [ | 209 | dependencies = [ |
210 | "chalk-derive", | 210 | "chalk-derive", |
211 | "chalk-ir", | 211 | "chalk-ir", |
@@ -737,9 +737,9 @@ checksum = "2448f6066e80e3bfc792e9c98bf705b4b0fc6e8ef5b43e5889aff0eaa9c58743" | |||
737 | 737 | ||
738 | [[package]] | 738 | [[package]] |
739 | name = "libloading" | 739 | name = "libloading" |
740 | version = "0.6.3" | 740 | version = "0.6.4" |
741 | source = "registry+https://github.com/rust-lang/crates.io-index" | 741 | source = "registry+https://github.com/rust-lang/crates.io-index" |
742 | checksum = "2443d8f0478b16759158b2f66d525991a05491138bc05814ef52a250148ef4f9" | 742 | checksum = "3557c9384f7f757f6d139cd3a4c62ef4e850696c16bf27924a5538c8a09717a1" |
743 | dependencies = [ | 743 | dependencies = [ |
744 | "cfg-if 0.1.10", | 744 | "cfg-if 0.1.10", |
745 | "winapi 0.3.9", | 745 | "winapi 0.3.9", |
@@ -747,9 +747,9 @@ dependencies = [ | |||
747 | 747 | ||
748 | [[package]] | 748 | [[package]] |
749 | name = "libmimalloc-sys" | 749 | name = "libmimalloc-sys" |
750 | version = "0.1.17" | 750 | version = "0.1.18" |
751 | source = "registry+https://github.com/rust-lang/crates.io-index" | 751 | source = "registry+https://github.com/rust-lang/crates.io-index" |
752 | checksum = "2bd65748dfb74a807e8379fd49bf4d38964d92b8637b56f173f87e1d70433368" | 752 | checksum = "82151ff13433c4d403cb15d0e6fbda14b24d65bd1a5b33f7d52ec983cc00752d" |
753 | dependencies = [ | 753 | dependencies = [ |
754 | "cmake", | 754 | "cmake", |
755 | ] | 755 | ] |
@@ -859,9 +859,9 @@ dependencies = [ | |||
859 | 859 | ||
860 | [[package]] | 860 | [[package]] |
861 | name = "mimalloc" | 861 | name = "mimalloc" |
862 | version = "0.1.21" | 862 | version = "0.1.22" |
863 | source = "registry+https://github.com/rust-lang/crates.io-index" | 863 | source = "registry+https://github.com/rust-lang/crates.io-index" |
864 | checksum = "2e18b3d01186c4a7bec0b56cff9a4b9d5a1461aa162c7b8eaf2e5ee10b265b02" | 864 | checksum = "4a5d2c9cb18f9cdc6d88f4aca6d3d8ea89c4c8202d6facfc7e56efdee97b80fa" |
865 | dependencies = [ | 865 | dependencies = [ |
866 | "libmimalloc-sys", | 866 | "libmimalloc-sys", |
867 | ] | 867 | ] |
@@ -981,9 +981,9 @@ dependencies = [ | |||
981 | 981 | ||
982 | [[package]] | 982 | [[package]] |
983 | name = "object" | 983 | name = "object" |
984 | version = "0.20.0" | 984 | version = "0.21.1" |
985 | source = "registry+https://github.com/rust-lang/crates.io-index" | 985 | source = "registry+https://github.com/rust-lang/crates.io-index" |
986 | checksum = "1ab52be62400ca80aa00285d25253d7f7c437b7375c4de678f5405d3afe82ca5" | 986 | checksum = "37fd5004feb2ce328a52b0b3d01dbf4ffff72583493900ed15f22d4111c51693" |
987 | 987 | ||
988 | [[package]] | 988 | [[package]] |
989 | name = "once_cell" | 989 | name = "once_cell" |
@@ -1383,18 +1383,18 @@ checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" | |||
1383 | 1383 | ||
1384 | [[package]] | 1384 | [[package]] |
1385 | name = "scroll" | 1385 | name = "scroll" |
1386 | version = "0.10.1" | 1386 | version = "0.10.2" |
1387 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1387 | source = "registry+https://github.com/rust-lang/crates.io-index" |
1388 | checksum = "abb2332cb595d33f7edd5700f4cbf94892e680c7f0ae56adab58a35190b66cb1" | 1388 | checksum = "fda28d4b4830b807a8b43f7b0e6b5df875311b3e7621d84577188c175b6ec1ec" |
1389 | dependencies = [ | 1389 | dependencies = [ |
1390 | "scroll_derive", | 1390 | "scroll_derive", |
1391 | ] | 1391 | ] |
1392 | 1392 | ||
1393 | [[package]] | 1393 | [[package]] |
1394 | name = "scroll_derive" | 1394 | name = "scroll_derive" |
1395 | version = "0.10.2" | 1395 | version = "0.10.3" |
1396 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1396 | source = "registry+https://github.com/rust-lang/crates.io-index" |
1397 | checksum = "e367622f934864ffa1c704ba2b82280aab856e3d8213c84c5720257eb34b15b9" | 1397 | checksum = "6dfde5d1531034db129e95c76ac857e2baecea3443579d493d02224950b0fb6d" |
1398 | dependencies = [ | 1398 | dependencies = [ |
1399 | "proc-macro2", | 1399 | "proc-macro2", |
1400 | "quote", | 1400 | "quote", |
@@ -1510,9 +1510,9 @@ version = "0.0.0" | |||
1510 | 1510 | ||
1511 | [[package]] | 1511 | [[package]] |
1512 | name = "syn" | 1512 | name = "syn" |
1513 | version = "1.0.42" | 1513 | version = "1.0.44" |
1514 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1514 | source = "registry+https://github.com/rust-lang/crates.io-index" |
1515 | checksum = "9c51d92969d209b54a98397e1b91c8ae82d8c87a7bb87df0b29aa2ad81454228" | 1515 | checksum = "e03e57e4fcbfe7749842d53e24ccb9aa12b7252dbe5e91d2acad31834c8b8fdd" |
1516 | dependencies = [ | 1516 | dependencies = [ |
1517 | "proc-macro2", | 1517 | "proc-macro2", |
1518 | "quote", | 1518 | "quote", |
diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs index 9aaa280c8..a101d724e 100644 --- a/crates/hir/src/code_model.rs +++ b/crates/hir/src/code_model.rs | |||
@@ -186,6 +186,16 @@ impl_from!( | |||
186 | for ModuleDef | 186 | for ModuleDef |
187 | ); | 187 | ); |
188 | 188 | ||
189 | impl From<VariantDef> for ModuleDef { | ||
190 | fn from(var: VariantDef) -> Self { | ||
191 | match var { | ||
192 | VariantDef::Struct(t) => Adt::from(t).into(), | ||
193 | VariantDef::Union(t) => Adt::from(t).into(), | ||
194 | VariantDef::EnumVariant(t) => t.into(), | ||
195 | } | ||
196 | } | ||
197 | } | ||
198 | |||
189 | impl ModuleDef { | 199 | impl ModuleDef { |
190 | pub fn module(self, db: &dyn HirDatabase) -> Option<Module> { | 200 | pub fn module(self, db: &dyn HirDatabase) -> Option<Module> { |
191 | match self { | 201 | match self { |
@@ -752,6 +762,13 @@ impl Function { | |||
752 | pub fn diagnostics(self, db: &dyn HirDatabase, sink: &mut DiagnosticSink) { | 762 | pub fn diagnostics(self, db: &dyn HirDatabase, sink: &mut DiagnosticSink) { |
753 | hir_ty::diagnostics::validate_body(db, self.id.into(), sink) | 763 | hir_ty::diagnostics::validate_body(db, self.id.into(), sink) |
754 | } | 764 | } |
765 | |||
766 | /// Whether this function declaration has a definition. | ||
767 | /// | ||
768 | /// This is false in the case of required (not provided) trait methods. | ||
769 | pub fn has_body(self, db: &dyn HirDatabase) -> bool { | ||
770 | db.function_data(self.id).has_body | ||
771 | } | ||
755 | } | 772 | } |
756 | 773 | ||
757 | // Note: logically, this belongs to `hir_ty`, but we are not using it there yet. | 774 | // Note: logically, this belongs to `hir_ty`, but we are not using it there yet. |
diff --git a/crates/hir_def/src/body/lower.rs b/crates/hir_def/src/body/lower.rs index 2d91bb21f..01e72690a 100644 --- a/crates/hir_def/src/body/lower.rs +++ b/crates/hir_def/src/body/lower.rs | |||
@@ -107,7 +107,10 @@ impl ExprCollector<'_> { | |||
107 | let param_pat = self.alloc_pat( | 107 | let param_pat = self.alloc_pat( |
108 | Pat::Bind { | 108 | Pat::Bind { |
109 | name: name![self], | 109 | name: name![self], |
110 | mode: BindingAnnotation::Unannotated, | 110 | mode: BindingAnnotation::new( |
111 | self_param.mut_token().is_some() && self_param.amp_token().is_none(), | ||
112 | false, | ||
113 | ), | ||
111 | subpat: None, | 114 | subpat: None, |
112 | }, | 115 | }, |
113 | Either::Right(ptr), | 116 | Either::Right(ptr), |
diff --git a/crates/hir_def/src/data.rs b/crates/hir_def/src/data.rs index 6190906da..ff1ef0df6 100644 --- a/crates/hir_def/src/data.rs +++ b/crates/hir_def/src/data.rs | |||
@@ -25,6 +25,7 @@ pub struct FunctionData { | |||
25 | /// True if the first param is `self`. This is relevant to decide whether this | 25 | /// True if the first param is `self`. This is relevant to decide whether this |
26 | /// can be called as a method. | 26 | /// can be called as a method. |
27 | pub has_self_param: bool, | 27 | pub has_self_param: bool, |
28 | pub has_body: bool, | ||
28 | pub is_unsafe: bool, | 29 | pub is_unsafe: bool, |
29 | pub is_varargs: bool, | 30 | pub is_varargs: bool, |
30 | pub visibility: RawVisibility, | 31 | pub visibility: RawVisibility, |
@@ -42,6 +43,7 @@ impl FunctionData { | |||
42 | ret_type: func.ret_type.clone(), | 43 | ret_type: func.ret_type.clone(), |
43 | attrs: item_tree.attrs(ModItem::from(loc.id.value).into()).clone(), | 44 | attrs: item_tree.attrs(ModItem::from(loc.id.value).into()).clone(), |
44 | has_self_param: func.has_self_param, | 45 | has_self_param: func.has_self_param, |
46 | has_body: func.has_body, | ||
45 | is_unsafe: func.is_unsafe, | 47 | is_unsafe: func.is_unsafe, |
46 | is_varargs: func.is_varargs, | 48 | is_varargs: func.is_varargs, |
47 | visibility: item_tree[func.visibility].clone(), | 49 | visibility: item_tree[func.visibility].clone(), |
diff --git a/crates/hir_def/src/item_tree.rs b/crates/hir_def/src/item_tree.rs index 0fd91b9d0..8a1121bbd 100644 --- a/crates/hir_def/src/item_tree.rs +++ b/crates/hir_def/src/item_tree.rs | |||
@@ -505,6 +505,7 @@ pub struct Function { | |||
505 | pub visibility: RawVisibilityId, | 505 | pub visibility: RawVisibilityId, |
506 | pub generic_params: GenericParamsId, | 506 | pub generic_params: GenericParamsId, |
507 | pub has_self_param: bool, | 507 | pub has_self_param: bool, |
508 | pub has_body: bool, | ||
508 | pub is_unsafe: bool, | 509 | pub is_unsafe: bool, |
509 | pub params: Box<[TypeRef]>, | 510 | pub params: Box<[TypeRef]>, |
510 | pub is_varargs: bool, | 511 | pub is_varargs: bool, |
diff --git a/crates/hir_def/src/item_tree/lower.rs b/crates/hir_def/src/item_tree/lower.rs index 54814f141..3328639cf 100644 --- a/crates/hir_def/src/item_tree/lower.rs +++ b/crates/hir_def/src/item_tree/lower.rs | |||
@@ -330,12 +330,15 @@ impl Ctx { | |||
330 | ret_type | 330 | ret_type |
331 | }; | 331 | }; |
332 | 332 | ||
333 | let has_body = func.body().is_some(); | ||
334 | |||
333 | let ast_id = self.source_ast_id_map.ast_id(func); | 335 | let ast_id = self.source_ast_id_map.ast_id(func); |
334 | let mut res = Function { | 336 | let mut res = Function { |
335 | name, | 337 | name, |
336 | visibility, | 338 | visibility, |
337 | generic_params: GenericParamsId::EMPTY, | 339 | generic_params: GenericParamsId::EMPTY, |
338 | has_self_param, | 340 | has_self_param, |
341 | has_body, | ||
339 | is_unsafe: func.unsafe_token().is_some(), | 342 | is_unsafe: func.unsafe_token().is_some(), |
340 | params: params.into_boxed_slice(), | 343 | params: params.into_boxed_slice(), |
341 | is_varargs, | 344 | is_varargs, |
diff --git a/crates/hir_def/src/item_tree/tests.rs b/crates/hir_def/src/item_tree/tests.rs index 1a806cda5..4b354c4c1 100644 --- a/crates/hir_def/src/item_tree/tests.rs +++ b/crates/hir_def/src/item_tree/tests.rs | |||
@@ -240,9 +240,9 @@ fn smoke() { | |||
240 | > #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("assoc_const"))] }, input: None }]) }] | 240 | > #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("assoc_const"))] }, input: None }]) }] |
241 | > Const { name: Some(Name(Text("CONST"))), visibility: RawVisibilityId("pub(self)"), type_ref: Path(Path { type_anchor: None, mod_path: ModPath { kind: Plain, segments: [Name(Text("u8"))] }, generic_args: [None] }), ast_id: FileAstId::<syntax::ast::generated::nodes::Const>(9) } | 241 | > Const { name: Some(Name(Text("CONST"))), visibility: RawVisibilityId("pub(self)"), type_ref: Path(Path { type_anchor: None, mod_path: ModPath { kind: Plain, segments: [Name(Text("u8"))] }, generic_args: [None] }), ast_id: FileAstId::<syntax::ast::generated::nodes::Const>(9) } |
242 | > #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("assoc_method"))] }, input: None }]) }] | 242 | > #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("assoc_method"))] }, input: None }]) }] |
243 | > Function { name: Name(Text("method")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(4294967295), has_self_param: true, is_unsafe: false, params: [Reference(Path(Path { type_anchor: None, mod_path: ModPath { kind: Plain, segments: [Name(Text("Self"))] }, generic_args: [None] }), Shared)], is_varargs: false, ret_type: Tuple([]), ast_id: FileAstId::<syntax::ast::generated::nodes::Fn>(10) } | 243 | > Function { name: Name(Text("method")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(4294967295), has_self_param: true, has_body: false, is_unsafe: false, params: [Reference(Path(Path { type_anchor: None, mod_path: ModPath { kind: Plain, segments: [Name(Text("Self"))] }, generic_args: [None] }), Shared)], is_varargs: false, ret_type: Tuple([]), ast_id: FileAstId::<syntax::ast::generated::nodes::Fn>(10) } |
244 | > #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("assoc_dfl_method"))] }, input: None }]) }] | 244 | > #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("assoc_dfl_method"))] }, input: None }]) }] |
245 | > Function { name: Name(Text("dfl_method")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(4294967295), has_self_param: true, is_unsafe: false, params: [Reference(Path(Path { type_anchor: None, mod_path: ModPath { kind: Plain, segments: [Name(Text("Self"))] }, generic_args: [None] }), Mut)], is_varargs: false, ret_type: Tuple([]), ast_id: FileAstId::<syntax::ast::generated::nodes::Fn>(11) } | 245 | > Function { name: Name(Text("dfl_method")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(4294967295), has_self_param: true, has_body: true, is_unsafe: false, params: [Reference(Path(Path { type_anchor: None, mod_path: ModPath { kind: Plain, segments: [Name(Text("Self"))] }, generic_args: [None] }), Mut)], is_varargs: false, ret_type: Tuple([]), ast_id: FileAstId::<syntax::ast::generated::nodes::Fn>(11) } |
246 | #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("struct0"))] }, input: None }]) }] | 246 | #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("struct0"))] }, input: None }]) }] |
247 | Struct { name: Name(Text("Struct0")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(1), fields: Unit, ast_id: FileAstId::<syntax::ast::generated::nodes::Struct>(3), kind: Unit } | 247 | Struct { name: Name(Text("Struct0")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(1), fields: Unit, ast_id: FileAstId::<syntax::ast::generated::nodes::Struct>(3), kind: Unit } |
248 | #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("struct1"))] }, input: None }]) }] | 248 | #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("struct1"))] }, input: None }]) }] |
@@ -275,12 +275,12 @@ fn simple_inner_items() { | |||
275 | 275 | ||
276 | top-level items: | 276 | top-level items: |
277 | Impl { generic_params: GenericParamsId(0), target_trait: Some(Path(Path { type_anchor: None, mod_path: ModPath { kind: Plain, segments: [Name(Text("D"))] }, generic_args: [None] })), target_type: Path(Path { type_anchor: None, mod_path: ModPath { kind: Plain, segments: [Name(Text("Response"))] }, generic_args: [Some(GenericArgs { args: [Type(Path(Path { type_anchor: None, mod_path: ModPath { kind: Plain, segments: [Name(Text("T"))] }, generic_args: [None] }))], has_self_type: false, bindings: [] })] }), is_negative: false, items: [Function(Idx::<Function>(1))], ast_id: FileAstId::<syntax::ast::generated::nodes::Impl>(0) } | 277 | Impl { generic_params: GenericParamsId(0), target_trait: Some(Path(Path { type_anchor: None, mod_path: ModPath { kind: Plain, segments: [Name(Text("D"))] }, generic_args: [None] })), target_type: Path(Path { type_anchor: None, mod_path: ModPath { kind: Plain, segments: [Name(Text("Response"))] }, generic_args: [Some(GenericArgs { args: [Type(Path(Path { type_anchor: None, mod_path: ModPath { kind: Plain, segments: [Name(Text("T"))] }, generic_args: [None] }))], has_self_type: false, bindings: [] })] }), is_negative: false, items: [Function(Idx::<Function>(1))], ast_id: FileAstId::<syntax::ast::generated::nodes::Impl>(0) } |
278 | > Function { name: Name(Text("foo")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(4294967295), has_self_param: false, is_unsafe: false, params: [], is_varargs: false, ret_type: Tuple([]), ast_id: FileAstId::<syntax::ast::generated::nodes::Fn>(1) } | 278 | > Function { name: Name(Text("foo")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(4294967295), has_self_param: false, has_body: true, is_unsafe: false, params: [], is_varargs: false, ret_type: Tuple([]), ast_id: FileAstId::<syntax::ast::generated::nodes::Fn>(1) } |
279 | 279 | ||
280 | inner items: | 280 | inner items: |
281 | 281 | ||
282 | for AST FileAstId::<syntax::ast::generated::nodes::Item>(2): | 282 | for AST FileAstId::<syntax::ast::generated::nodes::Item>(2): |
283 | Function { name: Name(Text("end")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(1), has_self_param: false, is_unsafe: false, params: [], is_varargs: false, ret_type: Tuple([]), ast_id: FileAstId::<syntax::ast::generated::nodes::Fn>(2) } | 283 | Function { name: Name(Text("end")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(1), has_self_param: false, has_body: true, is_unsafe: false, params: [], is_varargs: false, ret_type: Tuple([]), ast_id: FileAstId::<syntax::ast::generated::nodes::Fn>(2) } |
284 | 284 | ||
285 | "#]], | 285 | "#]], |
286 | ); | 286 | ); |
@@ -303,9 +303,9 @@ fn extern_attrs() { | |||
303 | 303 | ||
304 | top-level items: | 304 | top-level items: |
305 | #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("attr_a"))] }, input: None }, Attr { path: ModPath { kind: Plain, segments: [Name(Text("block_attr"))] }, input: None }]) }] | 305 | #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("attr_a"))] }, input: None }, Attr { path: ModPath { kind: Plain, segments: [Name(Text("block_attr"))] }, input: None }]) }] |
306 | Function { name: Name(Text("a")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(4294967295), has_self_param: false, is_unsafe: true, params: [], is_varargs: false, ret_type: Tuple([]), ast_id: FileAstId::<syntax::ast::generated::nodes::Fn>(1) } | 306 | Function { name: Name(Text("a")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(4294967295), has_self_param: false, has_body: true, is_unsafe: true, params: [], is_varargs: false, ret_type: Tuple([]), ast_id: FileAstId::<syntax::ast::generated::nodes::Fn>(1) } |
307 | #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("attr_b"))] }, input: None }, Attr { path: ModPath { kind: Plain, segments: [Name(Text("block_attr"))] }, input: None }]) }] | 307 | #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("attr_b"))] }, input: None }, Attr { path: ModPath { kind: Plain, segments: [Name(Text("block_attr"))] }, input: None }]) }] |
308 | Function { name: Name(Text("b")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(4294967295), has_self_param: false, is_unsafe: true, params: [], is_varargs: false, ret_type: Tuple([]), ast_id: FileAstId::<syntax::ast::generated::nodes::Fn>(2) } | 308 | Function { name: Name(Text("b")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(4294967295), has_self_param: false, has_body: true, is_unsafe: true, params: [], is_varargs: false, ret_type: Tuple([]), ast_id: FileAstId::<syntax::ast::generated::nodes::Fn>(2) } |
309 | "##]], | 309 | "##]], |
310 | ); | 310 | ); |
311 | } | 311 | } |
@@ -329,9 +329,9 @@ fn trait_attrs() { | |||
329 | #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("trait_attr"))] }, input: None }]) }] | 329 | #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("trait_attr"))] }, input: None }]) }] |
330 | Trait { name: Name(Text("Tr")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(0), auto: false, items: [Function(Idx::<Function>(0)), Function(Idx::<Function>(1))], ast_id: FileAstId::<syntax::ast::generated::nodes::Trait>(0) } | 330 | Trait { name: Name(Text("Tr")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(0), auto: false, items: [Function(Idx::<Function>(0)), Function(Idx::<Function>(1))], ast_id: FileAstId::<syntax::ast::generated::nodes::Trait>(0) } |
331 | > #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("attr_a"))] }, input: None }]) }] | 331 | > #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("attr_a"))] }, input: None }]) }] |
332 | > Function { name: Name(Text("a")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(4294967295), has_self_param: false, is_unsafe: false, params: [], is_varargs: false, ret_type: Tuple([]), ast_id: FileAstId::<syntax::ast::generated::nodes::Fn>(1) } | 332 | > Function { name: Name(Text("a")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(4294967295), has_self_param: false, has_body: true, is_unsafe: false, params: [], is_varargs: false, ret_type: Tuple([]), ast_id: FileAstId::<syntax::ast::generated::nodes::Fn>(1) } |
333 | > #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("attr_b"))] }, input: None }]) }] | 333 | > #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("attr_b"))] }, input: None }]) }] |
334 | > Function { name: Name(Text("b")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(4294967295), has_self_param: false, is_unsafe: false, params: [], is_varargs: false, ret_type: Tuple([]), ast_id: FileAstId::<syntax::ast::generated::nodes::Fn>(2) } | 334 | > Function { name: Name(Text("b")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(4294967295), has_self_param: false, has_body: true, is_unsafe: false, params: [], is_varargs: false, ret_type: Tuple([]), ast_id: FileAstId::<syntax::ast::generated::nodes::Fn>(2) } |
335 | "##]], | 335 | "##]], |
336 | ); | 336 | ); |
337 | } | 337 | } |
@@ -355,9 +355,9 @@ fn impl_attrs() { | |||
355 | #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("impl_attr"))] }, input: None }]) }] | 355 | #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("impl_attr"))] }, input: None }]) }] |
356 | Impl { generic_params: GenericParamsId(4294967295), target_trait: None, target_type: Path(Path { type_anchor: None, mod_path: ModPath { kind: Plain, segments: [Name(Text("Ty"))] }, generic_args: [None] }), is_negative: false, items: [Function(Idx::<Function>(0)), Function(Idx::<Function>(1))], ast_id: FileAstId::<syntax::ast::generated::nodes::Impl>(0) } | 356 | Impl { generic_params: GenericParamsId(4294967295), target_trait: None, target_type: Path(Path { type_anchor: None, mod_path: ModPath { kind: Plain, segments: [Name(Text("Ty"))] }, generic_args: [None] }), is_negative: false, items: [Function(Idx::<Function>(0)), Function(Idx::<Function>(1))], ast_id: FileAstId::<syntax::ast::generated::nodes::Impl>(0) } |
357 | > #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("attr_a"))] }, input: None }]) }] | 357 | > #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("attr_a"))] }, input: None }]) }] |
358 | > Function { name: Name(Text("a")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(4294967295), has_self_param: false, is_unsafe: false, params: [], is_varargs: false, ret_type: Tuple([]), ast_id: FileAstId::<syntax::ast::generated::nodes::Fn>(1) } | 358 | > Function { name: Name(Text("a")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(4294967295), has_self_param: false, has_body: true, is_unsafe: false, params: [], is_varargs: false, ret_type: Tuple([]), ast_id: FileAstId::<syntax::ast::generated::nodes::Fn>(1) } |
359 | > #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("attr_b"))] }, input: None }]) }] | 359 | > #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("attr_b"))] }, input: None }]) }] |
360 | > Function { name: Name(Text("b")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(4294967295), has_self_param: false, is_unsafe: false, params: [], is_varargs: false, ret_type: Tuple([]), ast_id: FileAstId::<syntax::ast::generated::nodes::Fn>(2) } | 360 | > Function { name: Name(Text("b")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(4294967295), has_self_param: false, has_body: true, is_unsafe: false, params: [], is_varargs: false, ret_type: Tuple([]), ast_id: FileAstId::<syntax::ast::generated::nodes::Fn>(2) } |
361 | "##]], | 361 | "##]], |
362 | ); | 362 | ); |
363 | } | 363 | } |
@@ -408,13 +408,13 @@ fn inner_item_attrs() { | |||
408 | inner attrs: Attrs { entries: None } | 408 | inner attrs: Attrs { entries: None } |
409 | 409 | ||
410 | top-level items: | 410 | top-level items: |
411 | Function { name: Name(Text("foo")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(4294967295), has_self_param: false, is_unsafe: false, params: [], is_varargs: false, ret_type: Tuple([]), ast_id: FileAstId::<syntax::ast::generated::nodes::Fn>(0) } | 411 | Function { name: Name(Text("foo")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(4294967295), has_self_param: false, has_body: true, is_unsafe: false, params: [], is_varargs: false, ret_type: Tuple([]), ast_id: FileAstId::<syntax::ast::generated::nodes::Fn>(0) } |
412 | 412 | ||
413 | inner items: | 413 | inner items: |
414 | 414 | ||
415 | for AST FileAstId::<syntax::ast::generated::nodes::Item>(1): | 415 | for AST FileAstId::<syntax::ast::generated::nodes::Item>(1): |
416 | #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("on_inner"))] }, input: None }]) }] | 416 | #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("on_inner"))] }, input: None }]) }] |
417 | Function { name: Name(Text("inner")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(4294967295), has_self_param: false, is_unsafe: false, params: [], is_varargs: false, ret_type: Tuple([]), ast_id: FileAstId::<syntax::ast::generated::nodes::Fn>(1) } | 417 | Function { name: Name(Text("inner")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(4294967295), has_self_param: false, has_body: true, is_unsafe: false, params: [], is_varargs: false, ret_type: Tuple([]), ast_id: FileAstId::<syntax::ast::generated::nodes::Fn>(1) } |
418 | 418 | ||
419 | "##]], | 419 | "##]], |
420 | ); | 420 | ); |
diff --git a/crates/hir_ty/Cargo.toml b/crates/hir_ty/Cargo.toml index 0f3c85926..e9c62c6aa 100644 --- a/crates/hir_ty/Cargo.toml +++ b/crates/hir_ty/Cargo.toml | |||
@@ -17,9 +17,9 @@ ena = "0.14.0" | |||
17 | log = "0.4.8" | 17 | log = "0.4.8" |
18 | rustc-hash = "1.1.0" | 18 | rustc-hash = "1.1.0" |
19 | scoped-tls = "1" | 19 | scoped-tls = "1" |
20 | chalk-solve = "0.32" | 20 | chalk-solve = "0.33" |
21 | chalk-ir = "0.32" | 21 | chalk-ir = "0.33" |
22 | chalk-recursive = "0.32" | 22 | chalk-recursive = "0.33" |
23 | 23 | ||
24 | stdx = { path = "../stdx", version = "0.0.0" } | 24 | stdx = { path = "../stdx", version = "0.0.0" } |
25 | hir_def = { path = "../hir_def", version = "0.0.0" } | 25 | hir_def = { path = "../hir_def", version = "0.0.0" } |
diff --git a/crates/ide/src/link_rewrite.rs b/crates/ide/src/doc_links.rs index c317a2379..06af36b73 100644 --- a/crates/ide/src/link_rewrite.rs +++ b/crates/ide/src/doc_links.rs | |||
@@ -1,13 +1,27 @@ | |||
1 | //! Resolves and rewrites links in markdown documentation. | 1 | //! Resolves and rewrites links in markdown documentation. |
2 | //! | ||
3 | //! Most of the implementation can be found in [`hir::doc_links`]. | ||
4 | 2 | ||
5 | use hir::{Adt, Crate, HasAttrs, ModuleDef}; | 3 | use std::iter::once; |
6 | use ide_db::{defs::Definition, RootDatabase}; | 4 | |
5 | use itertools::Itertools; | ||
7 | use pulldown_cmark::{CowStr, Event, LinkType, Options, Parser, Tag}; | 6 | use pulldown_cmark::{CowStr, Event, LinkType, Options, Parser, Tag}; |
8 | use pulldown_cmark_to_cmark::{cmark_with_options, Options as CmarkOptions}; | 7 | use pulldown_cmark_to_cmark::{cmark_with_options, Options as CmarkOptions}; |
9 | use url::Url; | 8 | use url::Url; |
10 | 9 | ||
10 | use hir::{ | ||
11 | db::{DefDatabase, HirDatabase}, | ||
12 | Adt, AsAssocItem, AsName, AssocItem, AssocItemContainer, Crate, Field, HasAttrs, ItemInNs, | ||
13 | ModuleDef, | ||
14 | }; | ||
15 | use ide_db::{ | ||
16 | defs::{classify_name, classify_name_ref, Definition}, | ||
17 | RootDatabase, | ||
18 | }; | ||
19 | use syntax::{ast, match_ast, AstNode, SyntaxKind::*, SyntaxToken, TokenAtOffset, T}; | ||
20 | |||
21 | use crate::{FilePosition, Semantics}; | ||
22 | |||
23 | pub type DocumentationLink = String; | ||
24 | |||
11 | /// Rewrite documentation links in markdown to point to an online host (e.g. docs.rs) | 25 | /// Rewrite documentation links in markdown to point to an online host (e.g. docs.rs) |
12 | pub fn rewrite_links(db: &RootDatabase, markdown: &str, definition: &Definition) -> String { | 26 | pub fn rewrite_links(db: &RootDatabase, markdown: &str, definition: &Definition) -> String { |
13 | let doc = Parser::new_with_broken_link_callback( | 27 | let doc = Parser::new_with_broken_link_callback( |
@@ -80,6 +94,70 @@ pub fn remove_links(markdown: &str) -> String { | |||
80 | out | 94 | out |
81 | } | 95 | } |
82 | 96 | ||
97 | // FIXME: | ||
98 | // BUG: For Option::Some | ||
99 | // Returns https://doc.rust-lang.org/nightly/core/prelude/v1/enum.Option.html#variant.Some | ||
100 | // Instead of https://doc.rust-lang.org/nightly/core/option/enum.Option.html | ||
101 | // | ||
102 | // This should cease to be a problem if RFC2988 (Stable Rustdoc URLs) is implemented | ||
103 | // https://github.com/rust-lang/rfcs/pull/2988 | ||
104 | fn get_doc_link(db: &RootDatabase, definition: Definition) -> Option<String> { | ||
105 | // Get the outermost definition for the moduledef. This is used to resolve the public path to the type, | ||
106 | // then we can join the method, field, etc onto it if required. | ||
107 | let target_def: ModuleDef = match definition { | ||
108 | Definition::ModuleDef(moddef) => match moddef { | ||
109 | ModuleDef::Function(f) => f | ||
110 | .as_assoc_item(db) | ||
111 | .and_then(|assoc| match assoc.container(db) { | ||
112 | AssocItemContainer::Trait(t) => Some(t.into()), | ||
113 | AssocItemContainer::ImplDef(impld) => { | ||
114 | impld.target_ty(db).as_adt().map(|adt| adt.into()) | ||
115 | } | ||
116 | }) | ||
117 | .unwrap_or_else(|| f.clone().into()), | ||
118 | moddef => moddef, | ||
119 | }, | ||
120 | Definition::Field(f) => f.parent_def(db).into(), | ||
121 | // FIXME: Handle macros | ||
122 | _ => return None, | ||
123 | }; | ||
124 | |||
125 | let ns = ItemInNs::from(target_def.clone()); | ||
126 | |||
127 | let module = definition.module(db)?; | ||
128 | let krate = module.krate(); | ||
129 | let import_map = db.import_map(krate.into()); | ||
130 | let base = once(krate.declaration_name(db)?.to_string()) | ||
131 | .chain(import_map.path_of(ns)?.segments.iter().map(|name| name.to_string())) | ||
132 | .join("/"); | ||
133 | |||
134 | let filename = get_symbol_filename(db, &target_def); | ||
135 | let fragment = match definition { | ||
136 | Definition::ModuleDef(moddef) => match moddef { | ||
137 | ModuleDef::Function(f) => { | ||
138 | get_symbol_fragment(db, &FieldOrAssocItem::AssocItem(AssocItem::Function(f))) | ||
139 | } | ||
140 | ModuleDef::Const(c) => { | ||
141 | get_symbol_fragment(db, &FieldOrAssocItem::AssocItem(AssocItem::Const(c))) | ||
142 | } | ||
143 | ModuleDef::TypeAlias(ty) => { | ||
144 | get_symbol_fragment(db, &FieldOrAssocItem::AssocItem(AssocItem::TypeAlias(ty))) | ||
145 | } | ||
146 | _ => None, | ||
147 | }, | ||
148 | Definition::Field(field) => get_symbol_fragment(db, &FieldOrAssocItem::Field(field)), | ||
149 | _ => None, | ||
150 | }; | ||
151 | |||
152 | get_doc_url(db, &krate) | ||
153 | .and_then(|url| url.join(&base).ok()) | ||
154 | .and_then(|url| filename.as_deref().and_then(|f| url.join(f).ok())) | ||
155 | .and_then( | ||
156 | |url| if let Some(fragment) = fragment { url.join(&fragment).ok() } else { Some(url) }, | ||
157 | ) | ||
158 | .map(|url| url.into_string()) | ||
159 | } | ||
160 | |||
83 | fn rewrite_intra_doc_link( | 161 | fn rewrite_intra_doc_link( |
84 | db: &RootDatabase, | 162 | db: &RootDatabase, |
85 | def: Definition, | 163 | def: Definition, |
@@ -138,7 +216,29 @@ fn rewrite_url_link(db: &RootDatabase, def: ModuleDef, target: &str) -> Option<S | |||
138 | .map(|url| url.into_string()) | 216 | .map(|url| url.into_string()) |
139 | } | 217 | } |
140 | 218 | ||
141 | // Rewrites a markdown document, resolving links using `callback` and additionally striping prefixes/suffixes on link titles. | 219 | /// Retrieve a link to documentation for the given symbol. |
220 | pub(crate) fn external_docs( | ||
221 | db: &RootDatabase, | ||
222 | position: &FilePosition, | ||
223 | ) -> Option<DocumentationLink> { | ||
224 | let sema = Semantics::new(db); | ||
225 | let file = sema.parse(position.file_id).syntax().clone(); | ||
226 | let token = pick_best(file.token_at_offset(position.offset))?; | ||
227 | let token = sema.descend_into_macros(token); | ||
228 | |||
229 | let node = token.parent(); | ||
230 | let definition = match_ast! { | ||
231 | match node { | ||
232 | ast::NameRef(name_ref) => classify_name_ref(&sema, &name_ref).map(|d| d.definition(sema.db)), | ||
233 | ast::Name(name) => classify_name(&sema, &name).map(|d| d.definition(sema.db)), | ||
234 | _ => None, | ||
235 | } | ||
236 | }; | ||
237 | |||
238 | get_doc_link(db, definition?) | ||
239 | } | ||
240 | |||
241 | /// Rewrites a markdown document, applying 'callback' to each link. | ||
142 | fn map_links<'e>( | 242 | fn map_links<'e>( |
143 | events: impl Iterator<Item = Event<'e>>, | 243 | events: impl Iterator<Item = Event<'e>>, |
144 | callback: impl Fn(&str, &str) -> (String, String), | 244 | callback: impl Fn(&str, &str) -> (String, String), |
@@ -239,6 +339,12 @@ fn ns_from_intra_spec(s: &str) -> Option<hir::Namespace> { | |||
239 | .next() | 339 | .next() |
240 | } | 340 | } |
241 | 341 | ||
342 | /// Get the root URL for the documentation of a crate. | ||
343 | /// | ||
344 | /// ``` | ||
345 | /// https://doc.rust-lang.org/std/iter/trait.Iterator.html#tymethod.next | ||
346 | /// ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
347 | /// ``` | ||
242 | fn get_doc_url(db: &RootDatabase, krate: &Crate) -> Option<Url> { | 348 | fn get_doc_url(db: &RootDatabase, krate: &Crate) -> Option<Url> { |
243 | krate | 349 | krate |
244 | .get_html_root_url(db) | 350 | .get_html_root_url(db) |
@@ -255,8 +361,11 @@ fn get_doc_url(db: &RootDatabase, krate: &Crate) -> Option<Url> { | |||
255 | 361 | ||
256 | /// Get the filename and extension generated for a symbol by rustdoc. | 362 | /// Get the filename and extension generated for a symbol by rustdoc. |
257 | /// | 363 | /// |
258 | /// Example: `struct.Shard.html` | 364 | /// ``` |
259 | fn get_symbol_filename(db: &RootDatabase, definition: &ModuleDef) -> Option<String> { | 365 | /// https://doc.rust-lang.org/std/iter/trait.Iterator.html#tymethod.next |
366 | /// ^^^^^^^^^^^^^^^^^^^ | ||
367 | /// ``` | ||
368 | fn get_symbol_filename(db: &dyn HirDatabase, definition: &ModuleDef) -> Option<String> { | ||
260 | Some(match definition { | 369 | Some(match definition { |
261 | ModuleDef::Adt(adt) => match adt { | 370 | ModuleDef::Adt(adt) => match adt { |
262 | Adt::Struct(s) => format!("struct.{}.html", s.name(db)), | 371 | Adt::Struct(s) => format!("struct.{}.html", s.name(db)), |
@@ -266,7 +375,7 @@ fn get_symbol_filename(db: &RootDatabase, definition: &ModuleDef) -> Option<Stri | |||
266 | ModuleDef::Module(_) => "index.html".to_string(), | 375 | ModuleDef::Module(_) => "index.html".to_string(), |
267 | ModuleDef::Trait(t) => format!("trait.{}.html", t.name(db)), | 376 | ModuleDef::Trait(t) => format!("trait.{}.html", t.name(db)), |
268 | ModuleDef::TypeAlias(t) => format!("type.{}.html", t.name(db)), | 377 | ModuleDef::TypeAlias(t) => format!("type.{}.html", t.name(db)), |
269 | ModuleDef::BuiltinType(t) => format!("primitive.{}.html", t), | 378 | ModuleDef::BuiltinType(t) => format!("primitive.{}.html", t.as_name()), |
270 | ModuleDef::Function(f) => format!("fn.{}.html", f.name(db)), | 379 | ModuleDef::Function(f) => format!("fn.{}.html", f.name(db)), |
271 | ModuleDef::EnumVariant(ev) => { | 380 | ModuleDef::EnumVariant(ev) => { |
272 | format!("enum.{}.html#variant.{}", ev.parent_enum(db).name(db), ev.name(db)) | 381 | format!("enum.{}.html#variant.{}", ev.parent_enum(db).name(db), ev.name(db)) |
@@ -275,3 +384,163 @@ fn get_symbol_filename(db: &RootDatabase, definition: &ModuleDef) -> Option<Stri | |||
275 | ModuleDef::Static(s) => format!("static.{}.html", s.name(db)?), | 384 | ModuleDef::Static(s) => format!("static.{}.html", s.name(db)?), |
276 | }) | 385 | }) |
277 | } | 386 | } |
387 | |||
388 | enum FieldOrAssocItem { | ||
389 | Field(Field), | ||
390 | AssocItem(AssocItem), | ||
391 | } | ||
392 | |||
393 | /// Get the fragment required to link to a specific field, method, associated type, or associated constant. | ||
394 | /// | ||
395 | /// ``` | ||
396 | /// https://doc.rust-lang.org/std/iter/trait.Iterator.html#tymethod.next | ||
397 | /// ^^^^^^^^^^^^^^ | ||
398 | /// ``` | ||
399 | fn get_symbol_fragment(db: &dyn HirDatabase, field_or_assoc: &FieldOrAssocItem) -> Option<String> { | ||
400 | Some(match field_or_assoc { | ||
401 | FieldOrAssocItem::Field(field) => format!("#structfield.{}", field.name(db)), | ||
402 | FieldOrAssocItem::AssocItem(assoc) => match assoc { | ||
403 | AssocItem::Function(function) => { | ||
404 | let is_trait_method = matches!( | ||
405 | function.as_assoc_item(db).map(|assoc| assoc.container(db)), | ||
406 | Some(AssocItemContainer::Trait(..)) | ||
407 | ); | ||
408 | // This distinction may get more complicated when specialisation is available. | ||
409 | // Rustdoc makes this decision based on whether a method 'has defaultness'. | ||
410 | // Currently this is only the case for provided trait methods. | ||
411 | if is_trait_method && !function.has_body(db) { | ||
412 | format!("#tymethod.{}", function.name(db)) | ||
413 | } else { | ||
414 | format!("#method.{}", function.name(db)) | ||
415 | } | ||
416 | } | ||
417 | AssocItem::Const(constant) => format!("#associatedconstant.{}", constant.name(db)?), | ||
418 | AssocItem::TypeAlias(ty) => format!("#associatedtype.{}", ty.name(db)), | ||
419 | }, | ||
420 | }) | ||
421 | } | ||
422 | |||
423 | fn pick_best(tokens: TokenAtOffset<SyntaxToken>) -> Option<SyntaxToken> { | ||
424 | return tokens.max_by_key(priority); | ||
425 | fn priority(n: &SyntaxToken) -> usize { | ||
426 | match n.kind() { | ||
427 | IDENT | INT_NUMBER => 3, | ||
428 | T!['('] | T![')'] => 2, | ||
429 | kind if kind.is_trivia() => 0, | ||
430 | _ => 1, | ||
431 | } | ||
432 | } | ||
433 | } | ||
434 | |||
435 | #[cfg(test)] | ||
436 | mod tests { | ||
437 | use expect_test::{expect, Expect}; | ||
438 | |||
439 | use crate::fixture; | ||
440 | |||
441 | fn check(ra_fixture: &str, expect: Expect) { | ||
442 | let (analysis, position) = fixture::position(ra_fixture); | ||
443 | let url = analysis.external_docs(position).unwrap().expect("could not find url for symbol"); | ||
444 | |||
445 | expect.assert_eq(&url) | ||
446 | } | ||
447 | |||
448 | #[test] | ||
449 | fn test_doc_url_struct() { | ||
450 | check( | ||
451 | r#" | ||
452 | pub struct Fo<|>o; | ||
453 | "#, | ||
454 | expect![[r#"https://docs.rs/test/*/test/struct.Foo.html"#]], | ||
455 | ); | ||
456 | } | ||
457 | |||
458 | #[test] | ||
459 | fn test_doc_url_fn() { | ||
460 | check( | ||
461 | r#" | ||
462 | pub fn fo<|>o() {} | ||
463 | "#, | ||
464 | expect![[r##"https://docs.rs/test/*/test/fn.foo.html#method.foo"##]], | ||
465 | ); | ||
466 | } | ||
467 | |||
468 | #[test] | ||
469 | fn test_doc_url_inherent_method() { | ||
470 | check( | ||
471 | r#" | ||
472 | pub struct Foo; | ||
473 | |||
474 | impl Foo { | ||
475 | pub fn met<|>hod() {} | ||
476 | } | ||
477 | |||
478 | "#, | ||
479 | expect![[r##"https://docs.rs/test/*/test/struct.Foo.html#method.method"##]], | ||
480 | ); | ||
481 | } | ||
482 | |||
483 | #[test] | ||
484 | fn test_doc_url_trait_provided_method() { | ||
485 | check( | ||
486 | r#" | ||
487 | pub trait Bar { | ||
488 | fn met<|>hod() {} | ||
489 | } | ||
490 | |||
491 | "#, | ||
492 | expect![[r##"https://docs.rs/test/*/test/trait.Bar.html#method.method"##]], | ||
493 | ); | ||
494 | } | ||
495 | |||
496 | #[test] | ||
497 | fn test_doc_url_trait_required_method() { | ||
498 | check( | ||
499 | r#" | ||
500 | pub trait Foo { | ||
501 | fn met<|>hod(); | ||
502 | } | ||
503 | |||
504 | "#, | ||
505 | expect![[r##"https://docs.rs/test/*/test/trait.Foo.html#tymethod.method"##]], | ||
506 | ); | ||
507 | } | ||
508 | |||
509 | #[test] | ||
510 | fn test_doc_url_field() { | ||
511 | check( | ||
512 | r#" | ||
513 | pub struct Foo { | ||
514 | pub fie<|>ld: () | ||
515 | } | ||
516 | |||
517 | "#, | ||
518 | expect![[r##"https://docs.rs/test/*/test/struct.Foo.html#structfield.field"##]], | ||
519 | ); | ||
520 | } | ||
521 | |||
522 | // FIXME: ImportMap will return re-export paths instead of public module | ||
523 | // paths. The correct path to documentation will never be a re-export. | ||
524 | // This problem stops us from resolving stdlib items included in the prelude | ||
525 | // such as `Option::Some` correctly. | ||
526 | #[ignore = "ImportMap may return re-exports"] | ||
527 | #[test] | ||
528 | fn test_reexport_order() { | ||
529 | check( | ||
530 | r#" | ||
531 | pub mod wrapper { | ||
532 | pub use module::Item; | ||
533 | |||
534 | pub mod module { | ||
535 | pub struct Item; | ||
536 | } | ||
537 | } | ||
538 | |||
539 | fn foo() { | ||
540 | let bar: wrapper::It<|>em; | ||
541 | } | ||
542 | "#, | ||
543 | expect![[r#"https://docs.rs/test/*/test/wrapper/module/struct.Item.html"#]], | ||
544 | ) | ||
545 | } | ||
546 | } | ||
diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs index 53265488e..6290b35bd 100644 --- a/crates/ide/src/hover.rs +++ b/crates/ide/src/hover.rs | |||
@@ -14,7 +14,7 @@ use test_utils::mark; | |||
14 | 14 | ||
15 | use crate::{ | 15 | use crate::{ |
16 | display::{macro_label, ShortLabel, ToNav, TryToNav}, | 16 | display::{macro_label, ShortLabel, ToNav, TryToNav}, |
17 | link_rewrite::{remove_links, rewrite_links}, | 17 | doc_links::{remove_links, rewrite_links}, |
18 | markdown_remove::remove_markdown, | 18 | markdown_remove::remove_markdown, |
19 | markup::Markup, | 19 | markup::Markup, |
20 | runnables::runnable, | 20 | runnables::runnable, |
diff --git a/crates/ide/src/lib.rs b/crates/ide/src/lib.rs index d54c06b14..686cee3a1 100644 --- a/crates/ide/src/lib.rs +++ b/crates/ide/src/lib.rs | |||
@@ -45,8 +45,8 @@ mod status; | |||
45 | mod syntax_highlighting; | 45 | mod syntax_highlighting; |
46 | mod syntax_tree; | 46 | mod syntax_tree; |
47 | mod typing; | 47 | mod typing; |
48 | mod link_rewrite; | ||
49 | mod markdown_remove; | 48 | mod markdown_remove; |
49 | mod doc_links; | ||
50 | 50 | ||
51 | use std::sync::Arc; | 51 | use std::sync::Arc; |
52 | 52 | ||
@@ -384,6 +384,14 @@ impl Analysis { | |||
384 | self.with_db(|db| hover::hover(db, position, links_in_hover, markdown)) | 384 | self.with_db(|db| hover::hover(db, position, links_in_hover, markdown)) |
385 | } | 385 | } |
386 | 386 | ||
387 | /// Return URL(s) for the documentation of the symbol under the cursor. | ||
388 | pub fn external_docs( | ||
389 | &self, | ||
390 | position: FilePosition, | ||
391 | ) -> Cancelable<Option<doc_links::DocumentationLink>> { | ||
392 | self.with_db(|db| doc_links::external_docs(db, &position)) | ||
393 | } | ||
394 | |||
387 | /// Computes parameter information for the given call expression. | 395 | /// Computes parameter information for the given call expression. |
388 | pub fn call_info(&self, position: FilePosition) -> Cancelable<Option<CallInfo>> { | 396 | pub fn call_info(&self, position: FilePosition) -> Cancelable<Option<CallInfo>> { |
389 | self.with_db(|db| call_info::call_info(db, position)) | 397 | self.with_db(|db| call_info::call_info(db, position)) |
diff --git a/crates/ide/src/syntax_highlighting.rs b/crates/ide/src/syntax_highlighting.rs index d9fc25d88..6aafd6fd5 100644 --- a/crates/ide/src/syntax_highlighting.rs +++ b/crates/ide/src/syntax_highlighting.rs | |||
@@ -68,7 +68,7 @@ pub(crate) fn highlight( | |||
68 | // When we leave a node, the we use it to flatten the highlighted ranges. | 68 | // When we leave a node, the we use it to flatten the highlighted ranges. |
69 | let mut stack = HighlightedRangeStack::new(); | 69 | let mut stack = HighlightedRangeStack::new(); |
70 | 70 | ||
71 | let mut current_macro_call: Option<ast::MacroCall> = None; | 71 | let mut current_macro_call: Option<(ast::MacroCall, Option<MacroMatcherParseState>)> = None; |
72 | let mut format_string: Option<SyntaxElement> = None; | 72 | let mut format_string: Option<SyntaxElement> = None; |
73 | 73 | ||
74 | // Walk all nodes, keeping track of whether we are inside a macro or not. | 74 | // Walk all nodes, keeping track of whether we are inside a macro or not. |
@@ -92,7 +92,6 @@ pub(crate) fn highlight( | |||
92 | // Track "inside macro" state | 92 | // Track "inside macro" state |
93 | match event.clone().map(|it| it.into_node().and_then(ast::MacroCall::cast)) { | 93 | match event.clone().map(|it| it.into_node().and_then(ast::MacroCall::cast)) { |
94 | WalkEvent::Enter(Some(mc)) => { | 94 | WalkEvent::Enter(Some(mc)) => { |
95 | current_macro_call = Some(mc.clone()); | ||
96 | if let Some(range) = macro_call_range(&mc) { | 95 | if let Some(range) = macro_call_range(&mc) { |
97 | stack.add(HighlightedRange { | 96 | stack.add(HighlightedRange { |
98 | range, | 97 | range, |
@@ -100,7 +99,9 @@ pub(crate) fn highlight( | |||
100 | binding_hash: None, | 99 | binding_hash: None, |
101 | }); | 100 | }); |
102 | } | 101 | } |
102 | let mut is_macro_rules = None; | ||
103 | if let Some(name) = mc.is_macro_rules() { | 103 | if let Some(name) = mc.is_macro_rules() { |
104 | is_macro_rules = Some(MacroMatcherParseState::new()); | ||
104 | if let Some((highlight, binding_hash)) = highlight_element( | 105 | if let Some((highlight, binding_hash)) = highlight_element( |
105 | &sema, | 106 | &sema, |
106 | &mut bindings_shadow_count, | 107 | &mut bindings_shadow_count, |
@@ -114,10 +115,11 @@ pub(crate) fn highlight( | |||
114 | }); | 115 | }); |
115 | } | 116 | } |
116 | } | 117 | } |
118 | current_macro_call = Some((mc.clone(), is_macro_rules)); | ||
117 | continue; | 119 | continue; |
118 | } | 120 | } |
119 | WalkEvent::Leave(Some(mc)) => { | 121 | WalkEvent::Leave(Some(mc)) => { |
120 | assert!(current_macro_call == Some(mc)); | 122 | assert!(current_macro_call.map(|it| it.0) == Some(mc)); |
121 | current_macro_call = None; | 123 | current_macro_call = None; |
122 | format_string = None; | 124 | format_string = None; |
123 | } | 125 | } |
@@ -146,6 +148,20 @@ pub(crate) fn highlight( | |||
146 | WalkEvent::Leave(_) => continue, | 148 | WalkEvent::Leave(_) => continue, |
147 | }; | 149 | }; |
148 | 150 | ||
151 | // check if in matcher part of a macro_rules rule | ||
152 | if let Some((_, Some(ref mut state))) = current_macro_call { | ||
153 | if let Some(tok) = element.as_token() { | ||
154 | if matches!( | ||
155 | update_macro_rules_state(tok, state), | ||
156 | RuleState::Matcher | RuleState::Expander | ||
157 | ) { | ||
158 | if skip_metavariables(element.clone()) { | ||
159 | continue; | ||
160 | } | ||
161 | } | ||
162 | } | ||
163 | } | ||
164 | |||
149 | let range = element.text_range(); | 165 | let range = element.text_range(); |
150 | 166 | ||
151 | let element_to_highlight = if current_macro_call.is_some() && element.kind() != COMMENT { | 167 | let element_to_highlight = if current_macro_call.is_some() && element.kind() != COMMENT { |
@@ -918,3 +934,99 @@ fn highlight_name_ref_by_syntax(name: ast::NameRef, sema: &Semantics<RootDatabas | |||
918 | _ => default.into(), | 934 | _ => default.into(), |
919 | } | 935 | } |
920 | } | 936 | } |
937 | |||
938 | struct MacroMatcherParseState { | ||
939 | /// Opening and corresponding closing bracket of the matcher or expander of the current rule | ||
940 | paren_ty: Option<(SyntaxKind, SyntaxKind)>, | ||
941 | paren_level: usize, | ||
942 | rule_state: RuleState, | ||
943 | /// Whether we are inside the outer `{` `}` macro block that holds the rules | ||
944 | in_invoc_body: bool, | ||
945 | } | ||
946 | |||
947 | impl MacroMatcherParseState { | ||
948 | fn new() -> Self { | ||
949 | MacroMatcherParseState { | ||
950 | paren_ty: None, | ||
951 | paren_level: 0, | ||
952 | in_invoc_body: false, | ||
953 | rule_state: RuleState::None, | ||
954 | } | ||
955 | } | ||
956 | } | ||
957 | |||
958 | #[derive(Copy, Clone, PartialEq)] | ||
959 | enum RuleState { | ||
960 | Matcher, | ||
961 | Expander, | ||
962 | Between, | ||
963 | None, | ||
964 | } | ||
965 | |||
966 | impl RuleState { | ||
967 | fn transition(&mut self) { | ||
968 | *self = match self { | ||
969 | RuleState::Matcher => RuleState::Between, | ||
970 | RuleState::Expander => RuleState::None, | ||
971 | RuleState::Between => RuleState::Expander, | ||
972 | RuleState::None => RuleState::Matcher, | ||
973 | }; | ||
974 | } | ||
975 | } | ||
976 | |||
977 | fn update_macro_rules_state(tok: &SyntaxToken, state: &mut MacroMatcherParseState) -> RuleState { | ||
978 | if !state.in_invoc_body { | ||
979 | if tok.kind() == T!['{'] { | ||
980 | state.in_invoc_body = true; | ||
981 | } | ||
982 | return state.rule_state; | ||
983 | } | ||
984 | |||
985 | match state.paren_ty { | ||
986 | Some((open, close)) => { | ||
987 | if tok.kind() == open { | ||
988 | state.paren_level += 1; | ||
989 | } else if tok.kind() == close { | ||
990 | state.paren_level -= 1; | ||
991 | if state.paren_level == 0 { | ||
992 | let res = state.rule_state; | ||
993 | state.rule_state.transition(); | ||
994 | state.paren_ty = None; | ||
995 | return res; | ||
996 | } | ||
997 | } | ||
998 | } | ||
999 | None => { | ||
1000 | match tok.kind() { | ||
1001 | T!['('] => { | ||
1002 | state.paren_ty = Some((T!['('], T![')'])); | ||
1003 | } | ||
1004 | T!['{'] => { | ||
1005 | state.paren_ty = Some((T!['{'], T!['}'])); | ||
1006 | } | ||
1007 | T!['['] => { | ||
1008 | state.paren_ty = Some((T!['['], T![']'])); | ||
1009 | } | ||
1010 | _ => (), | ||
1011 | } | ||
1012 | if state.paren_ty.is_some() { | ||
1013 | state.paren_level = 1; | ||
1014 | state.rule_state.transition(); | ||
1015 | } | ||
1016 | } | ||
1017 | } | ||
1018 | state.rule_state | ||
1019 | } | ||
1020 | |||
1021 | fn skip_metavariables(element: SyntaxElement) -> bool { | ||
1022 | let tok = match element.as_token() { | ||
1023 | Some(tok) => tok, | ||
1024 | None => return false, | ||
1025 | }; | ||
1026 | let is_fragment = || tok.prev_token().map(|tok| tok.kind()) == Some(T![$]); | ||
1027 | match tok.kind() { | ||
1028 | IDENT if is_fragment() => true, | ||
1029 | kind if kind.is_keyword() && is_fragment() => true, | ||
1030 | _ => false, | ||
1031 | } | ||
1032 | } | ||
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 1b681b2c6..43f1b32fd 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html | |||
@@ -37,7 +37,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd | |||
37 | </style> | 37 | </style> |
38 | <pre><code><span class="macro">macro_rules!</span> <span class="macro declaration">println</span> <span class="punctuation">{</span> | 38 | <pre><code><span class="macro">macro_rules!</span> <span class="macro declaration">println</span> <span class="punctuation">{</span> |
39 | <span class="punctuation">(</span><span class="punctuation">$</span><span class="punctuation">(</span><span class="punctuation">$</span>arg<span class="punctuation">:</span>tt<span class="punctuation">)</span><span class="punctuation">*</span><span class="punctuation">)</span> <span class="operator">=</span><span class="punctuation">></span> <span class="punctuation">(</span><span class="punctuation">{</span> | 39 | <span class="punctuation">(</span><span class="punctuation">$</span><span class="punctuation">(</span><span class="punctuation">$</span>arg<span class="punctuation">:</span>tt<span class="punctuation">)</span><span class="punctuation">*</span><span class="punctuation">)</span> <span class="operator">=</span><span class="punctuation">></span> <span class="punctuation">(</span><span class="punctuation">{</span> |
40 | <span class="punctuation">$</span><span class="keyword">crate</span><span class="punctuation">:</span><span class="punctuation">:</span>io<span class="punctuation">:</span><span class="punctuation">:</span>_print<span class="punctuation">(</span><span class="punctuation">$</span><span class="keyword">crate</span><span class="punctuation">:</span><span class="punctuation">:</span>format_args_nl<span class="punctuation">!</span><span class="punctuation">(</span><span class="punctuation">$</span><span class="punctuation">(</span><span class="punctuation">$</span>arg<span class="punctuation">)</span><span class="punctuation">*</span><span class="punctuation">)</span><span class="punctuation">)</span><span class="punctuation">;</span> | 40 | <span class="punctuation">$</span>crate<span class="punctuation">:</span><span class="punctuation">:</span>io<span class="punctuation">:</span><span class="punctuation">:</span>_print<span class="punctuation">(</span><span class="punctuation">$</span>crate<span class="punctuation">:</span><span class="punctuation">:</span>format_args_nl<span class="punctuation">!</span><span class="punctuation">(</span><span class="punctuation">$</span><span class="punctuation">(</span><span class="punctuation">$</span>arg<span class="punctuation">)</span><span class="punctuation">*</span><span class="punctuation">)</span><span class="punctuation">)</span><span class="punctuation">;</span> |
41 | <span class="punctuation">}</span><span class="punctuation">)</span> | 41 | <span class="punctuation">}</span><span class="punctuation">)</span> |
42 | <span class="punctuation">}</span> | 42 | <span class="punctuation">}</span> |
43 | #[rustc_builtin_macro] | 43 | #[rustc_builtin_macro] |
diff --git a/crates/ide/src/syntax_highlighting/test_data/highlighting.html b/crates/ide/src/syntax_highlighting/test_data/highlighting.html index 1d8a3c404..0bb0928e4 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlighting.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlighting.html | |||
@@ -62,7 +62,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd | |||
62 | 62 | ||
63 | <span class="keyword">impl</span> <span class="struct">Foo</span> <span class="punctuation">{</span> | 63 | <span class="keyword">impl</span> <span class="struct">Foo</span> <span class="punctuation">{</span> |
64 | <span class="keyword">fn</span> <span class="function declaration">baz</span><span class="punctuation">(</span><span class="keyword">mut</span> <span class="self_keyword mutable">self</span><span class="punctuation">,</span> <span class="value_param declaration">f</span><span class="punctuation">:</span> <span class="struct">Foo</span><span class="punctuation">)</span> <span class="operator">-></span> <span class="builtin_type">i32</span> <span class="punctuation">{</span> | 64 | <span class="keyword">fn</span> <span class="function declaration">baz</span><span class="punctuation">(</span><span class="keyword">mut</span> <span class="self_keyword mutable">self</span><span class="punctuation">,</span> <span class="value_param declaration">f</span><span class="punctuation">:</span> <span class="struct">Foo</span><span class="punctuation">)</span> <span class="operator">-></span> <span class="builtin_type">i32</span> <span class="punctuation">{</span> |
65 | <span class="value_param">f</span><span class="punctuation">.</span><span class="function consuming">baz</span><span class="punctuation">(</span><span class="self_keyword consuming">self</span><span class="punctuation">)</span> | 65 | <span class="value_param">f</span><span class="punctuation">.</span><span class="function consuming">baz</span><span class="punctuation">(</span><span class="self_keyword mutable consuming">self</span><span class="punctuation">)</span> |
66 | <span class="punctuation">}</span> | 66 | <span class="punctuation">}</span> |
67 | 67 | ||
68 | <span class="keyword">fn</span> <span class="function declaration">qux</span><span class="punctuation">(</span><span class="operator">&</span><span class="keyword">mut</span> <span class="self_keyword mutable">self</span><span class="punctuation">)</span> <span class="punctuation">{</span> | 68 | <span class="keyword">fn</span> <span class="function declaration">qux</span><span class="punctuation">(</span><span class="operator">&</span><span class="keyword">mut</span> <span class="self_keyword mutable">self</span><span class="punctuation">)</span> <span class="punctuation">{</span> |
@@ -115,6 +115,10 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd | |||
115 | <span class="punctuation">}</span> | 115 | <span class="punctuation">}</span> |
116 | <span class="punctuation">}</span> | 116 | <span class="punctuation">}</span> |
117 | 117 | ||
118 | <span class="macro">macro_rules!</span> <span class="macro declaration">keyword_frag</span> <span class="punctuation">{</span> | ||
119 | <span class="punctuation">(</span><span class="punctuation">$</span>type<span class="punctuation">:</span>ty<span class="punctuation">)</span> <span class="operator">=</span><span class="punctuation">></span> <span class="punctuation">(</span><span class="punctuation">$</span>type<span class="punctuation">)</span> | ||
120 | <span class="punctuation">}</span> | ||
121 | |||
118 | <span class="comment">// comment</span> | 122 | <span class="comment">// comment</span> |
119 | <span class="keyword">fn</span> <span class="function declaration">main</span><span class="punctuation">(</span><span class="punctuation">)</span> <span class="punctuation">{</span> | 123 | <span class="keyword">fn</span> <span class="function declaration">main</span><span class="punctuation">(</span><span class="punctuation">)</span> <span class="punctuation">{</span> |
120 | <span class="macro">println!</span><span class="punctuation">(</span><span class="string_literal">"Hello, {}!"</span><span class="punctuation">,</span> <span class="numeric_literal">92</span><span class="punctuation">)</span><span class="punctuation">;</span> | 124 | <span class="macro">println!</span><span class="punctuation">(</span><span class="string_literal">"Hello, {}!"</span><span class="punctuation">,</span> <span class="numeric_literal">92</span><span class="punctuation">)</span><span class="punctuation">;</span> |
diff --git a/crates/ide/src/syntax_highlighting/tests.rs b/crates/ide/src/syntax_highlighting/tests.rs index 694c4b7fa..126363b8b 100644 --- a/crates/ide/src/syntax_highlighting/tests.rs +++ b/crates/ide/src/syntax_highlighting/tests.rs | |||
@@ -89,6 +89,10 @@ macro_rules! noop { | |||
89 | } | 89 | } |
90 | } | 90 | } |
91 | 91 | ||
92 | macro_rules! keyword_frag { | ||
93 | ($type:ty) => ($type) | ||
94 | } | ||
95 | |||
92 | // comment | 96 | // comment |
93 | fn main() { | 97 | fn main() { |
94 | println!("Hello, {}!", 92); | 98 | println!("Hello, {}!", 92); |
diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs index 4e3340b0d..215be850f 100644 --- a/crates/rust-analyzer/src/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs | |||
@@ -1301,6 +1301,18 @@ pub(crate) fn handle_semantic_tokens_range( | |||
1301 | Ok(Some(semantic_tokens.into())) | 1301 | Ok(Some(semantic_tokens.into())) |
1302 | } | 1302 | } |
1303 | 1303 | ||
1304 | pub(crate) fn handle_open_docs( | ||
1305 | snap: GlobalStateSnapshot, | ||
1306 | params: lsp_types::TextDocumentPositionParams, | ||
1307 | ) -> Result<Option<lsp_types::Url>> { | ||
1308 | let _p = profile::span("handle_open_docs"); | ||
1309 | let position = from_proto::file_position(&snap, params)?; | ||
1310 | |||
1311 | let remote = snap.analysis.external_docs(position)?; | ||
1312 | |||
1313 | Ok(remote.and_then(|remote| Url::parse(&remote).ok())) | ||
1314 | } | ||
1315 | |||
1304 | fn implementation_title(count: usize) -> String { | 1316 | fn implementation_title(count: usize) -> String { |
1305 | if count == 1 { | 1317 | if count == 1 { |
1306 | "1 implementation".into() | 1318 | "1 implementation".into() |
diff --git a/crates/rust-analyzer/src/lsp_ext.rs b/crates/rust-analyzer/src/lsp_ext.rs index fee0bb69c..f31f8d900 100644 --- a/crates/rust-analyzer/src/lsp_ext.rs +++ b/crates/rust-analyzer/src/lsp_ext.rs | |||
@@ -347,3 +347,11 @@ pub struct CommandLink { | |||
347 | #[serde(skip_serializing_if = "Option::is_none")] | 347 | #[serde(skip_serializing_if = "Option::is_none")] |
348 | pub tooltip: Option<String>, | 348 | pub tooltip: Option<String>, |
349 | } | 349 | } |
350 | |||
351 | pub enum ExternalDocs {} | ||
352 | |||
353 | impl Request for ExternalDocs { | ||
354 | type Params = lsp_types::TextDocumentPositionParams; | ||
355 | type Result = Option<lsp_types::Url>; | ||
356 | const METHOD: &'static str = "experimental/externalDocs"; | ||
357 | } | ||
diff --git a/crates/rust-analyzer/src/main_loop.rs b/crates/rust-analyzer/src/main_loop.rs index 4b7ac8224..06b38d99c 100644 --- a/crates/rust-analyzer/src/main_loop.rs +++ b/crates/rust-analyzer/src/main_loop.rs | |||
@@ -384,6 +384,7 @@ impl GlobalState { | |||
384 | .on::<lsp_ext::CodeActionRequest>(handlers::handle_code_action)? | 384 | .on::<lsp_ext::CodeActionRequest>(handlers::handle_code_action)? |
385 | .on::<lsp_ext::ResolveCodeActionRequest>(handlers::handle_resolve_code_action)? | 385 | .on::<lsp_ext::ResolveCodeActionRequest>(handlers::handle_resolve_code_action)? |
386 | .on::<lsp_ext::HoverRequest>(handlers::handle_hover)? | 386 | .on::<lsp_ext::HoverRequest>(handlers::handle_hover)? |
387 | .on::<lsp_ext::ExternalDocs>(handlers::handle_open_docs)? | ||
387 | .on::<lsp_types::request::OnTypeFormatting>(handlers::handle_on_type_formatting)? | 388 | .on::<lsp_types::request::OnTypeFormatting>(handlers::handle_on_type_formatting)? |
388 | .on::<lsp_types::request::DocumentSymbolRequest>(handlers::handle_document_symbol)? | 389 | .on::<lsp_types::request::DocumentSymbolRequest>(handlers::handle_document_symbol)? |
389 | .on::<lsp_types::request::WorkspaceSymbol>(handlers::handle_workspace_symbol)? | 390 | .on::<lsp_types::request::WorkspaceSymbol>(handlers::handle_workspace_symbol)? |
diff --git a/crates/stdx/src/macros.rs b/crates/stdx/src/macros.rs index bf298460f..f5ee3484b 100644 --- a/crates/stdx/src/macros.rs +++ b/crates/stdx/src/macros.rs | |||
@@ -18,7 +18,13 @@ macro_rules! format_to { | |||
18 | }; | 18 | }; |
19 | } | 19 | } |
20 | 20 | ||
21 | // Generates `From` impls for `Enum E { Foo(Foo), Bar(Bar) }` enums | 21 | /// Generates `From` impls for `Enum E { Foo(Foo), Bar(Bar) }` enums |
22 | /// | ||
23 | /// # Example | ||
24 | /// | ||
25 | /// ```rust | ||
26 | /// impl_from!(Struct, Union, Enum for Adt); | ||
27 | /// ``` | ||
22 | #[macro_export] | 28 | #[macro_export] |
23 | macro_rules! impl_from { | 29 | macro_rules! impl_from { |
24 | ($($variant:ident $(($($sub_variant:ident),*))?),* for $enum:ident) => { | 30 | ($($variant:ident $(($($sub_variant:ident),*))?),* for $enum:ident) => { |
diff --git a/docs/dev/lsp-extensions.md b/docs/dev/lsp-extensions.md index f1160bb1c..3f861f3e0 100644 --- a/docs/dev/lsp-extensions.md +++ b/docs/dev/lsp-extensions.md | |||
@@ -129,7 +129,7 @@ As a result of the command call the client will get the respective workspace edi | |||
129 | 129 | ||
130 | **Server Capability:** `{ "parentModule": boolean }` | 130 | **Server Capability:** `{ "parentModule": boolean }` |
131 | 131 | ||
132 | This request is send from client to server to handle "Goto Parent Module" editor action. | 132 | This request is sent from client to server to handle "Goto Parent Module" editor action. |
133 | 133 | ||
134 | **Method:** `experimental/parentModule` | 134 | **Method:** `experimental/parentModule` |
135 | 135 | ||
@@ -163,7 +163,7 @@ mod foo; | |||
163 | 163 | ||
164 | **Server Capability:** `{ "joinLines": boolean }` | 164 | **Server Capability:** `{ "joinLines": boolean }` |
165 | 165 | ||
166 | This request is send from client to server to handle "Join Lines" editor action. | 166 | This request is sent from client to server to handle "Join Lines" editor action. |
167 | 167 | ||
168 | **Method:** `experimental/joinLines` | 168 | **Method:** `experimental/joinLines` |
169 | 169 | ||
@@ -210,7 +210,7 @@ fn main() { | |||
210 | 210 | ||
211 | **Server Capability:** `{ "onEnter": boolean }` | 211 | **Server Capability:** `{ "onEnter": boolean }` |
212 | 212 | ||
213 | This request is send from client to server to handle <kbd>Enter</kbd> keypress. | 213 | This request is sent from client to server to handle <kbd>Enter</kbd> keypress. |
214 | 214 | ||
215 | **Method:** `experimental/onEnter` | 215 | **Method:** `experimental/onEnter` |
216 | 216 | ||
@@ -261,7 +261,7 @@ As proper cursor positioning is raison-d'etat for `onEnter`, it uses `SnippetTex | |||
261 | 261 | ||
262 | **Server Capability:** `{ "ssr": boolean }` | 262 | **Server Capability:** `{ "ssr": boolean }` |
263 | 263 | ||
264 | This request is send from client to server to handle structural search replace -- automated syntax tree based transformation of the source. | 264 | This request is sent from client to server to handle structural search replace -- automated syntax tree based transformation of the source. |
265 | 265 | ||
266 | **Method:** `experimental/ssr` | 266 | **Method:** `experimental/ssr` |
267 | 267 | ||
@@ -303,7 +303,7 @@ SSR with query `foo($a, $b) ==>> ($a).foo($b)` will transform, eg `foo(y + 5, z) | |||
303 | 303 | ||
304 | **Server Capability:** `{ "matchingBrace": boolean }` | 304 | **Server Capability:** `{ "matchingBrace": boolean }` |
305 | 305 | ||
306 | This request is send from client to server to handle "Matching Brace" editor action. | 306 | This request is sent from client to server to handle "Matching Brace" editor action. |
307 | 307 | ||
308 | **Method:** `experimental/matchingBrace` | 308 | **Method:** `experimental/matchingBrace` |
309 | 309 | ||
@@ -348,7 +348,7 @@ Moreover, it would be cool if editors didn't need to implement even basic langua | |||
348 | 348 | ||
349 | **Server Capability:** `{ "runnables": { "kinds": string[] } }` | 349 | **Server Capability:** `{ "runnables": { "kinds": string[] } }` |
350 | 350 | ||
351 | This request is send from client to server to get the list of things that can be run (tests, binaries, `cargo check -p`). | 351 | This request is sent from client to server to get the list of things that can be run (tests, binaries, `cargo check -p`). |
352 | 352 | ||
353 | **Method:** `experimental/runnables` | 353 | **Method:** `experimental/runnables` |
354 | 354 | ||
@@ -386,6 +386,17 @@ rust-analyzer supports only one `kind`, `"cargo"`. The `args` for `"cargo"` look | |||
386 | } | 386 | } |
387 | ``` | 387 | ``` |
388 | 388 | ||
389 | ## Open External Documentation | ||
390 | |||
391 | This request is sent from client to server to get a URL to documentation for the symbol under the cursor, if available. | ||
392 | |||
393 | **Method** `experimental/externalDocs` | ||
394 | |||
395 | **Request:**: `TextDocumentPositionParams` | ||
396 | |||
397 | **Response** `string | null` | ||
398 | |||
399 | |||
389 | ## Analyzer Status | 400 | ## Analyzer Status |
390 | 401 | ||
391 | **Method:** `rust-analyzer/analyzerStatus` | 402 | **Method:** `rust-analyzer/analyzerStatus` |
@@ -477,7 +488,7 @@ Expands macro call at a given position. | |||
477 | 488 | ||
478 | **Method:** `rust-analyzer/inlayHints` | 489 | **Method:** `rust-analyzer/inlayHints` |
479 | 490 | ||
480 | This request is send from client to server to render "inlay hints" -- virtual text inserted into editor to show things like inferred types. | 491 | This request is sent from client to server to render "inlay hints" -- virtual text inserted into editor to show things like inferred types. |
481 | Generally, the client should re-query inlay hints after every modification. | 492 | Generally, the client should re-query inlay hints after every modification. |
482 | Note that we plan to move this request to `experimental/inlayHints`, as it is not really Rust-specific, but the current API is not necessary the right one. | 493 | Note that we plan to move this request to `experimental/inlayHints`, as it is not really Rust-specific, but the current API is not necessary the right one. |
483 | Upstream issue: https://github.com/microsoft/language-server-protocol/issues/956 | 494 | Upstream issue: https://github.com/microsoft/language-server-protocol/issues/956 |
diff --git a/editors/code/package.json b/editors/code/package.json index 6a712a8a8..4bd3117fc 100644 --- a/editors/code/package.json +++ b/editors/code/package.json | |||
@@ -182,6 +182,11 @@ | |||
182 | "command": "rust-analyzer.toggleInlayHints", | 182 | "command": "rust-analyzer.toggleInlayHints", |
183 | "title": "Toggle inlay hints", | 183 | "title": "Toggle inlay hints", |
184 | "category": "Rust Analyzer" | 184 | "category": "Rust Analyzer" |
185 | }, | ||
186 | { | ||
187 | "command": "rust-analyzer.openDocs", | ||
188 | "title": "Open docs under cursor", | ||
189 | "category": "Rust Analyzer" | ||
185 | } | 190 | } |
186 | ], | 191 | ], |
187 | "keybindings": [ | 192 | "keybindings": [ |
@@ -1044,6 +1049,10 @@ | |||
1044 | { | 1049 | { |
1045 | "command": "rust-analyzer.toggleInlayHints", | 1050 | "command": "rust-analyzer.toggleInlayHints", |
1046 | "when": "inRustProject" | 1051 | "when": "inRustProject" |
1052 | }, | ||
1053 | { | ||
1054 | "command": "rust-analyzer.openDocs", | ||
1055 | "when": "inRustProject" | ||
1047 | } | 1056 | } |
1048 | ] | 1057 | ] |
1049 | } | 1058 | } |
diff --git a/editors/code/src/commands.ts b/editors/code/src/commands.ts index 1a90f1b7d..1445e41d3 100644 --- a/editors/code/src/commands.ts +++ b/editors/code/src/commands.ts | |||
@@ -419,10 +419,31 @@ export function gotoLocation(ctx: Ctx): Cmd { | |||
419 | }; | 419 | }; |
420 | } | 420 | } |
421 | 421 | ||
422 | export function openDocs(ctx: Ctx): Cmd { | ||
423 | return async () => { | ||
424 | |||
425 | const client = ctx.client; | ||
426 | const editor = vscode.window.activeTextEditor; | ||
427 | if (!editor || !client) { | ||
428 | return; | ||
429 | }; | ||
430 | |||
431 | const position = editor.selection.active; | ||
432 | const textDocument = { uri: editor.document.uri.toString() }; | ||
433 | |||
434 | const doclink = await client.sendRequest(ra.openDocs, { position, textDocument }); | ||
435 | |||
436 | if (doclink != null) { | ||
437 | vscode.commands.executeCommand("vscode.open", vscode.Uri.parse(doclink)); | ||
438 | } | ||
439 | }; | ||
440 | |||
441 | } | ||
442 | |||
422 | export function resolveCodeAction(ctx: Ctx): Cmd { | 443 | export function resolveCodeAction(ctx: Ctx): Cmd { |
423 | const client = ctx.client; | 444 | const client = ctx.client; |
424 | return async (params: ra.ResolveCodeActionParams) => { | 445 | return async () => { |
425 | const item: lc.WorkspaceEdit = await client.sendRequest(ra.resolveCodeAction, params); | 446 | const item: lc.WorkspaceEdit = await client.sendRequest(ra.resolveCodeAction, null); |
426 | if (!item) { | 447 | if (!item) { |
427 | return; | 448 | return; |
428 | } | 449 | } |
diff --git a/editors/code/src/lsp_ext.ts b/editors/code/src/lsp_ext.ts index f286b68a6..fc8e120b3 100644 --- a/editors/code/src/lsp_ext.ts +++ b/editors/code/src/lsp_ext.ts | |||
@@ -118,3 +118,5 @@ export interface CommandLinkGroup { | |||
118 | title?: string; | 118 | title?: string; |
119 | commands: CommandLink[]; | 119 | commands: CommandLink[]; |
120 | } | 120 | } |
121 | |||
122 | export const openDocs = new lc.RequestType<lc.TextDocumentPositionParams, string | void, void>('experimental/externalDocs'); | ||
diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts index 2896d90ac..09543e348 100644 --- a/editors/code/src/main.ts +++ b/editors/code/src/main.ts | |||
@@ -110,6 +110,7 @@ async function tryActivate(context: vscode.ExtensionContext) { | |||
110 | ctx.registerCommand('run', commands.run); | 110 | ctx.registerCommand('run', commands.run); |
111 | ctx.registerCommand('debug', commands.debug); | 111 | ctx.registerCommand('debug', commands.debug); |
112 | ctx.registerCommand('newDebugConfig', commands.newDebugConfig); | 112 | ctx.registerCommand('newDebugConfig', commands.newDebugConfig); |
113 | ctx.registerCommand('openDocs', commands.openDocs); | ||
113 | 114 | ||
114 | defaultOnEnter.dispose(); | 115 | defaultOnEnter.dispose(); |
115 | ctx.registerCommand('onEnter', commands.onEnter); | 116 | ctx.registerCommand('onEnter', commands.onEnter); |