rust-analyzer/crates/ra_ide/src/assists.rs

44 lines
1.3 KiB
Rust
Raw Normal View History

//! FIXME: write short doc here
2020-02-06 17:46:11 +00:00
use ra_assists::{resolved_assists, AssistAction, AssistLabel};
use ra_db::{FilePosition, FileRange};
use ra_ide_db::RootDatabase;
2020-02-06 11:52:32 +00:00
2020-02-06 15:26:43 +00:00
use crate::{FileId, SourceChange, SourceFileEdit};
2019-02-03 18:26:35 +00:00
2019-02-24 10:53:35 +00:00
pub use ra_assists::AssistId;
#[derive(Debug)]
pub struct Assist {
pub id: AssistId,
2020-01-01 23:39:01 +00:00
pub label: String,
pub group_label: Option<String>,
pub source_change: SourceChange,
2019-02-24 10:53:35 +00:00
}
pub(crate) fn assists(db: &RootDatabase, frange: FileRange) -> Vec<Assist> {
2020-02-06 17:46:11 +00:00
resolved_assists(db, frange)
2019-02-03 18:26:35 +00:00
.into_iter()
2020-01-11 22:40:36 +00:00
.map(|assist| {
2019-02-03 18:26:35 +00:00
let file_id = frange.file_id;
2020-01-11 22:40:36 +00:00
let assist_label = &assist.label;
2020-01-01 23:39:01 +00:00
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),
2020-01-01 23:39:01 +00:00
}
2019-02-03 18:26:35 +00:00
})
2019-02-03 16:27:36 +00:00
.collect()
}
2020-01-01 23:39:01 +00:00
fn action_to_edit(
action: AssistAction,
file_id: FileId,
assist_label: &AssistLabel,
) -> SourceChange {
let file_edit = SourceFileEdit { file_id, edit: action.edit };
SourceChange::source_file_edit(assist_label.label.clone(), file_edit)
.with_cursor_opt(action.cursor_position.map(|offset| FilePosition { offset, file_id }))
2020-01-01 23:39:01 +00:00
}