aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists/src')
-rw-r--r--crates/ra_assists/src/assist_config.rs5
-rw-r--r--crates/ra_assists/src/assist_context.rs8
-rw-r--r--crates/ra_assists/src/lib.rs12
-rw-r--r--crates/ra_assists/src/tests.rs25
4 files changed, 24 insertions, 26 deletions
diff --git a/crates/ra_assists/src/assist_config.rs b/crates/ra_assists/src/assist_config.rs
index c0a0226fb..cda2abfb9 100644
--- a/crates/ra_assists/src/assist_config.rs
+++ b/crates/ra_assists/src/assist_config.rs
@@ -4,9 +4,12 @@
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
7use crate::AssistKind;
8
7#[derive(Clone, Debug, PartialEq, Eq)] 9#[derive(Clone, Debug, PartialEq, Eq)]
8pub struct AssistConfig { 10pub struct AssistConfig {
9 pub snippet_cap: Option<SnippetCap>, 11 pub snippet_cap: Option<SnippetCap>,
12 pub allowed: Option<Vec<AssistKind>>,
10} 13}
11 14
12impl AssistConfig { 15impl AssistConfig {
@@ -22,6 +25,6 @@ pub struct SnippetCap {
22 25
23impl Default for AssistConfig { 26impl Default for AssistConfig {
24 fn default() -> Self { 27 fn default() -> Self {
25 AssistConfig { snippet_cap: Some(SnippetCap { _private: () }) } 28 AssistConfig { snippet_cap: Some(SnippetCap { _private: () }), allowed: None }
26 } 29 }
27} 30}
diff --git a/crates/ra_assists/src/assist_context.rs b/crates/ra_assists/src/assist_context.rs
index 9ca2cfe68..3407df856 100644
--- a/crates/ra_assists/src/assist_context.rs
+++ b/crates/ra_assists/src/assist_context.rs
@@ -57,7 +57,6 @@ pub(crate) struct AssistContext<'a> {
57 pub(crate) sema: Semantics<'a, RootDatabase>, 57 pub(crate) sema: Semantics<'a, RootDatabase>,
58 pub(crate) frange: FileRange, 58 pub(crate) frange: FileRange,
59 source_file: SourceFile, 59 source_file: SourceFile,
60 allowed: Option<Vec<AssistKind>>,
61} 60}
62 61
63impl<'a> AssistContext<'a> { 62impl<'a> AssistContext<'a> {
@@ -65,10 +64,9 @@ impl<'a> AssistContext<'a> {
65 sema: Semantics<'a, RootDatabase>, 64 sema: Semantics<'a, RootDatabase>,
66 config: &'a AssistConfig, 65 config: &'a AssistConfig,
67 frange: FileRange, 66 frange: FileRange,
68 allowed: Option<Vec<AssistKind>>,
69 ) -> AssistContext<'a> { 67 ) -> AssistContext<'a> {
70 let source_file = sema.parse(frange.file_id); 68 let source_file = sema.parse(frange.file_id);
71 AssistContext { config, sema, frange, source_file, allowed } 69 AssistContext { config, sema, frange, source_file }
72 } 70 }
73 71
74 pub(crate) fn db(&self) -> &RootDatabase { 72 pub(crate) fn db(&self) -> &RootDatabase {
@@ -114,7 +112,7 @@ impl Assists {
114 resolve: true, 112 resolve: true,
115 file: ctx.frange.file_id, 113 file: ctx.frange.file_id,
116 buf: Vec::new(), 114 buf: Vec::new(),
117 allowed: ctx.allowed.clone(), 115 allowed: ctx.config.allowed.clone(),
118 } 116 }
119 } 117 }
120 118
@@ -123,7 +121,7 @@ impl Assists {
123 resolve: false, 121 resolve: false,
124 file: ctx.frange.file_id, 122 file: ctx.frange.file_id,
125 buf: Vec::new(), 123 buf: Vec::new(),
126 allowed: ctx.allowed.clone(), 124 allowed: ctx.config.allowed.clone(),
127 } 125 }
128 } 126 }
129 127
diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs
index 13a283760..465b90415 100644
--- a/crates/ra_assists/src/lib.rs
+++ b/crates/ra_assists/src/lib.rs
@@ -86,14 +86,9 @@ impl Assist {
86 /// 86 ///
87 /// Assists are returned in the "unresolved" state, that is only labels are 87 /// Assists are returned in the "unresolved" state, that is only labels are
88 /// returned, without actual edits. 88 /// returned, without actual edits.
89 pub fn unresolved( 89 pub fn unresolved(db: &RootDatabase, config: &AssistConfig, range: FileRange) -> Vec<Assist> {
90 db: &RootDatabase,
91 config: &AssistConfig,
92 range: FileRange,
93 allowed: Option<Vec<AssistKind>>,
94 ) -> Vec<Assist> {
95 let sema = Semantics::new(db); 90 let sema = Semantics::new(db);
96 let ctx = AssistContext::new(sema, config, range, allowed); 91 let ctx = AssistContext::new(sema, config, range);
97 let mut acc = Assists::new_unresolved(&ctx); 92 let mut acc = Assists::new_unresolved(&ctx);
98 handlers::all().iter().for_each(|handler| { 93 handlers::all().iter().for_each(|handler| {
99 handler(&mut acc, &ctx); 94 handler(&mut acc, &ctx);
@@ -109,10 +104,9 @@ impl Assist {
109 db: &RootDatabase, 104 db: &RootDatabase,
110 config: &AssistConfig, 105 config: &AssistConfig,
111 range: FileRange, 106 range: FileRange,
112 allowed: Option<Vec<AssistKind>>,
113 ) -> Vec<ResolvedAssist> { 107 ) -> Vec<ResolvedAssist> {
114 let sema = Semantics::new(db); 108 let sema = Semantics::new(db);
115 let ctx = AssistContext::new(sema, config, range, allowed); 109 let ctx = AssistContext::new(sema, config, range);
116 let mut acc = Assists::new_resolved(&ctx); 110 let mut acc = Assists::new_resolved(&ctx);
117 handlers::all().iter().for_each(|handler| { 111 handlers::all().iter().for_each(|handler| {
118 handler(&mut acc, &ctx); 112 handler(&mut acc, &ctx);
diff --git a/crates/ra_assists/src/tests.rs b/crates/ra_assists/src/tests.rs
index 861622d86..18fcb9049 100644
--- a/crates/ra_assists/src/tests.rs
+++ b/crates/ra_assists/src/tests.rs
@@ -35,14 +35,14 @@ fn check_doc_test(assist_id: &str, before: &str, after: &str) {
35 let before = db.file_text(file_id).to_string(); 35 let before = db.file_text(file_id).to_string();
36 let frange = FileRange { file_id, range: selection.into() }; 36 let frange = FileRange { file_id, range: selection.into() };
37 37
38 let mut assist = Assist::resolved(&db, &AssistConfig::default(), frange, None) 38 let mut assist = Assist::resolved(&db, &AssistConfig::default(), frange)
39 .into_iter() 39 .into_iter()
40 .find(|assist| assist.assist.id.0 == assist_id) 40 .find(|assist| assist.assist.id.0 == assist_id)
41 .unwrap_or_else(|| { 41 .unwrap_or_else(|| {
42 panic!( 42 panic!(
43 "\n\nAssist is not applicable: {}\nAvailable assists: {}", 43 "\n\nAssist is not applicable: {}\nAvailable assists: {}",
44 assist_id, 44 assist_id,
45 Assist::resolved(&db, &AssistConfig::default(), frange, None) 45 Assist::resolved(&db, &AssistConfig::default(), frange)
46 .into_iter() 46 .into_iter()
47 .map(|assist| assist.assist.id.0) 47 .map(|assist| assist.assist.id.0)
48 .collect::<Vec<_>>() 48 .collect::<Vec<_>>()
@@ -73,7 +73,7 @@ fn check(handler: Handler, before: &str, expected: ExpectedResult) {
73 73
74 let sema = Semantics::new(&db); 74 let sema = Semantics::new(&db);
75 let config = AssistConfig::default(); 75 let config = AssistConfig::default();
76 let ctx = AssistContext::new(sema, &config, frange, None); 76 let ctx = AssistContext::new(sema, &config, frange);
77 let mut acc = Assists::new_resolved(&ctx); 77 let mut acc = Assists::new_resolved(&ctx);
78 handler(&mut acc, &ctx); 78 handler(&mut acc, &ctx);
79 let mut res = acc.finish_resolved(); 79 let mut res = acc.finish_resolved();
@@ -105,7 +105,7 @@ fn assist_order_field_struct() {
105 let (before_cursor_pos, before) = extract_offset(before); 105 let (before_cursor_pos, before) = extract_offset(before);
106 let (db, file_id) = with_single_file(&before); 106 let (db, file_id) = with_single_file(&before);
107 let frange = FileRange { file_id, range: TextRange::empty(before_cursor_pos) }; 107 let frange = FileRange { file_id, range: TextRange::empty(before_cursor_pos) };
108 let assists = Assist::resolved(&db, &AssistConfig::default(), frange, None); 108 let assists = Assist::resolved(&db, &AssistConfig::default(), frange);
109 let mut assists = assists.iter(); 109 let mut assists = assists.iter();
110 110
111 assert_eq!( 111 assert_eq!(
@@ -128,7 +128,7 @@ fn assist_order_if_expr() {
128 let (range, before) = extract_range(before); 128 let (range, before) = extract_range(before);
129 let (db, file_id) = with_single_file(&before); 129 let (db, file_id) = with_single_file(&before);
130 let frange = FileRange { file_id, range }; 130 let frange = FileRange { file_id, range };
131 let assists = Assist::resolved(&db, &AssistConfig::default(), frange, None); 131 let assists = Assist::resolved(&db, &AssistConfig::default(), frange);
132 let mut assists = assists.iter(); 132 let mut assists = assists.iter();
133 133
134 assert_eq!(assists.next().expect("expected assist").assist.label, "Extract into variable"); 134 assert_eq!(assists.next().expect("expected assist").assist.label, "Extract into variable");
@@ -150,9 +150,10 @@ fn assist_filter_works() {
150 let frange = FileRange { file_id, range }; 150 let frange = FileRange { file_id, range };
151 151
152 { 152 {
153 let allowed = Some(vec![AssistKind::Refactor]); 153 let mut cfg = AssistConfig::default();
154 cfg.allowed = Some(vec![AssistKind::Refactor]);
154 155
155 let assists = Assist::resolved(&db, &AssistConfig::default(), frange, allowed); 156 let assists = Assist::resolved(&db, &cfg, frange);
156 let mut assists = assists.iter(); 157 let mut assists = assists.iter();
157 158
158 assert_eq!(assists.next().expect("expected assist").assist.label, "Extract into variable"); 159 assert_eq!(assists.next().expect("expected assist").assist.label, "Extract into variable");
@@ -160,8 +161,9 @@ fn assist_filter_works() {
160 } 161 }
161 162
162 { 163 {
163 let allowed = Some(vec![AssistKind::RefactorExtract]); 164 let mut cfg = AssistConfig::default();
164 let assists = Assist::resolved(&db, &AssistConfig::default(), frange, allowed); 165 cfg.allowed = Some(vec![AssistKind::RefactorExtract]);
166 let assists = Assist::resolved(&db, &cfg, frange);
165 assert_eq!(assists.len(), 1); 167 assert_eq!(assists.len(), 1);
166 168
167 let mut assists = assists.iter(); 169 let mut assists = assists.iter();
@@ -169,8 +171,9 @@ fn assist_filter_works() {
169 } 171 }
170 172
171 { 173 {
172 let allowed = Some(vec![AssistKind::QuickFix]); 174 let mut cfg = AssistConfig::default();
173 let assists = Assist::resolved(&db, &AssistConfig::default(), frange, allowed); 175 cfg.allowed = Some(vec![AssistKind::QuickFix]);
176 let assists = Assist::resolved(&db, &cfg, frange);
174 assert!(assists.is_empty(), "All asserts but quickfixes should be filtered out"); 177 assert!(assists.is_empty(), "All asserts but quickfixes should be filtered out");
175 } 178 }
176} 179}