Nicer API

This commit is contained in:
Aleksey Kladov 2020-05-07 17:29:23 +02:00
parent c6b81bc013
commit 28fcff125a
3 changed files with 42 additions and 42 deletions

View file

@ -29,6 +29,9 @@ pub(crate) use crate::assist_context::{AssistContext, Assists};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AssistId(pub &'static str);
#[derive(Clone, Debug)]
pub struct GroupLabel(pub String);
#[derive(Debug, Clone)]
pub struct Assist {
pub id: AssistId,
@ -40,10 +43,41 @@ pub struct Assist {
pub target: TextRange,
}
#[derive(Clone, Debug)]
pub struct GroupLabel(pub String);
#[derive(Debug, Clone)]
pub struct ResolvedAssist {
pub assist: Assist,
pub source_change: SourceChange,
}
impl Assist {
/// Return all the assists applicable at the given position.
///
/// Assists are returned in the "unresolved" state, that is only labels are
/// returned, without actual edits.
pub fn unresolved(db: &RootDatabase, range: FileRange) -> Vec<Assist> {
let sema = Semantics::new(db);
let ctx = AssistContext::new(sema, range);
let mut acc = Assists::new_unresolved(&ctx);
handlers::all().iter().for_each(|handler| {
handler(&mut acc, &ctx);
});
acc.finish_unresolved()
}
/// Return all the assists applicable at the given position.
///
/// Assists are returned in the "resolved" state, that is with edit fully
/// computed.
pub fn resolved(db: &RootDatabase, range: FileRange) -> Vec<ResolvedAssist> {
let sema = Semantics::new(db);
let ctx = AssistContext::new(sema, range);
let mut acc = Assists::new_resolved(&ctx);
handlers::all().iter().for_each(|handler| {
handler(&mut acc, &ctx);
});
acc.finish_resolved()
}
pub(crate) fn new(
id: AssistId,
label: String,
@ -56,40 +90,6 @@ impl Assist {
}
}
#[derive(Debug, Clone)]
pub struct ResolvedAssist {
pub assist: Assist,
pub source_change: SourceChange,
}
/// Return all the assists applicable at the given position.
///
/// Assists are returned in the "unresolved" state, that is only labels are
/// returned, without actual edits.
pub fn unresolved_assists(db: &RootDatabase, range: FileRange) -> Vec<Assist> {
let sema = Semantics::new(db);
let ctx = AssistContext::new(sema, range);
let mut acc = Assists::new_unresolved(&ctx);
handlers::all().iter().for_each(|handler| {
handler(&mut acc, &ctx);
});
acc.finish_unresolved()
}
/// Return all the assists applicable at the given position.
///
/// Assists are returned in the "resolved" state, that is with edit fully
/// computed.
pub fn resolved_assists(db: &RootDatabase, range: FileRange) -> Vec<ResolvedAssist> {
let sema = Semantics::new(db);
let ctx = AssistContext::new(sema, range);
let mut acc = Assists::new_resolved(&ctx);
handlers::all().iter().for_each(|handler| {
handler(&mut acc, &ctx);
});
acc.finish_resolved()
}
mod handlers {
use crate::{AssistContext, Assists};

View file

@ -11,7 +11,7 @@ use test_utils::{
RangeOrOffset,
};
use crate::{handlers::Handler, resolved_assists, AssistContext, Assists};
use crate::{handlers::Handler, Assist, AssistContext, Assists};
pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) {
let (mut db, file_id) = RootDatabase::with_single_file(text);
@ -41,14 +41,14 @@ fn check_doc_test(assist_id: &str, before: &str, after: &str) {
let (db, file_id) = crate::tests::with_single_file(&before);
let frange = FileRange { file_id, range: selection.into() };
let mut assist = resolved_assists(&db, frange)
let mut assist = Assist::resolved(&db, frange)
.into_iter()
.find(|assist| assist.assist.id.0 == assist_id)
.unwrap_or_else(|| {
panic!(
"\n\nAssist is not applicable: {}\nAvailable assists: {}",
assist_id,
resolved_assists(&db, frange)
Assist::resolved(&db, frange)
.into_iter()
.map(|assist| assist.assist.id.0)
.collect::<Vec<_>>()
@ -136,7 +136,7 @@ fn assist_order_field_struct() {
let (before_cursor_pos, before) = extract_offset(before);
let (db, file_id) = with_single_file(&before);
let frange = FileRange { file_id, range: TextRange::empty(before_cursor_pos) };
let assists = resolved_assists(&db, frange);
let assists = Assist::resolved(&db, frange);
let mut assists = assists.iter();
assert_eq!(
@ -159,7 +159,7 @@ fn assist_order_if_expr() {
let (range, before) = extract_range(before);
let (db, file_id) = with_single_file(&before);
let frange = FileRange { file_id, range };
let assists = resolved_assists(&db, frange);
let assists = Assist::resolved(&db, frange);
let mut assists = assists.iter();
assert_eq!(assists.next().expect("expected assist").assist.label, "Extract into variable");

View file

@ -472,7 +472,7 @@ impl Analysis {
/// position.
pub fn assists(&self, frange: FileRange) -> Cancelable<Vec<Assist>> {
self.with_db(|db| {
ra_assists::resolved_assists(db, frange)
ra_assists::Assist::resolved(db, frange)
.into_iter()
.map(|assist| Assist {
id: assist.assist.id,