diff options
Diffstat (limited to 'crates/ra_assists/src/utils.rs')
-rw-r--r-- | crates/ra_assists/src/utils.rs | 275 |
1 files changed, 0 insertions, 275 deletions
diff --git a/crates/ra_assists/src/utils.rs b/crates/ra_assists/src/utils.rs deleted file mode 100644 index 0de6fdf3f..000000000 --- a/crates/ra_assists/src/utils.rs +++ /dev/null | |||
@@ -1,275 +0,0 @@ | |||
1 | //! Assorted functions shared by several assists. | ||
2 | pub(crate) mod insert_use; | ||
3 | |||
4 | use std::{iter, ops}; | ||
5 | |||
6 | use hir::{Adt, Crate, Enum, ScopeDef, Semantics, Trait, Type}; | ||
7 | use ra_ide_db::RootDatabase; | ||
8 | use ra_syntax::{ | ||
9 | ast::{self, make, NameOwner}, | ||
10 | AstNode, | ||
11 | SyntaxKind::*, | ||
12 | SyntaxNode, TextSize, T, | ||
13 | }; | ||
14 | use rustc_hash::FxHashSet; | ||
15 | |||
16 | use crate::assist_config::SnippetCap; | ||
17 | |||
18 | pub(crate) use insert_use::{find_insert_use_container, insert_use_statement}; | ||
19 | |||
20 | #[derive(Clone, Copy, Debug)] | ||
21 | pub(crate) enum Cursor<'a> { | ||
22 | Replace(&'a SyntaxNode), | ||
23 | Before(&'a SyntaxNode), | ||
24 | } | ||
25 | |||
26 | impl<'a> Cursor<'a> { | ||
27 | fn node(self) -> &'a SyntaxNode { | ||
28 | match self { | ||
29 | Cursor::Replace(node) | Cursor::Before(node) => node, | ||
30 | } | ||
31 | } | ||
32 | } | ||
33 | |||
34 | pub(crate) fn render_snippet(_cap: SnippetCap, node: &SyntaxNode, cursor: Cursor) -> String { | ||
35 | assert!(cursor.node().ancestors().any(|it| it == *node)); | ||
36 | let range = cursor.node().text_range() - node.text_range().start(); | ||
37 | let range: ops::Range<usize> = range.into(); | ||
38 | |||
39 | let mut placeholder = cursor.node().to_string(); | ||
40 | escape(&mut placeholder); | ||
41 | let tab_stop = match cursor { | ||
42 | Cursor::Replace(placeholder) => format!("${{0:{}}}", placeholder), | ||
43 | Cursor::Before(placeholder) => format!("$0{}", placeholder), | ||
44 | }; | ||
45 | |||
46 | let mut buf = node.to_string(); | ||
47 | buf.replace_range(range, &tab_stop); | ||
48 | return buf; | ||
49 | |||
50 | fn escape(buf: &mut String) { | ||
51 | stdx::replace(buf, '{', r"\{"); | ||
52 | stdx::replace(buf, '}', r"\}"); | ||
53 | stdx::replace(buf, '$', r"\$"); | ||
54 | } | ||
55 | } | ||
56 | |||
57 | pub fn get_missing_assoc_items( | ||
58 | sema: &Semantics<RootDatabase>, | ||
59 | impl_def: &ast::Impl, | ||
60 | ) -> Vec<hir::AssocItem> { | ||
61 | // Names must be unique between constants and functions. However, type aliases | ||
62 | // may share the same name as a function or constant. | ||
63 | let mut impl_fns_consts = FxHashSet::default(); | ||
64 | let mut impl_type = FxHashSet::default(); | ||
65 | |||
66 | if let Some(item_list) = impl_def.assoc_item_list() { | ||
67 | for item in item_list.assoc_items() { | ||
68 | match item { | ||
69 | ast::AssocItem::Fn(f) => { | ||
70 | if let Some(n) = f.name() { | ||
71 | impl_fns_consts.insert(n.syntax().to_string()); | ||
72 | } | ||
73 | } | ||
74 | |||
75 | ast::AssocItem::TypeAlias(t) => { | ||
76 | if let Some(n) = t.name() { | ||
77 | impl_type.insert(n.syntax().to_string()); | ||
78 | } | ||
79 | } | ||
80 | |||
81 | ast::AssocItem::Const(c) => { | ||
82 | if let Some(n) = c.name() { | ||
83 | impl_fns_consts.insert(n.syntax().to_string()); | ||
84 | } | ||
85 | } | ||
86 | ast::AssocItem::MacroCall(_) => (), | ||
87 | } | ||
88 | } | ||
89 | } | ||
90 | |||
91 | resolve_target_trait(sema, impl_def).map_or(vec![], |target_trait| { | ||
92 | target_trait | ||
93 | .items(sema.db) | ||
94 | .iter() | ||
95 | .filter(|i| match i { | ||
96 | hir::AssocItem::Function(f) => { | ||
97 | !impl_fns_consts.contains(&f.name(sema.db).to_string()) | ||
98 | } | ||
99 | hir::AssocItem::TypeAlias(t) => !impl_type.contains(&t.name(sema.db).to_string()), | ||
100 | hir::AssocItem::Const(c) => c | ||
101 | .name(sema.db) | ||
102 | .map(|n| !impl_fns_consts.contains(&n.to_string())) | ||
103 | .unwrap_or_default(), | ||
104 | }) | ||
105 | .cloned() | ||
106 | .collect() | ||
107 | }) | ||
108 | } | ||
109 | |||
110 | pub(crate) fn resolve_target_trait( | ||
111 | sema: &Semantics<RootDatabase>, | ||
112 | impl_def: &ast::Impl, | ||
113 | ) -> Option<hir::Trait> { | ||
114 | let ast_path = | ||
115 | impl_def.trait_().map(|it| it.syntax().clone()).and_then(ast::PathType::cast)?.path()?; | ||
116 | |||
117 | match sema.resolve_path(&ast_path) { | ||
118 | Some(hir::PathResolution::Def(hir::ModuleDef::Trait(def))) => Some(def), | ||
119 | _ => None, | ||
120 | } | ||
121 | } | ||
122 | |||
123 | pub(crate) fn vis_offset(node: &SyntaxNode) -> TextSize { | ||
124 | node.children_with_tokens() | ||
125 | .find(|it| !matches!(it.kind(), WHITESPACE | COMMENT | ATTR)) | ||
126 | .map(|it| it.text_range().start()) | ||
127 | .unwrap_or_else(|| node.text_range().start()) | ||
128 | } | ||
129 | |||
130 | pub(crate) fn invert_boolean_expression(expr: ast::Expr) -> ast::Expr { | ||
131 | if let Some(expr) = invert_special_case(&expr) { | ||
132 | return expr; | ||
133 | } | ||
134 | make::expr_prefix(T![!], expr) | ||
135 | } | ||
136 | |||
137 | fn invert_special_case(expr: &ast::Expr) -> Option<ast::Expr> { | ||
138 | match expr { | ||
139 | ast::Expr::BinExpr(bin) => match bin.op_kind()? { | ||
140 | ast::BinOp::NegatedEqualityTest => bin.replace_op(T![==]).map(|it| it.into()), | ||
141 | ast::BinOp::EqualityTest => bin.replace_op(T![!=]).map(|it| it.into()), | ||
142 | _ => None, | ||
143 | }, | ||
144 | ast::Expr::PrefixExpr(pe) if pe.op_kind()? == ast::PrefixOp::Not => pe.expr(), | ||
145 | // FIXME: | ||
146 | // ast::Expr::Literal(true | false ) | ||
147 | _ => None, | ||
148 | } | ||
149 | } | ||
150 | |||
151 | #[derive(Clone, Copy)] | ||
152 | pub enum TryEnum { | ||
153 | Result, | ||
154 | Option, | ||
155 | } | ||
156 | |||
157 | impl TryEnum { | ||
158 | const ALL: [TryEnum; 2] = [TryEnum::Option, TryEnum::Result]; | ||
159 | |||
160 | pub fn from_ty(sema: &Semantics<RootDatabase>, ty: &Type) -> Option<TryEnum> { | ||
161 | let enum_ = match ty.as_adt() { | ||
162 | Some(Adt::Enum(it)) => it, | ||
163 | _ => return None, | ||
164 | }; | ||
165 | TryEnum::ALL.iter().find_map(|&var| { | ||
166 | if &enum_.name(sema.db).to_string() == var.type_name() { | ||
167 | return Some(var); | ||
168 | } | ||
169 | None | ||
170 | }) | ||
171 | } | ||
172 | |||
173 | pub(crate) fn happy_case(self) -> &'static str { | ||
174 | match self { | ||
175 | TryEnum::Result => "Ok", | ||
176 | TryEnum::Option => "Some", | ||
177 | } | ||
178 | } | ||
179 | |||
180 | pub(crate) fn sad_pattern(self) -> ast::Pat { | ||
181 | match self { | ||
182 | TryEnum::Result => make::tuple_struct_pat( | ||
183 | make::path_unqualified(make::path_segment(make::name_ref("Err"))), | ||
184 | iter::once(make::wildcard_pat().into()), | ||
185 | ) | ||
186 | .into(), | ||
187 | TryEnum::Option => make::ident_pat(make::name("None")).into(), | ||
188 | } | ||
189 | } | ||
190 | |||
191 | fn type_name(self) -> &'static str { | ||
192 | match self { | ||
193 | TryEnum::Result => "Result", | ||
194 | TryEnum::Option => "Option", | ||
195 | } | ||
196 | } | ||
197 | } | ||
198 | |||
199 | /// Helps with finding well-know things inside the standard library. This is | ||
200 | /// somewhat similar to the known paths infra inside hir, but it different; We | ||
201 | /// want to make sure that IDE specific paths don't become interesting inside | ||
202 | /// the compiler itself as well. | ||
203 | pub(crate) struct FamousDefs<'a, 'b>(pub(crate) &'a Semantics<'b, RootDatabase>, pub(crate) Crate); | ||
204 | |||
205 | #[allow(non_snake_case)] | ||
206 | impl FamousDefs<'_, '_> { | ||
207 | #[cfg(test)] | ||
208 | pub(crate) const FIXTURE: &'static str = r#"//- /libcore.rs crate:core | ||
209 | pub mod convert { | ||
210 | pub trait From<T> { | ||
211 | fn from(T) -> Self; | ||
212 | } | ||
213 | } | ||
214 | |||
215 | pub mod option { | ||
216 | pub enum Option<T> { None, Some(T)} | ||
217 | } | ||
218 | |||
219 | pub mod prelude { | ||
220 | pub use crate::{convert::From, option::Option::{self, *}}; | ||
221 | } | ||
222 | #[prelude_import] | ||
223 | pub use prelude::*; | ||
224 | "#; | ||
225 | |||
226 | pub(crate) fn core_convert_From(&self) -> Option<Trait> { | ||
227 | self.find_trait("core:convert:From") | ||
228 | } | ||
229 | |||
230 | pub(crate) fn core_option_Option(&self) -> Option<Enum> { | ||
231 | self.find_enum("core:option:Option") | ||
232 | } | ||
233 | |||
234 | fn find_trait(&self, path: &str) -> Option<Trait> { | ||
235 | match self.find_def(path)? { | ||
236 | hir::ScopeDef::ModuleDef(hir::ModuleDef::Trait(it)) => Some(it), | ||
237 | _ => None, | ||
238 | } | ||
239 | } | ||
240 | |||
241 | fn find_enum(&self, path: &str) -> Option<Enum> { | ||
242 | match self.find_def(path)? { | ||
243 | hir::ScopeDef::ModuleDef(hir::ModuleDef::Adt(hir::Adt::Enum(it))) => Some(it), | ||
244 | _ => None, | ||
245 | } | ||
246 | } | ||
247 | |||
248 | fn find_def(&self, path: &str) -> Option<ScopeDef> { | ||
249 | let db = self.0.db; | ||
250 | let mut path = path.split(':'); | ||
251 | let trait_ = path.next_back()?; | ||
252 | let std_crate = path.next()?; | ||
253 | let std_crate = self | ||
254 | .1 | ||
255 | .dependencies(db) | ||
256 | .into_iter() | ||
257 | .find(|dep| &dep.name.to_string() == std_crate)? | ||
258 | .krate; | ||
259 | |||
260 | let mut module = std_crate.root_module(db); | ||
261 | for segment in path { | ||
262 | module = module.children(db).find_map(|child| { | ||
263 | let name = child.name(db)?; | ||
264 | if &name.to_string() == segment { | ||
265 | Some(child) | ||
266 | } else { | ||
267 | None | ||
268 | } | ||
269 | })?; | ||
270 | } | ||
271 | let def = | ||
272 | module.scope(db, None).into_iter().find(|(name, _def)| &name.to_string() == trait_)?.1; | ||
273 | Some(def) | ||
274 | } | ||
275 | } | ||