internal: prepare to move assist definitions

This commit is contained in:
Aleksey Kladov 2021-06-14 13:18:03 +03:00
parent 1d2772c2c7
commit 2e8dab631b
4 changed files with 30 additions and 33 deletions

View file

@ -535,7 +535,7 @@ impl Analysis {
) -> Cancellable<Vec<Assist>> { ) -> Cancellable<Vec<Assist>> {
self.with_db(|db| { self.with_db(|db| {
let ssr_assists = ssr::ssr_assists(db, &resolve, frange); let ssr_assists = ssr::ssr_assists(db, &resolve, frange);
let mut acc = Assist::get(db, config, resolve, frange); let mut acc = ide_assists::assists(db, config, resolve, frange);
acc.extend(ssr_assists.into_iter()); acc.extend(ssr_assists.into_iter());
acc acc
}) })
@ -576,7 +576,7 @@ impl Analysis {
Vec::new() Vec::new()
}; };
let mut res = Assist::get(db, assist_config, resolve, frange); let mut res = ide_assists::assists(db, assist_config, resolve, frange);
res.extend(ssr_assists.into_iter()); res.extend(ssr_assists.into_iter());
res.extend(diagnostic_assists.into_iter()); res.extend(diagnostic_assists.into_iter());

View file

@ -151,22 +151,20 @@ pub struct Assist {
pub source_change: Option<SourceChange>, pub source_change: Option<SourceChange>,
} }
impl Assist { /// Return all the assists applicable at the given position.
/// Return all the assists applicable at the given position. pub fn assists(
pub fn get( db: &RootDatabase,
db: &RootDatabase, config: &AssistConfig,
config: &AssistConfig, resolve: AssistResolveStrategy,
resolve: AssistResolveStrategy, range: FileRange,
range: FileRange, ) -> Vec<Assist> {
) -> Vec<Assist> { let sema = Semantics::new(db);
let sema = Semantics::new(db); let ctx = AssistContext::new(sema, config, range);
let ctx = AssistContext::new(sema, config, range); let mut acc = Assists::new(&ctx, resolve);
let mut acc = Assists::new(&ctx, resolve); handlers::all().iter().for_each(|handler| {
handlers::all().iter().for_each(|handler| { handler(&mut acc, &ctx);
handler(&mut acc, &ctx); });
}); acc.finish()
acc.finish()
}
} }
mod handlers { mod handlers {

View file

@ -16,8 +16,8 @@ use syntax::TextRange;
use test_utils::{assert_eq_text, extract_offset}; use test_utils::{assert_eq_text, extract_offset};
use crate::{ use crate::{
handlers::Handler, Assist, AssistConfig, AssistContext, AssistKind, AssistResolveStrategy, assists, handlers::Handler, Assist, AssistConfig, AssistContext, AssistKind,
Assists, SingleResolve, AssistResolveStrategy, Assists, SingleResolve,
}; };
pub(crate) const TEST_CONFIG: AssistConfig = AssistConfig { pub(crate) const TEST_CONFIG: AssistConfig = AssistConfig {
@ -78,14 +78,14 @@ fn check_doc_test(assist_id: &str, before: &str, after: &str) {
let before = db.file_text(file_id).to_string(); let before = db.file_text(file_id).to_string();
let frange = FileRange { file_id, range: selection.into() }; let frange = FileRange { file_id, range: selection.into() };
let assist = Assist::get(&db, &TEST_CONFIG, AssistResolveStrategy::All, frange) let assist = assists(&db, &TEST_CONFIG, AssistResolveStrategy::All, frange)
.into_iter() .into_iter()
.find(|assist| assist.id.0 == assist_id) .find(|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,
Assist::get(&db, &TEST_CONFIG, AssistResolveStrategy::None, frange) assists(&db, &TEST_CONFIG, AssistResolveStrategy::None, frange)
.into_iter() .into_iter()
.map(|assist| assist.id.0) .map(|assist| assist.id.0)
.collect::<Vec<_>>() .collect::<Vec<_>>()
@ -210,7 +210,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 = Assist::get(&db, &TEST_CONFIG, AssistResolveStrategy::None, frange); let assists = assists(&db, &TEST_CONFIG, AssistResolveStrategy::None, frange);
let mut assists = assists.iter(); let mut assists = assists.iter();
assert_eq!(assists.next().expect("expected assist").label, "Change visibility to pub(crate)"); assert_eq!(assists.next().expect("expected assist").label, "Change visibility to pub(crate)");
@ -235,7 +235,7 @@ pub fn test_some_range(a: int) -> bool {
"#, "#,
); );
let assists = Assist::get(&db, &TEST_CONFIG, AssistResolveStrategy::None, frange); let assists = assists(&db, &TEST_CONFIG, AssistResolveStrategy::None, frange);
let expected = labels(&assists); let expected = labels(&assists);
expect![[r#" expect![[r#"
@ -264,7 +264,7 @@ pub fn test_some_range(a: int) -> bool {
let mut cfg = TEST_CONFIG; let mut cfg = TEST_CONFIG;
cfg.allowed = Some(vec![AssistKind::Refactor]); cfg.allowed = Some(vec![AssistKind::Refactor]);
let assists = Assist::get(&db, &cfg, AssistResolveStrategy::None, frange); let assists = assists(&db, &cfg, AssistResolveStrategy::None, frange);
let expected = labels(&assists); let expected = labels(&assists);
expect![[r#" expect![[r#"
@ -279,7 +279,7 @@ pub fn test_some_range(a: int) -> bool {
{ {
let mut cfg = TEST_CONFIG; let mut cfg = TEST_CONFIG;
cfg.allowed = Some(vec![AssistKind::RefactorExtract]); cfg.allowed = Some(vec![AssistKind::RefactorExtract]);
let assists = Assist::get(&db, &cfg, AssistResolveStrategy::None, frange); let assists = assists(&db, &cfg, AssistResolveStrategy::None, frange);
let expected = labels(&assists); let expected = labels(&assists);
expect![[r#" expect![[r#"
@ -292,7 +292,7 @@ pub fn test_some_range(a: int) -> bool {
{ {
let mut cfg = TEST_CONFIG; let mut cfg = TEST_CONFIG;
cfg.allowed = Some(vec![AssistKind::QuickFix]); cfg.allowed = Some(vec![AssistKind::QuickFix]);
let assists = Assist::get(&db, &cfg, AssistResolveStrategy::None, frange); let assists = assists(&db, &cfg, AssistResolveStrategy::None, frange);
let expected = labels(&assists); let expected = labels(&assists);
expect![[r#""#]].assert_eq(&expected); expect![[r#""#]].assert_eq(&expected);
@ -317,7 +317,7 @@ pub fn test_some_range(a: int) -> bool {
cfg.allowed = Some(vec![AssistKind::RefactorExtract]); cfg.allowed = Some(vec![AssistKind::RefactorExtract]);
{ {
let assists = Assist::get(&db, &cfg, AssistResolveStrategy::None, frange); let assists = assists(&db, &cfg, AssistResolveStrategy::None, frange);
assert_eq!(2, assists.len()); assert_eq!(2, assists.len());
let mut assists = assists.into_iter(); let mut assists = assists.into_iter();
@ -353,7 +353,7 @@ pub fn test_some_range(a: int) -> bool {
} }
{ {
let assists = Assist::get( let assists = assists(
&db, &db,
&cfg, &cfg,
AssistResolveStrategy::Single(SingleResolve { AssistResolveStrategy::Single(SingleResolve {
@ -397,7 +397,7 @@ pub fn test_some_range(a: int) -> bool {
} }
{ {
let assists = Assist::get( let assists = assists(
&db, &db,
&cfg, &cfg,
AssistResolveStrategy::Single(SingleResolve { AssistResolveStrategy::Single(SingleResolve {
@ -462,7 +462,7 @@ pub fn test_some_range(a: int) -> bool {
} }
{ {
let assists = Assist::get(&db, &cfg, AssistResolveStrategy::All, frange); let assists = assists(&db, &cfg, AssistResolveStrategy::All, frange);
assert_eq!(2, assists.len()); assert_eq!(2, assists.len());
let mut assists = assists.into_iter(); let mut assists = assists.into_iter();

View file

@ -27,7 +27,6 @@ mod unresolved_proc_macro;
mod field_shorthand; mod field_shorthand;
use hir::{diagnostics::AnyDiagnostic, Semantics}; use hir::{diagnostics::AnyDiagnostic, Semantics};
use ide_assists::AssistResolveStrategy;
use ide_db::{ use ide_db::{
base_db::{FileId, SourceDatabase}, base_db::{FileId, SourceDatabase},
label::Label, label::Label,
@ -43,7 +42,7 @@ use syntax::{
use text_edit::TextEdit; use text_edit::TextEdit;
use unlinked_file::UnlinkedFile; use unlinked_file::UnlinkedFile;
use ide_assists::{Assist, AssistId, AssistKind}; use ide_assists::{Assist, AssistId, AssistKind, AssistResolveStrategy};
#[derive(Copy, Clone, Debug, PartialEq)] #[derive(Copy, Clone, Debug, PartialEq)]
pub struct DiagnosticCode(pub &'static str); pub struct DiagnosticCode(pub &'static str);