mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-27 05:23:24 +00:00
Merge #4327
4327: Refactor assists r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
d38741f681
8 changed files with 42 additions and 45 deletions
|
@ -38,8 +38,7 @@ impl AssistInfo {
|
|||
|
||||
pub(crate) fn into_resolved(self) -> Option<ResolvedAssist> {
|
||||
let label = self.label;
|
||||
let group_label = self.group_label;
|
||||
self.action.map(|action| ResolvedAssist { label, group_label, action })
|
||||
self.action.map(|action| ResolvedAssist { label, action })
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -100,7 +99,7 @@ impl<'a> AssistCtx<'a> {
|
|||
label: impl Into<String>,
|
||||
f: impl FnOnce(&mut ActionBuilder),
|
||||
) -> Option<Assist> {
|
||||
let label = AssistLabel::new(label.into(), id);
|
||||
let label = AssistLabel::new(id, label.into(), None);
|
||||
|
||||
let mut info = AssistInfo::new(label);
|
||||
if self.should_compute_edit {
|
||||
|
@ -116,7 +115,8 @@ impl<'a> AssistCtx<'a> {
|
|||
}
|
||||
|
||||
pub(crate) fn add_assist_group(self, group_name: impl Into<String>) -> AssistGroup<'a> {
|
||||
AssistGroup { ctx: self, group_name: group_name.into(), assists: Vec::new() }
|
||||
let group = GroupLabel(group_name.into());
|
||||
AssistGroup { ctx: self, group, assists: Vec::new() }
|
||||
}
|
||||
|
||||
pub(crate) fn token_at_offset(&self) -> TokenAtOffset<SyntaxToken> {
|
||||
|
@ -146,7 +146,7 @@ impl<'a> AssistCtx<'a> {
|
|||
|
||||
pub(crate) struct AssistGroup<'a> {
|
||||
ctx: AssistCtx<'a>,
|
||||
group_name: String,
|
||||
group: GroupLabel,
|
||||
assists: Vec<AssistInfo>,
|
||||
}
|
||||
|
||||
|
@ -157,9 +157,9 @@ impl<'a> AssistGroup<'a> {
|
|||
label: impl Into<String>,
|
||||
f: impl FnOnce(&mut ActionBuilder),
|
||||
) {
|
||||
let label = AssistLabel::new(label.into(), id);
|
||||
let label = AssistLabel::new(id, label.into(), Some(self.group.clone()));
|
||||
|
||||
let mut info = AssistInfo::new(label).with_group(GroupLabel(self.group_name.clone()));
|
||||
let mut info = AssistInfo::new(label).with_group(self.group.clone());
|
||||
if self.ctx.should_compute_edit {
|
||||
let action = {
|
||||
let mut edit = ActionBuilder::new(&self.ctx);
|
||||
|
|
|
@ -17,13 +17,13 @@ mod doc_tests;
|
|||
pub mod utils;
|
||||
pub mod ast_transform;
|
||||
|
||||
use hir::Semantics;
|
||||
use ra_db::{FileId, FileRange};
|
||||
use ra_ide_db::RootDatabase;
|
||||
use ra_syntax::{TextRange, TextSize};
|
||||
use ra_text_edit::TextEdit;
|
||||
|
||||
pub(crate) use crate::assist_ctx::{Assist, AssistCtx, AssistHandler};
|
||||
use hir::Semantics;
|
||||
|
||||
/// Unique identifier of the assist, should not be shown to the user
|
||||
/// directly.
|
||||
|
@ -32,19 +32,20 @@ pub struct AssistId(pub &'static str);
|
|||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct AssistLabel {
|
||||
pub id: AssistId,
|
||||
/// Short description of the assist, as shown in the UI.
|
||||
pub label: String,
|
||||
pub id: AssistId,
|
||||
pub group: Option<GroupLabel>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct GroupLabel(pub String);
|
||||
|
||||
impl AssistLabel {
|
||||
pub(crate) fn new(label: String, id: AssistId) -> AssistLabel {
|
||||
pub(crate) fn new(id: AssistId, label: String, group: Option<GroupLabel>) -> AssistLabel {
|
||||
// FIXME: make fields private, so that this invariant can't be broken
|
||||
assert!(label.starts_with(|c: char| c.is_uppercase()));
|
||||
AssistLabel { label, id }
|
||||
AssistLabel { id, label, group }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -60,7 +61,6 @@ pub struct AssistAction {
|
|||
#[derive(Debug, Clone)]
|
||||
pub struct ResolvedAssist {
|
||||
pub label: AssistLabel,
|
||||
pub group_label: Option<GroupLabel>,
|
||||
pub action: AssistAction,
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
//! FIXME: write short doc here
|
||||
|
||||
use ra_assists::{resolved_assists, AssistAction, AssistLabel};
|
||||
use ra_assists::{resolved_assists, AssistAction};
|
||||
use ra_db::{FilePosition, FileRange};
|
||||
use ra_ide_db::RootDatabase;
|
||||
|
||||
|
@ -21,27 +21,22 @@ pub(crate) fn assists(db: &RootDatabase, frange: FileRange) -> Vec<Assist> {
|
|||
.into_iter()
|
||||
.map(|assist| {
|
||||
let file_id = frange.file_id;
|
||||
let assist_label = &assist.label;
|
||||
Assist {
|
||||
id: assist_label.id,
|
||||
label: assist_label.label.clone(),
|
||||
group_label: assist.group_label.map(|it| it.0),
|
||||
source_change: action_to_edit(assist.action, file_id, assist_label),
|
||||
id: assist.label.id,
|
||||
label: assist.label.label.clone(),
|
||||
group_label: assist.label.group.map(|it| it.0),
|
||||
source_change: action_to_edit(assist.action, file_id, assist.label.label.clone()),
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn action_to_edit(
|
||||
action: AssistAction,
|
||||
file_id: FileId,
|
||||
assist_label: &AssistLabel,
|
||||
) -> SourceChange {
|
||||
fn action_to_edit(action: AssistAction, file_id: FileId, label: String) -> SourceChange {
|
||||
let file_id = match action.file {
|
||||
ra_assists::AssistFile::TargetFile(it) => it,
|
||||
_ => file_id,
|
||||
};
|
||||
let file_edit = SourceFileEdit { file_id, edit: action.edit };
|
||||
SourceChange::source_file_edit(assist_label.label.clone(), file_edit)
|
||||
SourceChange::source_file_edit(label, file_edit)
|
||||
.with_cursor_opt(action.cursor_position.map(|offset| FilePosition { offset, file_id }))
|
||||
}
|
||||
|
|
|
@ -64,7 +64,7 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic>
|
|||
.unwrap_or_else(|| RelativePath::new(""))
|
||||
.join(&d.candidate);
|
||||
let create_file = FileSystemEdit::CreateFile { source_root, path };
|
||||
let fix = SourceChange::file_system_edit("create module", create_file);
|
||||
let fix = SourceChange::file_system_edit("Create module", create_file);
|
||||
res.borrow_mut().push(Diagnostic {
|
||||
range: sema.diagnostics_range(d).range,
|
||||
message: d.message(),
|
||||
|
@ -92,7 +92,7 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic>
|
|||
algo::diff(&d.ast(db).syntax(), &field_list.syntax()).into_text_edit(&mut builder);
|
||||
|
||||
Some(SourceChange::source_file_edit_from(
|
||||
"fill struct fields",
|
||||
"Fill struct fields",
|
||||
file_id,
|
||||
builder.finish(),
|
||||
))
|
||||
|
@ -117,7 +117,7 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic>
|
|||
let node = d.ast(db);
|
||||
let replacement = format!("Ok({})", node.syntax());
|
||||
let edit = TextEdit::replace(node.syntax().text_range(), replacement);
|
||||
let fix = SourceChange::source_file_edit_from("wrap with ok", file_id, edit);
|
||||
let fix = SourceChange::source_file_edit_from("Wrap with ok", file_id, edit);
|
||||
res.borrow_mut().push(Diagnostic {
|
||||
range: sema.diagnostics_range(d).range,
|
||||
message: d.message(),
|
||||
|
@ -199,7 +199,7 @@ fn check_struct_shorthand_initialization(
|
|||
message: "Shorthand struct initialization".to_string(),
|
||||
severity: Severity::WeakWarning,
|
||||
fix: Some(SourceChange::source_file_edit(
|
||||
"use struct shorthand initialization",
|
||||
"Use struct shorthand initialization",
|
||||
SourceFileEdit { file_id, edit },
|
||||
)),
|
||||
});
|
||||
|
@ -606,7 +606,7 @@ mod tests {
|
|||
range: 0..8,
|
||||
fix: Some(
|
||||
SourceChange {
|
||||
label: "create module",
|
||||
label: "Create module",
|
||||
source_file_edits: [],
|
||||
file_system_edits: [
|
||||
CreateFile {
|
||||
|
@ -655,7 +655,7 @@ mod tests {
|
|||
range: 224..233,
|
||||
fix: Some(
|
||||
SourceChange {
|
||||
label: "fill struct fields",
|
||||
label: "Fill struct fields",
|
||||
source_file_edits: [
|
||||
SourceFileEdit {
|
||||
file_id: FileId(
|
||||
|
|
|
@ -122,7 +122,7 @@ fn rename_mod(
|
|||
source_file_edits.extend(ref_edits);
|
||||
}
|
||||
|
||||
Some(SourceChange::from_edits("rename", source_file_edits, file_system_edits))
|
||||
Some(SourceChange::from_edits("Rename", source_file_edits, file_system_edits))
|
||||
}
|
||||
|
||||
fn rename_reference(
|
||||
|
@ -141,7 +141,7 @@ fn rename_reference(
|
|||
return None;
|
||||
}
|
||||
|
||||
Some(RangeInfo::new(range, SourceChange::source_file_edits("rename", edit)))
|
||||
Some(RangeInfo::new(range, SourceChange::source_file_edits("Rename", edit)))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
@ -530,7 +530,7 @@ mod tests {
|
|||
RangeInfo {
|
||||
range: 4..7,
|
||||
info: SourceChange {
|
||||
label: "rename",
|
||||
label: "Rename",
|
||||
source_file_edits: [
|
||||
SourceFileEdit {
|
||||
file_id: FileId(
|
||||
|
@ -582,7 +582,7 @@ mod tests {
|
|||
RangeInfo {
|
||||
range: 4..7,
|
||||
info: SourceChange {
|
||||
label: "rename",
|
||||
label: "Rename",
|
||||
source_file_edits: [
|
||||
SourceFileEdit {
|
||||
file_id: FileId(
|
||||
|
@ -665,7 +665,7 @@ mod tests {
|
|||
RangeInfo {
|
||||
range: 8..11,
|
||||
info: SourceChange {
|
||||
label: "rename",
|
||||
label: "Rename",
|
||||
source_file_edits: [
|
||||
SourceFileEdit {
|
||||
file_id: FileId(
|
||||
|
|
|
@ -35,8 +35,10 @@ impl SourceChange {
|
|||
/// Creates a new SourceChange with the given label,
|
||||
/// containing only the given `SourceFileEdits`.
|
||||
pub(crate) fn source_file_edits<L: Into<String>>(label: L, edits: Vec<SourceFileEdit>) -> Self {
|
||||
let label = label.into();
|
||||
assert!(label.starts_with(char::is_uppercase));
|
||||
SourceChange {
|
||||
label: label.into(),
|
||||
label: label,
|
||||
source_file_edits: edits,
|
||||
file_system_edits: vec![],
|
||||
cursor_position: None,
|
||||
|
|
|
@ -44,7 +44,7 @@ pub(crate) fn on_enter(db: &RootDatabase, position: FilePosition) -> Option<Sour
|
|||
|
||||
Some(
|
||||
SourceChange::source_file_edit(
|
||||
"on enter",
|
||||
"On enter",
|
||||
SourceFileEdit { edit, file_id: position.file_id },
|
||||
)
|
||||
.with_cursor(FilePosition { offset: cursor_position, file_id: position.file_id }),
|
||||
|
|
|
@ -337,7 +337,7 @@ fn main() {}
|
|||
"arguments": [
|
||||
{
|
||||
"cursorPosition": null,
|
||||
"label": "create module",
|
||||
"label": "Create module",
|
||||
"workspaceEdit": {
|
||||
"documentChanges": [
|
||||
{
|
||||
|
@ -349,9 +349,9 @@ fn main() {}
|
|||
}
|
||||
],
|
||||
"command": "rust-analyzer.applySourceChange",
|
||||
"title": "create module"
|
||||
"title": "Create module"
|
||||
},
|
||||
"title": "create module"
|
||||
"title": "Create module"
|
||||
}
|
||||
]),
|
||||
);
|
||||
|
@ -420,7 +420,7 @@ fn main() {{}}
|
|||
"arguments": [
|
||||
{
|
||||
"cursorPosition": null,
|
||||
"label": "create module",
|
||||
"label": "Create module",
|
||||
"workspaceEdit": {
|
||||
"documentChanges": [
|
||||
{
|
||||
|
@ -432,9 +432,9 @@ fn main() {{}}
|
|||
}
|
||||
],
|
||||
"command": "rust-analyzer.applySourceChange",
|
||||
"title": "create module"
|
||||
"title": "Create module"
|
||||
},
|
||||
"title": "create module"
|
||||
"title": "Create module"
|
||||
}
|
||||
]),
|
||||
);
|
||||
|
@ -500,7 +500,7 @@ fn main() {{}}
|
|||
"position": { "character": 4, "line": 1 },
|
||||
"textDocument": { "uri": "file:///[..]src/m0.rs" }
|
||||
},
|
||||
"label": "on enter",
|
||||
"label": "On enter",
|
||||
"workspaceEdit": {
|
||||
"documentChanges": [
|
||||
{
|
||||
|
@ -552,7 +552,7 @@ version = \"0.0.0\"
|
|||
"position": { "line": 1, "character": 4 },
|
||||
"textDocument": { "uri": "file:///[..]src/main.rs" }
|
||||
},
|
||||
"label": "on enter",
|
||||
"label": "On enter",
|
||||
"workspaceEdit": {
|
||||
"documentChanges": [
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue