diff options
author | Aleksey Kladov <[email protected]> | 2021-01-06 17:43:46 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2021-01-06 18:22:24 +0000 |
commit | f8a056117898c56d34d1758455bc54df50e2e426 (patch) | |
tree | 429e2c7284476213432b66b02ab9b76f6525eb92 /crates/assists | |
parent | 7ae4b8bdb62735ee767dff25ce1485ae8bffe199 (diff) |
Align config's API with usage
The config now is mostly immutable, optimize for that.
Diffstat (limited to 'crates/assists')
-rw-r--r-- | crates/assists/src/assist_config.rs | 32 | ||||
-rw-r--r-- | crates/assists/src/assist_context.rs | 10 | ||||
-rw-r--r-- | crates/assists/src/handlers/generate_function.rs | 3 | ||||
-rw-r--r-- | crates/assists/src/lib.rs | 2 | ||||
-rw-r--r-- | crates/assists/src/tests.rs | 38 | ||||
-rw-r--r-- | crates/assists/src/utils.rs | 7 |
6 files changed, 37 insertions, 55 deletions
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 @@ | |||
4 | //! module, and we use to statically check that we only produce snippet | 4 | //! module, and we use to statically check that we only produce snippet |
5 | //! assists if we are allowed to. | 5 | //! assists if we are allowed to. |
6 | 6 | ||
7 | use hir::PrefixKind; | 7 | use ide_db::helpers::{insert_use::MergeBehavior, SnippetCap}; |
8 | use ide_db::helpers::insert_use::MergeBehavior; | ||
9 | 8 | ||
10 | use crate::AssistKind; | 9 | use crate::AssistKind; |
11 | 10 | ||
@@ -16,35 +15,8 @@ pub struct AssistConfig { | |||
16 | pub insert_use: InsertUseConfig, | 15 | pub insert_use: InsertUseConfig, |
17 | } | 16 | } |
18 | 17 | ||
19 | impl AssistConfig { | ||
20 | pub fn allow_snippets(&mut self, yes: bool) { | ||
21 | self.snippet_cap = if yes { Some(SnippetCap { _private: () }) } else { None } | ||
22 | } | ||
23 | } | ||
24 | |||
25 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] | ||
26 | pub struct SnippetCap { | ||
27 | _private: (), | ||
28 | } | ||
29 | |||
30 | impl Default for AssistConfig { | ||
31 | fn default() -> Self { | ||
32 | AssistConfig { | ||
33 | snippet_cap: Some(SnippetCap { _private: () }), | ||
34 | allowed: None, | ||
35 | insert_use: InsertUseConfig::default(), | ||
36 | } | ||
37 | } | ||
38 | } | ||
39 | |||
40 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] | 18 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
41 | pub struct InsertUseConfig { | 19 | pub struct InsertUseConfig { |
42 | pub merge: Option<MergeBehavior>, | 20 | pub merge: Option<MergeBehavior>, |
43 | pub prefix_kind: PrefixKind, | 21 | pub prefix_kind: hir::PrefixKind, |
44 | } | ||
45 | |||
46 | impl Default for InsertUseConfig { | ||
47 | fn default() -> Self { | ||
48 | InsertUseConfig { merge: Some(MergeBehavior::Full), prefix_kind: PrefixKind::Plain } | ||
49 | } | ||
50 | } | 22 | } |
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; | |||
4 | 4 | ||
5 | use algo::find_covering_element; | 5 | use algo::find_covering_element; |
6 | use hir::Semantics; | 6 | use hir::Semantics; |
7 | use ide_db::base_db::{AnchoredPathBuf, FileId, FileRange}; | 7 | use ide_db::{ |
8 | base_db::{AnchoredPathBuf, FileId, FileRange}, | ||
9 | helpers::SnippetCap, | ||
10 | }; | ||
8 | use ide_db::{ | 11 | use ide_db::{ |
9 | label::Label, | 12 | label::Label, |
10 | source_change::{FileSystemEdit, SourceChange, SourceFileEdit}, | 13 | source_change::{FileSystemEdit, SourceChange, SourceFileEdit}, |
@@ -17,10 +20,7 @@ use syntax::{ | |||
17 | }; | 20 | }; |
18 | use text_edit::{TextEdit, TextEditBuilder}; | 21 | use text_edit::{TextEdit, TextEditBuilder}; |
19 | 22 | ||
20 | use crate::{ | 23 | use crate::{assist_config::AssistConfig, Assist, AssistId, AssistKind, GroupLabel}; |
21 | assist_config::{AssistConfig, SnippetCap}, | ||
22 | Assist, AssistId, AssistKind, GroupLabel, | ||
23 | }; | ||
24 | 24 | ||
25 | /// `AssistContext` allows to apply an assist or check if it could be applied. | 25 | /// `AssistContext` allows to apply an assist or check if it could be applied. |
26 | /// | 26 | /// |
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 @@ | |||
1 | use hir::HirDisplay; | 1 | use hir::HirDisplay; |
2 | use ide_db::base_db::FileId; | 2 | use ide_db::{base_db::FileId, helpers::SnippetCap}; |
3 | use rustc_hash::{FxHashMap, FxHashSet}; | 3 | use rustc_hash::{FxHashMap, FxHashSet}; |
4 | use syntax::{ | 4 | use syntax::{ |
5 | ast::{ | 5 | ast::{ |
@@ -11,7 +11,6 @@ use syntax::{ | |||
11 | }; | 11 | }; |
12 | 12 | ||
13 | use crate::{ | 13 | use crate::{ |
14 | assist_config::SnippetCap, | ||
15 | utils::{render_snippet, Cursor}, | 14 | utils::{render_snippet, Cursor}, |
16 | AssistContext, AssistId, AssistKind, Assists, | 15 | AssistContext, AssistId, AssistKind, Assists, |
17 | }; | 16 | }; |
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; | |||
24 | 24 | ||
25 | pub(crate) use crate::assist_context::{AssistContext, Assists}; | 25 | pub(crate) use crate::assist_context::{AssistContext, Assists}; |
26 | 26 | ||
27 | pub use assist_config::AssistConfig; | 27 | pub use assist_config::{AssistConfig, InsertUseConfig}; |
28 | 28 | ||
29 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | 29 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
30 | pub enum AssistKind { | 30 | 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 @@ | |||
1 | mod generated; | 1 | mod generated; |
2 | 2 | ||
3 | use hir::Semantics; | 3 | use hir::Semantics; |
4 | use ide_db::base_db::{fixture::WithFixture, FileId, FileRange, SourceDatabaseExt}; | 4 | use ide_db::{ |
5 | use ide_db::source_change::FileSystemEdit; | 5 | base_db::{fixture::WithFixture, FileId, FileRange, SourceDatabaseExt}, |
6 | use ide_db::RootDatabase; | 6 | helpers::{insert_use::MergeBehavior, SnippetCap}, |
7 | source_change::FileSystemEdit, | ||
8 | RootDatabase, | ||
9 | }; | ||
7 | use syntax::TextRange; | 10 | use syntax::TextRange; |
8 | use test_utils::{assert_eq_text, extract_offset, extract_range}; | 11 | use test_utils::{assert_eq_text, extract_offset, extract_range}; |
9 | 12 | ||
10 | use crate::{handlers::Handler, Assist, AssistConfig, AssistContext, AssistKind, Assists}; | 13 | use crate::{ |
14 | handlers::Handler, Assist, AssistConfig, AssistContext, AssistKind, Assists, InsertUseConfig, | ||
15 | }; | ||
11 | use stdx::{format_to, trim_indent}; | 16 | use stdx::{format_to, trim_indent}; |
12 | 17 | ||
18 | pub(crate) const TEST_CONFIG: AssistConfig = AssistConfig { | ||
19 | snippet_cap: SnippetCap::new(true), | ||
20 | allowed: None, | ||
21 | insert_use: InsertUseConfig { | ||
22 | merge: Some(MergeBehavior::Full), | ||
23 | prefix_kind: hir::PrefixKind::Plain, | ||
24 | }, | ||
25 | }; | ||
26 | |||
13 | pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) { | 27 | pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) { |
14 | RootDatabase::with_single_file(text) | 28 | RootDatabase::with_single_file(text) |
15 | } | 29 | } |
@@ -48,14 +62,14 @@ fn check_doc_test(assist_id: &str, before: &str, after: &str) { | |||
48 | let before = db.file_text(file_id).to_string(); | 62 | let before = db.file_text(file_id).to_string(); |
49 | let frange = FileRange { file_id, range: selection.into() }; | 63 | let frange = FileRange { file_id, range: selection.into() }; |
50 | 64 | ||
51 | let assist = Assist::get(&db, &AssistConfig::default(), true, frange) | 65 | let assist = Assist::get(&db, &TEST_CONFIG, true, frange) |
52 | .into_iter() | 66 | .into_iter() |
53 | .find(|assist| assist.id.0 == assist_id) | 67 | .find(|assist| assist.id.0 == assist_id) |
54 | .unwrap_or_else(|| { | 68 | .unwrap_or_else(|| { |
55 | panic!( | 69 | panic!( |
56 | "\n\nAssist is not applicable: {}\nAvailable assists: {}", | 70 | "\n\nAssist is not applicable: {}\nAvailable assists: {}", |
57 | assist_id, | 71 | assist_id, |
58 | Assist::get(&db, &AssistConfig::default(), false, frange) | 72 | Assist::get(&db, &TEST_CONFIG, false, frange) |
59 | .into_iter() | 73 | .into_iter() |
60 | .map(|assist| assist.id.0) | 74 | .map(|assist| assist.id.0) |
61 | .collect::<Vec<_>>() | 75 | .collect::<Vec<_>>() |
@@ -89,7 +103,7 @@ fn check(handler: Handler, before: &str, expected: ExpectedResult, assist_label: | |||
89 | let frange = FileRange { file_id: file_with_caret_id, range: range_or_offset.into() }; | 103 | let frange = FileRange { file_id: file_with_caret_id, range: range_or_offset.into() }; |
90 | 104 | ||
91 | let sema = Semantics::new(&db); | 105 | let sema = Semantics::new(&db); |
92 | let config = AssistConfig::default(); | 106 | let config = TEST_CONFIG; |
93 | let ctx = AssistContext::new(sema, &config, frange); | 107 | let ctx = AssistContext::new(sema, &config, frange); |
94 | let mut acc = Assists::new(&ctx, true); | 108 | let mut acc = Assists::new(&ctx, true); |
95 | handler(&mut acc, &ctx); | 109 | handler(&mut acc, &ctx); |
@@ -156,7 +170,7 @@ fn assist_order_field_struct() { | |||
156 | let (before_cursor_pos, before) = extract_offset(before); | 170 | let (before_cursor_pos, before) = extract_offset(before); |
157 | let (db, file_id) = with_single_file(&before); | 171 | let (db, file_id) = with_single_file(&before); |
158 | let frange = FileRange { file_id, range: TextRange::empty(before_cursor_pos) }; | 172 | let frange = FileRange { file_id, range: TextRange::empty(before_cursor_pos) }; |
159 | let assists = Assist::get(&db, &AssistConfig::default(), false, frange); | 173 | let assists = Assist::get(&db, &TEST_CONFIG, false, frange); |
160 | let mut assists = assists.iter(); | 174 | let mut assists = assists.iter(); |
161 | 175 | ||
162 | assert_eq!(assists.next().expect("expected assist").label, "Change visibility to pub(crate)"); | 176 | assert_eq!(assists.next().expect("expected assist").label, "Change visibility to pub(crate)"); |
@@ -176,7 +190,7 @@ fn assist_order_if_expr() { | |||
176 | let (range, before) = extract_range(before); | 190 | let (range, before) = extract_range(before); |
177 | let (db, file_id) = with_single_file(&before); | 191 | let (db, file_id) = with_single_file(&before); |
178 | let frange = FileRange { file_id, range }; | 192 | let frange = FileRange { file_id, range }; |
179 | let assists = Assist::get(&db, &AssistConfig::default(), false, frange); | 193 | let assists = Assist::get(&db, &TEST_CONFIG, false, frange); |
180 | let mut assists = assists.iter(); | 194 | let mut assists = assists.iter(); |
181 | 195 | ||
182 | assert_eq!(assists.next().expect("expected assist").label, "Extract into variable"); | 196 | assert_eq!(assists.next().expect("expected assist").label, "Extract into variable"); |
@@ -198,7 +212,7 @@ fn assist_filter_works() { | |||
198 | let frange = FileRange { file_id, range }; | 212 | let frange = FileRange { file_id, range }; |
199 | 213 | ||
200 | { | 214 | { |
201 | let mut cfg = AssistConfig::default(); | 215 | let mut cfg = TEST_CONFIG; |
202 | cfg.allowed = Some(vec![AssistKind::Refactor]); | 216 | cfg.allowed = Some(vec![AssistKind::Refactor]); |
203 | 217 | ||
204 | let assists = Assist::get(&db, &cfg, false, frange); | 218 | let assists = Assist::get(&db, &cfg, false, frange); |
@@ -209,7 +223,7 @@ fn assist_filter_works() { | |||
209 | } | 223 | } |
210 | 224 | ||
211 | { | 225 | { |
212 | let mut cfg = AssistConfig::default(); | 226 | let mut cfg = TEST_CONFIG; |
213 | cfg.allowed = Some(vec![AssistKind::RefactorExtract]); | 227 | cfg.allowed = Some(vec![AssistKind::RefactorExtract]); |
214 | let assists = Assist::get(&db, &cfg, false, frange); | 228 | let assists = Assist::get(&db, &cfg, false, frange); |
215 | assert_eq!(assists.len(), 1); | 229 | assert_eq!(assists.len(), 1); |
@@ -219,7 +233,7 @@ fn assist_filter_works() { | |||
219 | } | 233 | } |
220 | 234 | ||
221 | { | 235 | { |
222 | let mut cfg = AssistConfig::default(); | 236 | let mut cfg = TEST_CONFIG; |
223 | cfg.allowed = Some(vec![AssistKind::QuickFix]); | 237 | cfg.allowed = Some(vec![AssistKind::QuickFix]); |
224 | let assists = Assist::get(&db, &cfg, false, frange); | 238 | let assists = Assist::get(&db, &cfg, false, frange); |
225 | assert!(assists.is_empty(), "All asserts but quickfixes should be filtered out"); | 239 | 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; | |||
4 | use std::ops; | 4 | use std::ops; |
5 | 5 | ||
6 | use hir::HasSource; | 6 | use hir::HasSource; |
7 | use ide_db::RootDatabase; | 7 | use ide_db::{helpers::SnippetCap, RootDatabase}; |
8 | use itertools::Itertools; | 8 | use itertools::Itertools; |
9 | use syntax::{ | 9 | use syntax::{ |
10 | ast::edit::AstNodeEdit, | 10 | ast::edit::AstNodeEdit, |
@@ -16,10 +16,7 @@ use syntax::{ | |||
16 | SyntaxNode, TextSize, T, | 16 | SyntaxNode, TextSize, T, |
17 | }; | 17 | }; |
18 | 18 | ||
19 | use crate::{ | 19 | use crate::ast_transform::{self, AstTransform, QualifyPaths, SubstituteTypeParams}; |
20 | assist_config::SnippetCap, | ||
21 | ast_transform::{self, AstTransform, QualifyPaths, SubstituteTypeParams}, | ||
22 | }; | ||
23 | 20 | ||
24 | pub(crate) fn unwrap_trivial_block(block: ast::BlockExpr) -> ast::Expr { | 21 | pub(crate) fn unwrap_trivial_block(block: ast::BlockExpr) -> ast::Expr { |
25 | extract_trivial_expression(&block) | 22 | extract_trivial_expression(&block) |