From f8a056117898c56d34d1758455bc54df50e2e426 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 6 Jan 2021 20:43:46 +0300 Subject: Align config's API with usage The config now is mostly immutable, optimize for that. --- crates/assists/src/assist_config.rs | 32 ++------------------ crates/assists/src/assist_context.rs | 10 +++---- crates/assists/src/handlers/generate_function.rs | 3 +- crates/assists/src/lib.rs | 2 +- crates/assists/src/tests.rs | 38 ++++++++++++++++-------- crates/assists/src/utils.rs | 7 ++--- 6 files changed, 37 insertions(+), 55 deletions(-) (limited to 'crates/assists') diff --git a/crates/assists/src/assist_config.rs b/crates/assists/src/assist_config.rs index c458d9054..4fe8ea761 100644 --- a/crates/assists/src/assist_config.rs +++ b/crates/assists/src/assist_config.rs @@ -4,8 +4,7 @@ //! module, and we use to statically check that we only produce snippet //! assists if we are allowed to. -use hir::PrefixKind; -use ide_db::helpers::insert_use::MergeBehavior; +use ide_db::helpers::{insert_use::MergeBehavior, SnippetCap}; use crate::AssistKind; @@ -16,35 +15,8 @@ pub struct AssistConfig { pub insert_use: InsertUseConfig, } -impl AssistConfig { - pub fn allow_snippets(&mut self, yes: bool) { - self.snippet_cap = if yes { Some(SnippetCap { _private: () }) } else { None } - } -} - -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub struct SnippetCap { - _private: (), -} - -impl Default for AssistConfig { - fn default() -> Self { - AssistConfig { - snippet_cap: Some(SnippetCap { _private: () }), - allowed: None, - insert_use: InsertUseConfig::default(), - } - } -} - #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct InsertUseConfig { pub merge: Option, - pub prefix_kind: PrefixKind, -} - -impl Default for InsertUseConfig { - fn default() -> Self { - InsertUseConfig { merge: Some(MergeBehavior::Full), prefix_kind: PrefixKind::Plain } - } + pub prefix_kind: hir::PrefixKind, } diff --git a/crates/assists/src/assist_context.rs b/crates/assists/src/assist_context.rs index 4f59d39a9..91cc63427 100644 --- a/crates/assists/src/assist_context.rs +++ b/crates/assists/src/assist_context.rs @@ -4,7 +4,10 @@ use std::mem; use algo::find_covering_element; use hir::Semantics; -use ide_db::base_db::{AnchoredPathBuf, FileId, FileRange}; +use ide_db::{ + base_db::{AnchoredPathBuf, FileId, FileRange}, + helpers::SnippetCap, +}; use ide_db::{ label::Label, source_change::{FileSystemEdit, SourceChange, SourceFileEdit}, @@ -17,10 +20,7 @@ use syntax::{ }; use text_edit::{TextEdit, TextEditBuilder}; -use crate::{ - assist_config::{AssistConfig, SnippetCap}, - Assist, AssistId, AssistKind, GroupLabel, -}; +use crate::{assist_config::AssistConfig, Assist, AssistId, AssistKind, GroupLabel}; /// `AssistContext` allows to apply an assist or check if it could be applied. /// diff --git a/crates/assists/src/handlers/generate_function.rs b/crates/assists/src/handlers/generate_function.rs index f4cf155b6..d169abedb 100644 --- a/crates/assists/src/handlers/generate_function.rs +++ b/crates/assists/src/handlers/generate_function.rs @@ -1,5 +1,5 @@ use hir::HirDisplay; -use ide_db::base_db::FileId; +use ide_db::{base_db::FileId, helpers::SnippetCap}; use rustc_hash::{FxHashMap, FxHashSet}; use syntax::{ ast::{ @@ -11,7 +11,6 @@ use syntax::{ }; use crate::{ - assist_config::SnippetCap, utils::{render_snippet, Cursor}, AssistContext, AssistId, AssistKind, Assists, }; diff --git a/crates/assists/src/lib.rs b/crates/assists/src/lib.rs index 5e3a1b368..90009c55a 100644 --- a/crates/assists/src/lib.rs +++ b/crates/assists/src/lib.rs @@ -24,7 +24,7 @@ use syntax::TextRange; pub(crate) use crate::assist_context::{AssistContext, Assists}; -pub use assist_config::AssistConfig; +pub use assist_config::{AssistConfig, InsertUseConfig}; #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum AssistKind { diff --git a/crates/assists/src/tests.rs b/crates/assists/src/tests.rs index 21e448fb8..a4c6a7570 100644 --- a/crates/assists/src/tests.rs +++ b/crates/assists/src/tests.rs @@ -1,15 +1,29 @@ mod generated; use hir::Semantics; -use ide_db::base_db::{fixture::WithFixture, FileId, FileRange, SourceDatabaseExt}; -use ide_db::source_change::FileSystemEdit; -use ide_db::RootDatabase; +use ide_db::{ + base_db::{fixture::WithFixture, FileId, FileRange, SourceDatabaseExt}, + helpers::{insert_use::MergeBehavior, SnippetCap}, + source_change::FileSystemEdit, + RootDatabase, +}; use syntax::TextRange; use test_utils::{assert_eq_text, extract_offset, extract_range}; -use crate::{handlers::Handler, Assist, AssistConfig, AssistContext, AssistKind, Assists}; +use crate::{ + handlers::Handler, Assist, AssistConfig, AssistContext, AssistKind, Assists, InsertUseConfig, +}; use stdx::{format_to, trim_indent}; +pub(crate) const TEST_CONFIG: AssistConfig = AssistConfig { + snippet_cap: SnippetCap::new(true), + allowed: None, + insert_use: InsertUseConfig { + merge: Some(MergeBehavior::Full), + prefix_kind: hir::PrefixKind::Plain, + }, +}; + pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) { RootDatabase::with_single_file(text) } @@ -48,14 +62,14 @@ fn check_doc_test(assist_id: &str, before: &str, after: &str) { let before = db.file_text(file_id).to_string(); let frange = FileRange { file_id, range: selection.into() }; - let assist = Assist::get(&db, &AssistConfig::default(), true, frange) + let assist = Assist::get(&db, &TEST_CONFIG, true, frange) .into_iter() .find(|assist| assist.id.0 == assist_id) .unwrap_or_else(|| { panic!( "\n\nAssist is not applicable: {}\nAvailable assists: {}", assist_id, - Assist::get(&db, &AssistConfig::default(), false, frange) + Assist::get(&db, &TEST_CONFIG, false, frange) .into_iter() .map(|assist| assist.id.0) .collect::>() @@ -89,7 +103,7 @@ fn check(handler: Handler, before: &str, expected: ExpectedResult, assist_label: let frange = FileRange { file_id: file_with_caret_id, range: range_or_offset.into() }; let sema = Semantics::new(&db); - let config = AssistConfig::default(); + let config = TEST_CONFIG; let ctx = AssistContext::new(sema, &config, frange); let mut acc = Assists::new(&ctx, true); handler(&mut acc, &ctx); @@ -156,7 +170,7 @@ fn assist_order_field_struct() { let (before_cursor_pos, before) = extract_offset(before); let (db, file_id) = with_single_file(&before); let frange = FileRange { file_id, range: TextRange::empty(before_cursor_pos) }; - let assists = Assist::get(&db, &AssistConfig::default(), false, frange); + let assists = Assist::get(&db, &TEST_CONFIG, false, frange); let mut assists = assists.iter(); assert_eq!(assists.next().expect("expected assist").label, "Change visibility to pub(crate)"); @@ -176,7 +190,7 @@ fn assist_order_if_expr() { let (range, before) = extract_range(before); let (db, file_id) = with_single_file(&before); let frange = FileRange { file_id, range }; - let assists = Assist::get(&db, &AssistConfig::default(), false, frange); + let assists = Assist::get(&db, &TEST_CONFIG, false, frange); let mut assists = assists.iter(); assert_eq!(assists.next().expect("expected assist").label, "Extract into variable"); @@ -198,7 +212,7 @@ fn assist_filter_works() { let frange = FileRange { file_id, range }; { - let mut cfg = AssistConfig::default(); + let mut cfg = TEST_CONFIG; cfg.allowed = Some(vec![AssistKind::Refactor]); let assists = Assist::get(&db, &cfg, false, frange); @@ -209,7 +223,7 @@ fn assist_filter_works() { } { - let mut cfg = AssistConfig::default(); + let mut cfg = TEST_CONFIG; cfg.allowed = Some(vec![AssistKind::RefactorExtract]); let assists = Assist::get(&db, &cfg, false, frange); assert_eq!(assists.len(), 1); @@ -219,7 +233,7 @@ fn assist_filter_works() { } { - let mut cfg = AssistConfig::default(); + let mut cfg = TEST_CONFIG; cfg.allowed = Some(vec![AssistKind::QuickFix]); let assists = Assist::get(&db, &cfg, false, frange); assert!(assists.is_empty(), "All asserts but quickfixes should be filtered out"); diff --git a/crates/assists/src/utils.rs b/crates/assists/src/utils.rs index 8212cd129..9ea96eb73 100644 --- a/crates/assists/src/utils.rs +++ b/crates/assists/src/utils.rs @@ -4,7 +4,7 @@ pub(crate) mod import_assets; use std::ops; use hir::HasSource; -use ide_db::RootDatabase; +use ide_db::{helpers::SnippetCap, RootDatabase}; use itertools::Itertools; use syntax::{ ast::edit::AstNodeEdit, @@ -16,10 +16,7 @@ use syntax::{ SyntaxNode, TextSize, T, }; -use crate::{ - assist_config::SnippetCap, - ast_transform::{self, AstTransform, QualifyPaths, SubstituteTypeParams}, -}; +use crate::ast_transform::{self, AstTransform, QualifyPaths, SubstituteTypeParams}; pub(crate) fn unwrap_trivial_block(block: ast::BlockExpr) -> ast::Expr { extract_trivial_expression(&block) -- cgit v1.2.3