diff options
-rw-r--r-- | Cargo.lock | 18 | ||||
-rw-r--r-- | bors.toml | 1 | ||||
-rw-r--r-- | crates/assists/src/handlers/auto_import.rs | 269 | ||||
-rw-r--r-- | crates/assists/src/handlers/merge_imports.rs | 14 | ||||
-rw-r--r-- | crates/assists/src/handlers/replace_qualified_name_with_use.rs | 17 | ||||
-rw-r--r-- | crates/assists/src/utils.rs | 1 | ||||
-rw-r--r-- | crates/assists/src/utils/import_assets.rs | 268 | ||||
-rw-r--r-- | crates/assists/src/utils/insert_use.rs | 24 | ||||
-rw-r--r-- | crates/base_db/Cargo.toml | 2 | ||||
-rw-r--r-- | crates/ide/Cargo.toml | 4 | ||||
-rw-r--r-- | crates/ide/src/completion.rs | 2 | ||||
-rw-r--r-- | crates/ide/src/doc_links.rs | 25 | ||||
-rw-r--r-- | crates/project_model/src/sysroot.rs | 4 | ||||
-rw-r--r-- | docs/dev/README.md | 7 | ||||
-rw-r--r-- | docs/dev/lsp-extensions.md | 10 | ||||
-rw-r--r-- | docs/dev/style.md | 25 | ||||
-rw-r--r-- | xtask/tests/tidy.rs | 45 |
17 files changed, 469 insertions, 267 deletions
diff --git a/Cargo.lock b/Cargo.lock index b890b6e19..d470d84f2 100644 --- a/Cargo.lock +++ b/Cargo.lock | |||
@@ -1191,9 +1191,9 @@ dependencies = [ | |||
1191 | 1191 | ||
1192 | [[package]] | 1192 | [[package]] |
1193 | name = "pulldown-cmark" | 1193 | name = "pulldown-cmark" |
1194 | version = "0.7.2" | 1194 | version = "0.8.0" |
1195 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1195 | source = "registry+https://github.com/rust-lang/crates.io-index" |
1196 | checksum = "ca36dea94d187597e104a5c8e4b07576a8a45aa5db48a65e12940d3eb7461f55" | 1196 | checksum = "ffade02495f22453cd593159ea2f59827aae7f53fa8323f756799b670881dcf8" |
1197 | dependencies = [ | 1197 | dependencies = [ |
1198 | "bitflags", | 1198 | "bitflags", |
1199 | "memchr", | 1199 | "memchr", |
@@ -1202,9 +1202,9 @@ dependencies = [ | |||
1202 | 1202 | ||
1203 | [[package]] | 1203 | [[package]] |
1204 | name = "pulldown-cmark-to-cmark" | 1204 | name = "pulldown-cmark-to-cmark" |
1205 | version = "5.0.0" | 1205 | version = "6.0.0" |
1206 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1206 | source = "registry+https://github.com/rust-lang/crates.io-index" |
1207 | checksum = "32accf4473121d8c0b508ca5673363703762d6cc59cf25af1df48f653346f736" | 1207 | checksum = "e8f2b9878102358ec65434fdd1a9a161f8648bb2f531acc9260e4d094c96de23" |
1208 | dependencies = [ | 1208 | dependencies = [ |
1209 | "pulldown-cmark", | 1209 | "pulldown-cmark", |
1210 | ] | 1210 | ] |
@@ -1361,11 +1361,11 @@ checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" | |||
1361 | 1361 | ||
1362 | [[package]] | 1362 | [[package]] |
1363 | name = "salsa" | 1363 | name = "salsa" |
1364 | version = "0.15.2" | 1364 | version = "0.16.0" |
1365 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1365 | source = "registry+https://github.com/rust-lang/crates.io-index" |
1366 | checksum = "9ab29056d4fb4048a5f0d169c9b6e5526160c9ec37aded5a6879c2c9c445a8e4" | 1366 | checksum = "d8fadca2ab5de17acf66d744f4888049ca8f1bb9b8a1ab8afd9d032cc959c5dc" |
1367 | dependencies = [ | 1367 | dependencies = [ |
1368 | "crossbeam-utils 0.7.2", | 1368 | "crossbeam-utils 0.8.0", |
1369 | "indexmap", | 1369 | "indexmap", |
1370 | "lock_api", | 1370 | "lock_api", |
1371 | "log", | 1371 | "log", |
@@ -1378,9 +1378,9 @@ dependencies = [ | |||
1378 | 1378 | ||
1379 | [[package]] | 1379 | [[package]] |
1380 | name = "salsa-macros" | 1380 | name = "salsa-macros" |
1381 | version = "0.15.2" | 1381 | version = "0.16.0" |
1382 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1382 | source = "registry+https://github.com/rust-lang/crates.io-index" |
1383 | checksum = "a1c3aec007c63c4ed4cd7a018529fb0b5575c4562575fc6a40d6cd2ae0b792ef" | 1383 | checksum = "cd3904a4ba0a9d0211816177fd34b04c7095443f8cdacd11175064fe541c8fe2" |
1384 | dependencies = [ | 1384 | dependencies = [ |
1385 | "heck", | 1385 | "heck", |
1386 | "proc-macro2", | 1386 | "proc-macro2", |
@@ -6,3 +6,4 @@ status = [ | |||
6 | "TypeScript (windows-latest)", | 6 | "TypeScript (windows-latest)", |
7 | ] | 7 | ] |
8 | delete_merged_branches = true | 8 | delete_merged_branches = true |
9 | timeout_sec = 1200 # 20 min | ||
diff --git a/crates/assists/src/handlers/auto_import.rs b/crates/assists/src/handlers/auto_import.rs index d3ee98e5f..e595b5b93 100644 --- a/crates/assists/src/handlers/auto_import.rs +++ b/crates/assists/src/handlers/auto_import.rs | |||
@@ -1,21 +1,9 @@ | |||
1 | use std::collections::BTreeSet; | 1 | use syntax::ast; |
2 | |||
3 | use either::Either; | ||
4 | use hir::{ | ||
5 | AsAssocItem, AssocItemContainer, ModPath, Module, ModuleDef, PathResolution, Semantics, Trait, | ||
6 | Type, | ||
7 | }; | ||
8 | use ide_db::{imports_locator, RootDatabase}; | ||
9 | use insert_use::ImportScope; | ||
10 | use rustc_hash::FxHashSet; | ||
11 | use syntax::{ | ||
12 | ast::{self, AstNode}, | ||
13 | SyntaxNode, | ||
14 | }; | ||
15 | 2 | ||
16 | use crate::{ | 3 | use crate::{ |
17 | utils::insert_use, utils::mod_path_to_ast, AssistContext, AssistId, AssistKind, Assists, | 4 | utils::import_assets::{ImportAssets, ImportCandidate}, |
18 | GroupLabel, | 5 | utils::{insert_use, mod_path_to_ast, ImportScope}, |
6 | AssistContext, AssistId, AssistKind, Assists, GroupLabel, | ||
19 | }; | 7 | }; |
20 | 8 | ||
21 | // Assist: auto_import | 9 | // Assist: auto_import |
@@ -38,16 +26,24 @@ use crate::{ | |||
38 | // # pub mod std { pub mod collections { pub struct HashMap { } } } | 26 | // # pub mod std { pub mod collections { pub struct HashMap { } } } |
39 | // ``` | 27 | // ``` |
40 | pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { | 28 | pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { |
41 | let auto_import_assets = AutoImportAssets::new(ctx)?; | 29 | let import_assets = |
42 | let proposed_imports = auto_import_assets.search_for_imports(ctx); | 30 | if let Some(path_under_caret) = ctx.find_node_at_offset_with_descend::<ast::Path>() { |
31 | ImportAssets::for_regular_path(path_under_caret, &ctx.sema) | ||
32 | } else if let Some(method_under_caret) = | ||
33 | ctx.find_node_at_offset_with_descend::<ast::MethodCallExpr>() | ||
34 | { | ||
35 | ImportAssets::for_method_call(method_under_caret, &ctx.sema) | ||
36 | } else { | ||
37 | None | ||
38 | }?; | ||
39 | let proposed_imports = import_assets.search_for_imports(&ctx.sema, &ctx.config.insert_use); | ||
43 | if proposed_imports.is_empty() { | 40 | if proposed_imports.is_empty() { |
44 | return None; | 41 | return None; |
45 | } | 42 | } |
46 | 43 | ||
47 | let range = ctx.sema.original_range(&auto_import_assets.syntax_under_caret).range; | 44 | let range = ctx.sema.original_range(import_assets.syntax_under_caret()).range; |
48 | let group = auto_import_assets.get_import_group_message(); | 45 | let group = import_group_message(import_assets.import_candidate()); |
49 | let scope = | 46 | let scope = ImportScope::find_insert_use_container(import_assets.syntax_under_caret(), ctx)?; |
50 | ImportScope::find_insert_use_container(&auto_import_assets.syntax_under_caret, ctx)?; | ||
51 | let syntax = scope.as_syntax_node(); | 47 | let syntax = scope.as_syntax_node(); |
52 | for import in proposed_imports { | 48 | for import in proposed_imports { |
53 | acc.add_group( | 49 | acc.add_group( |
@@ -65,227 +61,18 @@ pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()> | |||
65 | Some(()) | 61 | Some(()) |
66 | } | 62 | } |
67 | 63 | ||
68 | #[derive(Debug)] | 64 | fn import_group_message(import_candidate: &ImportCandidate) -> GroupLabel { |
69 | struct AutoImportAssets { | 65 | let name = match import_candidate { |
70 | import_candidate: ImportCandidate, | 66 | ImportCandidate::UnqualifiedName(candidate) |
71 | module_with_name_to_import: Module, | 67 | | ImportCandidate::QualifierStart(candidate) => format!("Import {}", &candidate.name), |
72 | syntax_under_caret: SyntaxNode, | 68 | ImportCandidate::TraitAssocItem(candidate) => { |
73 | } | 69 | format!("Import a trait for item {}", &candidate.name) |
74 | |||
75 | impl AutoImportAssets { | ||
76 | fn new(ctx: &AssistContext) -> Option<Self> { | ||
77 | if let Some(path_under_caret) = ctx.find_node_at_offset_with_descend::<ast::Path>() { | ||
78 | Self::for_regular_path(path_under_caret, &ctx) | ||
79 | } else { | ||
80 | Self::for_method_call(ctx.find_node_at_offset_with_descend()?, &ctx) | ||
81 | } | ||
82 | } | ||
83 | |||
84 | fn for_method_call(method_call: ast::MethodCallExpr, ctx: &AssistContext) -> Option<Self> { | ||
85 | let syntax_under_caret = method_call.syntax().to_owned(); | ||
86 | let module_with_name_to_import = ctx.sema.scope(&syntax_under_caret).module()?; | ||
87 | Some(Self { | ||
88 | import_candidate: ImportCandidate::for_method_call(&ctx.sema, &method_call)?, | ||
89 | module_with_name_to_import, | ||
90 | syntax_under_caret, | ||
91 | }) | ||
92 | } | ||
93 | |||
94 | fn for_regular_path(path_under_caret: ast::Path, ctx: &AssistContext) -> Option<Self> { | ||
95 | let syntax_under_caret = path_under_caret.syntax().to_owned(); | ||
96 | if syntax_under_caret.ancestors().find_map(ast::Use::cast).is_some() { | ||
97 | return None; | ||
98 | } | ||
99 | |||
100 | let module_with_name_to_import = ctx.sema.scope(&syntax_under_caret).module()?; | ||
101 | Some(Self { | ||
102 | import_candidate: ImportCandidate::for_regular_path(&ctx.sema, &path_under_caret)?, | ||
103 | module_with_name_to_import, | ||
104 | syntax_under_caret, | ||
105 | }) | ||
106 | } | ||
107 | |||
108 | fn get_search_query(&self) -> &str { | ||
109 | match &self.import_candidate { | ||
110 | ImportCandidate::UnqualifiedName(name) => name, | ||
111 | ImportCandidate::QualifierStart(qualifier_start) => qualifier_start, | ||
112 | ImportCandidate::TraitAssocItem(_, trait_assoc_item_name) => trait_assoc_item_name, | ||
113 | ImportCandidate::TraitMethod(_, trait_method_name) => trait_method_name, | ||
114 | } | ||
115 | } | ||
116 | |||
117 | fn get_import_group_message(&self) -> GroupLabel { | ||
118 | let name = match &self.import_candidate { | ||
119 | ImportCandidate::UnqualifiedName(name) => format!("Import {}", name), | ||
120 | ImportCandidate::QualifierStart(qualifier_start) => { | ||
121 | format!("Import {}", qualifier_start) | ||
122 | } | ||
123 | ImportCandidate::TraitAssocItem(_, trait_assoc_item_name) => { | ||
124 | format!("Import a trait for item {}", trait_assoc_item_name) | ||
125 | } | ||
126 | ImportCandidate::TraitMethod(_, trait_method_name) => { | ||
127 | format!("Import a trait for method {}", trait_method_name) | ||
128 | } | ||
129 | }; | ||
130 | GroupLabel(name) | ||
131 | } | ||
132 | |||
133 | fn search_for_imports(&self, ctx: &AssistContext) -> BTreeSet<ModPath> { | ||
134 | let _p = profile::span("auto_import::search_for_imports"); | ||
135 | let db = ctx.db(); | ||
136 | let current_crate = self.module_with_name_to_import.krate(); | ||
137 | imports_locator::find_imports(&ctx.sema, current_crate, &self.get_search_query()) | ||
138 | .into_iter() | ||
139 | .filter_map(|candidate| match &self.import_candidate { | ||
140 | ImportCandidate::TraitAssocItem(assoc_item_type, _) => { | ||
141 | let located_assoc_item = match candidate { | ||
142 | Either::Left(ModuleDef::Function(located_function)) => located_function | ||
143 | .as_assoc_item(db) | ||
144 | .map(|assoc| assoc.container(db)) | ||
145 | .and_then(Self::assoc_to_trait), | ||
146 | Either::Left(ModuleDef::Const(located_const)) => located_const | ||
147 | .as_assoc_item(db) | ||
148 | .map(|assoc| assoc.container(db)) | ||
149 | .and_then(Self::assoc_to_trait), | ||
150 | _ => None, | ||
151 | }?; | ||
152 | |||
153 | let mut trait_candidates = FxHashSet::default(); | ||
154 | trait_candidates.insert(located_assoc_item.into()); | ||
155 | |||
156 | assoc_item_type | ||
157 | .iterate_path_candidates( | ||
158 | db, | ||
159 | current_crate, | ||
160 | &trait_candidates, | ||
161 | None, | ||
162 | |_, assoc| Self::assoc_to_trait(assoc.container(db)), | ||
163 | ) | ||
164 | .map(ModuleDef::from) | ||
165 | .map(Either::Left) | ||
166 | } | ||
167 | ImportCandidate::TraitMethod(function_callee, _) => { | ||
168 | let located_assoc_item = | ||
169 | if let Either::Left(ModuleDef::Function(located_function)) = candidate { | ||
170 | located_function | ||
171 | .as_assoc_item(db) | ||
172 | .map(|assoc| assoc.container(db)) | ||
173 | .and_then(Self::assoc_to_trait) | ||
174 | } else { | ||
175 | None | ||
176 | }?; | ||
177 | |||
178 | let mut trait_candidates = FxHashSet::default(); | ||
179 | trait_candidates.insert(located_assoc_item.into()); | ||
180 | |||
181 | function_callee | ||
182 | .iterate_method_candidates( | ||
183 | db, | ||
184 | current_crate, | ||
185 | &trait_candidates, | ||
186 | None, | ||
187 | |_, function| { | ||
188 | Self::assoc_to_trait(function.as_assoc_item(db)?.container(db)) | ||
189 | }, | ||
190 | ) | ||
191 | .map(ModuleDef::from) | ||
192 | .map(Either::Left) | ||
193 | } | ||
194 | _ => Some(candidate), | ||
195 | }) | ||
196 | .filter_map(|candidate| match candidate { | ||
197 | Either::Left(module_def) => self.module_with_name_to_import.find_use_path_prefixed( | ||
198 | db, | ||
199 | module_def, | ||
200 | ctx.config.insert_use.prefix_kind, | ||
201 | ), | ||
202 | Either::Right(macro_def) => self.module_with_name_to_import.find_use_path_prefixed( | ||
203 | db, | ||
204 | macro_def, | ||
205 | ctx.config.insert_use.prefix_kind, | ||
206 | ), | ||
207 | }) | ||
208 | .filter(|use_path| !use_path.segments.is_empty()) | ||
209 | .take(20) | ||
210 | .collect::<BTreeSet<_>>() | ||
211 | } | ||
212 | |||
213 | fn assoc_to_trait(assoc: AssocItemContainer) -> Option<Trait> { | ||
214 | if let AssocItemContainer::Trait(extracted_trait) = assoc { | ||
215 | Some(extracted_trait) | ||
216 | } else { | ||
217 | None | ||
218 | } | ||
219 | } | ||
220 | } | ||
221 | |||
222 | #[derive(Debug)] | ||
223 | enum ImportCandidate { | ||
224 | /// Simple name like 'HashMap' | ||
225 | UnqualifiedName(String), | ||
226 | /// First part of the qualified name. | ||
227 | /// For 'std::collections::HashMap', that will be 'std'. | ||
228 | QualifierStart(String), | ||
229 | /// A trait associated function (with no self parameter) or associated constant. | ||
230 | /// For 'test_mod::TestEnum::test_function', `Type` is the `test_mod::TestEnum` expression type | ||
231 | /// and `String` is the `test_function` | ||
232 | TraitAssocItem(Type, String), | ||
233 | /// A trait method with self parameter. | ||
234 | /// For 'test_enum.test_method()', `Type` is the `test_enum` expression type | ||
235 | /// and `String` is the `test_method` | ||
236 | TraitMethod(Type, String), | ||
237 | } | ||
238 | |||
239 | impl ImportCandidate { | ||
240 | fn for_method_call( | ||
241 | sema: &Semantics<RootDatabase>, | ||
242 | method_call: &ast::MethodCallExpr, | ||
243 | ) -> Option<Self> { | ||
244 | if sema.resolve_method_call(method_call).is_some() { | ||
245 | return None; | ||
246 | } | ||
247 | Some(Self::TraitMethod( | ||
248 | sema.type_of_expr(&method_call.receiver()?)?, | ||
249 | method_call.name_ref()?.syntax().to_string(), | ||
250 | )) | ||
251 | } | ||
252 | |||
253 | fn for_regular_path( | ||
254 | sema: &Semantics<RootDatabase>, | ||
255 | path_under_caret: &ast::Path, | ||
256 | ) -> Option<Self> { | ||
257 | if sema.resolve_path(path_under_caret).is_some() { | ||
258 | return None; | ||
259 | } | 70 | } |
260 | 71 | ImportCandidate::TraitMethod(candidate) => { | |
261 | let segment = path_under_caret.segment()?; | 72 | format!("Import a trait for method {}", &candidate.name) |
262 | if let Some(qualifier) = path_under_caret.qualifier() { | ||
263 | let qualifier_start = qualifier.syntax().descendants().find_map(ast::NameRef::cast)?; | ||
264 | let qualifier_start_path = | ||
265 | qualifier_start.syntax().ancestors().find_map(ast::Path::cast)?; | ||
266 | if let Some(qualifier_start_resolution) = sema.resolve_path(&qualifier_start_path) { | ||
267 | let qualifier_resolution = if qualifier_start_path == qualifier { | ||
268 | qualifier_start_resolution | ||
269 | } else { | ||
270 | sema.resolve_path(&qualifier)? | ||
271 | }; | ||
272 | if let PathResolution::Def(ModuleDef::Adt(assoc_item_path)) = qualifier_resolution { | ||
273 | Some(ImportCandidate::TraitAssocItem( | ||
274 | assoc_item_path.ty(sema.db), | ||
275 | segment.syntax().to_string(), | ||
276 | )) | ||
277 | } else { | ||
278 | None | ||
279 | } | ||
280 | } else { | ||
281 | Some(ImportCandidate::QualifierStart(qualifier_start.syntax().to_string())) | ||
282 | } | ||
283 | } else { | ||
284 | Some(ImportCandidate::UnqualifiedName( | ||
285 | segment.syntax().descendants().find_map(ast::NameRef::cast)?.syntax().to_string(), | ||
286 | )) | ||
287 | } | 73 | } |
288 | } | 74 | }; |
75 | GroupLabel(name) | ||
289 | } | 76 | } |
290 | 77 | ||
291 | #[cfg(test)] | 78 | #[cfg(test)] |
diff --git a/crates/assists/src/handlers/merge_imports.rs b/crates/assists/src/handlers/merge_imports.rs index fe33cee53..fd9c9e03c 100644 --- a/crates/assists/src/handlers/merge_imports.rs +++ b/crates/assists/src/handlers/merge_imports.rs | |||
@@ -73,6 +73,20 @@ mod tests { | |||
73 | use super::*; | 73 | use super::*; |
74 | 74 | ||
75 | #[test] | 75 | #[test] |
76 | fn test_merge_equal() { | ||
77 | check_assist( | ||
78 | merge_imports, | ||
79 | r" | ||
80 | use std::fmt<|>::{Display, Debug}; | ||
81 | use std::fmt::{Display, Debug}; | ||
82 | ", | ||
83 | r" | ||
84 | use std::fmt::{Debug, Display}; | ||
85 | ", | ||
86 | ) | ||
87 | } | ||
88 | |||
89 | #[test] | ||
76 | fn test_merge_first() { | 90 | fn test_merge_first() { |
77 | check_assist( | 91 | check_assist( |
78 | merge_imports, | 92 | merge_imports, |
diff --git a/crates/assists/src/handlers/replace_qualified_name_with_use.rs b/crates/assists/src/handlers/replace_qualified_name_with_use.rs index e95b971a4..c50bc7604 100644 --- a/crates/assists/src/handlers/replace_qualified_name_with_use.rs +++ b/crates/assists/src/handlers/replace_qualified_name_with_use.rs | |||
@@ -124,6 +124,23 @@ mod tests { | |||
124 | use super::*; | 124 | use super::*; |
125 | 125 | ||
126 | #[test] | 126 | #[test] |
127 | fn test_replace_already_imported() { | ||
128 | check_assist( | ||
129 | replace_qualified_name_with_use, | ||
130 | r"use std::fs; | ||
131 | |||
132 | fn main() { | ||
133 | std::f<|>s::Path | ||
134 | }", | ||
135 | r"use std::fs; | ||
136 | |||
137 | fn main() { | ||
138 | fs::Path | ||
139 | }", | ||
140 | ) | ||
141 | } | ||
142 | |||
143 | #[test] | ||
127 | fn test_replace_add_use_no_anchor() { | 144 | fn test_replace_add_use_no_anchor() { |
128 | check_assist( | 145 | check_assist( |
129 | replace_qualified_name_with_use, | 146 | replace_qualified_name_with_use, |
diff --git a/crates/assists/src/utils.rs b/crates/assists/src/utils.rs index c1847f601..b37b0d2b6 100644 --- a/crates/assists/src/utils.rs +++ b/crates/assists/src/utils.rs | |||
@@ -1,5 +1,6 @@ | |||
1 | //! Assorted functions shared by several assists. | 1 | //! Assorted functions shared by several assists. |
2 | pub(crate) mod insert_use; | 2 | pub(crate) mod insert_use; |
3 | pub(crate) mod import_assets; | ||
3 | 4 | ||
4 | use std::{iter, ops}; | 5 | use std::{iter, ops}; |
5 | 6 | ||
diff --git a/crates/assists/src/utils/import_assets.rs b/crates/assists/src/utils/import_assets.rs new file mode 100644 index 000000000..601f51098 --- /dev/null +++ b/crates/assists/src/utils/import_assets.rs | |||
@@ -0,0 +1,268 @@ | |||
1 | //! Look up accessible paths for items. | ||
2 | use std::collections::BTreeSet; | ||
3 | |||
4 | use either::Either; | ||
5 | use hir::{AsAssocItem, AssocItemContainer, ModuleDef, Semantics}; | ||
6 | use ide_db::{imports_locator, RootDatabase}; | ||
7 | use rustc_hash::FxHashSet; | ||
8 | use syntax::{ast, AstNode, SyntaxNode}; | ||
9 | |||
10 | use crate::assist_config::InsertUseConfig; | ||
11 | |||
12 | #[derive(Debug)] | ||
13 | pub(crate) enum ImportCandidate { | ||
14 | /// Simple name like 'HashMap' | ||
15 | UnqualifiedName(PathImportCandidate), | ||
16 | /// First part of the qualified name. | ||
17 | /// For 'std::collections::HashMap', that will be 'std'. | ||
18 | QualifierStart(PathImportCandidate), | ||
19 | /// A trait associated function (with no self parameter) or associated constant. | ||
20 | /// For 'test_mod::TestEnum::test_function', `ty` is the `test_mod::TestEnum` expression type | ||
21 | /// and `name` is the `test_function` | ||
22 | TraitAssocItem(TraitImportCandidate), | ||
23 | /// A trait method with self parameter. | ||
24 | /// For 'test_enum.test_method()', `ty` is the `test_enum` expression type | ||
25 | /// and `name` is the `test_method` | ||
26 | TraitMethod(TraitImportCandidate), | ||
27 | } | ||
28 | |||
29 | #[derive(Debug)] | ||
30 | pub(crate) struct TraitImportCandidate { | ||
31 | pub ty: hir::Type, | ||
32 | pub name: String, | ||
33 | } | ||
34 | |||
35 | #[derive(Debug)] | ||
36 | pub(crate) struct PathImportCandidate { | ||
37 | pub name: String, | ||
38 | } | ||
39 | |||
40 | #[derive(Debug)] | ||
41 | pub(crate) struct ImportAssets { | ||
42 | import_candidate: ImportCandidate, | ||
43 | module_with_name_to_import: hir::Module, | ||
44 | syntax_under_caret: SyntaxNode, | ||
45 | } | ||
46 | |||
47 | impl ImportAssets { | ||
48 | pub(crate) fn for_method_call( | ||
49 | method_call: ast::MethodCallExpr, | ||
50 | sema: &Semantics<RootDatabase>, | ||
51 | ) -> Option<Self> { | ||
52 | let syntax_under_caret = method_call.syntax().to_owned(); | ||
53 | let module_with_name_to_import = sema.scope(&syntax_under_caret).module()?; | ||
54 | Some(Self { | ||
55 | import_candidate: ImportCandidate::for_method_call(sema, &method_call)?, | ||
56 | module_with_name_to_import, | ||
57 | syntax_under_caret, | ||
58 | }) | ||
59 | } | ||
60 | |||
61 | pub(crate) fn for_regular_path( | ||
62 | path_under_caret: ast::Path, | ||
63 | sema: &Semantics<RootDatabase>, | ||
64 | ) -> Option<Self> { | ||
65 | let syntax_under_caret = path_under_caret.syntax().to_owned(); | ||
66 | if syntax_under_caret.ancestors().find_map(ast::Use::cast).is_some() { | ||
67 | return None; | ||
68 | } | ||
69 | |||
70 | let module_with_name_to_import = sema.scope(&syntax_under_caret).module()?; | ||
71 | Some(Self { | ||
72 | import_candidate: ImportCandidate::for_regular_path(sema, &path_under_caret)?, | ||
73 | module_with_name_to_import, | ||
74 | syntax_under_caret, | ||
75 | }) | ||
76 | } | ||
77 | |||
78 | pub(crate) fn syntax_under_caret(&self) -> &SyntaxNode { | ||
79 | &self.syntax_under_caret | ||
80 | } | ||
81 | |||
82 | pub(crate) fn import_candidate(&self) -> &ImportCandidate { | ||
83 | &self.import_candidate | ||
84 | } | ||
85 | |||
86 | fn get_search_query(&self) -> &str { | ||
87 | match &self.import_candidate { | ||
88 | ImportCandidate::UnqualifiedName(candidate) | ||
89 | | ImportCandidate::QualifierStart(candidate) => &candidate.name, | ||
90 | ImportCandidate::TraitAssocItem(candidate) | ||
91 | | ImportCandidate::TraitMethod(candidate) => &candidate.name, | ||
92 | } | ||
93 | } | ||
94 | |||
95 | pub(crate) fn search_for_imports( | ||
96 | &self, | ||
97 | sema: &Semantics<RootDatabase>, | ||
98 | config: &InsertUseConfig, | ||
99 | ) -> BTreeSet<hir::ModPath> { | ||
100 | let _p = profile::span("import_assists::search_for_imports"); | ||
101 | self.search_for(sema, Some(config.prefix_kind)) | ||
102 | } | ||
103 | |||
104 | /// This may return non-absolute paths if a part of the returned path is already imported into scope. | ||
105 | #[allow(dead_code)] | ||
106 | pub(crate) fn search_for_relative_paths( | ||
107 | &self, | ||
108 | sema: &Semantics<RootDatabase>, | ||
109 | ) -> BTreeSet<hir::ModPath> { | ||
110 | let _p = profile::span("import_assists::search_for_relative_paths"); | ||
111 | self.search_for(sema, None) | ||
112 | } | ||
113 | |||
114 | fn search_for( | ||
115 | &self, | ||
116 | sema: &Semantics<RootDatabase>, | ||
117 | prefixed: Option<hir::PrefixKind>, | ||
118 | ) -> BTreeSet<hir::ModPath> { | ||
119 | let db = sema.db; | ||
120 | let mut trait_candidates = FxHashSet::default(); | ||
121 | let current_crate = self.module_with_name_to_import.krate(); | ||
122 | |||
123 | let filter = |candidate: Either<hir::ModuleDef, hir::MacroDef>| { | ||
124 | trait_candidates.clear(); | ||
125 | match &self.import_candidate { | ||
126 | ImportCandidate::TraitAssocItem(trait_candidate) => { | ||
127 | let located_assoc_item = match candidate { | ||
128 | Either::Left(ModuleDef::Function(located_function)) => { | ||
129 | located_function.as_assoc_item(db) | ||
130 | } | ||
131 | Either::Left(ModuleDef::Const(located_const)) => { | ||
132 | located_const.as_assoc_item(db) | ||
133 | } | ||
134 | _ => None, | ||
135 | } | ||
136 | .map(|assoc| assoc.container(db)) | ||
137 | .and_then(Self::assoc_to_trait)?; | ||
138 | |||
139 | trait_candidates.insert(located_assoc_item.into()); | ||
140 | |||
141 | trait_candidate | ||
142 | .ty | ||
143 | .iterate_path_candidates( | ||
144 | db, | ||
145 | current_crate, | ||
146 | &trait_candidates, | ||
147 | None, | ||
148 | |_, assoc| Self::assoc_to_trait(assoc.container(db)), | ||
149 | ) | ||
150 | .map(ModuleDef::from) | ||
151 | .map(Either::Left) | ||
152 | } | ||
153 | ImportCandidate::TraitMethod(trait_candidate) => { | ||
154 | let located_assoc_item = | ||
155 | if let Either::Left(ModuleDef::Function(located_function)) = candidate { | ||
156 | located_function | ||
157 | .as_assoc_item(db) | ||
158 | .map(|assoc| assoc.container(db)) | ||
159 | .and_then(Self::assoc_to_trait) | ||
160 | } else { | ||
161 | None | ||
162 | }?; | ||
163 | |||
164 | trait_candidates.insert(located_assoc_item.into()); | ||
165 | |||
166 | trait_candidate | ||
167 | .ty | ||
168 | .iterate_method_candidates( | ||
169 | db, | ||
170 | current_crate, | ||
171 | &trait_candidates, | ||
172 | None, | ||
173 | |_, function| { | ||
174 | Self::assoc_to_trait(function.as_assoc_item(db)?.container(db)) | ||
175 | }, | ||
176 | ) | ||
177 | .map(ModuleDef::from) | ||
178 | .map(Either::Left) | ||
179 | } | ||
180 | _ => Some(candidate), | ||
181 | } | ||
182 | }; | ||
183 | |||
184 | imports_locator::find_imports(sema, current_crate, &self.get_search_query()) | ||
185 | .into_iter() | ||
186 | .filter_map(filter) | ||
187 | .filter_map(|candidate| { | ||
188 | let item: hir::ItemInNs = candidate.either(Into::into, Into::into); | ||
189 | if let Some(prefix_kind) = prefixed { | ||
190 | self.module_with_name_to_import.find_use_path_prefixed(db, item, prefix_kind) | ||
191 | } else { | ||
192 | self.module_with_name_to_import.find_use_path(db, item) | ||
193 | } | ||
194 | }) | ||
195 | .filter(|use_path| !use_path.segments.is_empty()) | ||
196 | .take(20) | ||
197 | .collect::<BTreeSet<_>>() | ||
198 | } | ||
199 | |||
200 | fn assoc_to_trait(assoc: AssocItemContainer) -> Option<hir::Trait> { | ||
201 | if let AssocItemContainer::Trait(extracted_trait) = assoc { | ||
202 | Some(extracted_trait) | ||
203 | } else { | ||
204 | None | ||
205 | } | ||
206 | } | ||
207 | } | ||
208 | |||
209 | impl ImportCandidate { | ||
210 | fn for_method_call( | ||
211 | sema: &Semantics<RootDatabase>, | ||
212 | method_call: &ast::MethodCallExpr, | ||
213 | ) -> Option<Self> { | ||
214 | match sema.resolve_method_call(method_call) { | ||
215 | Some(_) => None, | ||
216 | None => Some(Self::TraitMethod(TraitImportCandidate { | ||
217 | ty: sema.type_of_expr(&method_call.receiver()?)?, | ||
218 | name: method_call.name_ref()?.syntax().to_string(), | ||
219 | })), | ||
220 | } | ||
221 | } | ||
222 | |||
223 | fn for_regular_path( | ||
224 | sema: &Semantics<RootDatabase>, | ||
225 | path_under_caret: &ast::Path, | ||
226 | ) -> Option<Self> { | ||
227 | if sema.resolve_path(path_under_caret).is_some() { | ||
228 | return None; | ||
229 | } | ||
230 | |||
231 | let segment = path_under_caret.segment()?; | ||
232 | let candidate = if let Some(qualifier) = path_under_caret.qualifier() { | ||
233 | let qualifier_start = qualifier.syntax().descendants().find_map(ast::NameRef::cast)?; | ||
234 | let qualifier_start_path = | ||
235 | qualifier_start.syntax().ancestors().find_map(ast::Path::cast)?; | ||
236 | if let Some(qualifier_start_resolution) = sema.resolve_path(&qualifier_start_path) { | ||
237 | let qualifier_resolution = if qualifier_start_path == qualifier { | ||
238 | qualifier_start_resolution | ||
239 | } else { | ||
240 | sema.resolve_path(&qualifier)? | ||
241 | }; | ||
242 | match qualifier_resolution { | ||
243 | hir::PathResolution::Def(hir::ModuleDef::Adt(assoc_item_path)) => { | ||
244 | ImportCandidate::TraitAssocItem(TraitImportCandidate { | ||
245 | ty: assoc_item_path.ty(sema.db), | ||
246 | name: segment.syntax().to_string(), | ||
247 | }) | ||
248 | } | ||
249 | _ => return None, | ||
250 | } | ||
251 | } else { | ||
252 | ImportCandidate::QualifierStart(PathImportCandidate { | ||
253 | name: qualifier_start.syntax().to_string(), | ||
254 | }) | ||
255 | } | ||
256 | } else { | ||
257 | ImportCandidate::UnqualifiedName(PathImportCandidate { | ||
258 | name: segment | ||
259 | .syntax() | ||
260 | .descendants() | ||
261 | .find_map(ast::NameRef::cast)? | ||
262 | .syntax() | ||
263 | .to_string(), | ||
264 | }) | ||
265 | }; | ||
266 | Some(candidate) | ||
267 | } | ||
268 | } | ||
diff --git a/crates/assists/src/utils/insert_use.rs b/crates/assists/src/utils/insert_use.rs index bfd457d18..409985b3b 100644 --- a/crates/assists/src/utils/insert_use.rs +++ b/crates/assists/src/utils/insert_use.rs | |||
@@ -173,8 +173,15 @@ pub(crate) fn try_merge_trees( | |||
173 | let rhs_path = rhs.path()?; | 173 | let rhs_path = rhs.path()?; |
174 | 174 | ||
175 | let (lhs_prefix, rhs_prefix) = common_prefix(&lhs_path, &rhs_path)?; | 175 | let (lhs_prefix, rhs_prefix) = common_prefix(&lhs_path, &rhs_path)?; |
176 | let lhs = lhs.split_prefix(&lhs_prefix); | 176 | let (lhs, rhs) = if is_simple_path(lhs) |
177 | let rhs = rhs.split_prefix(&rhs_prefix); | 177 | && is_simple_path(rhs) |
178 | && lhs_path == lhs_prefix | ||
179 | && rhs_path == rhs_prefix | ||
180 | { | ||
181 | (lhs.clone(), rhs.clone()) | ||
182 | } else { | ||
183 | (lhs.split_prefix(&lhs_prefix), rhs.split_prefix(&rhs_prefix)) | ||
184 | }; | ||
178 | recursive_merge(&lhs, &rhs, merge) | 185 | recursive_merge(&lhs, &rhs, merge) |
179 | } | 186 | } |
180 | 187 | ||
@@ -250,6 +257,10 @@ fn recursive_merge( | |||
250 | use_trees.insert(idx, make::glob_use_tree()); | 257 | use_trees.insert(idx, make::glob_use_tree()); |
251 | continue; | 258 | continue; |
252 | } | 259 | } |
260 | |||
261 | if lhs_t.use_tree_list().is_none() && rhs_t.use_tree_list().is_none() { | ||
262 | continue; | ||
263 | } | ||
253 | } | 264 | } |
254 | let lhs = lhs_t.split_prefix(&lhs_prefix); | 265 | let lhs = lhs_t.split_prefix(&lhs_prefix); |
255 | let rhs = rhs_t.split_prefix(&rhs_prefix); | 266 | let rhs = rhs_t.split_prefix(&rhs_prefix); |
@@ -295,6 +306,10 @@ fn common_prefix(lhs: &ast::Path, rhs: &ast::Path) -> Option<(ast::Path, ast::Pa | |||
295 | } | 306 | } |
296 | } | 307 | } |
297 | 308 | ||
309 | fn is_simple_path(use_tree: &ast::UseTree) -> bool { | ||
310 | use_tree.use_tree_list().is_none() && use_tree.star_token().is_none() | ||
311 | } | ||
312 | |||
298 | fn path_is_self(path: &ast::Path) -> bool { | 313 | fn path_is_self(path: &ast::Path) -> bool { |
299 | path.segment().and_then(|seg| seg.self_token()).is_some() && path.qualifier().is_none() | 314 | path.segment().and_then(|seg| seg.self_token()).is_some() && path.qualifier().is_none() |
300 | } | 315 | } |
@@ -524,6 +539,11 @@ mod tests { | |||
524 | use test_utils::assert_eq_text; | 539 | use test_utils::assert_eq_text; |
525 | 540 | ||
526 | #[test] | 541 | #[test] |
542 | fn insert_existing() { | ||
543 | check_full("std::fs", "use std::fs;", "use std::fs;") | ||
544 | } | ||
545 | |||
546 | #[test] | ||
527 | fn insert_start() { | 547 | fn insert_start() { |
528 | check_none( | 548 | check_none( |
529 | "std::bar::AA", | 549 | "std::bar::AA", |
diff --git a/crates/base_db/Cargo.toml b/crates/base_db/Cargo.toml index f7bfcb0d7..1724d2f85 100644 --- a/crates/base_db/Cargo.toml +++ b/crates/base_db/Cargo.toml | |||
@@ -10,7 +10,7 @@ edition = "2018" | |||
10 | doctest = false | 10 | doctest = false |
11 | 11 | ||
12 | [dependencies] | 12 | [dependencies] |
13 | salsa = "0.15.2" | 13 | salsa = "0.16.0" |
14 | rustc-hash = "1.1.0" | 14 | rustc-hash = "1.1.0" |
15 | 15 | ||
16 | syntax = { path = "../syntax", version = "0.0.0" } | 16 | syntax = { path = "../syntax", version = "0.0.0" } |
diff --git a/crates/ide/Cargo.toml b/crates/ide/Cargo.toml index f0257403d..29dc9a6a8 100644 --- a/crates/ide/Cargo.toml +++ b/crates/ide/Cargo.toml | |||
@@ -16,8 +16,8 @@ itertools = "0.9.0" | |||
16 | log = "0.4.8" | 16 | log = "0.4.8" |
17 | rustc-hash = "1.1.0" | 17 | rustc-hash = "1.1.0" |
18 | oorandom = "11.1.2" | 18 | oorandom = "11.1.2" |
19 | pulldown-cmark-to-cmark = "5.0.0" | 19 | pulldown-cmark-to-cmark = "6.0.0" |
20 | pulldown-cmark = {version = "0.7.2", default-features = false} | 20 | pulldown-cmark = { version = "0.8.0", default-features = false } |
21 | url = "2.1.1" | 21 | url = "2.1.1" |
22 | 22 | ||
23 | stdx = { path = "../stdx", version = "0.0.0" } | 23 | stdx = { path = "../stdx", version = "0.0.0" } |
diff --git a/crates/ide/src/completion.rs b/crates/ide/src/completion.rs index 697f691b0..b0e35b2bd 100644 --- a/crates/ide/src/completion.rs +++ b/crates/ide/src/completion.rs | |||
@@ -61,6 +61,8 @@ pub use crate::completion::{ | |||
61 | // - `expr.refm` -> `&mut expr` | 61 | // - `expr.refm` -> `&mut expr` |
62 | // - `expr.not` -> `!expr` | 62 | // - `expr.not` -> `!expr` |
63 | // - `expr.dbg` -> `dbg!(expr)` | 63 | // - `expr.dbg` -> `dbg!(expr)` |
64 | // - `expr.dbgr` -> `dbg!(&expr)` | ||
65 | // - `expr.call` -> `(expr)` | ||
64 | // | 66 | // |
65 | // There also snippet completions: | 67 | // There also snippet completions: |
66 | // | 68 | // |
diff --git a/crates/ide/src/doc_links.rs b/crates/ide/src/doc_links.rs index 06af36b73..db3f911c8 100644 --- a/crates/ide/src/doc_links.rs +++ b/crates/ide/src/doc_links.rs | |||
@@ -1,9 +1,10 @@ | |||
1 | //! Resolves and rewrites links in markdown documentation. | 1 | //! Resolves and rewrites links in markdown documentation. |
2 | 2 | ||
3 | use std::convert::TryFrom; | ||
3 | use std::iter::once; | 4 | use std::iter::once; |
4 | 5 | ||
5 | use itertools::Itertools; | 6 | use itertools::Itertools; |
6 | use pulldown_cmark::{CowStr, Event, LinkType, Options, Parser, Tag}; | 7 | use pulldown_cmark::{BrokenLink, CowStr, Event, InlineStr, LinkType, Options, Parser, Tag}; |
7 | use pulldown_cmark_to_cmark::{cmark_with_options, Options as CmarkOptions}; | 8 | use pulldown_cmark_to_cmark::{cmark_with_options, Options as CmarkOptions}; |
8 | use url::Url; | 9 | use url::Url; |
9 | 10 | ||
@@ -24,11 +25,13 @@ pub type DocumentationLink = String; | |||
24 | 25 | ||
25 | /// Rewrite documentation links in markdown to point to an online host (e.g. docs.rs) | 26 | /// Rewrite documentation links in markdown to point to an online host (e.g. docs.rs) |
26 | pub fn rewrite_links(db: &RootDatabase, markdown: &str, definition: &Definition) -> String { | 27 | pub fn rewrite_links(db: &RootDatabase, markdown: &str, definition: &Definition) -> String { |
27 | let doc = Parser::new_with_broken_link_callback( | 28 | let mut cb = |link: BrokenLink| { |
28 | markdown, | 29 | Some(( |
29 | Options::empty(), | 30 | /*url*/ link.reference.to_owned().into(), |
30 | Some(&|label, _| Some((/*url*/ label.to_string(), /*title*/ label.to_string()))), | 31 | /*title*/ link.reference.to_owned().into(), |
31 | ); | 32 | )) |
33 | }; | ||
34 | let doc = Parser::new_with_broken_link_callback(markdown, Options::empty(), Some(&mut cb)); | ||
32 | 35 | ||
33 | let doc = map_links(doc, |target, title: &str| { | 36 | let doc = map_links(doc, |target, title: &str| { |
34 | // This check is imperfect, there's some overlap between valid intra-doc links | 37 | // This check is imperfect, there's some overlap between valid intra-doc links |
@@ -66,11 +69,11 @@ pub fn remove_links(markdown: &str) -> String { | |||
66 | let mut opts = Options::empty(); | 69 | let mut opts = Options::empty(); |
67 | opts.insert(Options::ENABLE_FOOTNOTES); | 70 | opts.insert(Options::ENABLE_FOOTNOTES); |
68 | 71 | ||
69 | let doc = Parser::new_with_broken_link_callback( | 72 | let mut cb = |_: BrokenLink| { |
70 | markdown, | 73 | let empty = InlineStr::try_from("").unwrap(); |
71 | opts, | 74 | Some((CowStr::Inlined(empty.clone()), CowStr::Inlined(empty))) |
72 | Some(&|_, _| Some((String::new(), String::new()))), | 75 | }; |
73 | ); | 76 | let doc = Parser::new_with_broken_link_callback(markdown, opts, Some(&mut cb)); |
74 | let doc = doc.filter_map(move |evt| match evt { | 77 | let doc = doc.filter_map(move |evt| match evt { |
75 | Event::Start(Tag::Link(link_type, ref target, ref title)) => { | 78 | Event::Start(Tag::Link(link_type, ref target, ref title)) => { |
76 | if link_type == LinkType::Inline && target.contains("://") { | 79 | if link_type == LinkType::Inline && target.contains("://") { |
diff --git a/crates/project_model/src/sysroot.rs b/crates/project_model/src/sysroot.rs index e529e07b0..b2ff98a15 100644 --- a/crates/project_model/src/sysroot.rs +++ b/crates/project_model/src/sysroot.rs | |||
@@ -49,6 +49,7 @@ impl Sysroot { | |||
49 | } | 49 | } |
50 | 50 | ||
51 | pub fn discover(cargo_toml: &AbsPath) -> Result<Sysroot> { | 51 | pub fn discover(cargo_toml: &AbsPath) -> Result<Sysroot> { |
52 | log::debug!("Discovering sysroot for {}", cargo_toml.display()); | ||
52 | let current_dir = cargo_toml.parent().unwrap(); | 53 | let current_dir = cargo_toml.parent().unwrap(); |
53 | let sysroot_src_dir = discover_sysroot_src_dir(current_dir)?; | 54 | let sysroot_src_dir = discover_sysroot_src_dir(current_dir)?; |
54 | let res = Sysroot::load(&sysroot_src_dir)?; | 55 | let res = Sysroot::load(&sysroot_src_dir)?; |
@@ -115,12 +116,14 @@ fn discover_sysroot_src_dir(current_dir: &AbsPath) -> Result<AbsPathBuf> { | |||
115 | if let Ok(path) = env::var("RUST_SRC_PATH") { | 116 | if let Ok(path) = env::var("RUST_SRC_PATH") { |
116 | let path = AbsPathBuf::try_from(path.as_str()) | 117 | let path = AbsPathBuf::try_from(path.as_str()) |
117 | .map_err(|path| format_err!("RUST_SRC_PATH must be absolute: {}", path.display()))?; | 118 | .map_err(|path| format_err!("RUST_SRC_PATH must be absolute: {}", path.display()))?; |
119 | log::debug!("Discovered sysroot by RUST_SRC_PATH: {}", path.display()); | ||
118 | return Ok(path); | 120 | return Ok(path); |
119 | } | 121 | } |
120 | 122 | ||
121 | let sysroot_path = { | 123 | let sysroot_path = { |
122 | let mut rustc = Command::new(toolchain::rustc()); | 124 | let mut rustc = Command::new(toolchain::rustc()); |
123 | rustc.current_dir(current_dir).args(&["--print", "sysroot"]); | 125 | rustc.current_dir(current_dir).args(&["--print", "sysroot"]); |
126 | log::debug!("Discovering sysroot by {:?}", rustc); | ||
124 | let stdout = utf8_stdout(rustc)?; | 127 | let stdout = utf8_stdout(rustc)?; |
125 | AbsPathBuf::assert(PathBuf::from(stdout)) | 128 | AbsPathBuf::assert(PathBuf::from(stdout)) |
126 | }; | 129 | }; |
@@ -150,6 +153,7 @@ fn get_rust_src(sysroot_path: &AbsPath) -> Option<AbsPathBuf> { | |||
150 | // FIXME: remove `src` when 1.47 comes out | 153 | // FIXME: remove `src` when 1.47 comes out |
151 | // https://github.com/rust-lang/rust/pull/73265 | 154 | // https://github.com/rust-lang/rust/pull/73265 |
152 | let rust_src = sysroot_path.join("lib/rustlib/src/rust"); | 155 | let rust_src = sysroot_path.join("lib/rustlib/src/rust"); |
156 | log::debug!("Checking sysroot (looking for `library` and `src` dirs): {}", rust_src.display()); | ||
153 | ["library", "src"].iter().map(|it| rust_src.join(it)).find(|it| it.exists()) | 157 | ["library", "src"].iter().map(|it| rust_src.join(it)).find(|it| it.exists()) |
154 | } | 158 | } |
155 | 159 | ||
diff --git a/docs/dev/README.md b/docs/dev/README.md index 36edddc70..90e74f226 100644 --- a/docs/dev/README.md +++ b/docs/dev/README.md | |||
@@ -35,7 +35,12 @@ https://rust-lang.zulipchat.com/#narrow/stream/185405-t-compiler.2Fwg-rls-2.2E0 | |||
35 | * [E-easy](https://github.com/rust-analyzer/rust-analyzer/issues?q=is%3Aopen+is%3Aissue+label%3AE-easy), | 35 | * [E-easy](https://github.com/rust-analyzer/rust-analyzer/issues?q=is%3Aopen+is%3Aissue+label%3AE-easy), |
36 | [E-medium](https://github.com/rust-analyzer/rust-analyzer/issues?q=is%3Aopen+is%3Aissue+label%3AE-medium), | 36 | [E-medium](https://github.com/rust-analyzer/rust-analyzer/issues?q=is%3Aopen+is%3Aissue+label%3AE-medium), |
37 | [E-hard](https://github.com/rust-analyzer/rust-analyzer/issues?q=is%3Aopen+is%3Aissue+label%3AE-hard), | 37 | [E-hard](https://github.com/rust-analyzer/rust-analyzer/issues?q=is%3Aopen+is%3Aissue+label%3AE-hard), |
38 | labels are *estimates* for how hard would be to write a fix. | 38 | [E-unknown](https://github.com/rust-analyzer/rust-analyzer/issues?q=is%3Aopen+is%3Aissue+label%3AE-unknown), |
39 | labels are *estimates* for how hard would be to write a fix. Each triaged issue should have one of these labels. | ||
40 | * [S-actionable](https://github.com/rust-analyzer/rust-analyzer/issues?q=is%3Aopen+is%3Aissue+label%3AS-actionable) and | ||
41 | [S-unactionable](https://github.com/rust-analyzer/rust-analyzer/issues?q=is%3Aopen+is%3Aissue+label%3AS-unactionable) | ||
42 | specify if there are concrete steps to resolve or advance an issue. Roughly, actionable issues need only work to be fixed, | ||
43 | while unactionable ones are effectively wont-fix. Each triaged issue should have one of these labels. | ||
39 | * [fun](https://github.com/rust-analyzer/rust-analyzer/issues?q=is%3Aopen+is%3Aissue+label%3Afun) | 44 | * [fun](https://github.com/rust-analyzer/rust-analyzer/issues?q=is%3Aopen+is%3Aissue+label%3Afun) |
40 | is for cool, but probably hard stuff. | 45 | is for cool, but probably hard stuff. |
41 | 46 | ||
diff --git a/docs/dev/lsp-extensions.md b/docs/dev/lsp-extensions.md index 3f861f3e0..43a69d6ce 100644 --- a/docs/dev/lsp-extensions.md +++ b/docs/dev/lsp-extensions.md | |||
@@ -1,3 +1,13 @@ | |||
1 | <!--- | ||
2 | lsp_ext.rs hash: 286f8bbac885531a | ||
3 | |||
4 | If you need to change the above hash to make the test pass, please check if you | ||
5 | need to adjust this doc as well and ping this issue: | ||
6 | |||
7 | https://github.com/rust-analyzer/rust-analyzer/issues/4604 | ||
8 | |||
9 | ---> | ||
10 | |||
1 | # LSP Extensions | 11 | # LSP Extensions |
2 | 12 | ||
3 | This document describes LSP extensions used by rust-analyzer. | 13 | This document describes LSP extensions used by rust-analyzer. |
diff --git a/docs/dev/style.md b/docs/dev/style.md index 59067d234..435de63e3 100644 --- a/docs/dev/style.md +++ b/docs/dev/style.md | |||
@@ -186,6 +186,31 @@ impl Person { | |||
186 | } | 186 | } |
187 | ``` | 187 | ``` |
188 | 188 | ||
189 | ## Constructors | ||
190 | |||
191 | Prefer `Default` to zero-argument `new` function | ||
192 | |||
193 | ```rust | ||
194 | // Good | ||
195 | #[derive(Default)] | ||
196 | struct Foo { | ||
197 | bar: Option<Bar> | ||
198 | } | ||
199 | |||
200 | // Not as good | ||
201 | struct Foo { | ||
202 | bar: Option<Bar> | ||
203 | } | ||
204 | |||
205 | impl Foo { | ||
206 | fn new() -> Foo { | ||
207 | Foo { bar: None } | ||
208 | } | ||
209 | } | ||
210 | ``` | ||
211 | |||
212 | Prefer `Default` even it has to be implemented manually. | ||
213 | |||
189 | ## Avoid Monomorphization | 214 | ## Avoid Monomorphization |
190 | 215 | ||
191 | Rust uses monomorphization to compile generic code, meaning that for each instantiation of a generic functions with concrete types, the function is compiled afresh, *per crate*. | 216 | Rust uses monomorphization to compile generic code, meaning that for each instantiation of a generic functions with concrete types, the function is compiled afresh, *per crate*. |
diff --git a/xtask/tests/tidy.rs b/xtask/tests/tidy.rs index 0c233f87d..b3bb9d543 100644 --- a/xtask/tests/tidy.rs +++ b/xtask/tests/tidy.rs | |||
@@ -45,6 +45,41 @@ fn smoke_test_docs_generation() { | |||
45 | } | 45 | } |
46 | 46 | ||
47 | #[test] | 47 | #[test] |
48 | fn check_lsp_extensions_docs() { | ||
49 | let expected_hash = { | ||
50 | let lsp_ext_rs = | ||
51 | fs2::read_to_string(project_root().join("crates/rust-analyzer/src/lsp_ext.rs")) | ||
52 | .unwrap(); | ||
53 | stable_hash(lsp_ext_rs.as_str()) | ||
54 | }; | ||
55 | |||
56 | let actual_hash = { | ||
57 | let lsp_extensions_md = | ||
58 | fs2::read_to_string(project_root().join("docs/dev/lsp-extensions.md")).unwrap(); | ||
59 | let text = lsp_extensions_md | ||
60 | .lines() | ||
61 | .find_map(|line| line.strip_prefix("lsp_ext.rs hash:")) | ||
62 | .unwrap() | ||
63 | .trim(); | ||
64 | u64::from_str_radix(text, 16).unwrap() | ||
65 | }; | ||
66 | |||
67 | if actual_hash != expected_hash { | ||
68 | panic!( | ||
69 | " | ||
70 | lsp_ext.rs was changed without touching lsp-extensions.md. | ||
71 | |||
72 | Expected hash: {:x} | ||
73 | Actual hash: {:x} | ||
74 | |||
75 | Please adjust docs/dev/lsp-extensions.md. | ||
76 | ", | ||
77 | expected_hash, actual_hash | ||
78 | ) | ||
79 | } | ||
80 | } | ||
81 | |||
82 | #[test] | ||
48 | fn rust_files_are_tidy() { | 83 | fn rust_files_are_tidy() { |
49 | let mut tidy_docs = TidyDocs::default(); | 84 | let mut tidy_docs = TidyDocs::default(); |
50 | for path in rust_files(&project_root().join("crates")) { | 85 | for path in rust_files(&project_root().join("crates")) { |
@@ -280,3 +315,13 @@ fn is_exclude_dir(p: &Path, dirs_to_exclude: &[&str]) -> bool { | |||
280 | .filter_map(|it| it.as_os_str().to_str()) | 315 | .filter_map(|it| it.as_os_str().to_str()) |
281 | .any(|it| dirs_to_exclude.contains(&it)) | 316 | .any(|it| dirs_to_exclude.contains(&it)) |
282 | } | 317 | } |
318 | |||
319 | #[allow(deprecated)] | ||
320 | fn stable_hash(text: &str) -> u64 { | ||
321 | use std::hash::{Hash, Hasher, SipHasher}; | ||
322 | |||
323 | let text = text.replace('\r', ""); | ||
324 | let mut hasher = SipHasher::default(); | ||
325 | text.hash(&mut hasher); | ||
326 | hasher.finish() | ||
327 | } | ||