diff options
author | Aleksey Kladov <[email protected]> | 2021-05-22 14:23:07 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2021-05-22 14:23:07 +0100 |
commit | e6776c3e1b66c8946873d20e1e3bc1d743c952fe (patch) | |
tree | d966d440b955cf3aa0258484170048790283b309 /crates/ide_assists/src/ast_transform.rs | |
parent | 47d7434dde215460fc95916f2703c6925f58dcce (diff) |
use more precise name
Diffstat (limited to 'crates/ide_assists/src/ast_transform.rs')
-rw-r--r-- | crates/ide_assists/src/ast_transform.rs | 164 |
1 files changed, 0 insertions, 164 deletions
diff --git a/crates/ide_assists/src/ast_transform.rs b/crates/ide_assists/src/ast_transform.rs deleted file mode 100644 index 5d9cc5551..000000000 --- a/crates/ide_assists/src/ast_transform.rs +++ /dev/null | |||
@@ -1,164 +0,0 @@ | |||
1 | //! `AstTransformer`s are functions that replace nodes in an AST and can be easily combined. | ||
2 | use hir::{HirDisplay, SemanticsScope}; | ||
3 | use ide_db::helpers::mod_path_to_ast; | ||
4 | use rustc_hash::FxHashMap; | ||
5 | use syntax::{ | ||
6 | ast::{self, AstNode}, | ||
7 | ted, | ||
8 | }; | ||
9 | |||
10 | /// `AstTransform` helps with applying bulk transformations to syntax nodes. | ||
11 | /// | ||
12 | /// This is mostly useful for IDE code generation. If you paste some existing | ||
13 | /// code into a new context (for example, to add method overrides to an `impl` | ||
14 | /// block), you generally want to appropriately qualify the names, and sometimes | ||
15 | /// you might want to substitute generic parameters as well: | ||
16 | /// | ||
17 | /// ``` | ||
18 | /// mod x { | ||
19 | /// pub struct A<V>; | ||
20 | /// pub trait T<U> { fn foo(&self, _: U) -> A<U>; } | ||
21 | /// } | ||
22 | /// | ||
23 | /// mod y { | ||
24 | /// use x::T; | ||
25 | /// | ||
26 | /// impl T<()> for () { | ||
27 | /// // If we invoke **Add Missing Members** here, we want to copy-paste `foo`. | ||
28 | /// // But we want a slightly-modified version of it: | ||
29 | /// fn foo(&self, _: ()) -> x::A<()> {} | ||
30 | /// } | ||
31 | /// } | ||
32 | /// ``` | ||
33 | /// | ||
34 | /// So, a single `AstTransform` describes such function from `SyntaxNode` to | ||
35 | /// `SyntaxNode`. Note that the API here is a bit too high-order and high-brow. | ||
36 | /// We'd want to somehow express this concept simpler, but so far nobody got to | ||
37 | /// simplifying this! | ||
38 | pub(crate) struct AstTransform<'a> { | ||
39 | pub(crate) subst: (hir::Trait, ast::Impl), | ||
40 | pub(crate) target_scope: &'a SemanticsScope<'a>, | ||
41 | pub(crate) source_scope: &'a SemanticsScope<'a>, | ||
42 | } | ||
43 | |||
44 | impl<'a> AstTransform<'a> { | ||
45 | pub(crate) fn apply(&self, item: ast::AssocItem) { | ||
46 | if let Some(ctx) = self.build_ctx() { | ||
47 | ctx.apply(item) | ||
48 | } | ||
49 | } | ||
50 | fn build_ctx(&self) -> Option<Ctx<'a>> { | ||
51 | let db = self.source_scope.db; | ||
52 | let target_module = self.target_scope.module()?; | ||
53 | let source_module = self.source_scope.module()?; | ||
54 | |||
55 | let substs = get_syntactic_substs(self.subst.1.clone()).unwrap_or_default(); | ||
56 | let generic_def: hir::GenericDef = self.subst.0.into(); | ||
57 | let substs_by_param: FxHashMap<_, _> = generic_def | ||
58 | .type_params(db) | ||
59 | .into_iter() | ||
60 | // this is a trait impl, so we need to skip the first type parameter -- this is a bit hacky | ||
61 | .skip(1) | ||
62 | // The actual list of trait type parameters may be longer than the one | ||
63 | // used in the `impl` block due to trailing default type parameters. | ||
64 | // For that case we extend the `substs` with an empty iterator so we | ||
65 | // can still hit those trailing values and check if they actually have | ||
66 | // a default type. If they do, go for that type from `hir` to `ast` so | ||
67 | // the resulting change can be applied correctly. | ||
68 | .zip(substs.into_iter().map(Some).chain(std::iter::repeat(None))) | ||
69 | .filter_map(|(k, v)| match v { | ||
70 | Some(v) => Some((k, v)), | ||
71 | None => { | ||
72 | let default = k.default(db)?; | ||
73 | Some(( | ||
74 | k, | ||
75 | ast::make::ty(&default.display_source_code(db, source_module.into()).ok()?), | ||
76 | )) | ||
77 | } | ||
78 | }) | ||
79 | .collect(); | ||
80 | |||
81 | let res = Ctx { substs: substs_by_param, target_module, source_scope: self.source_scope }; | ||
82 | Some(res) | ||
83 | } | ||
84 | } | ||
85 | |||
86 | struct Ctx<'a> { | ||
87 | substs: FxHashMap<hir::TypeParam, ast::Type>, | ||
88 | target_module: hir::Module, | ||
89 | source_scope: &'a SemanticsScope<'a>, | ||
90 | } | ||
91 | |||
92 | impl<'a> Ctx<'a> { | ||
93 | fn apply(&self, item: ast::AssocItem) { | ||
94 | for event in item.syntax().preorder() { | ||
95 | let node = match event { | ||
96 | syntax::WalkEvent::Enter(_) => continue, | ||
97 | syntax::WalkEvent::Leave(it) => it, | ||
98 | }; | ||
99 | if let Some(path) = ast::Path::cast(node.clone()) { | ||
100 | self.transform_path(path); | ||
101 | } | ||
102 | } | ||
103 | } | ||
104 | fn transform_path(&self, path: ast::Path) -> Option<()> { | ||
105 | if path.qualifier().is_some() { | ||
106 | return None; | ||
107 | } | ||
108 | if path.segment().and_then(|s| s.param_list()).is_some() { | ||
109 | // don't try to qualify `Fn(Foo) -> Bar` paths, they are in prelude anyway | ||
110 | return None; | ||
111 | } | ||
112 | |||
113 | let resolution = self.source_scope.speculative_resolve(&path)?; | ||
114 | |||
115 | match resolution { | ||
116 | hir::PathResolution::TypeParam(tp) => { | ||
117 | if let Some(subst) = self.substs.get(&tp) { | ||
118 | ted::replace(path.syntax(), subst.clone_subtree().clone_for_update().syntax()) | ||
119 | } | ||
120 | } | ||
121 | hir::PathResolution::Def(def) => { | ||
122 | let found_path = | ||
123 | self.target_module.find_use_path(self.source_scope.db.upcast(), def)?; | ||
124 | let res = mod_path_to_ast(&found_path).clone_for_update(); | ||
125 | if let Some(args) = path.segment().and_then(|it| it.generic_arg_list()) { | ||
126 | if let Some(segment) = res.segment() { | ||
127 | let old = segment.get_or_create_generic_arg_list(); | ||
128 | ted::replace(old.syntax(), args.clone_subtree().syntax().clone_for_update()) | ||
129 | } | ||
130 | } | ||
131 | ted::replace(path.syntax(), res.syntax()) | ||
132 | } | ||
133 | hir::PathResolution::Local(_) | ||
134 | | hir::PathResolution::ConstParam(_) | ||
135 | | hir::PathResolution::SelfType(_) | ||
136 | | hir::PathResolution::Macro(_) | ||
137 | | hir::PathResolution::AssocItem(_) => (), | ||
138 | } | ||
139 | Some(()) | ||
140 | } | ||
141 | } | ||
142 | |||
143 | // FIXME: It would probably be nicer if we could get this via HIR (i.e. get the | ||
144 | // trait ref, and then go from the types in the substs back to the syntax). | ||
145 | fn get_syntactic_substs(impl_def: ast::Impl) -> Option<Vec<ast::Type>> { | ||
146 | let target_trait = impl_def.trait_()?; | ||
147 | let path_type = match target_trait { | ||
148 | ast::Type::PathType(path) => path, | ||
149 | _ => return None, | ||
150 | }; | ||
151 | let generic_arg_list = path_type.path()?.segment()?.generic_arg_list()?; | ||
152 | |||
153 | let mut result = Vec::new(); | ||
154 | for generic_arg in generic_arg_list.generic_args() { | ||
155 | match generic_arg { | ||
156 | ast::GenericArg::TypeArg(type_arg) => result.push(type_arg.ty()?), | ||
157 | ast::GenericArg::AssocTypeArg(_) | ||
158 | | ast::GenericArg::LifetimeArg(_) | ||
159 | | ast::GenericArg::ConstArg(_) => (), | ||
160 | } | ||
161 | } | ||
162 | |||
163 | Some(result) | ||
164 | } | ||