-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathrainbow_utils.js
More file actions
1339 lines (1181 loc) · 61.6 KB
/
rainbow_utils.js
File metadata and controls
1339 lines (1181 loc) · 61.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const os = require('os');
const fs = require('fs');
const path = require('path');
const rbql = require('./rbql_core/rbql-js/rbql.js');
const rbql_csv = require('./rbql_core/rbql-js/rbql_csv.js');
const csv_utils = require('./rbql_core/rbql-js/csv_utils.js');
const fast_load_utils = require('./fast_load_utils.js');
const wcwidth = require('./contrib/wcwidth/index.js');
// TODO Allow the number regex to end with dot e.g. 12. or 1245. without the fractional part.
// Otherwise as soon as someone start typing a fractional part it immediately renders the whole column as non-number for live CSV editing.
const number_regex = /^([0-9]+)(\.[0-9]+)?$/;
// Copypasted from extension.js
const QUOTED_RFC_POLICY = 'quoted_rfc';
const QUOTED_POLICY = 'quoted';
const max_preview_field_length = 250;
class AssertionError extends Error {}
function assert(condition, message=null) {
if (!condition) {
if (!message) {
message = 'Assertion error';
}
throw new AssertionError(message);
}
}
function get_default_js_udf_content() {
let default_content = `// This file can be used to store RBQL UDFs. Example:
//
// function foo(value) {
// return 'foo ' + String(value.length);
// }
//
// Functions defined in this file can be used in RBQL queries e.g.
// SELECT foo(a1), a2 WHERE foo(a3) != 'foo 5' LIMIT 10
//
// Don't forget to save this file after editing!
//
// Write your own functions below this line:
`.replace(new RegExp(/^ */, 'mg'), '');
return default_content;
}
function get_default_python_udf_content() {
let default_content = `# This file can be used to store RBQL UDFs. Example:
#
# def foo(value):
# return 'foo ' + str(len(value))
#
#
# Functions defined in this file can be used in RBQL queries e.g.
# SELECT foo(a1), a2 WHERE foo(a3) != 'foo 5' LIMIT 10
#
# Don't forget to save this file after editing!
#
# Write your own functions below this line:
`.replace(new RegExp(/^ */, 'mg'), '');
return default_content;
}
function get_cursor_position_if_unambiguous(active_editor) {
let selections = active_editor.selections;
if (!selections || selections.length != 1) {
// Support only single-cursor, multicursor is ambiguous.
return null;
}
let selection = selections[0];
let position = selection.active;
if (!position.isEqual(selection.anchor)) {
// Selections are ambiguous.
return null;
}
return position;
}
function is_ascii(src_str) {
return /^[\x00-\x7F]*$/.test(src_str);
}
class ColumnStat {
constructor(enable_double_width_alignment) {
this.enable_double_width_alignment = enable_double_width_alignment;
this.can_be_numeric = true;
this.max_total_length = 0;
this.max_int_length = 0;
this.max_fractional_length = 0;
this.only_ascii = enable_double_width_alignment ? true : null;
this.has_wide_chars = enable_double_width_alignment ? false : null;
}
mark_non_numeric() {
this.can_be_numeric = false;
this.max_int_length = 0;
this.max_fractional_length = 0;
}
is_numeric() {
return this.max_int_length > 0;
}
reconcile(rhs) {
this.max_total_length = Math.max(this.max_total_length, rhs.max_total_length);
if (this.enable_double_width_alignment) {
assert(rhs.enable_double_width_alignment, "Unable to reconcile");
this.only_ascii = this.only_ascii && rhs.only_ascii;
this.has_wide_chars = this.has_wide_chars || rhs.has_wide_chars;
}
if (!rhs.can_be_numeric) {
this.mark_non_numeric();
}
if (!this.can_be_numeric) {
return;
}
this.max_int_length = Math.max(this.max_int_length, rhs.max_int_length);
this.max_fractional_length = Math.max(this.max_fractional_length, rhs.max_fractional_length);
}
get_adjusted_total_length() {
// The sum of integer and float parts can be bigger than the max width, e.g. here:
// value
// 0.12
// 1234
return Math.max(this.max_total_length, this.max_int_length + this.max_fractional_length);
}
get_adjusted_int_length() {
// This is needed when the header is wider than numeric components and/or their sum.
return Math.max(this.max_int_length, this.max_total_length - this.max_fractional_length);
}
update_from_field(multiline_field_segments, is_first_record) {
if (multiline_field_segments.length > 1) {
// We don't allow multiline fields to be numeric for simplicity.
this.mark_non_numeric();
}
for (let field_segment of multiline_field_segments) {
this.max_total_length = Math.max(this.max_total_length, field_segment.length);
if (this.enable_double_width_alignment) {
this.only_ascii = this.only_ascii && is_ascii(field_segment);
let visual_field_length = this.only_ascii ? field_segment.length : wcwidth(field_segment);
this.has_wide_chars = this.has_wide_chars || visual_field_length != field_segment.length;
this.max_total_length = Math.max(this.max_total_length, visual_field_length);
}
if (!this.can_be_numeric) {
continue;
}
// TODO improve number_regex and subsequent logic to work with numeric fields with leading/trailing spaces for virtual alignment.
let match_result = number_regex.exec(field_segment);
if (match_result === null) {
if (!is_first_record && field_segment.length) { // Checking field_length here allows numeric columns to have some of the fields empty.
// We only mark the column as non-numeric if we know that this is not a header line.
this.mark_non_numeric();
}
continue;
}
let cur_integer_part_length = match_result[1].length;
this.max_int_length = Math.max(this.max_int_length, cur_integer_part_length);
let cur_fractional_part_length = match_result[2] === undefined ? 0 : match_result[2].length;
this.max_fractional_length = Math.max(this.max_fractional_length, cur_fractional_part_length);
}
}
evaluate_align_field(field, is_first_record, is_first_in_line, is_last_in_line, post_delim_readability_gap_length) {
// Align field, use Math.max() to avoid negative delta_length which can happen theorethically due to async doc edit.
let visual_field_length = this.has_wide_chars ? wcwidth(field) : field.length;
let readability_gap = is_first_in_line ? 0 : post_delim_readability_gap_length;
if (!this.is_numeric()) {
let delta_length = Math.max(this.max_total_length - visual_field_length, 0);
let trailing_length = is_last_in_line ? 0 : delta_length;
return [readability_gap, trailing_length];
}
if (is_first_record) {
if (number_regex.exec(field) === null) {
// The line must be a header - align it using max_width rule.
let delta_length = Math.max(this.get_adjusted_total_length() - visual_field_length, 0);
let trailing_length = is_last_in_line ? 0 : delta_length;
return [readability_gap, trailing_length];
}
}
let dot_pos = field.indexOf('.');
let cur_integer_part_length = dot_pos == -1 ? field.length : dot_pos;
// Here `cur_fractional_part_length` includes the leading dot too.
let cur_fractional_part_length = dot_pos == -1 ? 0 : field.length - dot_pos;
let integer_delta_length = Math.max(this.get_adjusted_int_length() - cur_integer_part_length, 0);
let fractional_delta_length = Math.max(this.max_fractional_length - cur_fractional_part_length);
let trailing_length = is_last_in_line ? 0 : fractional_delta_length;
return [integer_delta_length + readability_gap, trailing_length];
}
}
function update_column_stats_from_record(record_fields, is_first_record, all_columns_stats, enable_double_width_alignment) {
for (let fnum = 0; fnum < record_fields.length; fnum++) {
if (all_columns_stats.length <= fnum) {
all_columns_stats.push(new ColumnStat(enable_double_width_alignment));
}
all_columns_stats[fnum].update_from_field(record_fields[fnum], is_first_record);
}
}
function get_trimmed_rfc_record_fields_from_record(record) {
let record_fields = [];
for (let fnum = 0; fnum < record.length; fnum++) {
let field_segments = record[fnum].split('\n');
field_segments = field_segments.map(v => v.trim());
record_fields.push(field_segments);
}
return record_fields;
}
function calculate_column_offsets(column_stats, delim_length, post_delim_readability_gap_length=1) {
let result = [];
if (!column_stats.length) {
return result;
}
result.push(0);
for (let i = 1; i < column_stats.length; i++) {
let previous_length = column_stats[i - 1].get_adjusted_total_length();
let previous_offset = result[i - 1];
result.push(previous_offset + previous_length + delim_length + post_delim_readability_gap_length);
}
assert(result.length == column_stats.length);
return result;
}
function calc_column_stats(active_doc, delim, policy, comment_prefix, enable_double_width_alignment) {
let [records, _num_records_parsed, _fields_info, first_defective_line, _first_trailing_space_line, comments] = fast_load_utils.parse_document_records(active_doc, delim, policy, comment_prefix, /*stop_on_warning=*/true, /*max_records_to_parse=*/-1, /*collect_records=*/true, /*preserve_quotes_and_whitespaces=*/true);
if (first_defective_line !== null) {
return [null, first_defective_line, null, null];
}
let all_columns_stats = [];
let is_first_record = true;
for (let record of records) {
// TODO for optimization we can actually return a new array of record_fields to avoid doing the same thing again in align.
let record_fields = get_trimmed_rfc_record_fields_from_record(record);
update_column_stats_from_record(record_fields, is_first_record, all_columns_stats, enable_double_width_alignment);
is_first_record = false;
}
return [all_columns_stats, null, records, comments];
}
function calc_column_stats_for_fragment(row_infos, header_lnum, enable_double_width_alignment) {
// Note: `row_infos` can contain non-consecutive fragments of the document e.g. header and view range below.
let all_columns_stats = [];
for (let row_info of row_infos) {
if (row_info.comment_range !== null) {
continue;
}
let is_assumed_header_record = row_info.record_ranges.length && row_info.record_ranges[0].length && row_info.record_ranges[0][0].start.line == header_lnum;
update_column_stats_from_record(row_info.record_fields, is_assumed_header_record, all_columns_stats, enable_double_width_alignment);
}
return all_columns_stats;
}
function reconcile_whole_doc_and_local_column_stats(whole_doc_column_stats, local_column_stats) {
let max_num_fields = Math.max(whole_doc_column_stats.length, local_column_stats.length);
for (let i = 0; i < max_num_fields; i++) {
if (i >= whole_doc_column_stats.length) {
break;
}
if (i >= local_column_stats.length) {
local_column_stats.push(whole_doc_column_stats[i]);
continue;
}
local_column_stats[i].reconcile(whole_doc_column_stats[i]);
}
}
function evaluate_rfc_align_field(field, is_first_record, column_stat, column_offset, is_field_segment, is_first_in_line, is_last_in_line, post_delim_readability_gap_length=1) {
let [num_before, num_after] = column_stat.evaluate_align_field(field, is_first_record, is_first_in_line, is_last_in_line, post_delim_readability_gap_length);
if (is_field_segment) {
num_before += column_offset;
}
return [num_before, num_after];
}
function rfc_align_field(field, is_first_record, column_stat, column_offset, is_field_segment, is_first_in_line, is_last_in_line) {
let [num_before, num_after] = evaluate_rfc_align_field(field, is_first_record, column_stat, column_offset, is_field_segment, is_first_in_line, is_last_in_line);
return ' '.repeat(num_before) + field + ' '.repeat(num_after);
}
function calc_max_column_widths(records) {
if (!records.length) {
return [];
}
let column_widths = [];
for (let row = 0; row < records.length; row++) {
for (let col = 0; col < records[row].length; col++) {
if (column_widths.length <= col) {
column_widths.push(0);
}
column_widths[col] = Math.max(column_widths[col], records[row][col].length);
}
}
return column_widths;
}
function generate_markdown_lines(records) {
for (let row = 0; row < records.length; row++) {
for (let col = 0; col < records[row].length; col++) {
// Escape pipe char.
records[row][col] = records[row][col].replaceAll('|', '\\|');
}
}
let max_column_widths = calc_max_column_widths(records);
let markdown_table_cells = [];
for (let row = 0; row < records.length; row++) {
let markdown_row_cells = [];
for (let col = 0; col < records[row].length; col++) {
// Here we ensure that `delta_length` is positive just in case: the substraction result can't become negative.
let delta_length = Math.max(0, max_column_widths[col] - records[row][col].length);
let aligned_cell = records[row][col] + ' '.repeat(delta_length);
markdown_row_cells.push(aligned_cell);
}
markdown_table_cells.push(markdown_row_cells);
}
let markdown_lines = markdown_table_cells.map(v => '| ' + v.join(' | ') + ' |');
let header_separator_line = '| ' + (max_column_widths.map(v => '-'.repeat(v))).join(' | ') + ' |';
markdown_lines.splice(1, 0, header_separator_line);
return markdown_lines;
}
class RecordCommentMerger {
constructor(records, comments) {
this.records = records;
this.comments = comments;
this.nr = 0;
this.next_comment = 0;
}
get_next() {
// Returns tuple (record, comment).
if (this.has_comments_left() && (!this.has_records_left() || this.comments[this.next_comment].record_num <= this.nr)) {
let result = [null, this.comments[this.next_comment].comment_text];
this.next_comment += 1;
return result;
}
if (this.has_records_left()) {
let result = [this.records[this.nr], null];
this.nr += 1;
return result;
}
return [null, null];
}
has_comments_left() {
return this.next_comment < this.comments.length;
}
has_records_left() {
return this.nr < this.records.length;
}
has_entries_left() {
return this.has_comments_left() || this.has_records_left();
}
}
function generate_inlay_hints(vscode, visible_range, header_lnum, table_ranges, all_columns_stats, delim_length, alignment_char, enable_vertical_grid, post_delim_readability_gap_length) {
// NOTE: `table_ranges` can contain non-consecutive fragments of the document e.g. header and view range below.
assert(alignment_char.length == 1);
let column_offsets = calculate_column_offsets(all_columns_stats, delim_length, post_delim_readability_gap_length);
let inlay_hints = [];
// Setting hint display margin too high could prevent lower hint labels from diplaying. There is a non-configurable VSCode limit apparently, see also https://github.com/mechatroner/vscode_rainbow_csv/issues/205
// We set higher limit below because it is the bottom lines that would be non-aligned due to the max inlay hint limit reached. Actually we might not need the bottom limit at all.
// Plus the more typical scroll direction is from top to bottom.
let leading_aligned_margin = 30;
let trailing_aligned_margin = 30;
let vscode_max_num_inline_hints = 1500;
if ((leading_aligned_margin + visible_range.end.line - visible_range.start.line + trailing_aligned_margin) * all_columns_stats.length > vscode_max_num_inline_hints) {
leading_aligned_margin = 5;
}
let hint_display_start_line = visible_range.start.line - leading_aligned_margin;
let hint_display_end_line = visible_range.end.line + trailing_aligned_margin;
for (let row_info of table_ranges) {
if (row_info.comment_range !== null) {
continue;
}
let is_assumed_header_record = row_info.record_ranges.length && row_info.record_ranges[0].length && row_info.record_ranges[0][0].start.line === header_lnum;
if (row_info.record_fields.length != row_info.record_ranges.length) {
break; // Should never happen.
}
for (let fnum = 0; fnum < row_info.record_fields.length; fnum++) {
if (fnum >= all_columns_stats.length) {
break; // Should never happen.
}
let is_last_field = fnum + 1 == row_info.record_fields.length;
let field_segments = row_info.record_fields[fnum];
let field_segments_ranges = row_info.record_ranges[fnum];
if (field_segments.length != field_segments_ranges.length) {
break; // Should never happen.
}
for (let i = 0; i < field_segments.length; i++) {
let field_segment_range = field_segments_ranges[i];
let is_field_segment = i > 0;
let is_last_in_line = is_last_field || i + 1 < field_segments.length;
let is_first_in_line = (fnum == 0) || is_field_segment;
let [num_before, num_after] = evaluate_rfc_align_field(field_segments[i], is_assumed_header_record, all_columns_stats[fnum], column_offsets[fnum], is_field_segment, is_first_in_line, is_last_in_line, post_delim_readability_gap_length);
if (num_before > 0) {
let hint_label = '';
if (!enable_vertical_grid || is_field_segment) {
hint_label += alignment_char.repeat(num_before);
} else {
// TODO find a way to "draw" vertical grid by extending hints for multiline rfc fields. Add a screenshot to README when implemented.
hint_label = '\u2588';
hint_label += alignment_char.repeat(num_before - 1);
}
let hint_line = field_segment_range.start.line;
if ((hint_line === header_lnum) || (hint_line >= hint_display_start_line && hint_line <= hint_display_end_line)) {
inlay_hints.push(new vscode.InlayHint(field_segment_range.start, hint_label));
}
}
if (num_after > 0) {
let hint_label = alignment_char.repeat(num_after);
let hint_line = field_segment_range.end.line;
if ((hint_line === header_lnum) || (hint_line >= hint_display_start_line && hint_line <= hint_display_end_line)) {
inlay_hints.push(new vscode.InlayHint(field_segment_range.end, hint_label));
}
}
}
}
}
return inlay_hints;
}
function align_columns(records, comments, column_stats, delim) {
// Unlike shrink_columns, here we don't compute `has_edit` flag because it is
// 1: Algorithmically complicated (especially for multiline fields) and we also can't just compare fields lengths like in shrink.
// 2: The alignment procedure is opinionated and "Already aligned" report has little value.
// Because of this in case of executing "Align" command consecutively N times one would have to run undo N times too.
// Here records do not contain delim and column_stats were calculated without taking the delim length into account.
let column_offsets = calculate_column_offsets(column_stats, delim.length);
let result_lines = [];
let is_first_record = true;
let merger = new RecordCommentMerger(records, comments);
while (merger.has_entries_left()) {
let [record, comment] = merger.get_next();
assert((comment === null) != (record === null));
if (record === null) {
result_lines.push(comment);
continue;
}
let aligned_fields = [];
let record_fields = get_trimmed_rfc_record_fields_from_record(record);
for (let fnum = 0; fnum < record_fields.length; fnum++) {
if (fnum >= column_stats.length) // Safeguard against async doc edit, should never happen.
break;
let is_last_field = fnum + 1 == record_fields.length;
let field_segments = record_fields[fnum];
for (let i = 0; i < field_segments.length; i++) {
let is_field_segment = i > 0;
if (is_field_segment) {
result_lines.push(aligned_fields.join(delim));
aligned_fields = [];
}
let is_last_in_line = is_last_field || i + 1 < field_segments.length;
let is_first_in_line = (fnum == 0) || is_field_segment;
let aligned_field = rfc_align_field(field_segments[i], is_first_record, column_stats[fnum], column_offsets[fnum], is_field_segment, is_first_in_line, is_last_in_line);
aligned_fields.push(aligned_field);
}
}
is_first_record = false;
result_lines.push(aligned_fields.join(delim));
}
return result_lines;
}
function shrink_columns(active_doc, delim, policy, comment_prefix) {
let [records, _num_records_parsed, _fields_info, first_defective_line, _first_trailing_space_line, comments] = fast_load_utils.parse_document_records(active_doc, delim, policy, comment_prefix, /*stop_on_warning=*/true, /*max_records_to_parse=*/-1, /*collect_records=*/true, /*preserve_quotes_and_whitespaces=*/true);
if (first_defective_line !== null) {
return [null, first_defective_line];
}
let result_lines = [];
let has_edit = false;
let merger = new RecordCommentMerger(records, comments);
while (merger.has_entries_left()) {
let [record, comment] = merger.get_next();
assert((comment === null) != (record === null));
if (record === null) {
result_lines.push(comment);
continue;
}
let aligned_fields = [];
for (let fnum = 0; fnum < record.length; fnum++) {
let field = record[fnum];
let field_lines = field.split('\n');
for (let i = 0; i < field_lines.length; i++) {
if (i > 0) {
result_lines.push(aligned_fields.join(delim));
aligned_fields = [];
}
let aligned_field = field_lines[i].trim();
if (aligned_field.length != field_lines[i].length) {
// Unlike in align function here we can just compare resulting length to decide if change has occured.
has_edit = true;
}
aligned_fields.push(aligned_field);
}
}
result_lines.push(aligned_fields.join(delim));
}
if (!has_edit)
return [null, null];
return [result_lines.join('\n'), null];
}
function make_table_name_key(file_path) {
return 'rbql_table_name:' + file_path;
}
function expanduser(filepath) {
if (filepath.charAt(0) === '~') {
return path.join(os.homedir(), filepath.slice(1));
}
return filepath;
}
function find_table_path(vscode_global_state, main_table_dir, table_id) {
// If table_id is a relative path it could be relative either to the current directory or to the main table dir.
var candidate_path = expanduser(table_id);
if (fs.existsSync(candidate_path)) {
return candidate_path;
}
if (main_table_dir && !path.isAbsolute(candidate_path)) {
candidate_path = path.join(main_table_dir, candidate_path);
if (fs.existsSync(candidate_path)) {
return candidate_path;
}
}
let table_path = vscode_global_state ? vscode_global_state.get(make_table_name_key(table_id)) : null;
if (table_path && fs.existsSync(table_path)) {
return table_path;
}
return null;
}
async function read_header(table_path, encoding) {
if (encoding == 'latin-1')
encoding = 'binary';
let readline = require('readline');
let input_reader = readline.createInterface({ input: fs.createReadStream(table_path, {encoding: encoding}) });
let closed = false;
let promise_resolve = null;
let promise_reject = null;
let output_promise = new Promise(function(resolve, reject) {
promise_resolve = resolve;
promise_reject = reject;
});
input_reader.on('line', line => {
if (!closed) {
closed = true;
input_reader.close();
promise_resolve(line);
}
});
input_reader.on('error', error => {
promise_reject(error);
});
return output_promise;
}
function get_header_line(document, comment_prefix) {
const num_lines = document.lineCount;
for (let lnum = 0; lnum < num_lines; ++lnum) {
const line_text = document.lineAt(lnum).text;
if (!comment_prefix || !line_text.startsWith(comment_prefix)) {
return [lnum, line_text];
}
}
return [null, null];
}
function make_inconsistent_num_fields_warning(table_name, inconsistent_records_info) {
let [record_num_1, num_fields_1, record_num_2, num_fields_2] = rbql.sample_first_two_inconsistent_records(inconsistent_records_info);
let warn_msg = `Number of fields in "${table_name}" table is not consistent: `;
warn_msg += `e.g. record ${record_num_1 + 1} -> ${num_fields_1} fields, record ${record_num_2 + 1} -> ${num_fields_2} fields`;
return warn_msg;
}
class RbqlIOHandlingError extends Error {}
class VSCodeRecordIterator extends rbql.RBQLInputIterator {
constructor(document, delim, policy, has_header=false, comment_prefix=null, table_name='input', variable_prefix='a', trim_whitespaces=false) {
// We could have done a hack here actually: convert the document to stream/buffer and then use the standard reader.
super();
this.has_header = has_header;
this.table_name = table_name;
this.variable_prefix = variable_prefix;
this.NR = 0; // Record number.
this.NL = 0; // Line number (NL != NR when the CSV file has comments or multiline fields).
let fail_on_warning = policy == 'quoted_rfc';
let [_num_records_parsed, _comments] = [null, null];
[this.records, _num_records_parsed, this.fields_info, this.first_defective_line, this._first_trailing_space_line, _comments] = fast_load_utils.parse_document_records(document, delim, policy, comment_prefix, fail_on_warning, /*max_records_to_parse=*/-1, /*collect_records=*/true, /*preserve_quotes_and_whitespaces=*/false, /*detect_trailing_spaces=*/false, /*min_num_fields_for_autodetection=*/-1, trim_whitespaces);
if (fail_on_warning && this.first_defective_line !== null) {
throw new RbqlIOHandlingError(`Inconsistent double quote escaping in ${this.table_name} table at record ${this.records.length}, line ${this.first_defective_line + 1}`);
}
this.first_record = this.records.length ? this.records[0] : [];
this.next_record_index = 0;
}
stop() {
}
async get_variables_map(query_text) {
let variable_map = new Object();
rbql.parse_basic_variables(query_text, this.variable_prefix, variable_map);
rbql.parse_array_variables(query_text, this.variable_prefix, variable_map);
if (this.has_header) {
rbql.parse_attribute_variables(query_text, this.variable_prefix, this.first_record, 'CSV header line', variable_map);
rbql.parse_dictionary_variables(query_text, this.variable_prefix, this.first_record, variable_map);
}
return variable_map;
}
async get_header() {
return this.has_header ? this.first_record : null;
}
do_get_record() {
if (this.next_record_index >= this.records.length) {
return null;
}
let record = this.records[this.next_record_index];
this.next_record_index += 1;
return record;
}
async get_record() {
if (this.NR == 0 && this.has_header) {
this.do_get_record(); // Skip the header record.
}
this.NR += 1;
let record = this.do_get_record();
return record;
}
get_warnings() {
let result = [];
if (this.first_defective_line !== null)
result.push(`Inconsistent double quote escaping in ${this.table_name} table. E.g. at line ${this.first_defective_line + 1}`);
if (this.fields_info.size > 1)
result.push(make_inconsistent_num_fields_warning(this.table_name, this.fields_info));
return result;
}
}
class VSCodeWriter extends rbql.RBQLOutputWriter {
constructor(delim, policy) {
super();
this.delim = delim;
this.policy = policy;
this.header_len = null;
this.null_in_output = false;
this.delim_in_simple_output = false;
this.output_lines = [];
if (policy == 'simple') {
this.polymorphic_join = this.simple_join;
} else if (policy == 'quoted') {
this.polymorphic_join = this.quoted_join;
} else if (policy == 'quoted_rfc') {
this.polymorphic_join = this.quoted_join_rfc;
} else if (policy == 'monocolumn') {
this.polymorphic_join = this.mono_join;
} else if (policy == 'whitespace') {
this.polymorphic_join = this.simple_join;
} else {
throw new RbqlIOHandlingError('Unknown output csv policy');
}
}
set_header(header) {
if (header !== null) {
this.header_len = header.length;
this.write(header);
}
}
quoted_join(fields) {
let delim = this.delim;
var quoted_fields = fields.map(function(v) { return csv_utils.quote_field(String(v), delim); });
return quoted_fields.join(this.delim);
};
quoted_join_rfc(fields) {
let delim = this.delim;
var quoted_fields = fields.map(function(v) { return csv_utils.rfc_quote_field(String(v), delim); });
return quoted_fields.join(this.delim);
};
mono_join(fields) {
if (fields.length > 1) {
throw new RbqlIOHandlingError('Unable to use "Monocolumn" output format: some records have more than one field');
}
return fields[0];
};
simple_join(fields) {
var res = fields.join(this.delim);
if (fields.join('').indexOf(this.delim) != -1) {
this.delim_in_simple_output = true;
}
return res;
};
normalize_fields(out_fields) {
for (var i = 0; i < out_fields.length; i++) {
if (out_fields[i] == null) {
this.null_in_output = true;
out_fields[i] = '';
} else if (Array.isArray(out_fields[i])) {
this.normalize_fields(out_fields[i]);
out_fields[i] = out_fields[i].join(this.sub_array_delim);
}
}
};
write(fields) {
if (this.header_len !== null && fields.length != this.header_len)
throw new RbqlIOHandlingError(`Inconsistent number of columns in output header and the current record: ${this.header_len} != ${fields.length}`);
this.normalize_fields(fields);
this.output_lines.push(this.polymorphic_join(fields));
return true;
};
async finish() {
}
get_warnings() {
let result = [];
if (this.null_in_output)
result.push('null values in output were replaced by empty strings');
if (this.delim_in_simple_output)
result.push('Some output fields contain separator');
return result;
};
}
class VSCodeTableRegistry {
constructor(){}
get_iterator_by_table_id(_table_id) {
throw new RbqlIOHandlingError("JOIN queries are currently not supported in vscode.dev web version.");
}
get_warnings() {
return [];
};
}
async function rbql_query_web(query_text, input_document, input_delim, input_policy, output_delim, output_policy, output_warnings, with_headers, comment_prefix=null, trim_whitespaces=false) {
let user_init_code = ''; // TODO find a way to have init code.
let join_tables_registry = new VSCodeTableRegistry(); // TODO find a way to have join registry.
let input_iterator = new VSCodeRecordIterator(input_document, input_delim, input_policy, with_headers, comment_prefix, /*table_name=*/'input', /*variable_prefix=*/'a', trim_whitespaces);
let output_writer = new VSCodeWriter(output_delim, output_policy);
await rbql.query(query_text, input_iterator, output_writer, output_warnings, join_tables_registry, user_init_code);
return output_writer.output_lines;
}
class VSCodeFileSystemCSVRegistry extends rbql.RBQLTableRegistry {
constructor(vscode_global_state, input_file_dir, delim, policy, encoding, has_header=false, comment_prefix=null, options=null) {
super();
this.vscode_global_state = vscode_global_state;
this.input_file_dir = input_file_dir;
this.delim = delim;
this.policy = policy;
this.encoding = encoding;
this.has_header = has_header;
this.comment_prefix = comment_prefix;
this.stream = null;
this.record_iterator = null;
this.options = options;
this.bulk_input_path = null;
this.table_path = null;
}
get_iterator_by_table_id(table_id) {
this.table_path = find_table_path(this.vscode_global_state, this.input_file_dir, table_id);
if (this.table_path === null) {
throw new RbqlIOHandlingError(`Unable to find join table "${table_id}"`);
}
if (this.options && this.options['bulk_read']) {
this.bulk_input_path = this.table_path;
} else {
this.stream = fs.createReadStream(this.table_path);
}
this.record_iterator = new rbql_csv.CSVRecordIterator(this.stream, this.bulk_input_path, this.encoding, this.delim, this.policy, this.has_header, this.comment_prefix, table_id, 'b');
return this.record_iterator;
};
get_warnings(output_warnings) {
if (this.record_iterator && this.has_header) {
output_warnings.push(`The first record in JOIN file ${path.basename(this.table_path)} was also treated as header (and skipped)`);
}
}
}
async function rbql_query_node(vscode_global_state, query_text, input_path, input_delim, input_policy, output_path, output_delim, output_policy, csv_encoding, output_warnings, with_headers, comment_prefix, user_init_code, options) {
let input_stream = null;
let bulk_input_path = null;
if (options['bulk_read'] && input_path) {
bulk_input_path = input_path;
} else {
input_stream = input_path === null ? process.stdin : fs.createReadStream(input_path);
}
let [output_stream, close_output_on_finish] = output_path === null ? [process.stdout, false] : [fs.createWriteStream(output_path), true];
if (input_delim == '"' && input_policy == 'quoted')
throw new RbqlIOHandlingError('Double quote delimiter is incompatible with "quoted" policy');
if (csv_encoding == 'latin-1')
csv_encoding = 'binary';
if (!rbql_csv.is_ascii(query_text) && csv_encoding == 'binary')
throw new RbqlIOHandlingError('To use non-ascii characters in query enable UTF-8 encoding instead of latin-1/binary');
if ((!rbql_csv.is_ascii(input_delim) || !rbql_csv.is_ascii(output_delim)) && csv_encoding == 'binary')
throw new RbqlIOHandlingError('To use non-ascii characters in query enable UTF-8 encoding instead of latin-1/binary');
let default_init_source_path = path.join(os.homedir(), '.rbql_init_source.js');
if (user_init_code == '' && fs.existsSync(default_init_source_path)) {
user_init_code = rbql_csv.read_user_init_code(default_init_source_path);
}
let input_file_dir = input_path ? path.dirname(input_path) : null;
let join_tables_registry = new VSCodeFileSystemCSVRegistry(vscode_global_state, input_file_dir, input_delim, input_policy, csv_encoding, with_headers, comment_prefix, options);
let input_iterator = new rbql_csv.CSVRecordIterator(input_stream, bulk_input_path, csv_encoding, input_delim, input_policy, with_headers, comment_prefix, 'input', 'a', options['trim_whitespaces']);
let output_writer = new rbql_csv.CSVWriter(output_stream, close_output_on_finish, csv_encoding, output_delim, output_policy);
await rbql.query(query_text, input_iterator, output_writer, output_warnings, join_tables_registry, user_init_code);
join_tables_registry.get_warnings(output_warnings);
}
function make_multiline_row_info(vscode, delim_length, include_delim_length_in_ranges, newline_marker, fields, start_line, expected_end_line_for_control) {
// Semantic ranges in VSCode can't span multiple lines, so we use this workaround.
let record_ranges = [];
let record_fields = [];
let lnum_current = start_line;
let pos_in_editor_line = 0;
let next_pos_in_editor_line = 0;
for (let i = 0; i < fields.length; i++) {
let pos_in_multiline_field = 0;
// Group tokens belonging to the same logical field.
let multiline_field_singleline_ranges = [];
let multiline_field_singleline_components = [];
while (true) {
let newline_marker_pos = fields[i].indexOf(newline_marker, pos_in_multiline_field);
if (newline_marker_pos == -1)
break;
multiline_field_singleline_components.push(fields[i].substring(pos_in_multiline_field, newline_marker_pos));
multiline_field_singleline_ranges.push(new vscode.Range(lnum_current, pos_in_editor_line, lnum_current, pos_in_editor_line + newline_marker_pos - pos_in_multiline_field));
lnum_current += 1;
pos_in_editor_line = 0;
next_pos_in_editor_line = 0;
pos_in_multiline_field = newline_marker_pos + newline_marker.length;
}
next_pos_in_editor_line += fields[i].length - pos_in_multiline_field;
let current_range_end = next_pos_in_editor_line;
if (i + 1 < fields.length) {
next_pos_in_editor_line += delim_length;
if (include_delim_length_in_ranges) {
current_range_end = next_pos_in_editor_line;
}
}
// Note that `multiline_field_singleline_components` NEVER include the delim, while `multiline_field_singleline_ranges` COULD include the delim.
multiline_field_singleline_components.push(fields[i].substring(pos_in_multiline_field));
multiline_field_singleline_ranges.push(new vscode.Range(lnum_current, pos_in_editor_line, lnum_current, current_range_end));
record_fields.push(multiline_field_singleline_components);
record_ranges.push(multiline_field_singleline_ranges);
// From semantic tokenization perspective the end of token doesn't include the last character of vscode.Range i.e. it treats the range as [) interval, unlike the Range.contains() function which treats ranges as [] intervals.
pos_in_editor_line = next_pos_in_editor_line;
}
if (lnum_current != expected_end_line_for_control) {
return null;
}
return new RowInfo(record_ranges, record_fields, /*comment_prefix=*/null);
}
function is_opening_rfc_line(line_text, delim) {
// The line is oppening if by adding a character (to avoid accidental double double quote) and single double quote at the end we can make it parsable without warning!
// Some lines can be simultaneously opening and closing, e.g. `",a1,a2` or `a1,a2,"`
let [_record, warning] = csv_utils.split_quoted_str(line_text + 'x"', delim);
return !warning;
}
class RowInfo {
// TODO consider adding parsing_error_range.
constructor(record_ranges, record_fields, comment_range) {
this.record_ranges = record_ranges;
this.record_fields = record_fields;
this.comment_range = comment_range;
}
}
function extend_range_by_margin(vscode, doc, range, margin) {
let begin_line = Math.max(0, range.start.line - margin);
let end_line_inclusive = Math.min(doc.lineCount - 1, range.end.line + margin);
return new vscode.Range(begin_line, range.start.character, end_line_inclusive, range.end.character);
}
function parse_document_range_rfc(vscode, doc, delim, include_delim_length_in_ranges, comment_prefix, range) {
let begin_line = Math.max(0, range.start.line);
let end_line = Math.min(doc.lineCount, range.end.line + 1);
let table_ranges = [];
let first_defective_line = null;
let line_aggregator = new csv_utils.MultilineRecordAggregator(comment_prefix);
// The first or the second line in range with an odd number of double quotes is a start line, after finding it we can use the standard parsing algorithm.
for (let lnum = begin_line; lnum < end_line; lnum++) {
let line_text = doc.lineAt(lnum).text;
if (lnum + 1 == doc.lineCount && !line_text)
break;
let inside_multiline_record_before = line_aggregator.is_inside_multiline_record();
let start_line = lnum - line_aggregator.get_num_lines_in_record();
line_aggregator.add_line(line_text);
let inside_multiline_record_after = line_aggregator.is_inside_multiline_record();
if (!inside_multiline_record_before && inside_multiline_record_after) {
// Must be an odd-num line, check if this is an openning line - otherwise reset ranges.
if (!is_opening_rfc_line(line_text, delim)) {
table_ranges = [];
line_aggregator.reset();
}
}
if (line_aggregator.has_comment_line) {
let comment_range = new vscode.Range(lnum, 0, lnum, line_text.length);
table_ranges.push(new RowInfo(/*record_ranges=*/null, /*record_fields=*/null, comment_range));
line_aggregator.reset();
} else if (line_aggregator.has_full_record) {
const newline_marker = '\r\n'; // Use '\r\n' here to guarantee that this sequence is not present anywhere in the lines themselves. We also compare expected_end_line_for_control at the end.
let combined_line = line_aggregator.get_full_line(newline_marker);
line_aggregator.reset();
let [fields, warning] = csv_utils.smart_split(combined_line, delim, QUOTED_POLICY, /*preserve_quotes_and_whitespaces=*/true);
if (warning) {
if (first_defective_line === null) {
first_defective_line = lnum;
}
} else {
let row_info = make_multiline_row_info(vscode, delim.length, include_delim_length_in_ranges, newline_marker, fields, start_line, /*expected_end_line_for_control=*/lnum);
if (row_info !== null) {
// If row_info is null it means that `expected_end_line_for_control` doesn't match and something went very wrong with the newline_marker join workaround.
table_ranges.push(row_info);
}
}
}
}
return [table_ranges, first_defective_line];
}
function parse_document_range_single_line(vscode, doc, delim, include_delim_length_in_ranges, policy, comment_prefix, range) {
let table_ranges = [];
let begin_line = Math.max(0, range.start.line);
let end_line = Math.min(doc.lineCount, range.end.line + 1);
let first_defective_line = null;
for (let lnum = begin_line; lnum < end_line; lnum++) {
let record_ranges = [];
let record_fields = [];
let line_text = doc.lineAt(lnum).text;
if (lnum + 1 == doc.lineCount && !line_text)