Skip to content

Commit d38741f

Browse files
bors[bot]matklad
andauthored
Merge #4327
4327: Refactor assists r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
2 parents a4778dd + 3908fad commit d38741f

File tree

8 files changed

+42
-45
lines changed

8 files changed

+42
-45
lines changed

crates/ra_assists/src/assist_ctx.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ impl AssistInfo {
3838

3939
pub(crate) fn into_resolved(self) -> Option<ResolvedAssist> {
4040
let label = self.label;
41-
let group_label = self.group_label;
42-
self.action.map(|action| ResolvedAssist { label, group_label, action })
41+
self.action.map(|action| ResolvedAssist { label, action })
4342
}
4443
}
4544

@@ -100,7 +99,7 @@ impl<'a> AssistCtx<'a> {
10099
label: impl Into<String>,
101100
f: impl FnOnce(&mut ActionBuilder),
102101
) -> Option<Assist> {
103-
let label = AssistLabel::new(label.into(), id);
102+
let label = AssistLabel::new(id, label.into(), None);
104103

105104
let mut info = AssistInfo::new(label);
106105
if self.should_compute_edit {
@@ -116,7 +115,8 @@ impl<'a> AssistCtx<'a> {
116115
}
117116

118117
pub(crate) fn add_assist_group(self, group_name: impl Into<String>) -> AssistGroup<'a> {
119-
AssistGroup { ctx: self, group_name: group_name.into(), assists: Vec::new() }
118+
let group = GroupLabel(group_name.into());
119+
AssistGroup { ctx: self, group, assists: Vec::new() }
120120
}
121121

122122
pub(crate) fn token_at_offset(&self) -> TokenAtOffset<SyntaxToken> {
@@ -146,7 +146,7 @@ impl<'a> AssistCtx<'a> {
146146

147147
pub(crate) struct AssistGroup<'a> {
148148
ctx: AssistCtx<'a>,
149-
group_name: String,
149+
group: GroupLabel,
150150
assists: Vec<AssistInfo>,
151151
}
152152

@@ -157,9 +157,9 @@ impl<'a> AssistGroup<'a> {
157157
label: impl Into<String>,
158158
f: impl FnOnce(&mut ActionBuilder),
159159
) {
160-
let label = AssistLabel::new(label.into(), id);
160+
let label = AssistLabel::new(id, label.into(), Some(self.group.clone()));
161161

162-
let mut info = AssistInfo::new(label).with_group(GroupLabel(self.group_name.clone()));
162+
let mut info = AssistInfo::new(label).with_group(self.group.clone());
163163
if self.ctx.should_compute_edit {
164164
let action = {
165165
let mut edit = ActionBuilder::new(&self.ctx);

crates/ra_assists/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ mod doc_tests;
1717
pub mod utils;
1818
pub mod ast_transform;
1919

20+
use hir::Semantics;
2021
use ra_db::{FileId, FileRange};
2122
use ra_ide_db::RootDatabase;
2223
use ra_syntax::{TextRange, TextSize};
2324
use ra_text_edit::TextEdit;
2425

2526
pub(crate) use crate::assist_ctx::{Assist, AssistCtx, AssistHandler};
26-
use hir::Semantics;
2727

2828
/// Unique identifier of the assist, should not be shown to the user
2929
/// directly.
@@ -32,19 +32,20 @@ pub struct AssistId(pub &'static str);
3232

3333
#[derive(Debug, Clone)]
3434
pub struct AssistLabel {
35+
pub id: AssistId,
3536
/// Short description of the assist, as shown in the UI.
3637
pub label: String,
37-
pub id: AssistId,
38+
pub group: Option<GroupLabel>,
3839
}
3940

4041
#[derive(Clone, Debug)]
4142
pub struct GroupLabel(pub String);
4243

4344
impl AssistLabel {
44-
pub(crate) fn new(label: String, id: AssistId) -> AssistLabel {
45+
pub(crate) fn new(id: AssistId, label: String, group: Option<GroupLabel>) -> AssistLabel {
4546
// FIXME: make fields private, so that this invariant can't be broken
4647
assert!(label.starts_with(|c: char| c.is_uppercase()));
47-
AssistLabel { label, id }
48+
AssistLabel { id, label, group }
4849
}
4950
}
5051

@@ -60,7 +61,6 @@ pub struct AssistAction {
6061
#[derive(Debug, Clone)]
6162
pub struct ResolvedAssist {
6263
pub label: AssistLabel,
63-
pub group_label: Option<GroupLabel>,
6464
pub action: AssistAction,
6565
}
6666

crates/ra_ide/src/assists.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! FIXME: write short doc here
22
3-
use ra_assists::{resolved_assists, AssistAction, AssistLabel};
3+
use ra_assists::{resolved_assists, AssistAction};
44
use ra_db::{FilePosition, FileRange};
55
use ra_ide_db::RootDatabase;
66

@@ -21,27 +21,22 @@ pub(crate) fn assists(db: &RootDatabase, frange: FileRange) -> Vec<Assist> {
2121
.into_iter()
2222
.map(|assist| {
2323
let file_id = frange.file_id;
24-
let assist_label = &assist.label;
2524
Assist {
26-
id: assist_label.id,
27-
label: assist_label.label.clone(),
28-
group_label: assist.group_label.map(|it| it.0),
29-
source_change: action_to_edit(assist.action, file_id, assist_label),
25+
id: assist.label.id,
26+
label: assist.label.label.clone(),
27+
group_label: assist.label.group.map(|it| it.0),
28+
source_change: action_to_edit(assist.action, file_id, assist.label.label.clone()),
3029
}
3130
})
3231
.collect()
3332
}
3433

35-
fn action_to_edit(
36-
action: AssistAction,
37-
file_id: FileId,
38-
assist_label: &AssistLabel,
39-
) -> SourceChange {
34+
fn action_to_edit(action: AssistAction, file_id: FileId, label: String) -> SourceChange {
4035
let file_id = match action.file {
4136
ra_assists::AssistFile::TargetFile(it) => it,
4237
_ => file_id,
4338
};
4439
let file_edit = SourceFileEdit { file_id, edit: action.edit };
45-
SourceChange::source_file_edit(assist_label.label.clone(), file_edit)
40+
SourceChange::source_file_edit(label, file_edit)
4641
.with_cursor_opt(action.cursor_position.map(|offset| FilePosition { offset, file_id }))
4742
}

crates/ra_ide/src/diagnostics.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic>
6464
.unwrap_or_else(|| RelativePath::new(""))
6565
.join(&d.candidate);
6666
let create_file = FileSystemEdit::CreateFile { source_root, path };
67-
let fix = SourceChange::file_system_edit("create module", create_file);
67+
let fix = SourceChange::file_system_edit("Create module", create_file);
6868
res.borrow_mut().push(Diagnostic {
6969
range: sema.diagnostics_range(d).range,
7070
message: d.message(),
@@ -92,7 +92,7 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic>
9292
algo::diff(&d.ast(db).syntax(), &field_list.syntax()).into_text_edit(&mut builder);
9393

9494
Some(SourceChange::source_file_edit_from(
95-
"fill struct fields",
95+
"Fill struct fields",
9696
file_id,
9797
builder.finish(),
9898
))
@@ -117,7 +117,7 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic>
117117
let node = d.ast(db);
118118
let replacement = format!("Ok({})", node.syntax());
119119
let edit = TextEdit::replace(node.syntax().text_range(), replacement);
120-
let fix = SourceChange::source_file_edit_from("wrap with ok", file_id, edit);
120+
let fix = SourceChange::source_file_edit_from("Wrap with ok", file_id, edit);
121121
res.borrow_mut().push(Diagnostic {
122122
range: sema.diagnostics_range(d).range,
123123
message: d.message(),
@@ -199,7 +199,7 @@ fn check_struct_shorthand_initialization(
199199
message: "Shorthand struct initialization".to_string(),
200200
severity: Severity::WeakWarning,
201201
fix: Some(SourceChange::source_file_edit(
202-
"use struct shorthand initialization",
202+
"Use struct shorthand initialization",
203203
SourceFileEdit { file_id, edit },
204204
)),
205205
});
@@ -606,7 +606,7 @@ mod tests {
606606
range: 0..8,
607607
fix: Some(
608608
SourceChange {
609-
label: "create module",
609+
label: "Create module",
610610
source_file_edits: [],
611611
file_system_edits: [
612612
CreateFile {
@@ -655,7 +655,7 @@ mod tests {
655655
range: 224..233,
656656
fix: Some(
657657
SourceChange {
658-
label: "fill struct fields",
658+
label: "Fill struct fields",
659659
source_file_edits: [
660660
SourceFileEdit {
661661
file_id: FileId(

crates/ra_ide/src/references/rename.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ fn rename_mod(
122122
source_file_edits.extend(ref_edits);
123123
}
124124

125-
Some(SourceChange::from_edits("rename", source_file_edits, file_system_edits))
125+
Some(SourceChange::from_edits("Rename", source_file_edits, file_system_edits))
126126
}
127127

128128
fn rename_reference(
@@ -141,7 +141,7 @@ fn rename_reference(
141141
return None;
142142
}
143143

144-
Some(RangeInfo::new(range, SourceChange::source_file_edits("rename", edit)))
144+
Some(RangeInfo::new(range, SourceChange::source_file_edits("Rename", edit)))
145145
}
146146

147147
#[cfg(test)]
@@ -530,7 +530,7 @@ mod tests {
530530
RangeInfo {
531531
range: 4..7,
532532
info: SourceChange {
533-
label: "rename",
533+
label: "Rename",
534534
source_file_edits: [
535535
SourceFileEdit {
536536
file_id: FileId(
@@ -582,7 +582,7 @@ mod tests {
582582
RangeInfo {
583583
range: 4..7,
584584
info: SourceChange {
585-
label: "rename",
585+
label: "Rename",
586586
source_file_edits: [
587587
SourceFileEdit {
588588
file_id: FileId(
@@ -665,7 +665,7 @@ mod tests {
665665
RangeInfo {
666666
range: 8..11,
667667
info: SourceChange {
668-
label: "rename",
668+
label: "Rename",
669669
source_file_edits: [
670670
SourceFileEdit {
671671
file_id: FileId(

crates/ra_ide/src/source_change.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ impl SourceChange {
3535
/// Creates a new SourceChange with the given label,
3636
/// containing only the given `SourceFileEdits`.
3737
pub(crate) fn source_file_edits<L: Into<String>>(label: L, edits: Vec<SourceFileEdit>) -> Self {
38+
let label = label.into();
39+
assert!(label.starts_with(char::is_uppercase));
3840
SourceChange {
39-
label: label.into(),
41+
label: label,
4042
source_file_edits: edits,
4143
file_system_edits: vec![],
4244
cursor_position: None,

crates/ra_ide/src/typing/on_enter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub(crate) fn on_enter(db: &RootDatabase, position: FilePosition) -> Option<Sour
4444

4545
Some(
4646
SourceChange::source_file_edit(
47-
"on enter",
47+
"On enter",
4848
SourceFileEdit { edit, file_id: position.file_id },
4949
)
5050
.with_cursor(FilePosition { offset: cursor_position, file_id: position.file_id }),

crates/rust-analyzer/tests/heavy_tests/main.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ fn main() {}
337337
"arguments": [
338338
{
339339
"cursorPosition": null,
340-
"label": "create module",
340+
"label": "Create module",
341341
"workspaceEdit": {
342342
"documentChanges": [
343343
{
@@ -349,9 +349,9 @@ fn main() {}
349349
}
350350
],
351351
"command": "rust-analyzer.applySourceChange",
352-
"title": "create module"
352+
"title": "Create module"
353353
},
354-
"title": "create module"
354+
"title": "Create module"
355355
}
356356
]),
357357
);
@@ -420,7 +420,7 @@ fn main() {{}}
420420
"arguments": [
421421
{
422422
"cursorPosition": null,
423-
"label": "create module",
423+
"label": "Create module",
424424
"workspaceEdit": {
425425
"documentChanges": [
426426
{
@@ -432,9 +432,9 @@ fn main() {{}}
432432
}
433433
],
434434
"command": "rust-analyzer.applySourceChange",
435-
"title": "create module"
435+
"title": "Create module"
436436
},
437-
"title": "create module"
437+
"title": "Create module"
438438
}
439439
]),
440440
);
@@ -500,7 +500,7 @@ fn main() {{}}
500500
"position": { "character": 4, "line": 1 },
501501
"textDocument": { "uri": "file:///[..]src/m0.rs" }
502502
},
503-
"label": "on enter",
503+
"label": "On enter",
504504
"workspaceEdit": {
505505
"documentChanges": [
506506
{
@@ -552,7 +552,7 @@ version = \"0.0.0\"
552552
"position": { "line": 1, "character": 4 },
553553
"textDocument": { "uri": "file:///[..]src/main.rs" }
554554
},
555-
"label": "on enter",
555+
"label": "On enter",
556556
"workspaceEdit": {
557557
"documentChanges": [
558558
{

0 commit comments

Comments
 (0)