mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 14:21:44 +00:00
Nicer API
This commit is contained in:
parent
c6b81bc013
commit
28fcff125a
3 changed files with 42 additions and 42 deletions
|
@ -29,6 +29,9 @@ pub(crate) use crate::assist_context::{AssistContext, Assists};
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
pub struct AssistId(pub &'static str);
|
pub struct AssistId(pub &'static str);
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub struct GroupLabel(pub String);
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Assist {
|
pub struct Assist {
|
||||||
pub id: AssistId,
|
pub id: AssistId,
|
||||||
|
@ -40,10 +43,41 @@ pub struct Assist {
|
||||||
pub target: TextRange,
|
pub target: TextRange,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct GroupLabel(pub String);
|
pub struct ResolvedAssist {
|
||||||
|
pub assist: Assist,
|
||||||
|
pub source_change: SourceChange,
|
||||||
|
}
|
||||||
|
|
||||||
impl Assist {
|
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(
|
pub(crate) fn new(
|
||||||
id: AssistId,
|
id: AssistId,
|
||||||
label: String,
|
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 {
|
mod handlers {
|
||||||
use crate::{AssistContext, Assists};
|
use crate::{AssistContext, Assists};
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,7 @@ use test_utils::{
|
||||||
RangeOrOffset,
|
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) {
|
pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) {
|
||||||
let (mut db, file_id) = RootDatabase::with_single_file(text);
|
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 (db, file_id) = crate::tests::with_single_file(&before);
|
||||||
let frange = FileRange { file_id, range: selection.into() };
|
let frange = FileRange { file_id, range: selection.into() };
|
||||||
|
|
||||||
let mut assist = resolved_assists(&db, frange)
|
let mut assist = Assist::resolved(&db, frange)
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.find(|assist| assist.assist.id.0 == assist_id)
|
.find(|assist| assist.assist.id.0 == assist_id)
|
||||||
.unwrap_or_else(|| {
|
.unwrap_or_else(|| {
|
||||||
panic!(
|
panic!(
|
||||||
"\n\nAssist is not applicable: {}\nAvailable assists: {}",
|
"\n\nAssist is not applicable: {}\nAvailable assists: {}",
|
||||||
assist_id,
|
assist_id,
|
||||||
resolved_assists(&db, frange)
|
Assist::resolved(&db, frange)
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|assist| assist.assist.id.0)
|
.map(|assist| assist.assist.id.0)
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
|
@ -136,7 +136,7 @@ fn assist_order_field_struct() {
|
||||||
let (before_cursor_pos, before) = extract_offset(before);
|
let (before_cursor_pos, before) = extract_offset(before);
|
||||||
let (db, file_id) = with_single_file(&before);
|
let (db, file_id) = with_single_file(&before);
|
||||||
let frange = FileRange { file_id, range: TextRange::empty(before_cursor_pos) };
|
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();
|
let mut assists = assists.iter();
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
@ -159,7 +159,7 @@ fn assist_order_if_expr() {
|
||||||
let (range, before) = extract_range(before);
|
let (range, before) = extract_range(before);
|
||||||
let (db, file_id) = with_single_file(&before);
|
let (db, file_id) = with_single_file(&before);
|
||||||
let frange = FileRange { file_id, range };
|
let frange = FileRange { file_id, range };
|
||||||
let assists = resolved_assists(&db, frange);
|
let assists = Assist::resolved(&db, frange);
|
||||||
let mut assists = assists.iter();
|
let mut assists = assists.iter();
|
||||||
|
|
||||||
assert_eq!(assists.next().expect("expected assist").assist.label, "Extract into variable");
|
assert_eq!(assists.next().expect("expected assist").assist.label, "Extract into variable");
|
||||||
|
|
|
@ -472,7 +472,7 @@ impl Analysis {
|
||||||
/// position.
|
/// position.
|
||||||
pub fn assists(&self, frange: FileRange) -> Cancelable<Vec<Assist>> {
|
pub fn assists(&self, frange: FileRange) -> Cancelable<Vec<Assist>> {
|
||||||
self.with_db(|db| {
|
self.with_db(|db| {
|
||||||
ra_assists::resolved_assists(db, frange)
|
ra_assists::Assist::resolved(db, frange)
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|assist| Assist {
|
.map(|assist| Assist {
|
||||||
id: assist.assist.id,
|
id: assist.assist.id,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue