@@ -193,3 +193,43 @@ fn rustfmt_args(config: &Config, config_path: &Path) -> Vec<String> {
193
193
194
194
args
195
195
}
196
+
197
+ #[ cfg( test) ]
198
+ mod tests {
199
+ use super :: * ;
200
+ use crate :: config:: FmtConfig ;
201
+ use lsp_types:: { Position , Range , TextEdit } ;
202
+
203
+ #[ test]
204
+ fn calc_text_edits ( ) {
205
+ let config = || FmtConfig :: default ( ) . get_rustfmt_config ( ) . clone ( ) ;
206
+ let format = |x : & str | Rustfmt :: Internal . calc_text_edits ( x. to_string ( ) , config ( ) ) . unwrap ( ) ;
207
+ let line_range = |start, end| Range {
208
+ start : Position { line : start, character : 0 } ,
209
+ end : Position { line : end, character : u64:: max_value ( ) } ,
210
+ } ;
211
+ // Handle single-line text wrt. added/removed trailing newline
212
+ assert_eq ! (
213
+ format( "fn main() {} " ) ,
214
+ vec![ TextEdit { range: line_range( 0 , 0 ) , new_text: "fn main() {}\n " . to_owned( ) } ]
215
+ ) ;
216
+
217
+ assert_eq ! (
218
+ format( "fn main() {} \n " ) ,
219
+ vec![ TextEdit { range: line_range( 0 , 0 ) , new_text: "fn main() {}" . to_owned( ) } ]
220
+ ) ;
221
+
222
+ assert_eq ! (
223
+ format( "\n fn main() {} \n " ) ,
224
+ vec![ TextEdit { range: line_range( 0 , 1 ) , new_text: "fn main() {}" . to_owned( ) } ]
225
+ ) ;
226
+ // Check that we send two separate edits
227
+ assert_eq ! (
228
+ format( " struct Upper ;\n \n struct Lower ;" ) ,
229
+ vec![
230
+ TextEdit { range: line_range( 0 , 0 ) , new_text: "struct Upper;" . to_owned( ) } ,
231
+ TextEdit { range: line_range( 2 , 2 ) , new_text: "struct Lower;\n " . to_owned( ) }
232
+ ]
233
+ ) ;
234
+ }
235
+ }
0 commit comments