@konomanoasa/tree-sitter-posix-awk 0.19.0 → 0.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,13 +8,11 @@ use tree_sitter::{
8
8
  struct EreCase {
9
9
  name: &'static str,
10
10
  source: &'static str,
11
- leaves: &'static [(&'static str, usize, usize)],
12
11
  }
13
12
 
14
13
  struct LeafCase {
15
14
  name: &'static str,
16
15
  source: &'static str,
17
- kind: &'static str,
18
16
  range: Range<usize>,
19
17
  }
20
18
 
@@ -22,85 +20,71 @@ const LEAF_CASES: &[LeafCase] = &[
22
20
  LeafCase {
23
21
  name: "a complete name remains one leaf",
24
22
  source: "foo\n",
25
- kind: "name",
26
23
  range: 0..3,
27
24
  },
28
25
  LeafCase {
29
26
  name: "the name before consecutive continuations remains separate",
30
27
  source: "fo\\\n\\\no\n",
31
- kind: "name",
32
28
  range: 0..2,
33
29
  },
34
30
  LeafCase {
35
31
  name: "the name after consecutive continuations remains separate",
36
32
  source: "fo\\\n\\\no\n",
37
- kind: "name",
38
33
  range: 6..7,
39
34
  },
40
35
  LeafCase {
41
36
  name: "a keyword excludes its following continuation",
42
37
  source: "BEGIN\\\n{}\n",
43
- kind: "begin_keyword",
44
38
  range: 0..5,
45
39
  },
46
40
  LeafCase {
47
41
  name: "a builtin excludes its following continuation",
48
42
  source: "{length\\\n(x)}\n",
49
- kind: "builtin_func_name",
50
43
  range: 1..7,
51
44
  },
52
45
  LeafCase {
53
46
  name: "a function name excludes its adjacent parenthesis",
54
47
  source: "{foo(x)}\n",
55
- kind: "func_name",
56
48
  range: 1..4,
57
49
  },
58
50
  LeafCase {
59
51
  name: "a continuation before the parenthesis makes a variable name",
60
52
  source: "{foo\\\n(x)}\n",
61
- kind: "name",
62
53
  range: 1..4,
63
54
  },
64
55
  LeafCase {
65
56
  name: "a decimal constant includes its exponent and suffix",
66
57
  source: "1.2e+3F\n",
67
- kind: "number",
68
58
  range: 0..7,
69
59
  },
70
60
  LeafCase {
71
61
  name: "a logical operator excludes its following continuation",
72
62
  source: "a||\\\nb\n",
73
- kind: "or",
74
63
  range: 1..3,
75
64
  },
76
65
  LeafCase {
77
66
  name: "Unicode string content remains one leaf",
78
67
  source: "\"é😀\"\n",
79
- kind: "string_content",
80
68
  range: 1..7,
81
69
  },
82
70
  LeafCase {
83
71
  name: "an octal escape excludes the following string character",
84
72
  source: "\"\\123x\"\n",
85
- kind: "escape_sequence",
86
73
  range: 1..5,
87
74
  },
88
75
  LeafCase {
89
76
  name: "an escaped slash excludes the closing delimiter",
90
77
  source: "/\\//\n",
91
- kind: "escaped_delimiter",
92
78
  range: 1..3,
93
79
  },
94
80
  LeafCase {
95
81
  name: "an ERE class name excludes compound delimiters",
96
82
  source: "/[[:alpha:]]/\n",
97
- kind: "class_name",
98
83
  range: 4..9,
99
84
  },
100
85
  LeafCase {
101
86
  name: "an ERE repetition count remains one leaf",
102
87
  source: "/a{12}/\n",
103
- kind: "duplication_count",
104
88
  range: 3..5,
105
89
  },
106
90
  ];
@@ -109,147 +93,34 @@ const ERE_CASES: &[EreCase] = &[
109
93
  EreCase {
110
94
  name: "an empty ERE retains only its delimiter ranges",
111
95
  source: "//",
112
- leaves: &[("/", 0, 1), ("/", 1, 2)],
113
96
  },
114
97
  EreCase {
115
98
  name: "empty groups retain delimiters within nested alternation and repetition",
116
99
  source: "/(()|())*?/",
117
- leaves: &[
118
- ("/", 0, 1),
119
- ("(", 1, 2),
120
- ("(", 2, 3),
121
- (")", 3, 4),
122
- ("|", 4, 5),
123
- ("(", 5, 6),
124
- (")", 6, 7),
125
- (")", 7, 8),
126
- ("*", 8, 9),
127
- ("repetition_modifier", 9, 10),
128
- ("/", 10, 11),
129
- ],
130
100
  },
131
101
  EreCase {
132
102
  name: "ordinary characters and complete escapes retain their source ranges",
133
103
  source: r"/a é)}.\n\/\\\141\q(a*?)/",
134
- leaves: &[
135
- ("/", 0, 1),
136
- ("ordinary_character", 1, 2),
137
- ("ordinary_character", 2, 3),
138
- ("ordinary_character", 3, 5),
139
- ("ordinary_character", 5, 6),
140
- ("ordinary_character", 6, 7),
141
- (".", 7, 8),
142
- ("escape_sequence", 8, 10),
143
- ("escaped_delimiter", 10, 12),
144
- ("quoted_character", 12, 14),
145
- ("escape_sequence", 14, 18),
146
- ("escape_sequence", 18, 20),
147
- ("(", 20, 21),
148
- ("ordinary_character", 21, 22),
149
- ("*", 22, 23),
150
- ("repetition_modifier", 23, 24),
151
- (")", 24, 25),
152
- ("/", 25, 26),
153
- ],
154
104
  },
155
105
  EreCase {
156
106
  name: "a blank before an escape remains content in every literal position",
157
107
  source: r"/ \.[ \]][[. \..]]/",
158
- leaves: &[
159
- ("/", 0, 1),
160
- ("ordinary_character", 1, 2),
161
- ("quoted_character", 2, 4),
162
- ("[", 4, 5),
163
- ("collating_element_single", 5, 6),
164
- ("escape_sequence", 6, 8),
165
- ("]", 8, 9),
166
- ("[", 9, 10),
167
- ("[", 10, 11),
168
- (".", 11, 12),
169
- ("collating_element_content", 12, 13),
170
- ("escape_sequence", 13, 15),
171
- (".", 15, 16),
172
- ("]", 16, 17),
173
- ("]", 17, 18),
174
- ("/", 18, 19),
175
- ],
176
108
  },
177
109
  EreCase {
178
110
  name: "collating forms separate raw fragments from adjacent escapes",
179
111
  source: r"/[[.a\nb.][=é\t犬=][.\/\141\q.]]/",
180
- leaves: &[
181
- ("/", 0, 1),
182
- ("[", 1, 2),
183
- ("[", 2, 3),
184
- (".", 3, 4),
185
- ("collating_element_content", 4, 5),
186
- ("escape_sequence", 5, 7),
187
- ("collating_element_content", 7, 8),
188
- (".", 8, 9),
189
- ("]", 9, 10),
190
- ("[", 10, 11),
191
- ("=", 11, 12),
192
- ("collating_element_content", 12, 14),
193
- ("escape_sequence", 14, 16),
194
- ("collating_element_content", 16, 19),
195
- ("=", 19, 20),
196
- ("]", 20, 21),
197
- ("[", 21, 22),
198
- (".", 22, 23),
199
- ("escaped_delimiter", 23, 25),
200
- ("escape_sequence", 25, 29),
201
- ("escape_sequence", 29, 31),
202
- (".", 31, 32),
203
- ("]", 32, 33),
204
- ("]", 33, 34),
205
- ("/", 34, 35),
206
- ],
207
112
  },
208
113
  EreCase {
209
114
  name: "raw compound punctuation, spaces and Unicode share one payload leaf",
210
115
  source: r"/[[. a-]🙂.]]/",
211
- leaves: &[
212
- ("/", 0, 1),
213
- ("[", 1, 2),
214
- ("[", 2, 3),
215
- (".", 3, 4),
216
- ("collating_element_multi", 4, 12),
217
- (".", 12, 13),
218
- ("]", 13, 14),
219
- ("]", 14, 15),
220
- ("/", 15, 16),
221
- ],
222
116
  },
223
117
  EreCase {
224
118
  name: "bracket literals distinguish raw brackets and a trailing hyphen",
225
119
  source: r"/[]a[\/\n-]/",
226
- leaves: &[
227
- ("/", 0, 1),
228
- ("[", 1, 2),
229
- ("collating_element_single", 2, 3),
230
- ("collating_element_single", 3, 4),
231
- ("collating_element_single", 4, 5),
232
- ("escaped_delimiter", 5, 7),
233
- ("escape_sequence", 7, 9),
234
- ("-", 9, 10),
235
- ("]", 10, 11),
236
- ("/", 11, 12),
237
- ],
238
120
  },
239
121
  EreCase {
240
122
  name: "a leading hyphen is a named endpoint before range and list hyphens",
241
123
  source: r"/[--ac-]/",
242
- leaves: &[
243
- ("/", 0, 1),
244
- ("[", 1, 2),
245
- ("collating_element_single", 2, 3),
246
- ("-", 3, 4),
247
- ("collating_element_single", 4, 5),
248
- ("collating_element_single", 5, 6),
249
- ("-", 6, 7),
250
- ("]", 7, 8),
251
- ("/", 8, 9),
252
- ],
253
124
  },
254
125
  ];
255
126
 
@@ -297,27 +168,6 @@ fn edit(
297
168
  });
298
169
  }
299
170
 
300
- fn assert_leaf_partition(
301
- node: Node<'_>,
302
- expected: &[(&str, usize, usize)],
303
- name: &str,
304
- ) {
305
- let mut leaves = Vec::new();
306
- collect_leaves(node, &mut leaves);
307
- let actual: Vec<_> = leaves
308
- .iter()
309
- .map(|leaf| (leaf.kind(), leaf.start_byte(), leaf.end_byte()))
310
- .collect();
311
- assert_eq!(actual, expected, "{name}");
312
- let mut end = node.start_byte();
313
- for leaf in leaves {
314
- assert_eq!(leaf.start_byte(), end, "{name}");
315
- assert!(leaf.end_byte() > end, "{name}");
316
- end = leaf.end_byte();
317
- }
318
- assert_eq!(end, node.end_byte(), "{name}");
319
- }
320
-
321
171
  fn assert_leaf_highlights(
322
172
  source: &str,
323
173
  tree: &Tree,
@@ -378,6 +228,35 @@ fn assert_same_cst(actual: Node<'_>, expected: Node<'_>, context: &str) {
378
228
  }
379
229
  }
380
230
 
231
+ fn assert_changed_scopes(old: &Tree, new: &Tree, context: &str) {
232
+ let scopes = |tree: &Tree| {
233
+ let mut scopes = vec![Vec::new(); tree.root_node().end_byte()];
234
+ let mut pending = vec![tree.root_node()];
235
+ while let Some(node) = pending.pop() {
236
+ for scope in &mut scopes[node.byte_range()] {
237
+ scope.push(node.kind_id());
238
+ }
239
+ for index in (0..node.child_count()).rev() {
240
+ pending.push(node.child(index).unwrap());
241
+ }
242
+ }
243
+ scopes
244
+ };
245
+ let old_scopes = scopes(old);
246
+ let new_scopes = scopes(new);
247
+ let ranges: Vec<_> = old.changed_ranges(new).collect();
248
+ for (byte, scope) in new_scopes.iter().enumerate() {
249
+ if old_scopes.get(byte) != Some(scope) {
250
+ assert!(
251
+ ranges
252
+ .iter()
253
+ .any(|range| range.start_byte <= byte && byte < range.end_byte),
254
+ "{context}: unreported scope change at byte {byte}: {ranges:?}",
255
+ );
256
+ }
257
+ }
258
+ }
259
+
381
260
  fn assert_fresh_cst(
382
261
  reused: &mut Parser,
383
262
  source: &[u8],
@@ -410,19 +289,6 @@ fn parses_valid_source() {
410
289
  Query::new(&language, grammar::HIGHLIGHTS_QUERY).unwrap();
411
290
  }
412
291
 
413
- #[test]
414
- fn ere_leaves_partition_complete_source_spellings() {
415
- for case in ERE_CASES {
416
- let tree = parse(&format!("{}\n", case.source), case.name);
417
- let ere = tree
418
- .root_node()
419
- .descendant_for_byte_range(0, case.source.len())
420
- .unwrap();
421
- assert_eq!(ere.kind(), "ere", "{}", case.name);
422
- assert_leaf_partition(ere, case.leaves, case.name);
423
- }
424
- }
425
-
426
292
  #[test]
427
293
  fn empty_ere_and_groups_own_only_opening_and_closing_delimiters() {
428
294
  for (kind, name, source, range) in [
@@ -585,6 +451,18 @@ fn fields_belong_to_direct_children() {
585
451
  ),
586
452
  ("nested field after arithmetic unary", "{$+$a}"),
587
453
  ("nested field after logical unary in print", "{print $!$a}"),
454
+ (
455
+ "ordinary updated fields without getline",
456
+ "{$$x++; $(-x)++; $-x++; ++$!x; $!$a}",
457
+ ),
458
+ (
459
+ "ordinary fields inside operators and arguments",
460
+ "{$a + $b * $!$c; $d ? $e : $f; f($1, ($a,$b) in arr)}",
461
+ ),
462
+ (
463
+ "nested getline targets without a source",
464
+ "{getline $(getline $(-$a)); print $+$a}",
465
+ ),
588
466
  (
589
467
  "postfix field and getline target aliases",
590
468
  "{$-x++; $(-x)++; $$x++; $getline x++; $getline $-x++; $getline $(x)++}\n",
@@ -682,36 +560,6 @@ fn unary_field_assignment_preserves_target_and_right_association() {
682
560
  }
683
561
  }
684
562
 
685
- #[test]
686
- fn unary_field_assignment_keeps_parenthesized_pipe_ownership() {
687
- for (name, source, pipe_source) in [
688
- (
689
- "assignment supplies pipe",
690
- "{($+a=b) | getline}\n",
691
- "($+a=b)",
692
- ),
693
- ("pipe supplies assignment", "{$+a=(b | getline)}\n", "b"),
694
- ] {
695
- let tree = parse(source, name);
696
- let mut pending = vec![tree.root_node()];
697
- let mut fields = Vec::new();
698
- let mut sources = Vec::new();
699
- while let Some(node) = pending.pop() {
700
- if node.kind() == "lvalue"
701
- && node.child_by_field_name("operator").is_some()
702
- {
703
- fields.push(node.utf8_text(source.as_bytes()).unwrap());
704
- }
705
- if let Some(child) = node.child_by_field_name("source") {
706
- sources.push(child.utf8_text(source.as_bytes()).unwrap());
707
- }
708
- pending.extend(node.children(&mut node.walk()));
709
- }
710
- assert_eq!(fields, ["$+a"], "{name}");
711
- assert_eq!(sources, [pipe_source], "{name}");
712
- }
713
- }
714
-
715
563
  #[test]
716
564
  fn nested_unary_fields_complete_before_the_outer_assignment() {
717
565
  for (name, atom, operator, right) in [
@@ -812,25 +660,10 @@ fn highlights_capture_only_leaves_and_cover_ere_and_token_spellings() {
812
660
  }
813
661
 
814
662
  #[test]
815
- fn lexical_tokens_keep_complete_leaf_ranges() {
816
- for case in LEAF_CASES {
817
- let tree = parse(case.source, case.name);
818
- let node = tree
819
- .root_node()
820
- .descendant_for_byte_range(case.range.start, case.range.end)
821
- .unwrap();
822
- assert_eq!(node.kind(), case.kind, "{}", case.name);
823
- assert_eq!(node.byte_range(), case.range, "{}", case.name);
824
- assert_eq!(node.child_count(), 0, "{}", case.name);
825
- }
826
- }
827
-
828
- #[test]
829
- fn continuation_markers_are_anonymous_and_exclude_newlines() {
663
+ fn continuation_markers_exclude_newlines_and_receive_leaf_highlights() {
830
664
  struct Case {
831
665
  name: &'static str,
832
666
  source: &'static str,
833
- markers: &'static [(usize, usize)],
834
667
  }
835
668
  assert!(!grammar::NODE_TYPES.contains("\"line_continuation\""));
836
669
  let query =
@@ -839,52 +672,42 @@ fn continuation_markers_are_anonymous_and_exclude_newlines() {
839
672
  Case {
840
673
  name: "a continuation alone creates no newline token",
841
674
  source: "\\\n",
842
- markers: &[(0, 1)],
843
675
  },
844
676
  Case {
845
677
  name: "source and AWK expression boundaries",
846
678
  source: "\\\n{a +\\\nb}\\\n",
847
- markers: &[(0, 1), (6, 7), (10, 11)],
848
679
  },
849
680
  Case {
850
681
  name: "consecutive continuations separate two names",
851
682
  source: "fo\\\n\\\no\n",
852
- markers: &[(2, 3), (4, 5)],
853
683
  },
854
684
  Case {
855
685
  name: "a continuation separates two numbers",
856
686
  source: "1\\\n2\n",
857
- markers: &[(1, 2)],
858
687
  },
859
688
  Case {
860
689
  name: "a continuation separates a definition name and parenthesis",
861
690
  source: "function f\\\n(){}",
862
- markers: &[(10, 11)],
863
691
  },
864
692
  Case {
865
693
  name: "a continuation follows a parameter separator",
866
694
  source: "function f(a,\\\nb){}",
867
- markers: &[(13, 14)],
868
695
  },
869
696
  Case {
870
697
  name: "a continuation separates complete string constants",
871
698
  source: "\"a\"\\\n\"b\"\n",
872
- markers: &[(3, 4)],
873
699
  },
874
700
  Case {
875
701
  name: "a continuation follows a complete static ERE",
876
702
  source: "/a/\\\n&& /b/\n",
877
- markers: &[(3, 4)],
878
703
  },
879
704
  Case {
880
705
  name: "string and ERE escapes remain distinct from continuations",
881
706
  source: "\"\\n\"\\\n&& /\\n/\n",
882
- markers: &[(4, 5)],
883
707
  },
884
708
  Case {
885
709
  name: "a comment backslash does not become a continuation marker",
886
710
  source: "# \\\n/a/\\\n&& /b/\n",
887
- markers: &[(7, 8)],
888
711
  },
889
712
  ] {
890
713
  let tree = parse(case.source, case.name);
@@ -899,8 +722,7 @@ fn continuation_markers_are_anonymous_and_exclude_newlines() {
899
722
  (node.start_byte(), node.end_byte())
900
723
  })
901
724
  .collect();
902
- assert_eq!(actual, case.markers, "{}", case.name);
903
- for (_, end) in case.markers {
725
+ for (_, end) in &actual {
904
726
  assert_eq!(case.source.as_bytes()[*end], b'\n');
905
727
  assert!(
906
728
  leaves.iter().all(|leaf| !leaf.byte_range().contains(end)),
@@ -911,7 +733,7 @@ fn continuation_markers_are_anonymous_and_exclude_newlines() {
911
733
  assert_leaf_highlights(
912
734
  case.source,
913
735
  &tree,
914
- case.markers.iter().map(|(start, end)| *start..*end),
736
+ actual.iter().map(|(start, end)| *start..*end),
915
737
  case.name,
916
738
  &query,
917
739
  );
@@ -1186,17 +1008,54 @@ fn removing_an_invalid_bracket_before_getline_restores_statements() {
1186
1008
  );
1187
1009
  }
1188
1010
 
1011
+ #[test]
1012
+ fn editing_a_field_getline_lookahead_during_recovery_restores_its_target() {
1013
+ let mut parser = parser();
1014
+ let source = "{ getline x $ getline y ++ $ getline z ++ }";
1015
+ let mut text = source.as_bytes().to_vec();
1016
+ let mut tree = parser.parse(&text, None).unwrap();
1017
+ assert!(!tree.root_node().has_error());
1018
+ for (start, deleted, inserted) in [
1019
+ (0, 1, "( "),
1020
+ (25, 3, ""),
1021
+ (3, 0, "++ "),
1022
+ (26, 1, ","),
1023
+ (26, 1, "y"),
1024
+ (3, 3, ""),
1025
+ (25, 0, "++ "),
1026
+ (0, 2, "{"),
1027
+ ] {
1028
+ edit(&mut tree, &mut text, start, deleted, inserted);
1029
+ tree = parser.parse(&text, Some(&tree)).unwrap();
1030
+ }
1031
+ assert_eq!(text, source.as_bytes());
1032
+ let expected = parser.parse(&text, None).unwrap();
1033
+ assert_same_cst(
1034
+ tree.root_node(),
1035
+ expected.root_node(),
1036
+ "repair after the token behind a field getline changed",
1037
+ );
1038
+ }
1039
+
1189
1040
  #[test]
1190
1041
  fn fixed_seed_getline_edit_histories_match_fresh_parses_after_recovery() {
1191
1042
  let seeds = [
1192
- "{ $getline < a < getline x++ < y }",
1193
- "{ $getline < a < getline x-- < y }",
1194
- "{ $getline < a < $getline < b < getline x++++ < y }",
1195
- "{ $getline < a < $getline < b < getline x++++++ < y }",
1196
- "{ getline $getline < a < getline x++ < y }",
1197
- "{ print ($getline < a < getline x++ < y) }",
1198
- "{ f($getline < a < getline x++ < y) }",
1199
- "{ x[$getline < a < getline x++ < y] }",
1043
+ "{ (getline line) + y++ }",
1044
+ "{ (getline line) < y }",
1045
+ "{ getline line <= y }",
1046
+ "{ f(getline line, ++y) }",
1047
+ "{ a[getline line, i++] }",
1048
+ "{ getline a[i++] }",
1049
+ "{ (getline a) + (getline b) + i++ }",
1050
+ "{ print (getline line) > out }",
1051
+ "{ $getline < (a < getline x++) < y }",
1052
+ "{ $getline < (a < getline x--) < y }",
1053
+ "{ $getline < $getline < getline x++++ < y }",
1054
+ "{ $getline < $getline < $getline < getline x++++++ < y }",
1055
+ "{ getline $getline < (a < getline x++) < y }",
1056
+ "{ print ($getline < (a < getline x++) < y) }",
1057
+ "{ f($getline < (a < getline x++) < y) }",
1058
+ "{ x[$getline < (a < getline x++) < y] }",
1200
1059
  "{ x = getline a + line b++ }",
1201
1060
  "{ a ? b : getline c-- ; (y) ? 1 : 2 }",
1202
1061
  "{ getline x++ }",
@@ -1209,8 +1068,8 @@ fn fixed_seed_getline_edit_histories_match_fresh_parses_after_recovery() {
1209
1068
  "{ print (getline x++ + y) }",
1210
1069
  "{ getline x\\\n++ }",
1211
1070
  "{ getline $getline < x++++ }",
1212
- "{ getline $getline < x | getline ++ }",
1213
- "{ getline $getline < a ? x : (y)++ }",
1071
+ "{ getline $getline < (x | getline) ++ }",
1072
+ "{ getline $getline < (a ? x : (y))++ }",
1214
1073
  "{ getline $getline < getline $getline < x++++ }",
1215
1074
  ];
1216
1075
  let mut random = 0x79d07b23u32;
@@ -1249,7 +1108,7 @@ fn fixed_seed_getline_edit_histories_match_fresh_parses_after_recovery() {
1249
1108
  };
1250
1109
  steps.push((start, deleted, inserted.clone()));
1251
1110
  edit(&mut tree, &mut text, start, deleted, &inserted);
1252
- tree = parser.parse(&text, Some(&tree)).unwrap();
1111
+ let next = parser.parse(&text, Some(&tree)).unwrap();
1253
1112
  let fresh = parser.parse(&text, None).unwrap();
1254
1113
  let expected = independent.parse(&text, None).unwrap();
1255
1114
  let context = format!(
@@ -1258,8 +1117,12 @@ fn fixed_seed_getline_edit_histories_match_fresh_parses_after_recovery() {
1258
1117
  );
1259
1118
  assert_same_cst(fresh.root_node(), expected.root_node(), &context);
1260
1119
  if !fresh.root_node().has_error() {
1261
- assert_same_cst(tree.root_node(), fresh.root_node(), &context);
1120
+ assert_same_cst(next.root_node(), fresh.root_node(), &context);
1121
+ if !tree.root_node().has_error() {
1122
+ assert_changed_scopes(&tree, &next, &context);
1123
+ }
1262
1124
  }
1125
+ tree = next;
1263
1126
  }
1264
1127
  assert_eq!(
1265
1128
  text,
@@ -1333,252 +1196,117 @@ fn getline_token_edit_histories_match_fresh_parses() {
1333
1196
  }
1334
1197
 
1335
1198
  #[test]
1336
- fn getline_postfix_concatenation_keeps_its_operands() {
1337
- for (source, concatenation, left, right) in [
1338
- ("{ -getline x++ }\n", "-getline x++", "-getline", "x++"),
1339
- (
1340
- "{ a + getline x++ }\n",
1341
- "a + getline x++",
1342
- "a + getline",
1343
- "x++",
1344
- ),
1345
- ("{ a getline x++ }\n", "a getline x++", "a getline", "x++"),
1346
- (
1347
- "{ q = $-getline x++ }\n",
1348
- "$-getline x++",
1349
- "$-getline",
1350
- "x++",
1351
- ),
1352
- (
1353
- "{ a ^ getline x++ }\n",
1354
- "a ^ getline x++",
1355
- "a ^ getline",
1356
- "x++",
1357
- ),
1358
- (
1359
- "{ getline $-getline x++ }\n",
1360
- "getline $-getline x++",
1361
- "getline $-getline",
1362
- "x++",
1363
- ),
1199
+ fn changed_ranges_include_getline_ownership_changes() {
1200
+ for (name, source, removed, inserted) in [
1364
1201
  (
1365
- "{ ++$getline x-- }\n",
1366
- "++$getline x--",
1367
- "++$getline",
1368
- "x--",
1202
+ "file source moves inward",
1203
+ "{ $getline $-getline < 1 ++ }",
1204
+ "++",
1205
+ "",
1369
1206
  ),
1370
1207
  (
1371
- "{ print $-getline x++ }\n",
1372
- "$-getline x++",
1373
- "$-getline",
1374
- "x++",
1208
+ "file source crosses two targets",
1209
+ "{ $getline $!getline $!getline < 1 ++ }",
1210
+ "++",
1211
+ "",
1375
1212
  ),
1376
- ] {
1377
- let tree = parse(source, source);
1378
- let start = source.find(concatenation).unwrap();
1379
- let expression = tree
1380
- .root_node()
1381
- .descendant_for_byte_range(start, start + concatenation.len())
1382
- .unwrap();
1383
- assert!(
1384
- expression.child_by_field_name("operator").is_none(),
1385
- "{source}"
1386
- );
1387
- for (field, text) in [("left", left), ("right", right)] {
1388
- let child = expression.child_by_field_name(field).unwrap();
1389
- assert_eq!(&source[child.byte_range()], text, "{source}: {field}");
1390
- }
1391
- let right = expression.child_by_field_name("right").unwrap();
1392
- let operator = right.child_by_field_name("operator").unwrap();
1393
- assert!(matches!(operator.kind(), "incr" | "decr"), "{source}");
1394
- }
1395
- }
1396
-
1397
- #[test]
1398
- fn redirected_getline_field_updates_keep_their_source_and_target() {
1399
- for (source, expression, filename, target, operator) in [
1400
1213
  (
1401
- "{ $getline < \"f\" ++ }\n",
1402
- "$getline < \"f\" ++",
1403
- "\"f\"",
1404
- None,
1214
+ "grouped source",
1215
+ "{ $getline $-getline < (1) ++ }",
1405
1216
  "++",
1217
+ "",
1406
1218
  ),
1219
+ ("call source", "{ $getline $-getline < f(1) ++ }", "++", ""),
1407
1220
  (
1408
- "{ $getline x < \"f\" -- }\n",
1409
- "$getline x < \"f\" --",
1410
- "\"f\"",
1411
- Some("x"),
1412
- "--",
1221
+ "subscript source",
1222
+ "{ $getline $-getline < a[1] ++ }",
1223
+ "++",
1224
+ "",
1413
1225
  ),
1414
1226
  (
1415
- "{ $getline $-x < \"f\" ++ }\n",
1416
- "$getline $-x < \"f\" ++",
1417
- "\"f\"",
1418
- Some("$-x"),
1227
+ "source inside call arguments",
1228
+ "{ f($getline $-getline < 1 ++, x) }",
1419
1229
  "++",
1230
+ "",
1420
1231
  ),
1421
1232
  (
1422
- "{ print $getline < -1 ++ }\n",
1423
- "$getline < -1 ++",
1424
- "-1",
1425
- None,
1233
+ "source inside a subscript",
1234
+ "{ a[$getline $-getline < 1 ++, x] }",
1426
1235
  "++",
1236
+ "",
1427
1237
  ),
1428
1238
  (
1429
- "{ printf $getline < y++ -- }\n",
1430
- "$getline < y++ --",
1431
- "y++",
1432
- None,
1433
- "--",
1239
+ "source inside print arguments",
1240
+ "{ print ($getline $-getline < 1 ++), x }",
1241
+ "++",
1242
+ "",
1434
1243
  ),
1435
1244
  (
1436
- "{ $getline < f() ++ }\n",
1437
- "$getline < f() ++",
1438
- "f()",
1439
- None,
1245
+ "multiline source after Unicode",
1246
+ "{ print \"é\"; $getline $-getline < \\\n1 ++; print 2 }\n{ print 3 }",
1440
1247
  "++",
1248
+ "",
1441
1249
  ),
1442
- ] {
1443
- let tree = parse(source, source);
1444
- let start = source.find(expression).unwrap();
1445
- let update = tree
1446
- .root_node()
1447
- .descendant_for_byte_range(start, start + expression.len())
1448
- .unwrap();
1449
- let update_operator = update.child_by_field_name("operator").unwrap();
1450
- assert_eq!(&source[update_operator.byte_range()], operator, "{source}");
1451
- let field = update.child_by_field_name("operand").unwrap();
1452
- assert_eq!(field.kind(), "lvalue", "{source}");
1453
- assert_eq!(field.start_byte(), start, "{source}");
1454
- assert_eq!(
1455
- field.end_byte(),
1456
- update_operator.start_byte() - 1,
1457
- "{source}"
1458
- );
1459
- let input = field
1460
- .child_by_field_name("operand")
1461
- .unwrap()
1462
- .named_child(0)
1463
- .unwrap()
1464
- .named_child(0)
1465
- .unwrap();
1466
- assert_eq!(input.kind(), "non_unary_input_function", "{source}");
1467
- let file = input.child_by_field_name("source").unwrap();
1468
- assert_eq!(&source[file.byte_range()], filename, "{source}");
1469
- let get = input.child_by_field_name("get").unwrap();
1470
- assert_eq!(
1471
- get
1472
- .child_by_field_name("target")
1473
- .map(|node| &source[node.byte_range()]),
1474
- target,
1475
- "{source}"
1476
- );
1477
- }
1478
- }
1479
-
1480
- #[test]
1481
- fn getline_source_lookahead_preserves_target_ownership() {
1482
- for (expression, target) in [
1483
- ("getline $getline < \"f\"", Some("$getline < \"f\"")),
1484
- ("getline $getline < \"f\" ++", None),
1485
- ("getline $getline < x++", Some("$getline < x++")),
1486
- ("getline $getline < a < getline x++ < y", None),
1487
- ("getline $getline < x++++", None),
1488
- ("getline $getline $-x < \"f\" ++", None),
1489
- ("getline $getline < x | getline ++", None),
1490
1250
  (
1491
- "getline $getline < a ? x : y++",
1492
- Some("$getline < a ? x : y++"),
1251
+ "target and update change owners",
1252
+ "{ getline x++ }",
1253
+ "++",
1254
+ "++ y",
1493
1255
  ),
1494
- ("getline $getline < a ? x : (y)++", None),
1495
- ("getline $getline < 1e++", Some("$getline < 1e++")),
1496
- ("getline x++ $getline $getline < 1 ++", Some("x")),
1497
- ("getline $getline < $getline < 1 in a 1++", None),
1498
- ("getline $getline < $getline < x in a < \"f\" ++", None),
1499
- ("getline $getline < x < y < z | getline ++", None),
1500
- ("getline $getline < x ~ y ~ 1 | getline ++", None),
1501
- ("getline $getline < x ~ y | getline z ~ \"f\" ++", None),
1502
- ("getline $getline $-x < x | getline ++", None),
1503
1256
  (
1504
- "getline $getline < getline $getline < x++++",
1505
- Some("$getline < getline $getline < x++++"),
1257
+ "grammar path changes to selected path",
1258
+ "{ getline x }",
1259
+ "x",
1260
+ "x++",
1506
1261
  ),
1507
- ] {
1508
- let source = format!("{{ {expression} }}\n");
1509
- let tree = parse(&source, expression);
1510
- let keyword = tree.root_node().descendant_for_byte_range(2, 8).unwrap();
1511
- assert_eq!(keyword.kind(), "getline_keyword", "{expression}");
1512
- let get = keyword.parent().unwrap();
1513
- assert_eq!(get.kind(), "simple_get", "{expression}");
1514
- assert_eq!(
1515
- get
1516
- .child_by_field_name("target")
1517
- .map(|node| &source[node.byte_range()]),
1518
- target,
1519
- "{expression}",
1520
- );
1521
- }
1522
- }
1523
-
1524
- #[test]
1525
- fn source_getline_targets_can_leave_postfix_updates_for_enclosing_fields() {
1526
- for (source, targets, operands) in [
1527
1262
  (
1528
- "{ $getline < a < getline x++ < y }",
1529
- vec![None, Some("x")],
1530
- vec!["$getline < a < getline x"],
1263
+ "removing a group makes the comparison a file source",
1264
+ "{ (getline x) < file }",
1265
+ "(getline x)",
1266
+ "getline x",
1531
1267
  ),
1532
1268
  (
1533
- "{ $getline < a < getline x-- < y }",
1534
- vec![None, Some("x")],
1535
- vec!["$getline < a < getline x"],
1269
+ "a comparison changes to a file source operator",
1270
+ "{ getline x <= file }",
1271
+ "<=",
1272
+ "<",
1536
1273
  ),
1537
1274
  (
1538
- "{ $getline < a < $getline < b < getline x++++ < y }",
1539
- vec![None, None, Some("x")],
1540
- vec![
1541
- "$getline < a < $getline < b < getline x++",
1542
- "$getline < b < getline x",
1543
- ],
1275
+ "merging arguments changes getline ownership",
1276
+ "{ f(getline x, y++) }",
1277
+ ",",
1278
+ "+",
1544
1279
  ),
1545
1280
  (
1546
- "{ getline $getline < a < getline x++ < y }",
1547
- vec![None, None, Some("x")],
1548
- vec!["$getline < a < getline x"],
1281
+ "merging subscripts changes getline ownership",
1282
+ "{ a[getline x, y++] }",
1283
+ ",",
1284
+ "+",
1549
1285
  ),
1550
- ("{ $getline < getline x++ }", vec![None, None], vec!["x"]),
1551
1286
  (
1552
- "{ $getline < f(getline x++)++ < y }",
1553
- vec![None, None],
1554
- vec!["$getline < f(getline x++)", "x"],
1287
+ "an update after a target subscript needs selection",
1288
+ "{ getline a[i++] }",
1289
+ "]",
1290
+ "]++",
1555
1291
  ),
1556
1292
  ] {
1557
- let tree = parse(source, source);
1558
- let mut pending = vec![tree.root_node()];
1559
- let mut actual_targets = Vec::new();
1560
- let mut actual_operands = Vec::new();
1561
- while let Some(node) = pending.pop() {
1562
- if node.kind() == "simple_get" {
1563
- actual_targets.push(
1564
- node
1565
- .child_by_field_name("target")
1566
- .map(|target| &source[target.byte_range()]),
1567
- );
1568
- }
1569
- if let Some(operator) = node.child_by_field_name("operator")
1570
- && matches!(operator.kind(), "incr" | "decr")
1571
- {
1572
- let operand = node.child_by_field_name("operand").unwrap();
1573
- assert_eq!(operand.kind(), "lvalue", "{source}");
1574
- actual_operands.push(&source[operand.byte_range()]);
1575
- }
1576
- for index in (0..node.child_count()).rev() {
1577
- pending.push(node.child(index).unwrap());
1578
- }
1293
+ let mut parser = parser();
1294
+ let mut text = source.as_bytes().to_vec();
1295
+ let mut tree = parse(source, name);
1296
+ let start = source.find(removed).unwrap();
1297
+ for (deleted, inserted) in [
1298
+ (removed.len(), inserted),
1299
+ (inserted.len(), removed),
1300
+ (removed.len(), inserted),
1301
+ ] {
1302
+ edit(&mut tree, &mut text, start, deleted, inserted);
1303
+ let next = parser.parse(&text, Some(&tree)).unwrap();
1304
+ let fresh = parser.parse(&text, None).unwrap();
1305
+ assert!(!fresh.root_node().has_error(), "{name}");
1306
+ assert_same_cst(next.root_node(), fresh.root_node(), name);
1307
+ assert_changed_scopes(&tree, &next, name);
1308
+ tree = next;
1579
1309
  }
1580
- assert_eq!(actual_targets, targets, "{source}: targets");
1581
- assert_eq!(actual_operands, operands, "{source}: update operands");
1582
1310
  }
1583
1311
  }
1584
1312
 
@@ -1592,13 +1320,20 @@ fn getline_expression_edit_histories_restore_fresh_csts() {
1592
1320
  "{ getline $getline < \"f\" ++ }\n".to_string(),
1593
1321
  "{ getline $getline < x++ }\n".to_string(),
1594
1322
  "{ getline $getline < x++++ }\n".to_string(),
1595
- "{ getline $getline < x | getline ++ }\n".to_string(),
1596
- "{ getline $getline < a ? x : (y)++ }\n".to_string(),
1323
+ "{ getline $getline < (x | getline) ++ }\n".to_string(),
1324
+ "{ getline $getline < (a ? x : (y))++ }\n".to_string(),
1597
1325
  "{ getline $getline < a && # source\n x++ }\n".to_string(),
1598
1326
  "{ getline x++ $getline $getline < 1 ++ }\n".to_string(),
1599
1327
  "{ getline $getline < getline $getline < x++++ }\n".to_string(),
1600
- "{ getline $getline < $getline < 1 in a 1++ }\n".to_string(),
1601
- "{ getline $getline < x < y < z | getline ++ }\n".to_string(),
1328
+ "{ getline $getline < (($getline < 1 in a) 1)++ }\n".to_string(),
1329
+ "{ getline $getline < (x < (y < z | getline)) ++ }\n".to_string(),
1330
+ "{ $-getline $-getline < a }\n".to_string(),
1331
+ "{ $-getline $-getline y < a }\n".to_string(),
1332
+ "{ $-getline $-getline < a ++ }\n".to_string(),
1333
+ "{ $getline $-getline $-getline < a }\n".to_string(),
1334
+ "{ $getline $-getline < 1 ++ }\n".to_string(),
1335
+ "{ $getline $!getline < x -- ++ }\n".to_string(),
1336
+ "{ $getline $--$getline < 1 ++ }\n".to_string(),
1602
1337
  ];
1603
1338
  for prefix in [
1604
1339
  "",