aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-05-07 16:29:23 +0100
committerAleksey Kladov <[email protected]>2020-05-07 16:29:23 +0100
commit28fcff125a73ab2fc4aeaa100fc472af5178db20 (patch)
tree3021cbb36741e7efd6172561cab5e916a99cdc92 /crates/ra_assists
parentc6b81bc013b5278b917d109b723405e0df413323 (diff)
Nicer API
Diffstat (limited to 'crates/ra_assists')
-rw-r--r--crates/ra_assists/src/lib.rs72
-rw-r--r--crates/ra_assists/src/tests.rs10
2 files changed, 41 insertions, 41 deletions
diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs
index a91975d8c..b6dc7cb1b 100644
--- a/crates/ra_assists/src/lib.rs
+++ b/crates/ra_assists/src/lib.rs
@@ -29,6 +29,9 @@ pub(crate) use crate::assist_context::{AssistContext, Assists};
29#[derive(Debug, Clone, Copy, PartialEq, Eq)] 29#[derive(Debug, Clone, Copy, PartialEq, Eq)]
30pub struct AssistId(pub &'static str); 30pub struct AssistId(pub &'static str);
31 31
32#[derive(Clone, Debug)]
33pub struct GroupLabel(pub String);
34
32#[derive(Debug, Clone)] 35#[derive(Debug, Clone)]
33pub struct Assist { 36pub struct Assist {
34 pub id: AssistId, 37 pub id: AssistId,
@@ -40,10 +43,41 @@ pub struct Assist {
40 pub target: TextRange, 43 pub target: TextRange,
41} 44}
42 45
43#[derive(Clone, Debug)] 46#[derive(Debug, Clone)]
44pub struct GroupLabel(pub String); 47pub struct ResolvedAssist {
48 pub assist: Assist,
49 pub source_change: SourceChange,
50}
45 51
46impl Assist { 52impl Assist {
53 /// Return all the assists applicable at the given position.
54 ///
55 /// Assists are returned in the "unresolved" state, that is only labels are
56 /// returned, without actual edits.
57 pub fn unresolved(db: &RootDatabase, range: FileRange) -> Vec<Assist> {
58 let sema = Semantics::new(db);
59 let ctx = AssistContext::new(sema, range);
60 let mut acc = Assists::new_unresolved(&ctx);
61 handlers::all().iter().for_each(|handler| {
62 handler(&mut acc, &ctx);
63 });
64 acc.finish_unresolved()
65 }
66
67 /// Return all the assists applicable at the given position.
68 ///
69 /// Assists are returned in the "resolved" state, that is with edit fully
70 /// computed.
71 pub fn resolved(db: &RootDatabase, range: FileRange) -> Vec<ResolvedAssist> {
72 let sema = Semantics::new(db);
73 let ctx = AssistContext::new(sema, range);
74 let mut acc = Assists::new_resolved(&ctx);
75 handlers::all().iter().for_each(|handler| {
76 handler(&mut acc, &ctx);
77 });
78 acc.finish_resolved()
79 }
80
47 pub(crate) fn new( 81 pub(crate) fn new(
48 id: AssistId, 82 id: AssistId,
49 label: String, 83 label: String,
@@ -56,40 +90,6 @@ impl Assist {
56 } 90 }
57} 91}
58 92
59#[derive(Debug, Clone)]
60pub struct ResolvedAssist {
61 pub assist: Assist,
62 pub source_change: SourceChange,
63}
64
65/// Return all the assists applicable at the given position.
66///
67/// Assists are returned in the "unresolved" state, that is only labels are
68/// returned, without actual edits.
69pub fn unresolved_assists(db: &RootDatabase, range: FileRange) -> Vec<Assist> {
70 let sema = Semantics::new(db);
71 let ctx = AssistContext::new(sema, range);
72 let mut acc = Assists::new_unresolved(&ctx);
73 handlers::all().iter().for_each(|handler| {
74 handler(&mut acc, &ctx);
75 });
76 acc.finish_unresolved()
77}
78
79/// Return all the assists applicable at the given position.
80///
81/// Assists are returned in the "resolved" state, that is with edit fully
82/// computed.
83pub fn resolved_assists(db: &RootDatabase, range: FileRange) -> Vec<ResolvedAssist> {
84 let sema = Semantics::new(db);
85 let ctx = AssistContext::new(sema, range);
86 let mut acc = Assists::new_resolved(&ctx);
87 handlers::all().iter().for_each(|handler| {
88 handler(&mut acc, &ctx);
89 });
90 acc.finish_resolved()
91}
92
93mod handlers { 93mod handlers {
94 use crate::{AssistContext, Assists}; 94 use crate::{AssistContext, Assists};
95 95
diff --git a/crates/ra_assists/src/tests.rs b/crates/ra_assists/src/tests.rs
index a81c54d07..a3eacb8f1 100644
--- a/crates/ra_assists/src/tests.rs
+++ b/crates/ra_assists/src/tests.rs
@@ -11,7 +11,7 @@ use test_utils::{
11 RangeOrOffset, 11 RangeOrOffset,
12}; 12};
13 13
14use crate::{handlers::Handler, resolved_assists, AssistContext, Assists}; 14use crate::{handlers::Handler, Assist, AssistContext, Assists};
15 15
16pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) { 16pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) {
17 let (mut db, file_id) = RootDatabase::with_single_file(text); 17 let (mut db, file_id) = RootDatabase::with_single_file(text);
@@ -41,14 +41,14 @@ fn check_doc_test(assist_id: &str, before: &str, after: &str) {
41 let (db, file_id) = crate::tests::with_single_file(&before); 41 let (db, file_id) = crate::tests::with_single_file(&before);
42 let frange = FileRange { file_id, range: selection.into() }; 42 let frange = FileRange { file_id, range: selection.into() };
43 43
44 let mut assist = resolved_assists(&db, frange) 44 let mut assist = Assist::resolved(&db, frange)
45 .into_iter() 45 .into_iter()
46 .find(|assist| assist.assist.id.0 == assist_id) 46 .find(|assist| assist.assist.id.0 == assist_id)
47 .unwrap_or_else(|| { 47 .unwrap_or_else(|| {
48 panic!( 48 panic!(
49 "\n\nAssist is not applicable: {}\nAvailable assists: {}", 49 "\n\nAssist is not applicable: {}\nAvailable assists: {}",
50 assist_id, 50 assist_id,
51 resolved_assists(&db, frange) 51 Assist::resolved(&db, frange)
52 .into_iter() 52 .into_iter()
53 .map(|assist| assist.assist.id.0) 53 .map(|assist| assist.assist.id.0)
54 .collect::<Vec<_>>() 54 .collect::<Vec<_>>()
@@ -136,7 +136,7 @@ fn assist_order_field_struct() {
136 let (before_cursor_pos, before) = extract_offset(before); 136 let (before_cursor_pos, before) = extract_offset(before);
137 let (db, file_id) = with_single_file(&before); 137 let (db, file_id) = with_single_file(&before);
138 let frange = FileRange { file_id, range: TextRange::empty(before_cursor_pos) }; 138 let frange = FileRange { file_id, range: TextRange::empty(before_cursor_pos) };
139 let assists = resolved_assists(&db, frange); 139 let assists = Assist::resolved(&db, frange);
140 let mut assists = assists.iter(); 140 let mut assists = assists.iter();
141 141
142 assert_eq!( 142 assert_eq!(
@@ -159,7 +159,7 @@ fn assist_order_if_expr() {
159 let (range, before) = extract_range(before); 159 let (range, before) = extract_range(before);
160 let (db, file_id) = with_single_file(&before); 160 let (db, file_id) = with_single_file(&before);
161 let frange = FileRange { file_id, range }; 161 let frange = FileRange { file_id, range };
162 let assists = resolved_assists(&db, frange); 162 let assists = Assist::resolved(&db, frange);
163 let mut assists = assists.iter(); 163 let mut assists = assists.iter();
164 164
165 assert_eq!(assists.next().expect("expected assist").assist.label, "Extract into variable"); 165 assert_eq!(assists.next().expect("expected assist").assist.label, "Extract into variable");