Skip to content

Commit 5cd4eb6

Browse files
committed
Add preliminary implementation of extract struct from enum variant
1 parent d4daca9 commit 5cd4eb6

File tree

5 files changed

+403
-1
lines changed

5 files changed

+403
-1
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ra_assists/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ ra_fmt = { path = "../ra_fmt" }
2020
ra_prof = { path = "../ra_prof" }
2121
ra_db = { path = "../ra_db" }
2222
ra_ide_db = { path = "../ra_ide_db" }
23+
hir_expand = { path = "../ra_hir_expand", package = "ra_hir_expand" }
2324
hir = { path = "../ra_hir", package = "ra_hir" }
2425
test_utils = { path = "../test_utils" }

crates/ra_assists/src/assist_context.rs

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use algo::find_covering_element;
44
use hir::Semantics;
5-
use ra_db::{FileId, FileRange};
5+
use ra_db::{FileId, FileRange, FilePosition};
66
use ra_fmt::{leading_indent, reindent};
77
use ra_ide_db::{
88
source_change::{SourceChange, SourceFileEdit},
@@ -19,6 +19,7 @@ use crate::{
1919
assist_config::{AssistConfig, SnippetCap},
2020
Assist, AssistId, GroupLabel, ResolvedAssist,
2121
};
22+
use rustc_hash::FxHashMap;
2223

2324
/// `AssistContext` allows to apply an assist or check if it could be applied.
2425
///
@@ -138,6 +139,16 @@ impl Assists {
138139
let label = Assist::new(id, label.into(), None, target);
139140
self.add_impl(label, f)
140141
}
142+
pub(crate) fn add_in_multiple_files(
143+
&mut self,
144+
id: AssistId,
145+
label: impl Into<String>,
146+
target: TextRange,
147+
f: impl FnOnce(&mut AssistDirector),
148+
) -> Option<()> {
149+
let label = Assist::new(id, label.into(), None, target);
150+
self.add_impl_multiple_files(label, f)
151+
}
141152
pub(crate) fn add_group(
142153
&mut self,
143154
group: &GroupLabel,
@@ -162,6 +173,27 @@ impl Assists {
162173
Some(())
163174
}
164175

176+
fn add_impl_multiple_files(&mut self, label: Assist, f: impl FnOnce(&mut AssistDirector)) -> Option<()> {
177+
let change_label = label.label.clone();
178+
if !self.resolve {
179+
return None
180+
}
181+
let mut director = AssistDirector::new(change_label.clone());
182+
f(&mut director);
183+
let changes = director.finish();
184+
let file_edits: Vec<SourceFileEdit> = changes.into_iter()
185+
.map(|mut change| change.source_file_edits.pop().unwrap()).collect();
186+
187+
let source_change = SourceChange {
188+
source_file_edits: file_edits,
189+
file_system_edits: vec![],
190+
is_snippet: false,
191+
};
192+
193+
self.buf.push((label, Some(source_change)));
194+
Some(())
195+
}
196+
165197
fn finish(mut self) -> Vec<(Assist, Option<SourceChange>)> {
166198
self.buf.sort_by_key(|(label, _edit)| label.target.len());
167199
self.buf
@@ -255,3 +287,31 @@ impl AssistBuilder {
255287
res
256288
}
257289
}
290+
291+
pub(crate) struct AssistDirector {
292+
source_changes: Vec<SourceChange>,
293+
builders: FxHashMap<FileId, AssistBuilder>,
294+
change_label: String
295+
}
296+
297+
impl AssistDirector {
298+
fn new(change_label: String) -> AssistDirector {
299+
AssistDirector {
300+
source_changes: vec![],
301+
builders: FxHashMap::default(),
302+
change_label
303+
}
304+
}
305+
306+
pub(crate) fn perform(&mut self, file_id: FileId, f: impl FnOnce(&mut AssistBuilder)) {
307+
let mut builder = self.builders.entry(file_id).or_insert(AssistBuilder::new(file_id));
308+
f(&mut builder);
309+
}
310+
311+
fn finish(mut self) -> Vec<SourceChange> {
312+
for (file_id, builder) in self.builders.into_iter().collect::<Vec<(FileId, AssistBuilder)>>() {
313+
self.source_changes.push(builder.finish());
314+
}
315+
self.source_changes
316+
}
317+
}

0 commit comments

Comments
 (0)