rust/crates/ra_assists/src/lib.rs

329 lines
11 KiB
Rust
Raw Normal View History

2019-02-11 10:18:27 -06:00
//! `ra_assits` crate provides a bunch of code assists, also known as code
2019-02-03 12:26:35 -06:00
//! actions (in LSP) or intentions (in IntelliJ).
//!
//! An assist is a micro-refactoring, which is automatically activated in
//! certain context. For example, if the cursor is over `,`, a "swap `,`" assist
//! becomes available.
mod assist_ctx;
2019-02-24 05:22:25 -06:00
mod marks;
2019-02-03 12:26:35 -06:00
use itertools::Itertools;
2019-02-03 12:26:35 -06:00
use ra_text_edit::TextEdit;
use ra_syntax::{TextRange, TextUnit};
2019-02-03 12:26:35 -06:00
use ra_db::FileRange;
use hir::db::HirDatabase;
pub(crate) use crate::assist_ctx::{AssistCtx, Assist};
2019-02-24 04:53:35 -06:00
/// Unique identifier of the assist, should not be shown to the user
/// directly.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AssistId(pub &'static str);
#[derive(Debug, Clone)]
2019-02-03 12:26:35 -06:00
pub struct AssistLabel {
/// Short description of the assist, as shown in the UI.
pub label: String,
2019-02-24 04:53:35 -06:00
pub id: AssistId,
2019-02-03 12:26:35 -06:00
}
#[derive(Debug, Clone)]
2019-02-03 12:26:35 -06:00
pub struct AssistAction {
pub edit: TextEdit,
pub cursor_position: Option<TextUnit>,
2019-02-08 15:43:13 -06:00
pub target: Option<TextRange>,
2019-02-03 12:26:35 -06:00
}
/// 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 applicable_assists<H>(db: &H, range: FileRange) -> Vec<AssistLabel>
where
H: HirDatabase + 'static,
{
AssistCtx::with_ctx(db, range, false, |ctx| {
all_assists()
.iter()
.filter_map(|f| f(ctx.clone()))
.map(|a| match a {
Assist::Unresolved(labels) => labels,
2019-02-03 12:26:35 -06:00
Assist::Resolved(..) => unreachable!(),
})
.concat()
2019-02-03 12:26:35 -06:00
})
}
/// Return all the assists applicable at the given position.
///
/// Assists are returned in the "resolved" state, that is with edit fully
/// computed.
pub fn assists<H>(db: &H, range: FileRange) -> Vec<(AssistLabel, AssistAction)>
where
H: HirDatabase + 'static,
{
2019-02-08 15:43:13 -06:00
use std::cmp::Ordering;
2019-02-06 10:15:18 -06:00
AssistCtx::with_ctx(db, range, true, |ctx| {
2019-02-08 15:43:13 -06:00
let mut a = all_assists()
2019-02-03 12:26:35 -06:00
.iter()
.filter_map(|f| f(ctx.clone()))
.map(|a| match a {
Assist::Resolved(labels_actions) => labels_actions,
2019-02-03 12:26:35 -06:00
Assist::Unresolved(..) => unreachable!(),
})
.concat();
2019-02-08 18:57:08 -06:00
a.sort_by(|a, b| match (a.1.target, b.1.target) {
(Some(a), Some(b)) => a.len().cmp(&b.len()),
(Some(_), None) => Ordering::Less,
(None, Some(_)) => Ordering::Greater,
(None, None) => Ordering::Equal,
2019-02-08 15:43:13 -06:00
});
a
2019-02-03 12:26:35 -06:00
})
}
mod add_derive;
2019-04-08 12:56:37 -05:00
mod add_explicit_type;
2019-02-03 12:26:35 -06:00
mod add_impl;
mod flip_comma;
mod flip_binexpr;
2019-02-03 12:26:35 -06:00
mod change_visibility;
mod fill_match_arms;
mod fill_struct_fields;
2019-02-03 12:26:35 -06:00
mod introduce_variable;
2019-03-24 12:05:11 -05:00
mod inline_local_variable;
2019-02-03 12:26:35 -06:00
mod replace_if_let_with_match;
mod split_import;
mod remove_dbg;
2019-02-06 08:34:37 -06:00
mod auto_import;
mod add_missing_impl_members;
2019-02-06 08:34:37 -06:00
2019-02-03 12:26:35 -06:00
fn all_assists<DB: HirDatabase>() -> &'static [fn(AssistCtx<DB>) -> Option<Assist>] {
&[
add_derive::add_derive,
2019-04-08 12:56:37 -05:00
add_explicit_type::add_explicit_type,
2019-02-03 12:26:35 -06:00
add_impl::add_impl,
change_visibility::change_visibility,
fill_match_arms::fill_match_arms,
fill_struct_fields::fill_struct_fields,
2019-02-03 12:26:35 -06:00
flip_comma::flip_comma,
flip_binexpr::flip_binexpr,
2019-02-03 12:26:35 -06:00
introduce_variable::introduce_variable,
replace_if_let_with_match::replace_if_let_with_match,
split_import::split_import,
remove_dbg::remove_dbg,
2019-02-06 08:34:37 -06:00
auto_import::auto_import,
add_missing_impl_members::add_missing_impl_members,
2019-03-23 10:06:25 -05:00
add_missing_impl_members::add_missing_default_members,
2019-03-24 12:05:11 -05:00
inline_local_variable::inline_local_varialbe,
2019-02-03 12:26:35 -06:00
]
}
#[cfg(test)]
mod helpers {
use hir::mock::MockDatabase;
use ra_syntax::TextRange;
use ra_db::FileRange;
2019-02-08 17:47:44 -06:00
use test_utils::{extract_offset, extract_range, assert_eq_text, add_cursor};
2019-02-03 12:26:35 -06:00
use crate::{AssistCtx, Assist};
pub(crate) fn check_assist(
assist: fn(AssistCtx<MockDatabase>) -> Option<Assist>,
before: &str,
after: &str,
) {
check_assist_nth_action(assist, before, after, 0)
}
pub(crate) fn check_assist_range(
assist: fn(AssistCtx<MockDatabase>) -> Option<Assist>,
before: &str,
after: &str,
) {
check_assist_range_nth_action(assist, before, after, 0)
}
pub(crate) fn check_assist_target(
assist: fn(AssistCtx<MockDatabase>) -> Option<Assist>,
before: &str,
target: &str,
) {
check_assist_target_nth_action(assist, before, target, 0)
}
pub(crate) fn check_assist_range_target(
assist: fn(AssistCtx<MockDatabase>) -> Option<Assist>,
before: &str,
target: &str,
) {
check_assist_range_target_nth_action(assist, before, target, 0)
}
pub(crate) fn check_assist_nth_action(
assist: fn(AssistCtx<MockDatabase>) -> Option<Assist>,
before: &str,
after: &str,
index: usize,
2019-02-03 12:26:35 -06:00
) {
let (before_cursor_pos, before) = extract_offset(before);
let (db, _source_root, file_id) = MockDatabase::with_single_file(&before);
2019-02-08 05:49:43 -06:00
let frange =
FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) };
2019-02-03 12:26:35 -06:00
let assist =
AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable");
let labels_actions = match assist {
2019-02-03 12:26:35 -06:00
Assist::Unresolved(_) => unreachable!(),
Assist::Resolved(labels_actions) => labels_actions,
2019-02-03 12:26:35 -06:00
};
let (_, action) = labels_actions.get(index).expect("expect assist action at index");
2019-02-03 12:26:35 -06:00
let actual = action.edit.apply(&before);
let actual_cursor_pos = match action.cursor_position {
None => action
.edit
.apply_to_offset(before_cursor_pos)
.expect("cursor position is affected by the edit"),
Some(off) => off,
};
let actual = add_cursor(&actual, actual_cursor_pos);
assert_eq_text!(after, &actual);
}
pub(crate) fn check_assist_range_nth_action(
2019-02-03 12:26:35 -06:00
assist: fn(AssistCtx<MockDatabase>) -> Option<Assist>,
before: &str,
after: &str,
index: usize,
2019-02-03 12:26:35 -06:00
) {
let (range, before) = extract_range(before);
let (db, _source_root, file_id) = MockDatabase::with_single_file(&before);
let frange = FileRange { file_id, range };
let assist =
AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable");
let labels_actions = match assist {
2019-02-03 12:26:35 -06:00
Assist::Unresolved(_) => unreachable!(),
Assist::Resolved(labels_actions) => labels_actions,
2019-02-03 12:26:35 -06:00
};
let (_, action) = labels_actions.get(index).expect("expect assist action at index");
2019-02-03 12:26:35 -06:00
let mut actual = action.edit.apply(&before);
if let Some(pos) = action.cursor_position {
actual = add_cursor(&actual, pos);
}
assert_eq_text!(after, &actual);
}
pub(crate) fn check_assist_target_nth_action(
2019-02-08 17:34:05 -06:00
assist: fn(AssistCtx<MockDatabase>) -> Option<Assist>,
before: &str,
target: &str,
index: usize,
2019-02-08 17:34:05 -06:00
) {
let (before_cursor_pos, before) = extract_offset(before);
let (db, _source_root, file_id) = MockDatabase::with_single_file(&before);
let frange =
FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) };
let assist =
AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable");
let labels_actions = match assist {
2019-02-08 17:34:05 -06:00
Assist::Unresolved(_) => unreachable!(),
Assist::Resolved(labels_actions) => labels_actions,
2019-02-08 17:34:05 -06:00
};
let (_, action) = labels_actions.get(index).expect("expect assist action at index");
2019-02-08 17:34:05 -06:00
let range = action.target.expect("expected target on action");
assert_eq_text!(&before[range.start().to_usize()..range.end().to_usize()], target);
}
pub(crate) fn check_assist_range_target_nth_action(
2019-02-08 17:34:05 -06:00
assist: fn(AssistCtx<MockDatabase>) -> Option<Assist>,
before: &str,
target: &str,
index: usize,
2019-02-08 17:34:05 -06:00
) {
let (range, before) = extract_range(before);
let (db, _source_root, file_id) = MockDatabase::with_single_file(&before);
let frange = FileRange { file_id, range };
let assist =
AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable");
let labels_actions = match assist {
2019-02-08 17:34:05 -06:00
Assist::Unresolved(_) => unreachable!(),
Assist::Resolved(labels_actions) => labels_actions,
2019-02-08 17:34:05 -06:00
};
let (_, action) = labels_actions.get(index).expect("expect assist action at index");
2019-02-08 17:34:05 -06:00
let range = action.target.expect("expected target on action");
assert_eq_text!(&before[range.start().to_usize()..range.end().to_usize()], target);
}
2019-02-03 12:26:35 -06:00
pub(crate) fn check_assist_not_applicable(
assist: fn(AssistCtx<MockDatabase>) -> Option<Assist>,
before: &str,
) {
let (before_cursor_pos, before) = extract_offset(before);
let (db, _source_root, file_id) = MockDatabase::with_single_file(&before);
2019-02-08 05:49:43 -06:00
let frange =
FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) };
2019-02-03 12:26:35 -06:00
let assist = AssistCtx::with_ctx(&db, frange, true, assist);
assert!(assist.is_none());
}
pub(crate) fn check_assist_range_not_applicable(
assist: fn(AssistCtx<MockDatabase>) -> Option<Assist>,
before: &str,
) {
let (range, before) = extract_range(before);
let (db, _source_root, file_id) = MockDatabase::with_single_file(&before);
let frange = FileRange { file_id, range };
let assist = AssistCtx::with_ctx(&db, frange, true, assist);
assert!(assist.is_none());
}
2019-02-08 15:43:13 -06:00
}
#[cfg(test)]
mod tests {
use hir::mock::MockDatabase;
use ra_syntax::TextRange;
use ra_db::FileRange;
use test_utils::{extract_offset, extract_range};
2019-02-08 15:43:13 -06:00
#[test]
2019-02-08 17:34:05 -06:00
fn assist_order_field_struct() {
2019-02-08 15:43:13 -06:00
let before = "struct Foo { <|>bar: u32 }";
let (before_cursor_pos, before) = extract_offset(before);
let (db, _source_root, file_id) = MockDatabase::with_single_file(&before);
let frange =
FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) };
let assists = super::assists(&db, frange);
let mut assists = assists.iter();
assert_eq!(assists.next().expect("expected assist").0.label, "make pub(crate)");
assert_eq!(assists.next().expect("expected assist").0.label, "add `#[derive]`");
}
2019-02-03 12:26:35 -06:00
2019-02-08 17:34:05 -06:00
#[test]
fn assist_order_if_expr() {
let before = "
pub fn test_some_range(a: int) -> bool {
if let 2..6 = <|>5<|> {
2019-02-08 17:34:05 -06:00
true
} else {
false
}
}";
let (range, before) = extract_range(before);
2019-02-08 17:34:05 -06:00
let (db, _source_root, file_id) = MockDatabase::with_single_file(&before);
let frange = FileRange { file_id, range };
2019-02-08 17:34:05 -06:00
let assists = super::assists(&db, frange);
let mut assists = assists.iter();
assert_eq!(assists.next().expect("expected assist").0.label, "introduce variable");
assert_eq!(assists.next().expect("expected assist").0.label, "replace with match");
}
2019-02-03 12:26:35 -06:00
}