2019-09-08 04:10:53 -05:00
|
|
|
//! `ra_assists` 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-10-25 06:16:46 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod doc_tests;
|
2020-01-10 11:26:18 -06:00
|
|
|
pub mod ast_transform;
|
2019-02-11 11:07:21 -06:00
|
|
|
|
2020-01-15 12:20:20 -06:00
|
|
|
use either::Either;
|
2020-02-06 09:53:42 -06:00
|
|
|
use hir::ModuleDef;
|
2019-07-04 15:05:17 -05:00
|
|
|
use ra_db::FileRange;
|
2020-02-06 09:40:28 -06:00
|
|
|
use ra_ide_db::{imports_locator::ImportsLocatorIde, RootDatabase};
|
2020-01-24 01:33:18 -06:00
|
|
|
use ra_syntax::{TextRange, TextUnit};
|
2019-07-04 15:05:17 -05:00
|
|
|
use ra_text_edit::TextEdit;
|
2019-02-03 12:26:35 -06:00
|
|
|
|
2019-07-04 15:05:17 -05:00
|
|
|
pub(crate) use crate::assist_ctx::{Assist, AssistCtx};
|
2019-10-27 08:46:49 -05:00
|
|
|
pub use crate::assists::add_import::auto_import_text_edit;
|
2019-02-03 12:26:35 -06:00
|
|
|
|
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);
|
|
|
|
|
2019-02-11 11:07:21 -06:00
|
|
|
#[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
|
|
|
}
|
|
|
|
|
2019-02-11 11:07:21 -06:00
|
|
|
#[derive(Debug, Clone)]
|
2019-02-03 12:26:35 -06:00
|
|
|
pub struct AssistAction {
|
2020-01-01 17:39:01 -06:00
|
|
|
pub label: Option<String>,
|
2019-02-03 12:26:35 -06:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-01-11 16:40:36 -06:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct ResolvedAssist {
|
|
|
|
pub label: AssistLabel,
|
|
|
|
pub action_data: Either<AssistAction, Vec<AssistAction>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ResolvedAssist {
|
|
|
|
pub fn get_first_action(&self) -> AssistAction {
|
|
|
|
match &self.action_data {
|
|
|
|
Either::Left(action) => action.clone(),
|
|
|
|
Either::Right(actions) => actions[0].clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-25 06:16:46 -05:00
|
|
|
/// Return all the assists applicable at the given position.
|
2019-02-03 12:26:35 -06:00
|
|
|
///
|
|
|
|
/// Assists are returned in the "unresolved" state, that is only labels are
|
|
|
|
/// returned, without actual edits.
|
2020-02-06 09:53:42 -06:00
|
|
|
pub fn applicable_assists(db: &RootDatabase, range: FileRange) -> Vec<AssistLabel> {
|
2019-02-03 12:26:35 -06:00
|
|
|
AssistCtx::with_ctx(db, range, false, |ctx| {
|
2019-09-25 06:29:41 -05:00
|
|
|
assists::all()
|
2019-02-03 12:26:35 -06:00
|
|
|
.iter()
|
|
|
|
.filter_map(|f| f(ctx.clone()))
|
|
|
|
.map(|a| match a {
|
2019-10-27 10:22:14 -05:00
|
|
|
Assist::Unresolved { label } => label,
|
|
|
|
Assist::Resolved { .. } => unreachable!(),
|
2019-02-03 12:26:35 -06:00
|
|
|
})
|
2019-10-27 10:22:14 -05:00
|
|
|
.collect()
|
2019-02-03 12:26:35 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-23 18:19:09 -06:00
|
|
|
/// A functionality for locating imports for the given name.
|
|
|
|
///
|
|
|
|
/// Currently has to be a trait with the real implementation provided by the ra_ide_api crate,
|
|
|
|
/// due to the search functionality located there.
|
|
|
|
/// Later, this trait should be removed completely and the search functionality moved to a separate crate,
|
|
|
|
/// accessible from the ra_assists crate.
|
2020-01-23 13:15:16 -06:00
|
|
|
pub trait ImportsLocator {
|
2019-12-23 18:19:09 -06:00
|
|
|
/// Finds all imports for the given name and the module that contains this name.
|
2020-01-24 01:33:18 -06:00
|
|
|
fn find_imports(&mut self, name_to_import: &str) -> Vec<ModuleDef>;
|
2019-12-23 18:19:09 -06:00
|
|
|
}
|
|
|
|
|
2020-02-06 09:40:28 -06:00
|
|
|
impl ImportsLocator for ImportsLocatorIde<'_> {
|
|
|
|
fn find_imports(&mut self, name_to_import: &str) -> Vec<ModuleDef> {
|
|
|
|
self.find_imports(name_to_import)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-23 18:19:09 -06:00
|
|
|
/// Return all the assists applicable at the given position
|
|
|
|
/// and additional assists that need the imports locator functionality to work.
|
|
|
|
///
|
|
|
|
/// Assists are returned in the "resolved" state, that is with edit fully
|
|
|
|
/// computed.
|
2020-02-06 09:40:28 -06:00
|
|
|
pub fn assists_with_imports_locator(db: &RootDatabase, range: FileRange) -> Vec<ResolvedAssist> {
|
|
|
|
let mut imports_locator = ImportsLocatorIde::new(db);
|
2019-12-23 18:19:09 -06:00
|
|
|
AssistCtx::with_ctx(db, range, true, |ctx| {
|
|
|
|
let mut assists = assists::all()
|
|
|
|
.iter()
|
|
|
|
.map(|f| f(ctx.clone()))
|
|
|
|
.chain(
|
|
|
|
assists::all_with_imports_locator()
|
|
|
|
.iter()
|
|
|
|
.map(|f| f(ctx.clone(), &mut imports_locator)),
|
|
|
|
)
|
|
|
|
.filter_map(std::convert::identity)
|
|
|
|
.map(|a| match a {
|
|
|
|
Assist::Resolved { assist } => assist,
|
|
|
|
Assist::Unresolved { .. } => unreachable!(),
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
sort_assists(&mut assists);
|
|
|
|
assists
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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.
|
2020-02-06 09:53:42 -06:00
|
|
|
pub fn assists(db: &RootDatabase, range: FileRange) -> Vec<ResolvedAssist> {
|
2019-02-06 10:15:18 -06:00
|
|
|
AssistCtx::with_ctx(db, range, true, |ctx| {
|
2019-09-25 06:29:41 -05:00
|
|
|
let mut a = assists::all()
|
2019-02-03 12:26:35 -06:00
|
|
|
.iter()
|
|
|
|
.filter_map(|f| f(ctx.clone()))
|
|
|
|
.map(|a| match a {
|
2020-01-11 16:40:36 -06:00
|
|
|
Assist::Resolved { assist } => assist,
|
2019-10-27 10:22:14 -05:00
|
|
|
Assist::Unresolved { .. } => unreachable!(),
|
2019-02-03 12:26:35 -06:00
|
|
|
})
|
2019-12-23 18:19:09 -06:00
|
|
|
.collect();
|
|
|
|
sort_assists(&mut a);
|
2019-02-08 15:43:13 -06:00
|
|
|
a
|
2019-02-03 12:26:35 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-23 18:19:09 -06:00
|
|
|
fn sort_assists(assists: &mut Vec<ResolvedAssist>) {
|
|
|
|
use std::cmp::Ordering;
|
|
|
|
assists.sort_by(|a, b| match (a.get_first_action().target, b.get_first_action().target) {
|
|
|
|
(Some(a), Some(b)) => a.len().cmp(&b.len()),
|
|
|
|
(Some(_), None) => Ordering::Less,
|
|
|
|
(None, Some(_)) => Ordering::Greater,
|
|
|
|
(None, None) => Ordering::Equal,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-09-25 06:29:41 -05:00
|
|
|
mod assists {
|
2019-12-23 18:19:09 -06:00
|
|
|
use crate::{Assist, AssistCtx, ImportsLocator};
|
2019-09-25 06:29:41 -05:00
|
|
|
|
|
|
|
mod add_derive;
|
|
|
|
mod add_explicit_type;
|
|
|
|
mod add_impl;
|
2019-10-15 13:29:20 -05:00
|
|
|
mod add_custom_impl;
|
2019-11-09 09:56:36 -06:00
|
|
|
mod add_new;
|
2019-10-03 15:19:46 -05:00
|
|
|
mod apply_demorgan;
|
2019-12-23 18:19:09 -06:00
|
|
|
mod auto_import;
|
2019-11-21 12:51:40 -06:00
|
|
|
mod invert_if;
|
2019-09-25 06:29:41 -05:00
|
|
|
mod flip_comma;
|
|
|
|
mod flip_binexpr;
|
2019-10-26 15:24:48 -05:00
|
|
|
mod flip_trait_bound;
|
2019-09-25 06:29:41 -05:00
|
|
|
mod change_visibility;
|
|
|
|
mod fill_match_arms;
|
|
|
|
mod merge_match_arms;
|
|
|
|
mod introduce_variable;
|
|
|
|
mod inline_local_variable;
|
|
|
|
mod raw_string;
|
|
|
|
mod replace_if_let_with_match;
|
|
|
|
mod split_import;
|
|
|
|
mod remove_dbg;
|
2019-10-27 08:46:49 -05:00
|
|
|
pub(crate) mod add_import;
|
2019-09-25 06:29:41 -05:00
|
|
|
mod add_missing_impl_members;
|
|
|
|
mod move_guard;
|
|
|
|
mod move_bounds;
|
2019-10-20 13:00:09 -05:00
|
|
|
mod early_return;
|
2019-09-25 06:29:41 -05:00
|
|
|
|
2020-02-06 09:58:57 -06:00
|
|
|
pub(crate) fn all() -> &'static [fn(AssistCtx) -> Option<Assist>] {
|
2019-09-25 06:29:41 -05:00
|
|
|
&[
|
|
|
|
add_derive::add_derive,
|
|
|
|
add_explicit_type::add_explicit_type,
|
|
|
|
add_impl::add_impl,
|
2019-10-15 13:29:20 -05:00
|
|
|
add_custom_impl::add_custom_impl,
|
2019-11-09 09:56:36 -06:00
|
|
|
add_new::add_new,
|
2019-10-03 15:19:46 -05:00
|
|
|
apply_demorgan::apply_demorgan,
|
2019-11-21 12:51:40 -06:00
|
|
|
invert_if::invert_if,
|
2019-09-25 06:29:41 -05:00
|
|
|
change_visibility::change_visibility,
|
|
|
|
fill_match_arms::fill_match_arms,
|
|
|
|
merge_match_arms::merge_match_arms,
|
|
|
|
flip_comma::flip_comma,
|
|
|
|
flip_binexpr::flip_binexpr,
|
2019-10-26 15:24:48 -05:00
|
|
|
flip_trait_bound::flip_trait_bound,
|
2019-09-25 06:29:41 -05:00
|
|
|
introduce_variable::introduce_variable,
|
|
|
|
replace_if_let_with_match::replace_if_let_with_match,
|
|
|
|
split_import::split_import,
|
|
|
|
remove_dbg::remove_dbg,
|
2019-10-27 08:46:49 -05:00
|
|
|
add_import::add_import,
|
2019-09-25 06:29:41 -05:00
|
|
|
add_missing_impl_members::add_missing_impl_members,
|
|
|
|
add_missing_impl_members::add_missing_default_members,
|
2020-01-19 10:39:53 -06:00
|
|
|
inline_local_variable::inline_local_variable,
|
2019-09-25 06:29:41 -05:00
|
|
|
move_guard::move_guard_to_arm_body,
|
|
|
|
move_guard::move_arm_cond_to_match_guard,
|
|
|
|
move_bounds::move_bounds_to_where_clause,
|
|
|
|
raw_string::add_hash,
|
|
|
|
raw_string::make_raw_string,
|
|
|
|
raw_string::make_usual_string,
|
|
|
|
raw_string::remove_hash,
|
2019-10-20 13:00:09 -05:00
|
|
|
early_return::convert_to_guarded_return,
|
2019-09-25 06:29:41 -05:00
|
|
|
]
|
|
|
|
}
|
2019-12-23 18:19:09 -06:00
|
|
|
|
2020-02-06 09:58:57 -06:00
|
|
|
pub(crate) fn all_with_imports_locator<'a, F: ImportsLocator>(
|
|
|
|
) -> &'a [fn(AssistCtx, &mut F) -> Option<Assist>] {
|
2019-12-23 18:19:09 -06:00
|
|
|
&[auto_import::auto_import]
|
|
|
|
}
|
2019-02-03 12:26:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod helpers {
|
2020-01-26 16:16:18 -06:00
|
|
|
use hir::db::DefDatabase;
|
|
|
|
use ra_db::{fixture::WithFixture, FileId, FileRange};
|
2019-07-04 15:05:17 -05:00
|
|
|
use ra_syntax::TextRange;
|
|
|
|
use test_utils::{add_cursor, assert_eq_text, extract_offset, extract_range};
|
2019-02-03 12:26:35 -06:00
|
|
|
|
2020-02-06 09:53:42 -06:00
|
|
|
use crate::{Assist, AssistCtx, ImportsLocator};
|
|
|
|
use ra_ide_db::RootDatabase;
|
2020-01-26 16:16:18 -06:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2020-01-27 06:42:45 -06:00
|
|
|
// FIXME remove the `ModuleDefId` reexport from `ra_hir` when this gets removed.
|
2020-01-26 16:16:18 -06:00
|
|
|
pub(crate) struct TestImportsLocator {
|
2020-02-06 09:53:42 -06:00
|
|
|
db: Arc<RootDatabase>,
|
2020-01-26 16:16:18 -06:00
|
|
|
test_file_id: FileId,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TestImportsLocator {
|
2020-02-06 09:53:42 -06:00
|
|
|
pub(crate) fn new(db: Arc<RootDatabase>, test_file_id: FileId) -> Self {
|
2020-01-26 16:16:18 -06:00
|
|
|
TestImportsLocator { db, test_file_id }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ImportsLocator for TestImportsLocator {
|
|
|
|
fn find_imports(&mut self, name_to_import: &str) -> Vec<hir::ModuleDef> {
|
|
|
|
let crate_def_map = self.db.crate_def_map(self.db.test_crate());
|
2020-01-27 06:42:45 -06:00
|
|
|
let mut findings = Vec::new();
|
2020-01-26 16:16:18 -06:00
|
|
|
|
|
|
|
let mut module_ids_to_process =
|
|
|
|
crate_def_map.modules_for_file(self.test_file_id).collect::<Vec<_>>();
|
|
|
|
|
|
|
|
while !module_ids_to_process.is_empty() {
|
2020-01-27 06:42:45 -06:00
|
|
|
let mut more_ids_to_process = Vec::new();
|
2020-01-26 16:16:18 -06:00
|
|
|
for local_module_id in module_ids_to_process.drain(..) {
|
|
|
|
for (name, namespace_data) in
|
|
|
|
crate_def_map[local_module_id].scope.entries_without_primitives()
|
|
|
|
{
|
|
|
|
let found_a_match = &name.to_string() == name_to_import;
|
|
|
|
vec![namespace_data.types, namespace_data.values]
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(std::convert::identity)
|
|
|
|
.for_each(|(module_def_id, _)| {
|
|
|
|
if found_a_match {
|
|
|
|
findings.push(module_def_id.into());
|
|
|
|
}
|
|
|
|
if let hir::ModuleDefId::ModuleId(module_id) = module_def_id {
|
|
|
|
more_ids_to_process.push(module_id.local_id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
module_ids_to_process = more_ids_to_process;
|
|
|
|
}
|
|
|
|
|
|
|
|
findings
|
|
|
|
}
|
|
|
|
}
|
2019-02-03 12:26:35 -06:00
|
|
|
|
2020-02-06 09:58:57 -06:00
|
|
|
pub(crate) fn check_assist(assist: fn(AssistCtx) -> Option<Assist>, before: &str, after: &str) {
|
2019-02-03 12:26:35 -06:00
|
|
|
let (before_cursor_pos, before) = extract_offset(before);
|
2020-02-06 09:53:42 -06:00
|
|
|
let (db, file_id) = RootDatabase::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");
|
2019-10-27 10:22:14 -05:00
|
|
|
let action = match assist {
|
|
|
|
Assist::Unresolved { .. } => unreachable!(),
|
2020-01-11 16:40:36 -06:00
|
|
|
Assist::Resolved { assist } => assist.get_first_action(),
|
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);
|
|
|
|
}
|
|
|
|
|
2020-01-23 13:15:16 -06:00
|
|
|
pub(crate) fn check_assist_with_imports_locator<F: ImportsLocator>(
|
2020-02-06 09:58:57 -06:00
|
|
|
assist: fn(AssistCtx, &mut F) -> Option<Assist>,
|
2020-02-06 09:53:42 -06:00
|
|
|
imports_locator_provider: fn(db: Arc<RootDatabase>, file_id: FileId) -> F,
|
2019-12-23 18:19:09 -06:00
|
|
|
before: &str,
|
|
|
|
after: &str,
|
|
|
|
) {
|
|
|
|
let (before_cursor_pos, before) = extract_offset(before);
|
2020-02-06 09:53:42 -06:00
|
|
|
let (db, file_id) = RootDatabase::with_single_file(&before);
|
2020-01-26 16:16:18 -06:00
|
|
|
let db = Arc::new(db);
|
|
|
|
let mut imports_locator = imports_locator_provider(Arc::clone(&db), file_id);
|
2019-12-23 18:19:09 -06:00
|
|
|
let frange =
|
|
|
|
FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) };
|
2020-01-26 16:16:18 -06:00
|
|
|
let assist =
|
|
|
|
AssistCtx::with_ctx(db.as_ref(), frange, true, |ctx| assist(ctx, &mut imports_locator))
|
|
|
|
.expect("code action is not applicable");
|
2019-12-23 18:19:09 -06:00
|
|
|
let action = match assist {
|
|
|
|
Assist::Unresolved { .. } => unreachable!(),
|
|
|
|
Assist::Resolved { assist } => assist.get_first_action(),
|
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-10-27 10:22:14 -05:00
|
|
|
pub(crate) fn check_assist_range(
|
2020-02-06 09:58:57 -06:00
|
|
|
assist: fn(AssistCtx) -> Option<Assist>,
|
2019-02-03 12:26:35 -06:00
|
|
|
before: &str,
|
|
|
|
after: &str,
|
|
|
|
) {
|
|
|
|
let (range, before) = extract_range(before);
|
2020-02-06 09:53:42 -06:00
|
|
|
let (db, file_id) = RootDatabase::with_single_file(&before);
|
2019-02-03 12:26:35 -06:00
|
|
|
let frange = FileRange { file_id, range };
|
|
|
|
let assist =
|
|
|
|
AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable");
|
2019-10-27 10:22:14 -05:00
|
|
|
let action = match assist {
|
|
|
|
Assist::Unresolved { .. } => unreachable!(),
|
2020-01-11 16:40:36 -06:00
|
|
|
Assist::Resolved { assist } => assist.get_first_action(),
|
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);
|
|
|
|
}
|
|
|
|
|
2019-10-27 10:22:14 -05:00
|
|
|
pub(crate) fn check_assist_target(
|
2020-02-06 09:58:57 -06:00
|
|
|
assist: fn(AssistCtx) -> Option<Assist>,
|
2019-02-08 17:34:05 -06:00
|
|
|
before: &str,
|
|
|
|
target: &str,
|
|
|
|
) {
|
|
|
|
let (before_cursor_pos, before) = extract_offset(before);
|
2020-02-06 09:53:42 -06:00
|
|
|
let (db, file_id) = RootDatabase::with_single_file(&before);
|
2019-02-08 17:34:05 -06:00
|
|
|
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");
|
2019-10-27 10:22:14 -05:00
|
|
|
let action = match assist {
|
|
|
|
Assist::Unresolved { .. } => unreachable!(),
|
2020-01-11 16:40:36 -06:00
|
|
|
Assist::Resolved { assist } => assist.get_first_action(),
|
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-10-27 10:22:14 -05:00
|
|
|
pub(crate) fn check_assist_range_target(
|
2020-02-06 09:58:57 -06:00
|
|
|
assist: fn(AssistCtx) -> Option<Assist>,
|
2019-02-08 17:34:05 -06:00
|
|
|
before: &str,
|
|
|
|
target: &str,
|
|
|
|
) {
|
|
|
|
let (range, before) = extract_range(before);
|
2020-02-06 09:53:42 -06:00
|
|
|
let (db, file_id) = RootDatabase::with_single_file(&before);
|
2019-02-08 17:34:05 -06:00
|
|
|
let frange = FileRange { file_id, range };
|
|
|
|
let assist =
|
|
|
|
AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable");
|
2019-10-27 10:22:14 -05:00
|
|
|
let action = match assist {
|
|
|
|
Assist::Unresolved { .. } => unreachable!(),
|
2020-01-11 16:40:36 -06:00
|
|
|
Assist::Resolved { assist } => assist.get_first_action(),
|
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(
|
2020-02-06 09:58:57 -06:00
|
|
|
assist: fn(AssistCtx) -> Option<Assist>,
|
2019-02-03 12:26:35 -06:00
|
|
|
before: &str,
|
|
|
|
) {
|
|
|
|
let (before_cursor_pos, before) = extract_offset(before);
|
2020-02-06 09:53:42 -06:00
|
|
|
let (db, file_id) = RootDatabase::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());
|
|
|
|
}
|
2019-02-24 05:18:10 -06:00
|
|
|
|
2020-01-23 13:15:16 -06:00
|
|
|
pub(crate) fn check_assist_with_imports_locator_not_applicable<F: ImportsLocator>(
|
2020-02-06 09:58:57 -06:00
|
|
|
assist: fn(AssistCtx, &mut F) -> Option<Assist>,
|
2020-02-06 09:53:42 -06:00
|
|
|
imports_locator_provider: fn(db: Arc<RootDatabase>, file_id: FileId) -> F,
|
2019-12-23 18:19:09 -06:00
|
|
|
before: &str,
|
|
|
|
) {
|
|
|
|
let (before_cursor_pos, before) = extract_offset(before);
|
2020-02-06 09:53:42 -06:00
|
|
|
let (db, file_id) = RootDatabase::with_single_file(&before);
|
2020-01-26 16:16:18 -06:00
|
|
|
let db = Arc::new(db);
|
|
|
|
let mut imports_locator = imports_locator_provider(Arc::clone(&db), file_id);
|
2019-12-23 18:19:09 -06:00
|
|
|
let frange =
|
|
|
|
FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) };
|
2020-01-26 16:16:18 -06:00
|
|
|
let assist =
|
|
|
|
AssistCtx::with_ctx(db.as_ref(), frange, true, |ctx| assist(ctx, &mut imports_locator));
|
2019-12-23 18:19:09 -06:00
|
|
|
assert!(assist.is_none());
|
|
|
|
}
|
|
|
|
|
2019-02-24 05:18:10 -06:00
|
|
|
pub(crate) fn check_assist_range_not_applicable(
|
2020-02-06 09:58:57 -06:00
|
|
|
assist: fn(AssistCtx) -> Option<Assist>,
|
2019-02-24 05:18:10 -06:00
|
|
|
before: &str,
|
|
|
|
) {
|
|
|
|
let (range, before) = extract_range(before);
|
2020-02-06 09:53:42 -06:00
|
|
|
let (db, file_id) = RootDatabase::with_single_file(&before);
|
2019-02-24 05:18:10 -06:00
|
|
|
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 {
|
2019-11-04 13:12:49 -06:00
|
|
|
use ra_db::{fixture::WithFixture, FileRange};
|
2019-07-04 15:05:17 -05:00
|
|
|
use ra_syntax::TextRange;
|
2019-02-24 05:18:10 -06:00
|
|
|
use test_utils::{extract_offset, extract_range};
|
2019-02-08 15:43:13 -06:00
|
|
|
|
2020-02-06 09:53:42 -06:00
|
|
|
use ra_ide_db::RootDatabase;
|
2019-11-04 13:28:47 -06:00
|
|
|
|
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);
|
2020-02-06 09:53:42 -06:00
|
|
|
let (db, file_id) = RootDatabase::with_single_file(&before);
|
2019-02-08 15:43:13 -06:00
|
|
|
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();
|
|
|
|
|
2020-01-14 11:32:26 -06:00
|
|
|
assert_eq!(
|
2020-01-11 16:40:36 -06:00
|
|
|
assists.next().expect("expected assist").label.label,
|
2020-01-14 11:32:26 -06:00
|
|
|
"Change visibility to pub(crate)"
|
|
|
|
);
|
2020-01-11 16:40:36 -06:00
|
|
|
assert_eq!(assists.next().expect("expected assist").label.label, "Add `#[derive]`");
|
2019-02-08 15:43:13 -06:00
|
|
|
}
|
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 {
|
2019-02-24 05:18:10 -06:00
|
|
|
if let 2..6 = <|>5<|> {
|
2019-02-08 17:34:05 -06:00
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}";
|
2019-02-24 05:18:10 -06:00
|
|
|
let (range, before) = extract_range(before);
|
2020-02-06 09:53:42 -06:00
|
|
|
let (db, file_id) = RootDatabase::with_single_file(&before);
|
2019-02-24 05:18:10 -06:00
|
|
|
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();
|
|
|
|
|
2020-01-11 16:40:36 -06:00
|
|
|
assert_eq!(assists.next().expect("expected assist").label.label, "Extract into variable");
|
|
|
|
assert_eq!(assists.next().expect("expected assist").label.label, "Replace with match");
|
2019-02-08 17:34:05 -06:00
|
|
|
}
|
2019-02-03 12:26:35 -06:00
|
|
|
}
|