mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-12 21:28:51 +00:00
Name assist handlers
This commit is contained in:
parent
aa64a84b49
commit
561b4b11ff
29 changed files with 19 additions and 35 deletions
|
@ -19,6 +19,8 @@ pub(crate) enum Assist {
|
||||||
Resolved { assist: ResolvedAssist },
|
Resolved { assist: ResolvedAssist },
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) type AssistHandler = fn(AssistCtx) -> Option<Assist>;
|
||||||
|
|
||||||
/// `AssistCtx` allows to apply an assist or check if it could be applied.
|
/// `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
|
/// Assists use a somewhat over-engineered approach, given the current needs. The
|
||||||
|
|
|
@ -10,7 +10,7 @@ use ra_syntax::{
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
assist_ctx::{Assist, AssistCtx},
|
assist_ctx::{Assist, AssistCtx},
|
||||||
assists::invert_if::invert_boolean_expression,
|
handlers::invert_if::invert_boolean_expression,
|
||||||
AssistId,
|
AssistId,
|
||||||
};
|
};
|
||||||
|
|
|
@ -19,8 +19,8 @@ use ra_ide_db::RootDatabase;
|
||||||
use ra_syntax::{TextRange, TextUnit};
|
use ra_syntax::{TextRange, TextUnit};
|
||||||
use ra_text_edit::TextEdit;
|
use ra_text_edit::TextEdit;
|
||||||
|
|
||||||
pub(crate) use crate::assist_ctx::{Assist, AssistCtx};
|
pub(crate) use crate::assist_ctx::{Assist, AssistCtx, AssistHandler};
|
||||||
pub use crate::assists::add_import::auto_import_text_edit;
|
pub use crate::handlers::add_import::auto_import_text_edit;
|
||||||
|
|
||||||
/// Unique identifier of the assist, should not be shown to the user
|
/// Unique identifier of the assist, should not be shown to the user
|
||||||
/// directly.
|
/// directly.
|
||||||
|
@ -72,7 +72,7 @@ impl ResolvedAssist {
|
||||||
/// returned, without actual edits.
|
/// returned, without actual edits.
|
||||||
pub fn unresolved_assists(db: &RootDatabase, range: FileRange) -> Vec<AssistLabel> {
|
pub fn unresolved_assists(db: &RootDatabase, range: FileRange) -> Vec<AssistLabel> {
|
||||||
let ctx = AssistCtx::new(db, range, false);
|
let ctx = AssistCtx::new(db, range, false);
|
||||||
assists::all()
|
handlers::all()
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|f| f(ctx.clone()))
|
.filter_map(|f| f(ctx.clone()))
|
||||||
.map(|a| match a {
|
.map(|a| match a {
|
||||||
|
@ -88,7 +88,7 @@ pub fn unresolved_assists(db: &RootDatabase, range: FileRange) -> Vec<AssistLabe
|
||||||
/// computed.
|
/// computed.
|
||||||
pub fn resolved_assists(db: &RootDatabase, range: FileRange) -> Vec<ResolvedAssist> {
|
pub fn resolved_assists(db: &RootDatabase, range: FileRange) -> Vec<ResolvedAssist> {
|
||||||
let ctx = AssistCtx::new(db, range, true);
|
let ctx = AssistCtx::new(db, range, true);
|
||||||
let mut a = assists::all()
|
let mut a = handlers::all()
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|f| f(ctx.clone()))
|
.filter_map(|f| f(ctx.clone()))
|
||||||
.map(|a| match a {
|
.map(|a| match a {
|
||||||
|
@ -109,8 +109,8 @@ fn sort_assists(assists: &mut [ResolvedAssist]) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
mod assists {
|
mod handlers {
|
||||||
use crate::{Assist, AssistCtx};
|
use crate::AssistHandler;
|
||||||
|
|
||||||
mod add_derive;
|
mod add_derive;
|
||||||
mod add_explicit_type;
|
mod add_explicit_type;
|
||||||
|
@ -138,7 +138,7 @@ mod assists {
|
||||||
mod move_bounds;
|
mod move_bounds;
|
||||||
mod early_return;
|
mod early_return;
|
||||||
|
|
||||||
pub(crate) fn all() -> &'static [fn(AssistCtx) -> Option<Assist>] {
|
pub(crate) fn all() -> &'static [AssistHandler] {
|
||||||
&[
|
&[
|
||||||
add_derive::add_derive,
|
add_derive::add_derive,
|
||||||
add_explicit_type::add_explicit_type,
|
add_explicit_type::add_explicit_type,
|
||||||
|
@ -183,7 +183,7 @@ mod helpers {
|
||||||
use ra_syntax::TextRange;
|
use ra_syntax::TextRange;
|
||||||
use test_utils::{add_cursor, assert_eq_text, extract_offset, extract_range};
|
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) {
|
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);
|
||||||
|
@ -194,7 +194,7 @@ mod helpers {
|
||||||
(db, file_id)
|
(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 (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 =
|
let frange =
|
||||||
|
@ -218,11 +218,7 @@ mod helpers {
|
||||||
assert_eq_text!(after, &actual);
|
assert_eq_text!(after, &actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn check_assist_range(
|
pub(crate) fn check_assist_range(assist: AssistHandler, before: &str, after: &str) {
|
||||||
assist: fn(AssistCtx) -> Option<Assist>,
|
|
||||||
before: &str,
|
|
||||||
after: &str,
|
|
||||||
) {
|
|
||||||
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 };
|
||||||
|
@ -240,11 +236,7 @@ mod helpers {
|
||||||
assert_eq_text!(after, &actual);
|
assert_eq_text!(after, &actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn check_assist_target(
|
pub(crate) fn check_assist_target(assist: AssistHandler, before: &str, target: &str) {
|
||||||
assist: fn(AssistCtx) -> Option<Assist>,
|
|
||||||
before: &str,
|
|
||||||
target: &str,
|
|
||||||
) {
|
|
||||||
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 =
|
let frange =
|
||||||
|
@ -260,11 +252,7 @@ mod helpers {
|
||||||
assert_eq_text!(&before[range.start().to_usize()..range.end().to_usize()], target);
|
assert_eq_text!(&before[range.start().to_usize()..range.end().to_usize()], target);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn check_assist_range_target(
|
pub(crate) fn check_assist_range_target(assist: AssistHandler, before: &str, target: &str) {
|
||||||
assist: fn(AssistCtx) -> Option<Assist>,
|
|
||||||
before: &str,
|
|
||||||
target: &str,
|
|
||||||
) {
|
|
||||||
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 };
|
||||||
|
@ -279,10 +267,7 @@ mod helpers {
|
||||||
assert_eq_text!(&before[range.start().to_usize()..range.end().to_usize()], target);
|
assert_eq_text!(&before[range.start().to_usize()..range.end().to_usize()], target);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn check_assist_not_applicable(
|
pub(crate) fn check_assist_not_applicable(assist: AssistHandler, before: &str) {
|
||||||
assist: fn(AssistCtx) -> Option<Assist>,
|
|
||||||
before: &str,
|
|
||||||
) {
|
|
||||||
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 =
|
let frange =
|
||||||
|
@ -291,10 +276,7 @@ mod helpers {
|
||||||
assert!(assist.is_none());
|
assert!(assist.is_none());
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn check_assist_range_not_applicable(
|
pub(crate) fn check_assist_range_not_applicable(assist: AssistHandler, before: &str) {
|
||||||
assist: fn(AssistCtx) -> Option<Assist>,
|
|
||||||
before: &str,
|
|
||||||
) {
|
|
||||||
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 };
|
||||||
|
|
|
@ -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 SYNTAX_KINDS: &str = "crates/ra_parser/src/syntax_kind/generated.rs";
|
||||||
pub const AST: &str = "crates/ra_syntax/src/ast/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_TESTS: &str = "crates/ra_assists/src/doc_tests/generated.rs";
|
||||||
const ASSISTS_DOCS: &str = "docs/user/assists.md";
|
const ASSISTS_DOCS: &str = "docs/user/assists.md";
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,7 @@ use xtask::project_root;
|
||||||
fn is_exclude_dir(p: &Path) -> bool {
|
fn is_exclude_dir(p: &Path) -> bool {
|
||||||
// Test hopefully don't really need comments, and for assists we already
|
// Test hopefully don't really need comments, and for assists we already
|
||||||
// have special comments which are source of doc tests and user docs.
|
// 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;
|
let mut cur_path = p;
|
||||||
while let Some(path) = cur_path.parent() {
|
while let Some(path) = cur_path.parent() {
|
||||||
if exclude_dirs.iter().any(|dir| path.ends_with(dir)) {
|
if exclude_dirs.iter().any(|dir| path.ends_with(dir)) {
|
||||||
|
|
Loading…
Reference in a new issue