Skip to content

Commit a95bb13

Browse files
bors[bot]matklad
andauthored
Merge #4571
4571: KISS SourceChange r=matklad a=matklad The idea behind requiring the label is a noble one, but we are not really using it consistently anyway, and it should be easy to retrofit later, should we need it. bors r+ Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 2a36a2a + 2c04aad commit a95bb13

File tree

8 files changed

+112
-153
lines changed

8 files changed

+112
-153
lines changed

crates/ra_assists/src/assist_context.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use hir::Semantics;
55
use ra_db::{FileId, FileRange};
66
use ra_fmt::{leading_indent, reindent};
77
use ra_ide_db::{
8-
source_change::{SingleFileChange, SourceChange},
8+
source_change::{SourceChange, SourceFileEdit},
99
RootDatabase,
1010
};
1111
use ra_syntax::{
@@ -150,11 +150,10 @@ impl Assists {
150150
self.add_impl(label, f)
151151
}
152152
fn add_impl(&mut self, label: Assist, f: impl FnOnce(&mut AssistBuilder)) -> Option<()> {
153-
let change_label = label.label.clone();
154153
let source_change = if self.resolve {
155154
let mut builder = AssistBuilder::new(self.file);
156155
f(&mut builder);
157-
Some(builder.finish(change_label))
156+
Some(builder.finish())
158157
} else {
159158
None
160159
};
@@ -246,9 +245,10 @@ impl AssistBuilder {
246245
&mut self.edit
247246
}
248247

249-
fn finish(self, change_label: String) -> SourceChange {
248+
fn finish(self) -> SourceChange {
250249
let edit = self.edit.finish();
251-
let mut res = SingleFileChange { label: change_label, edit }.into_source_change(self.file);
250+
let source_file_edit = SourceFileEdit { file_id: self.file, edit };
251+
let mut res: SourceChange = source_file_edit.into();
252252
if self.is_snippet {
253253
res.is_snippet = true;
254254
}

crates/ra_ide/src/diagnostics.rs

Lines changed: 52 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use ra_syntax::{
2121
};
2222
use ra_text_edit::{TextEdit, TextEditBuilder};
2323

24-
use crate::{Diagnostic, FileId, FileSystemEdit, SourceChange, SourceFileEdit};
24+
use crate::{Diagnostic, FileId, FileSystemEdit, Fix, SourceChange, SourceFileEdit};
2525

2626
#[derive(Debug, Copy, Clone)]
2727
pub enum Severity {
@@ -63,8 +63,8 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic>
6363
.parent()
6464
.unwrap_or_else(|| RelativePath::new(""))
6565
.join(&d.candidate);
66-
let create_file = FileSystemEdit::CreateFile { source_root, path };
67-
let fix = SourceChange::file_system_edit("Create module", create_file);
66+
let fix =
67+
Fix::new("Create module", FileSystemEdit::CreateFile { source_root, path }.into());
6868
res.borrow_mut().push(Diagnostic {
6969
range: sema.diagnostics_range(d).range,
7070
message: d.message(),
@@ -88,14 +88,12 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic>
8888
field_list = field_list.append_field(&field);
8989
}
9090

91-
let mut builder = TextEditBuilder::default();
92-
algo::diff(&d.ast(db).syntax(), &field_list.syntax()).into_text_edit(&mut builder);
93-
94-
Some(SourceChange::source_file_edit_from(
95-
"Fill struct fields",
96-
file_id,
97-
builder.finish(),
98-
))
91+
let edit = {
92+
let mut builder = TextEditBuilder::default();
93+
algo::diff(&d.ast(db).syntax(), &field_list.syntax()).into_text_edit(&mut builder);
94+
builder.finish()
95+
};
96+
Some(Fix::new("Fill struct fields", SourceFileEdit { file_id, edit }.into()))
9997
};
10098

10199
res.borrow_mut().push(Diagnostic {
@@ -117,7 +115,8 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic>
117115
let node = d.ast(db);
118116
let replacement = format!("Ok({})", node.syntax());
119117
let edit = TextEdit::replace(node.syntax().text_range(), replacement);
120-
let fix = SourceChange::source_file_edit_from("Wrap with ok", file_id, edit);
118+
let source_change = SourceChange::source_file_edit_from(file_id, edit);
119+
let fix = Fix::new("Wrap with ok", source_change);
121120
res.borrow_mut().push(Diagnostic {
122121
range: sema.diagnostics_range(d).range,
123122
message: d.message(),
@@ -154,9 +153,9 @@ fn check_unnecessary_braces_in_use_statement(
154153
range,
155154
message: "Unnecessary braces in use statement".to_string(),
156155
severity: Severity::WeakWarning,
157-
fix: Some(SourceChange::source_file_edit(
156+
fix: Some(Fix::new(
158157
"Remove unnecessary braces",
159-
SourceFileEdit { file_id, edit },
158+
SourceFileEdit { file_id, edit }.into(),
160159
)),
161160
});
162161
}
@@ -198,9 +197,9 @@ fn check_struct_shorthand_initialization(
198197
range: record_field.syntax().text_range(),
199198
message: "Shorthand struct initialization".to_string(),
200199
severity: Severity::WeakWarning,
201-
fix: Some(SourceChange::source_file_edit(
200+
fix: Some(Fix::new(
202201
"Use struct shorthand initialization",
203-
SourceFileEdit { file_id, edit },
202+
SourceFileEdit { file_id, edit }.into(),
204203
)),
205204
});
206205
}
@@ -240,7 +239,7 @@ mod tests {
240239
let diagnostic =
241240
diagnostics.pop().unwrap_or_else(|| panic!("no diagnostics for:\n{}\n", before));
242241
let mut fix = diagnostic.fix.unwrap();
243-
let edit = fix.source_file_edits.pop().unwrap().edit;
242+
let edit = fix.source_change.source_file_edits.pop().unwrap().edit;
244243
let actual = {
245244
let mut actual = before.to_string();
246245
edit.apply(&mut actual);
@@ -258,7 +257,7 @@ mod tests {
258257
let (analysis, file_position) = analysis_and_position(fixture);
259258
let diagnostic = analysis.diagnostics(file_position.file_id).unwrap().pop().unwrap();
260259
let mut fix = diagnostic.fix.unwrap();
261-
let edit = fix.source_file_edits.pop().unwrap().edit;
260+
let edit = fix.source_change.source_file_edits.pop().unwrap().edit;
262261
let target_file_contents = analysis.file_text(file_position.file_id).unwrap();
263262
let actual = {
264263
let mut actual = target_file_contents.to_string();
@@ -295,7 +294,7 @@ mod tests {
295294
let (analysis, file_id) = single_file(before);
296295
let diagnostic = analysis.diagnostics(file_id).unwrap().pop().unwrap();
297296
let mut fix = diagnostic.fix.unwrap();
298-
let edit = fix.source_file_edits.pop().unwrap().edit;
297+
let edit = fix.source_change.source_file_edits.pop().unwrap().edit;
299298
let actual = {
300299
let mut actual = before.to_string();
301300
edit.apply(&mut actual);
@@ -616,22 +615,24 @@ mod tests {
616615
Diagnostic {
617616
message: "unresolved module",
618617
range: 0..8,
618+
severity: Error,
619619
fix: Some(
620-
SourceChange {
620+
Fix {
621621
label: "Create module",
622-
source_file_edits: [],
623-
file_system_edits: [
624-
CreateFile {
625-
source_root: SourceRootId(
626-
0,
627-
),
628-
path: "foo.rs",
629-
},
630-
],
631-
is_snippet: false,
622+
source_change: SourceChange {
623+
source_file_edits: [],
624+
file_system_edits: [
625+
CreateFile {
626+
source_root: SourceRootId(
627+
0,
628+
),
629+
path: "foo.rs",
630+
},
631+
],
632+
is_snippet: false,
633+
},
632634
},
633635
),
634-
severity: Error,
635636
},
636637
]
637638
"###);
@@ -665,29 +666,31 @@ mod tests {
665666
Diagnostic {
666667
message: "Missing structure fields:\n- b",
667668
range: 224..233,
669+
severity: Error,
668670
fix: Some(
669-
SourceChange {
671+
Fix {
670672
label: "Fill struct fields",
671-
source_file_edits: [
672-
SourceFileEdit {
673-
file_id: FileId(
674-
1,
675-
),
676-
edit: TextEdit {
677-
indels: [
678-
Indel {
679-
insert: "{a:42, b: ()}",
680-
delete: 3..9,
681-
},
682-
],
673+
source_change: SourceChange {
674+
source_file_edits: [
675+
SourceFileEdit {
676+
file_id: FileId(
677+
1,
678+
),
679+
edit: TextEdit {
680+
indels: [
681+
Indel {
682+
insert: "{a:42, b: ()}",
683+
delete: 3..9,
684+
},
685+
],
686+
},
683687
},
684-
},
685-
],
686-
file_system_edits: [],
687-
is_snippet: false,
688+
],
689+
file_system_edits: [],
690+
is_snippet: false,
691+
},
688692
},
689693
),
690-
severity: Error,
691694
},
692695
]
693696
"###);

crates/ra_ide/src/lib.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,22 @@ pub type Cancelable<T> = Result<T, Canceled>;
9797
pub struct Diagnostic {
9898
pub message: String,
9999
pub range: TextRange,
100-
pub fix: Option<SourceChange>,
101100
pub severity: Severity,
101+
pub fix: Option<Fix>,
102+
}
103+
104+
#[derive(Debug)]
105+
pub struct Fix {
106+
pub label: String,
107+
pub source_change: SourceChange,
108+
}
109+
110+
impl Fix {
111+
pub fn new(label: impl Into<String>, source_change: SourceChange) -> Self {
112+
let label = label.into();
113+
assert!(label.starts_with(char::is_uppercase) && !label.ends_with('.'));
114+
Self { label, source_change }
115+
}
102116
}
103117

104118
/// Info associated with a text range.
@@ -493,7 +507,7 @@ impl Analysis {
493507
) -> Cancelable<Result<SourceChange, SsrError>> {
494508
self.with_db(|db| {
495509
let edits = ssr::parse_search_replace(query, parse_only, db)?;
496-
Ok(SourceChange::source_file_edits("Structural Search Replace", edits))
510+
Ok(SourceChange::source_file_edits(edits))
497511
})
498512
}
499513

crates/ra_ide/src/references/rename.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ fn rename_mod(
128128
source_file_edits.extend(ref_edits);
129129
}
130130

131-
Some(SourceChange::from_edits("Rename", source_file_edits, file_system_edits))
131+
Some(SourceChange::from_edits(source_file_edits, file_system_edits))
132132
}
133133

134134
fn rename_to_self(db: &RootDatabase, position: FilePosition) -> Option<RangeInfo<SourceChange>> {
@@ -171,7 +171,7 @@ fn rename_to_self(db: &RootDatabase, position: FilePosition) -> Option<RangeInfo
171171
),
172172
});
173173

174-
Some(RangeInfo::new(range, SourceChange::source_file_edits("Rename", edits)))
174+
Some(RangeInfo::new(range, SourceChange::source_file_edits(edits)))
175175
}
176176

177177
fn text_edit_from_self_param(
@@ -234,7 +234,7 @@ fn rename_self_to_param(
234234
let range = ast::SelfParam::cast(self_token.parent())
235235
.map_or(self_token.text_range(), |p| p.syntax().text_range());
236236

237-
Some(RangeInfo::new(range, SourceChange::source_file_edits("Rename", edits)))
237+
Some(RangeInfo::new(range, SourceChange::source_file_edits(edits)))
238238
}
239239

240240
fn rename_reference(
@@ -253,7 +253,7 @@ fn rename_reference(
253253
return None;
254254
}
255255

256-
Some(RangeInfo::new(range, SourceChange::source_file_edits("Rename", edit)))
256+
Some(RangeInfo::new(range, SourceChange::source_file_edits(edit)))
257257
}
258258

259259
#[cfg(test)]
@@ -642,7 +642,6 @@ mod tests {
642642
RangeInfo {
643643
range: 4..7,
644644
info: SourceChange {
645-
label: "Rename",
646645
source_file_edits: [
647646
SourceFileEdit {
648647
file_id: FileId(
@@ -694,7 +693,6 @@ mod tests {
694693
RangeInfo {
695694
range: 4..7,
696695
info: SourceChange {
697-
label: "Rename",
698696
source_file_edits: [
699697
SourceFileEdit {
700698
file_id: FileId(
@@ -777,7 +775,6 @@ mod tests {
777775
RangeInfo {
778776
range: 8..11,
779777
info: SourceChange {
780-
label: "Rename",
781778
source_file_edits: [
782779
SourceFileEdit {
783780
file_id: FileId(

0 commit comments

Comments
 (0)