4327: Refactor assists r=matklad a=matklad



bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2020-05-05 19:36:53 +00:00 committed by GitHub
commit d38741f681
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 42 additions and 45 deletions

View file

@ -38,8 +38,7 @@ impl AssistInfo {
pub(crate) fn into_resolved(self) -> Option<ResolvedAssist> { pub(crate) fn into_resolved(self) -> Option<ResolvedAssist> {
let label = self.label; let label = self.label;
let group_label = self.group_label; self.action.map(|action| ResolvedAssist { label, action })
self.action.map(|action| ResolvedAssist { label, group_label, action })
} }
} }
@ -100,7 +99,7 @@ impl<'a> AssistCtx<'a> {
label: impl Into<String>, label: impl Into<String>,
f: impl FnOnce(&mut ActionBuilder), f: impl FnOnce(&mut ActionBuilder),
) -> Option<Assist> { ) -> Option<Assist> {
let label = AssistLabel::new(label.into(), id); let label = AssistLabel::new(id, label.into(), None);
let mut info = AssistInfo::new(label); let mut info = AssistInfo::new(label);
if self.should_compute_edit { 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> { 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> { pub(crate) fn token_at_offset(&self) -> TokenAtOffset<SyntaxToken> {
@ -146,7 +146,7 @@ impl<'a> AssistCtx<'a> {
pub(crate) struct AssistGroup<'a> { pub(crate) struct AssistGroup<'a> {
ctx: AssistCtx<'a>, ctx: AssistCtx<'a>,
group_name: String, group: GroupLabel,
assists: Vec<AssistInfo>, assists: Vec<AssistInfo>,
} }
@ -157,9 +157,9 @@ impl<'a> AssistGroup<'a> {
label: impl Into<String>, label: impl Into<String>,
f: impl FnOnce(&mut ActionBuilder), 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 { if self.ctx.should_compute_edit {
let action = { let action = {
let mut edit = ActionBuilder::new(&self.ctx); let mut edit = ActionBuilder::new(&self.ctx);

View file

@ -17,13 +17,13 @@ mod doc_tests;
pub mod utils; pub mod utils;
pub mod ast_transform; pub mod ast_transform;
use hir::Semantics;
use ra_db::{FileId, FileRange}; use ra_db::{FileId, FileRange};
use ra_ide_db::RootDatabase; use ra_ide_db::RootDatabase;
use ra_syntax::{TextRange, TextSize}; use ra_syntax::{TextRange, TextSize};
use ra_text_edit::TextEdit; use ra_text_edit::TextEdit;
pub(crate) use crate::assist_ctx::{Assist, AssistCtx, AssistHandler}; pub(crate) use crate::assist_ctx::{Assist, AssistCtx, AssistHandler};
use hir::Semantics;
/// Unique identifier of the assist, should not be shown to the user /// Unique identifier of the assist, should not be shown to the user
/// directly. /// directly.
@ -32,19 +32,20 @@ pub struct AssistId(pub &'static str);
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct AssistLabel { pub struct AssistLabel {
pub id: AssistId,
/// Short description of the assist, as shown in the UI. /// Short description of the assist, as shown in the UI.
pub label: String, pub label: String,
pub id: AssistId, pub group: Option<GroupLabel>,
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct GroupLabel(pub String); pub struct GroupLabel(pub String);
impl AssistLabel { 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 // FIXME: make fields private, so that this invariant can't be broken
assert!(label.starts_with(|c: char| c.is_uppercase())); 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)] #[derive(Debug, Clone)]
pub struct ResolvedAssist { pub struct ResolvedAssist {
pub label: AssistLabel, pub label: AssistLabel,
pub group_label: Option<GroupLabel>,
pub action: AssistAction, pub action: AssistAction,
} }

View file

@ -1,6 +1,6 @@
//! FIXME: write short doc here //! 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_db::{FilePosition, FileRange};
use ra_ide_db::RootDatabase; use ra_ide_db::RootDatabase;
@ -21,27 +21,22 @@ pub(crate) fn assists(db: &RootDatabase, frange: FileRange) -> Vec<Assist> {
.into_iter() .into_iter()
.map(|assist| { .map(|assist| {
let file_id = frange.file_id; let file_id = frange.file_id;
let assist_label = &assist.label;
Assist { Assist {
id: assist_label.id, id: assist.label.id,
label: assist_label.label.clone(), label: assist.label.label.clone(),
group_label: assist.group_label.map(|it| it.0), group_label: assist.label.group.map(|it| it.0),
source_change: action_to_edit(assist.action, file_id, assist_label), source_change: action_to_edit(assist.action, file_id, assist.label.label.clone()),
} }
}) })
.collect() .collect()
} }
fn action_to_edit( fn action_to_edit(action: AssistAction, file_id: FileId, label: String) -> SourceChange {
action: AssistAction,
file_id: FileId,
assist_label: &AssistLabel,
) -> SourceChange {
let file_id = match action.file { let file_id = match action.file {
ra_assists::AssistFile::TargetFile(it) => it, ra_assists::AssistFile::TargetFile(it) => it,
_ => file_id, _ => file_id,
}; };
let file_edit = SourceFileEdit { file_id, edit: action.edit }; 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 })) .with_cursor_opt(action.cursor_position.map(|offset| FilePosition { offset, file_id }))
} }

View file

@ -64,7 +64,7 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic>
.unwrap_or_else(|| RelativePath::new("")) .unwrap_or_else(|| RelativePath::new(""))
.join(&d.candidate); .join(&d.candidate);
let create_file = FileSystemEdit::CreateFile { source_root, path }; 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 { res.borrow_mut().push(Diagnostic {
range: sema.diagnostics_range(d).range, range: sema.diagnostics_range(d).range,
message: d.message(), 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); algo::diff(&d.ast(db).syntax(), &field_list.syntax()).into_text_edit(&mut builder);
Some(SourceChange::source_file_edit_from( Some(SourceChange::source_file_edit_from(
"fill struct fields", "Fill struct fields",
file_id, file_id,
builder.finish(), builder.finish(),
)) ))
@ -117,7 +117,7 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic>
let node = d.ast(db); let node = d.ast(db);
let replacement = format!("Ok({})", node.syntax()); let replacement = format!("Ok({})", node.syntax());
let edit = TextEdit::replace(node.syntax().text_range(), replacement); 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 { res.borrow_mut().push(Diagnostic {
range: sema.diagnostics_range(d).range, range: sema.diagnostics_range(d).range,
message: d.message(), message: d.message(),
@ -199,7 +199,7 @@ fn check_struct_shorthand_initialization(
message: "Shorthand struct initialization".to_string(), message: "Shorthand struct initialization".to_string(),
severity: Severity::WeakWarning, severity: Severity::WeakWarning,
fix: Some(SourceChange::source_file_edit( fix: Some(SourceChange::source_file_edit(
"use struct shorthand initialization", "Use struct shorthand initialization",
SourceFileEdit { file_id, edit }, SourceFileEdit { file_id, edit },
)), )),
}); });
@ -606,7 +606,7 @@ mod tests {
range: 0..8, range: 0..8,
fix: Some( fix: Some(
SourceChange { SourceChange {
label: "create module", label: "Create module",
source_file_edits: [], source_file_edits: [],
file_system_edits: [ file_system_edits: [
CreateFile { CreateFile {
@ -655,7 +655,7 @@ mod tests {
range: 224..233, range: 224..233,
fix: Some( fix: Some(
SourceChange { SourceChange {
label: "fill struct fields", label: "Fill struct fields",
source_file_edits: [ source_file_edits: [
SourceFileEdit { SourceFileEdit {
file_id: FileId( file_id: FileId(

View file

@ -122,7 +122,7 @@ fn rename_mod(
source_file_edits.extend(ref_edits); 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( fn rename_reference(
@ -141,7 +141,7 @@ fn rename_reference(
return None; return None;
} }
Some(RangeInfo::new(range, SourceChange::source_file_edits("rename", edit))) Some(RangeInfo::new(range, SourceChange::source_file_edits("Rename", edit)))
} }
#[cfg(test)] #[cfg(test)]
@ -530,7 +530,7 @@ mod tests {
RangeInfo { RangeInfo {
range: 4..7, range: 4..7,
info: SourceChange { info: SourceChange {
label: "rename", label: "Rename",
source_file_edits: [ source_file_edits: [
SourceFileEdit { SourceFileEdit {
file_id: FileId( file_id: FileId(
@ -582,7 +582,7 @@ mod tests {
RangeInfo { RangeInfo {
range: 4..7, range: 4..7,
info: SourceChange { info: SourceChange {
label: "rename", label: "Rename",
source_file_edits: [ source_file_edits: [
SourceFileEdit { SourceFileEdit {
file_id: FileId( file_id: FileId(
@ -665,7 +665,7 @@ mod tests {
RangeInfo { RangeInfo {
range: 8..11, range: 8..11,
info: SourceChange { info: SourceChange {
label: "rename", label: "Rename",
source_file_edits: [ source_file_edits: [
SourceFileEdit { SourceFileEdit {
file_id: FileId( file_id: FileId(

View file

@ -35,8 +35,10 @@ impl SourceChange {
/// Creates a new SourceChange with the given label, /// Creates a new SourceChange with the given label,
/// containing only the given `SourceFileEdits`. /// containing only the given `SourceFileEdits`.
pub(crate) fn source_file_edits<L: Into<String>>(label: L, edits: Vec<SourceFileEdit>) -> Self { 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 { SourceChange {
label: label.into(), label: label,
source_file_edits: edits, source_file_edits: edits,
file_system_edits: vec![], file_system_edits: vec![],
cursor_position: None, cursor_position: None,

View file

@ -44,7 +44,7 @@ pub(crate) fn on_enter(db: &RootDatabase, position: FilePosition) -> Option<Sour
Some( Some(
SourceChange::source_file_edit( SourceChange::source_file_edit(
"on enter", "On enter",
SourceFileEdit { edit, file_id: position.file_id }, SourceFileEdit { edit, file_id: position.file_id },
) )
.with_cursor(FilePosition { offset: cursor_position, file_id: position.file_id }), .with_cursor(FilePosition { offset: cursor_position, file_id: position.file_id }),

View file

@ -337,7 +337,7 @@ fn main() {}
"arguments": [ "arguments": [
{ {
"cursorPosition": null, "cursorPosition": null,
"label": "create module", "label": "Create module",
"workspaceEdit": { "workspaceEdit": {
"documentChanges": [ "documentChanges": [
{ {
@ -349,9 +349,9 @@ fn main() {}
} }
], ],
"command": "rust-analyzer.applySourceChange", "command": "rust-analyzer.applySourceChange",
"title": "create module" "title": "Create module"
}, },
"title": "create module" "title": "Create module"
} }
]), ]),
); );
@ -420,7 +420,7 @@ fn main() {{}}
"arguments": [ "arguments": [
{ {
"cursorPosition": null, "cursorPosition": null,
"label": "create module", "label": "Create module",
"workspaceEdit": { "workspaceEdit": {
"documentChanges": [ "documentChanges": [
{ {
@ -432,9 +432,9 @@ fn main() {{}}
} }
], ],
"command": "rust-analyzer.applySourceChange", "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 }, "position": { "character": 4, "line": 1 },
"textDocument": { "uri": "file:///[..]src/m0.rs" } "textDocument": { "uri": "file:///[..]src/m0.rs" }
}, },
"label": "on enter", "label": "On enter",
"workspaceEdit": { "workspaceEdit": {
"documentChanges": [ "documentChanges": [
{ {
@ -552,7 +552,7 @@ version = \"0.0.0\"
"position": { "line": 1, "character": 4 }, "position": { "line": 1, "character": 4 },
"textDocument": { "uri": "file:///[..]src/main.rs" } "textDocument": { "uri": "file:///[..]src/main.rs" }
}, },
"label": "on enter", "label": "On enter",
"workspaceEdit": { "workspaceEdit": {
"documentChanges": [ "documentChanges": [
{ {