@@ -137,18 +137,6 @@ impl Document {
137
137
diff_size : TextSize ,
138
138
is_addition : bool ,
139
139
) -> Affected {
140
- // special case: no previous statements -> always full range
141
- if self . positions . is_empty ( ) {
142
- let full_range = TextRange :: new ( 0 . into ( ) , content_size) ;
143
- return Affected {
144
- affected_range : full_range,
145
- affected_indices : Vec :: new ( ) ,
146
- prev_index : None ,
147
- next_index : None ,
148
- full_affected_range : full_range,
149
- } ;
150
- }
151
-
152
140
let mut start = change_range. start ( ) ;
153
141
let mut end = change_range. end ( ) . min ( content_size) ;
154
142
@@ -171,6 +159,16 @@ impl Document {
171
159
}
172
160
}
173
161
162
+ if affected_indices. is_empty ( ) && prev_index. is_none ( ) {
163
+ // if there is no prev_index and no intersection -> use 0
164
+ start = 0 . into ( ) ;
165
+ }
166
+
167
+ if affected_indices. is_empty ( ) && next_index. is_none ( ) {
168
+ // if there is no next_index and no intersection -> use content_size
169
+ end = content_size;
170
+ }
171
+
174
172
let first_affected_stmt_start = prev_index
175
173
. map ( |i| self . positions [ i] . 1 . start ( ) )
176
174
. unwrap_or ( start) ;
@@ -460,6 +458,72 @@ mod tests {
460
458
assert ! ( d. has_fatal_error( ) ) ;
461
459
}
462
460
461
+ #[ test]
462
+ fn comments_at_begin ( ) {
463
+ let path = PgTPath :: new ( "test.sql" ) ;
464
+ let input = "\n select id from users;\n " ;
465
+
466
+ let mut d = Document :: new ( input. to_string ( ) , 0 ) ;
467
+
468
+ let change1 = ChangeFileParams {
469
+ path : path. clone ( ) ,
470
+ version : 1 ,
471
+ changes : vec ! [ ChangeParams {
472
+ text: "-" . to_string( ) ,
473
+ range: Some ( TextRange :: new( 0 . into( ) , 0 . into( ) ) ) ,
474
+ } ] ,
475
+ } ;
476
+
477
+ let _changed1 = d. apply_file_change ( & change1) ;
478
+
479
+ assert_eq ! ( d. content, "-\n select id from users;\n " ) ;
480
+ assert_eq ! ( d. positions. len( ) , 2 ) ;
481
+
482
+ let change2 = ChangeFileParams {
483
+ path : path. clone ( ) ,
484
+ version : 2 ,
485
+ changes : vec ! [ ChangeParams {
486
+ text: "-" . to_string( ) ,
487
+ range: Some ( TextRange :: new( 1 . into( ) , 1 . into( ) ) ) ,
488
+ } ] ,
489
+ } ;
490
+
491
+ let _changed2 = d. apply_file_change ( & change2) ;
492
+
493
+ assert_eq ! ( d. content, "--\n select id from users;\n " ) ;
494
+ assert_eq ! ( d. positions. len( ) , 1 ) ;
495
+
496
+ let change3 = ChangeFileParams {
497
+ path : path. clone ( ) ,
498
+ version : 3 ,
499
+ changes : vec ! [ ChangeParams {
500
+ text: " " . to_string( ) ,
501
+ range: Some ( TextRange :: new( 2 . into( ) , 2 . into( ) ) ) ,
502
+ } ] ,
503
+ } ;
504
+
505
+ let _changed3 = d. apply_file_change ( & change3) ;
506
+
507
+ assert_eq ! ( d. content, "-- \n select id from users;\n " ) ;
508
+ assert_eq ! ( d. positions. len( ) , 1 ) ;
509
+
510
+ let change4 = ChangeFileParams {
511
+ path : path. clone ( ) ,
512
+ version : 3 ,
513
+ changes : vec ! [ ChangeParams {
514
+ text: "t" . to_string( ) ,
515
+ range: Some ( TextRange :: new( 3 . into( ) , 3 . into( ) ) ) ,
516
+ } ] ,
517
+ } ;
518
+
519
+ let _changed4 = d. apply_file_change ( & change4) ;
520
+
521
+ assert_eq ! ( d. content, "-- t\n select id from users;\n " ) ;
522
+ assert_eq ! ( d. positions. len( ) , 1 ) ;
523
+
524
+ assert_document_integrity ( & d) ;
525
+ }
526
+
463
527
#[ test]
464
528
fn typing_comments ( ) {
465
529
let path = PgTPath :: new ( "test.sql" ) ;
0 commit comments