Name assist handlers

This commit is contained in:
Aleksey Kladov 2020-02-07 15:53:31 +01:00
parent aa64a84b49
commit 561b4b11ff
29 changed files with 19 additions and 35 deletions

View file

@ -19,6 +19,8 @@ pub(crate) enum Assist {
Resolved { assist: ResolvedAssist },
}
pub(crate) type AssistHandler = fn(AssistCtx) -> Option<Assist>;
/// `AssistCtx` allows to apply an assist or check if it could be applied.
///
/// Assists use a somewhat over-engineered approach, given the current needs. The

View file

@ -10,7 +10,7 @@ use ra_syntax::{
use crate::{
assist_ctx::{Assist, AssistCtx},
assists::invert_if::invert_boolean_expression,
handlers::invert_if::invert_boolean_expression,
AssistId,
};

View file

@ -19,8 +19,8 @@ use ra_ide_db::RootDatabase;
use ra_syntax::{TextRange, TextUnit};
use ra_text_edit::TextEdit;
pub(crate) use crate::assist_ctx::{Assist, AssistCtx};
pub use crate::assists::add_import::auto_import_text_edit;
pub(crate) use crate::assist_ctx::{Assist, AssistCtx, AssistHandler};
pub use crate::handlers::add_import::auto_import_text_edit;
/// Unique identifier of the assist, should not be shown to the user
/// directly.
@ -72,7 +72,7 @@ impl ResolvedAssist {
/// returned, without actual edits.
pub fn unresolved_assists(db: &RootDatabase, range: FileRange) -> Vec<AssistLabel> {
let ctx = AssistCtx::new(db, range, false);
assists::all()
handlers::all()
.iter()
.filter_map(|f| f(ctx.clone()))
.map(|a| match a {
@ -88,7 +88,7 @@ pub fn unresolved_assists(db: &RootDatabase, range: FileRange) -> Vec<AssistLabe
/// computed.
pub fn resolved_assists(db: &RootDatabase, range: FileRange) -> Vec<ResolvedAssist> {
let ctx = AssistCtx::new(db, range, true);
let mut a = assists::all()
let mut a = handlers::all()
.iter()
.filter_map(|f| f(ctx.clone()))
.map(|a| match a {
@ -109,8 +109,8 @@ fn sort_assists(assists: &mut [ResolvedAssist]) {
});
}
mod assists {
use crate::{Assist, AssistCtx};
mod handlers {
use crate::AssistHandler;
mod add_derive;
mod add_explicit_type;
@ -138,7 +138,7 @@ mod assists {
mod move_bounds;
mod early_return;
pub(crate) fn all() -> &'static [fn(AssistCtx) -> Option<Assist>] {
pub(crate) fn all() -> &'static [AssistHandler] {
&[
add_derive::add_derive,
add_explicit_type::add_explicit_type,
@ -183,7 +183,7 @@ mod helpers {
use ra_syntax::TextRange;
use test_utils::{add_cursor, assert_eq_text, extract_offset, extract_range};
use crate::{Assist, AssistCtx};
use crate::{Assist, AssistCtx, AssistHandler};
pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) {
let (mut db, file_id) = RootDatabase::with_single_file(text);
@ -194,7 +194,7 @@ mod helpers {
(db, file_id)
}
pub(crate) fn check_assist(assist: fn(AssistCtx) -> Option<Assist>, before: &str, after: &str) {
pub(crate) fn check_assist(assist: AssistHandler, before: &str, after: &str) {
let (before_cursor_pos, before) = extract_offset(before);
let (db, file_id) = with_single_file(&before);
let frange =
@ -218,11 +218,7 @@ mod helpers {
assert_eq_text!(after, &actual);
}
pub(crate) fn check_assist_range(
assist: fn(AssistCtx) -> Option<Assist>,
before: &str,
after: &str,
) {
pub(crate) fn check_assist_range(assist: AssistHandler, before: &str, after: &str) {
let (range, before) = extract_range(before);
let (db, file_id) = with_single_file(&before);
let frange = FileRange { file_id, range };
@ -240,11 +236,7 @@ mod helpers {
assert_eq_text!(after, &actual);
}
pub(crate) fn check_assist_target(
assist: fn(AssistCtx) -> Option<Assist>,
before: &str,
target: &str,
) {
pub(crate) fn check_assist_target(assist: AssistHandler, before: &str, target: &str) {
let (before_cursor_pos, before) = extract_offset(before);
let (db, file_id) = with_single_file(&before);
let frange =
@ -260,11 +252,7 @@ mod helpers {
assert_eq_text!(&before[range.start().to_usize()..range.end().to_usize()], target);
}
pub(crate) fn check_assist_range_target(
assist: fn(AssistCtx) -> Option<Assist>,
before: &str,
target: &str,
) {
pub(crate) fn check_assist_range_target(assist: AssistHandler, before: &str, target: &str) {
let (range, before) = extract_range(before);
let (db, file_id) = with_single_file(&before);
let frange = FileRange { file_id, range };
@ -279,10 +267,7 @@ mod helpers {
assert_eq_text!(&before[range.start().to_usize()..range.end().to_usize()], target);
}
pub(crate) fn check_assist_not_applicable(
assist: fn(AssistCtx) -> Option<Assist>,
before: &str,
) {
pub(crate) fn check_assist_not_applicable(assist: AssistHandler, before: &str) {
let (before_cursor_pos, before) = extract_offset(before);
let (db, file_id) = with_single_file(&before);
let frange =
@ -291,10 +276,7 @@ mod helpers {
assert!(assist.is_none());
}
pub(crate) fn check_assist_range_not_applicable(
assist: fn(AssistCtx) -> Option<Assist>,
before: &str,
) {
pub(crate) fn check_assist_range_not_applicable(assist: AssistHandler, before: &str) {
let (range, before) = extract_range(before);
let (db, file_id) = with_single_file(&before);
let frange = FileRange { file_id, range };

View file

@ -25,7 +25,7 @@ const ERR_INLINE_TESTS_DIR: &str = "crates/ra_syntax/test_data/parser/inline/err
pub const SYNTAX_KINDS: &str = "crates/ra_parser/src/syntax_kind/generated.rs";
pub const AST: &str = "crates/ra_syntax/src/ast/generated.rs";
const ASSISTS_DIR: &str = "crates/ra_assists/src/assists";
const ASSISTS_DIR: &str = "crates/ra_assists/src/handlers";
const ASSISTS_TESTS: &str = "crates/ra_assists/src/doc_tests/generated.rs";
const ASSISTS_DOCS: &str = "docs/user/assists.md";

View file

@ -6,7 +6,7 @@ use xtask::project_root;
fn is_exclude_dir(p: &Path) -> bool {
// Test hopefully don't really need comments, and for assists we already
// have special comments which are source of doc tests and user docs.
let exclude_dirs = ["tests", "test_data", "assists"];
let exclude_dirs = ["tests", "test_data", "handlers"];
let mut cur_path = p;
while let Some(path) = cur_path.parent() {
if exclude_dirs.iter().any(|dir| path.ends_with(dir)) {