Skip to content

Commit fdd4df9

Browse files
committed
Use SourceChange for assists
1 parent 1116c9a commit fdd4df9

File tree

6 files changed

+60
-87
lines changed

6 files changed

+60
-87
lines changed

crates/ra_assists/src/assist_ctx.rs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
//! This module defines `AssistCtx` -- the API surface that is exposed to assists.
22
use hir::Semantics;
3-
use ra_db::FileRange;
3+
use ra_db::{FileId, FileRange};
44
use ra_fmt::{leading_indent, reindent};
5-
use ra_ide_db::RootDatabase;
5+
use ra_ide_db::{
6+
source_change::{SingleFileChange, SourceChange},
7+
RootDatabase,
8+
};
69
use ra_syntax::{
710
algo::{self, find_covering_element, find_node_at_offset, SyntaxRewriter},
811
AstNode, SourceFile, SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken, TextRange, TextSize,
912
TokenAtOffset,
1013
};
1114
use ra_text_edit::TextEditBuilder;
1215

13-
use crate::{AssistAction, AssistFile, AssistId, AssistLabel, GroupLabel, ResolvedAssist};
16+
use crate::{AssistFile, AssistId, AssistLabel, GroupLabel, ResolvedAssist};
1417

1518
#[derive(Clone, Debug)]
1619
pub(crate) struct Assist(pub(crate) Vec<AssistInfo>);
@@ -19,15 +22,15 @@ pub(crate) struct Assist(pub(crate) Vec<AssistInfo>);
1922
pub(crate) struct AssistInfo {
2023
pub(crate) label: AssistLabel,
2124
pub(crate) group_label: Option<GroupLabel>,
22-
pub(crate) action: Option<AssistAction>,
25+
pub(crate) action: Option<SourceChange>,
2326
}
2427

2528
impl AssistInfo {
2629
fn new(label: AssistLabel) -> AssistInfo {
2730
AssistInfo { label, group_label: None, action: None }
2831
}
2932

30-
fn resolved(self, action: AssistAction) -> AssistInfo {
33+
fn resolved(self, action: SourceChange) -> AssistInfo {
3134
AssistInfo { action: Some(action), ..self }
3235
}
3336

@@ -98,13 +101,13 @@ impl<'a> AssistCtx<'a> {
98101
f: impl FnOnce(&mut ActionBuilder),
99102
) -> Option<Assist> {
100103
let label = AssistLabel::new(id, label.into(), None, target);
101-
104+
let change_label = label.label.clone();
102105
let mut info = AssistInfo::new(label);
103106
if self.should_compute_edit {
104107
let action = {
105108
let mut edit = ActionBuilder::new(&self);
106109
f(&mut edit);
107-
edit.build()
110+
edit.build(change_label, self.frange.file_id)
108111
};
109112
info = info.resolved(action)
110113
};
@@ -157,13 +160,13 @@ impl<'a> AssistGroup<'a> {
157160
f: impl FnOnce(&mut ActionBuilder),
158161
) {
159162
let label = AssistLabel::new(id, label.into(), Some(self.group.clone()), target);
160-
163+
let change_label = label.label.clone();
161164
let mut info = AssistInfo::new(label).with_group(self.group.clone());
162165
if self.ctx.should_compute_edit {
163166
let action = {
164167
let mut edit = ActionBuilder::new(&self.ctx);
165168
f(&mut edit);
166-
edit.build()
169+
edit.build(change_label, self.ctx.frange.file_id)
167170
};
168171
info = info.resolved(action)
169172
};
@@ -255,11 +258,16 @@ impl<'a, 'b> ActionBuilder<'a, 'b> {
255258
self.file = assist_file
256259
}
257260

258-
fn build(self) -> AssistAction {
261+
fn build(self, change_label: String, current_file: FileId) -> SourceChange {
259262
let edit = self.edit.finish();
260263
if edit.is_empty() && self.cursor_position.is_none() {
261264
panic!("Only call `add_assist` if the assist can be applied")
262265
}
263-
AssistAction { edit, cursor_position: self.cursor_position, file: self.file }
266+
let file = match self.file {
267+
AssistFile::CurrentFile => current_file,
268+
AssistFile::TargetFile(it) => it,
269+
};
270+
SingleFileChange { label: change_label, edit, cursor_position: self.cursor_position }
271+
.into_source_change(file)
264272
}
265273
}

crates/ra_assists/src/lib.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ pub mod ast_transform;
1919

2020
use hir::Semantics;
2121
use ra_db::{FileId, FileRange};
22-
use ra_ide_db::RootDatabase;
23-
use ra_syntax::{TextRange, TextSize};
24-
use ra_text_edit::TextEdit;
22+
use ra_ide_db::{source_change::SourceChange, RootDatabase};
23+
use ra_syntax::TextRange;
2524

2625
pub(crate) use crate::assist_ctx::{Assist, AssistCtx};
2726

@@ -57,21 +56,14 @@ impl AssistLabel {
5756
}
5857
}
5958

60-
#[derive(Debug, Clone)]
61-
pub struct AssistAction {
62-
pub edit: TextEdit,
63-
pub cursor_position: Option<TextSize>,
64-
pub file: AssistFile,
65-
}
66-
6759
#[derive(Debug, Clone)]
6860
pub struct ResolvedAssist {
6961
pub label: AssistLabel,
70-
pub action: AssistAction,
62+
pub action: SourceChange,
7163
}
7264

7365
#[derive(Debug, Clone, Copy)]
74-
pub enum AssistFile {
66+
enum AssistFile {
7567
CurrentFile,
7668
TargetFile(FileId),
7769
}

crates/ra_assists/src/tests.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use test_utils::{
1111
RangeOrOffset,
1212
};
1313

14-
use crate::{handlers::Handler, resolved_assists, AssistCtx, AssistFile};
14+
use crate::{handlers::Handler, resolved_assists, AssistCtx};
1515

1616
pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) {
1717
let (mut db, file_id) = RootDatabase::with_single_file(text);
@@ -41,7 +41,7 @@ fn check_doc_test(assist_id: &str, before: &str, after: &str) {
4141
let (db, file_id) = crate::tests::with_single_file(&before);
4242
let frange = FileRange { file_id, range: selection.into() };
4343

44-
let assist = resolved_assists(&db, frange)
44+
let mut assist = resolved_assists(&db, frange)
4545
.into_iter()
4646
.find(|assist| assist.label.id.0 == assist_id)
4747
.unwrap_or_else(|| {
@@ -57,8 +57,9 @@ fn check_doc_test(assist_id: &str, before: &str, after: &str) {
5757
});
5858

5959
let actual = {
60+
let change = assist.action.source_file_edits.pop().unwrap();
6061
let mut actual = before.clone();
61-
assist.action.edit.apply(&mut actual);
62+
change.edit.apply(&mut actual);
6263
actual
6364
};
6465
assert_eq_text!(after, &actual);
@@ -93,26 +94,23 @@ fn check(assist: Handler, before: &str, expected: ExpectedResult) {
9394

9495
match (assist(assist_ctx), expected) {
9596
(Some(assist), ExpectedResult::After(after)) => {
96-
let action = assist.0[0].action.clone().unwrap();
97+
let mut action = assist.0[0].action.clone().unwrap();
98+
let change = action.source_file_edits.pop().unwrap();
9799

98-
let mut actual = if let AssistFile::TargetFile(file_id) = action.file {
99-
db.file_text(file_id).as_ref().to_owned()
100-
} else {
101-
text_without_caret
102-
};
103-
action.edit.apply(&mut actual);
100+
let mut actual = db.file_text(change.file_id).as_ref().to_owned();
101+
change.edit.apply(&mut actual);
104102

105103
match action.cursor_position {
106104
None => {
107105
if let RangeOrOffset::Offset(before_cursor_pos) = range_or_offset {
108-
let off = action
106+
let off = change
109107
.edit
110108
.apply_to_offset(before_cursor_pos)
111109
.expect("cursor position is affected by the edit");
112110
actual = add_cursor(&actual, off)
113111
}
114112
}
115-
Some(off) => actual = add_cursor(&actual, off),
113+
Some(off) => actual = add_cursor(&actual, off.offset),
116114
};
117115

118116
assert_eq_text!(after, &actual);

crates/ra_ide/src/assists.rs

Lines changed: 0 additions & 42 deletions
This file was deleted.

crates/ra_ide/src/lib.rs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ mod syntax_highlighting;
3131
mod parent_module;
3232
mod references;
3333
mod impls;
34-
mod assists;
3534
mod diagnostics;
3635
mod syntax_tree;
3736
mod folding_ranges;
@@ -64,7 +63,6 @@ use ra_syntax::{SourceFile, TextRange, TextSize};
6463
use crate::display::ToNav;
6564

6665
pub use crate::{
67-
assists::{Assist, AssistId},
6866
call_hierarchy::CallItem,
6967
completion::{
7068
CompletionConfig, CompletionItem, CompletionItemKind, CompletionScore, InsertTextFormat,
@@ -84,6 +82,7 @@ pub use crate::{
8482
};
8583

8684
pub use hir::Documentation;
85+
pub use ra_assists::AssistId;
8786
pub use ra_db::{
8887
Canceled, CrateGraph, CrateId, Edition, FileId, FilePosition, FileRange, SourceRootId,
8988
};
@@ -134,10 +133,12 @@ pub struct AnalysisHost {
134133
db: RootDatabase,
135134
}
136135

137-
impl Default for AnalysisHost {
138-
fn default() -> AnalysisHost {
139-
AnalysisHost::new(None)
140-
}
136+
#[derive(Debug)]
137+
pub struct Assist {
138+
pub id: AssistId,
139+
pub label: String,
140+
pub group_label: Option<String>,
141+
pub source_change: SourceChange,
141142
}
142143

143144
impl AnalysisHost {
@@ -187,6 +188,12 @@ impl AnalysisHost {
187188
}
188189
}
189190

191+
impl Default for AnalysisHost {
192+
fn default() -> AnalysisHost {
193+
AnalysisHost::new(None)
194+
}
195+
}
196+
190197
/// Analysis is a snapshot of a world state at a moment in time. It is the main
191198
/// entry point for asking semantic information about the world. When the world
192199
/// state is advanced using `AnalysisHost::apply_change` method, all existing
@@ -464,7 +471,17 @@ impl Analysis {
464471
/// Computes assists (aka code actions aka intentions) for the given
465472
/// position.
466473
pub fn assists(&self, frange: FileRange) -> Cancelable<Vec<Assist>> {
467-
self.with_db(|db| assists::assists(db, frange))
474+
self.with_db(|db| {
475+
ra_assists::resolved_assists(db, frange)
476+
.into_iter()
477+
.map(|assist| Assist {
478+
id: assist.label.id,
479+
label: assist.label.label,
480+
group_label: assist.label.group.map(|it| it.0),
481+
source_change: assist.action,
482+
})
483+
.collect()
484+
})
468485
}
469486

470487
/// Computes the set of diagnostics for the given file.

crates/ra_ide_db/src/source_change.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use ra_db::{FileId, FilePosition, RelativePathBuf, SourceRootId};
77
use ra_text_edit::{TextEdit, TextSize};
88

9-
#[derive(Debug)]
9+
#[derive(Debug, Clone)]
1010
pub struct SourceChange {
1111
/// For display in the undo log in the editor
1212
pub label: String,
@@ -90,13 +90,13 @@ impl SourceChange {
9090
}
9191
}
9292

93-
#[derive(Debug)]
93+
#[derive(Debug, Clone)]
9494
pub struct SourceFileEdit {
9595
pub file_id: FileId,
9696
pub edit: TextEdit,
9797
}
9898

99-
#[derive(Debug)]
99+
#[derive(Debug, Clone)]
100100
pub enum FileSystemEdit {
101101
CreateFile { source_root: SourceRootId, path: RelativePathBuf },
102102
MoveFile { src: FileId, dst_source_root: SourceRootId, dst_path: RelativePathBuf },

0 commit comments

Comments
 (0)