@@ -15,7 +15,10 @@ use ra_syntax::{
15
15
} ;
16
16
use ra_text_edit:: TextEditBuilder ;
17
17
18
- use crate :: { Assist , AssistId , GroupLabel , ResolvedAssist } ;
18
+ use crate :: {
19
+ assist_config:: { AssistConfig , SnippetCap } ,
20
+ Assist , AssistId , GroupLabel , ResolvedAssist ,
21
+ } ;
19
22
20
23
/// `AssistContext` allows to apply an assist or check if it could be applied.
21
24
///
@@ -48,17 +51,22 @@ use crate::{Assist, AssistId, GroupLabel, ResolvedAssist};
48
51
/// moment, because the LSP API is pretty awkward in this place, and it's much
49
52
/// easier to just compute the edit eagerly :-)
50
53
pub ( crate ) struct AssistContext < ' a > {
54
+ pub ( crate ) config : & ' a AssistConfig ,
51
55
pub ( crate ) sema : Semantics < ' a , RootDatabase > ,
52
56
pub ( crate ) db : & ' a RootDatabase ,
53
57
pub ( crate ) frange : FileRange ,
54
58
source_file : SourceFile ,
55
59
}
56
60
57
61
impl < ' a > AssistContext < ' a > {
58
- pub fn new ( sema : Semantics < ' a , RootDatabase > , frange : FileRange ) -> AssistContext < ' a > {
62
+ pub ( crate ) fn new (
63
+ sema : Semantics < ' a , RootDatabase > ,
64
+ config : & ' a AssistConfig ,
65
+ frange : FileRange ,
66
+ ) -> AssistContext < ' a > {
59
67
let source_file = sema. parse ( frange. file_id ) ;
60
68
let db = sema. db ;
61
- AssistContext { sema, db, frange, source_file }
69
+ AssistContext { config , sema, db, frange, source_file }
62
70
}
63
71
64
72
// NB, this ignores active selection.
@@ -165,11 +173,17 @@ pub(crate) struct AssistBuilder {
165
173
edit : TextEditBuilder ,
166
174
cursor_position : Option < TextSize > ,
167
175
file : FileId ,
176
+ is_snippet : bool ,
168
177
}
169
178
170
179
impl AssistBuilder {
171
180
pub ( crate ) fn new ( file : FileId ) -> AssistBuilder {
172
- AssistBuilder { edit : TextEditBuilder :: default ( ) , cursor_position : None , file }
181
+ AssistBuilder {
182
+ edit : TextEditBuilder :: default ( ) ,
183
+ cursor_position : None ,
184
+ file,
185
+ is_snippet : false ,
186
+ }
173
187
}
174
188
175
189
/// Remove specified `range` of text.
@@ -180,6 +194,16 @@ impl AssistBuilder {
180
194
pub ( crate ) fn insert ( & mut self , offset : TextSize , text : impl Into < String > ) {
181
195
self . edit . insert ( offset, text. into ( ) )
182
196
}
197
+ /// Append specified `text` at the given `offset`
198
+ pub ( crate ) fn insert_snippet (
199
+ & mut self ,
200
+ _cap : SnippetCap ,
201
+ offset : TextSize ,
202
+ text : impl Into < String > ,
203
+ ) {
204
+ self . is_snippet = true ;
205
+ self . edit . insert ( offset, text. into ( ) )
206
+ }
183
207
/// Replaces specified `range` of text with a given string.
184
208
pub ( crate ) fn replace ( & mut self , range : TextRange , replace_with : impl Into < String > ) {
185
209
self . edit . replace ( range, replace_with. into ( ) )
@@ -227,7 +251,12 @@ impl AssistBuilder {
227
251
if edit. is_empty ( ) && self . cursor_position . is_none ( ) {
228
252
panic ! ( "Only call `add_assist` if the assist can be applied" )
229
253
}
230
- SingleFileChange { label : change_label, edit, cursor_position : self . cursor_position }
231
- . into_source_change ( self . file )
254
+ let mut res =
255
+ SingleFileChange { label : change_label, edit, cursor_position : self . cursor_position }
256
+ . into_source_change ( self . file ) ;
257
+ if self . is_snippet {
258
+ res. is_snippet = true ;
259
+ }
260
+ res
232
261
}
233
262
}
0 commit comments