From 6a58ed8fa140da673945daf376ff56a20b42e637 Mon Sep 17 00:00:00 2001 From: Cary W Reams Date: Tue, 4 Jun 2024 14:51:17 -0500 Subject: [PATCH 1/7] issue #51: initial conversion of csv_align to accept range of lines --- autoload/rainbow_csv.vim | 46 +++++++++++++++++++++++----------------- plugin/rainbow_csv.vim | 3 ++- 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/autoload/rainbow_csv.vim b/autoload/rainbow_csv.vim index 21ee9d1..e52f7d6 100644 --- a/autoload/rainbow_csv.vim +++ b/autoload/rainbow_csv.vim @@ -17,7 +17,7 @@ let s:system_python_interpreter = '' let s:magic_chars = '^*$.~/[]\' -let s:named_syntax_map = {'csv': [',', 'quoted', ''], 'csv_semicolon': [';', 'quoted', ''], 'tsv': ["\t", 'simple', ''], 'csv_pipe': ['|', 'simple', ''], 'csv_whitespace': [" ", 'whitespace', ''], 'rfc_csv': [',', 'quoted_rfc', ''], 'rfc_semicolon': [';', 'quoted_rfc', '']} +let s:named_syntax_map = {'csv': [',', 'quoted', ''], 'csv_semicolon': [';', 'quoted', ''], 'tsv': ["\t", 'simple', ''], 'csv_pipe': ['|', 'simple', ''], 'csv_whitespace': [" ", 'whitespace', ''], 'rfc_csv': [',', 'quoted_rfc', ''], 'rfc_semicolon': [';', 'quoted_rfc', ''], 'markdown':['|', 'simple', ''], 'rmd':['|', 'simple', '']} let s:autodetection_delims = exists('g:rcsv_delimiters') ? g:rcsv_delimiters : ["\t", ",", ";", "|"] @@ -761,13 +761,12 @@ func! rainbow_csv#adjust_column_stats(column_stats) return adjusted_stats endfunc - -func! s:calc_column_stats(delim, policy, comment_prefix, progress_bucket_size) +func! s:calc_column_stats(delim, policy, comment_prefix, progress_bucket_size, first_line, last_line) " Result `column_stats` is a list of (max_total_len, max_int_part_len, max_fractional_part_len) tuples. let column_stats = [] - let lastLineNo = line("$") - let is_first_line = 1 - for linenum in range(1, lastLineNo) + let lastLineNo = a:last_line + let is_first_line = a:first_line + for linenum in range(a:first_line, lastLineNo) if (a:progress_bucket_size && linenum % a:progress_bucket_size == 0) let s:align_progress_bar_position = s:align_progress_bar_position + 1 call s:display_progress_bar(s:align_progress_bar_position) @@ -792,7 +791,6 @@ func! s:calc_column_stats(delim, policy, comment_prefix, progress_bucket_size) return [column_stats, 0] endfunc - func! rainbow_csv#align_field(field, is_first_line, max_field_components_lens, is_last_column) " Align field, use max() to avoid negative delta_length which can happen theorethically due to async doc edit. let extra_readability_whitespace_length = 1 @@ -820,40 +818,49 @@ func! rainbow_csv#align_field(field, is_first_line, max_field_components_lens, i return repeat(' ', integer_delta_length) . clean_field . trailing_spaces endfunc - -func! rainbow_csv#csv_align() +func! rainbow_csv#csv_align(...) " The first (statistic) pass of the function takes about 40% of runtime, the second (actual align) pass around 60% of runtime. " Numeric-aware logic by itself adds about 50% runtime compared to the basic string-based field width alignment " If there are lot of numeric columns this can additionally increase runtime by another 50% or more. - let show_progress_bar = wordcount()['bytes'] > 200000 + let show_progress_bar = (a:2 - a:1) > 200000 let [delim, policy, comment_prefix] = rainbow_csv#get_current_dialect() if policy == 'monocolumn' - echoerr "RainbowAlign is available only for highlighted CSV files" + echoerr "RainbowAlign[Range] is available only for highlighted CSV files" return endif if policy == 'quoted_rfc' - echoerr 'RainbowAlign not available for "rfc_csv" filetypes, consider using "csv" instead' + echoerr 'RainbowAlign[Range] not available for "rfc_csv" filetypes, consider using "csv" instead' return endif - let lastLineNo = line("$") + + if (a:1 == 1 && a:2 == line("$") && (&ft == 'markdown' || &ft == 'rmd')) + echoerr "RainbowAlign is available only for highlighted CSV files; try RainbowAlignRange" + return + endif + + let lastLineNo = a:2 let progress_bucket_size = (lastLineNo * 2) / s:progress_bar_size " multiply by 2 because we have two passes. if !show_progress_bar || progress_bucket_size < 10 let progress_bucket_size = 0 endif let s:align_progress_bar_position = 0 - let [column_stats, first_failed_line] = s:calc_column_stats(delim, policy, comment_prefix, progress_bucket_size) + + let [column_stats, first_failed_line] = s:calc_column_stats(delim, policy, comment_prefix, progress_bucket_size,a:1,a:2) if first_failed_line != 0 echoerr 'Unable to allign: Inconsistent double quotes at line ' . first_failed_line return endif + let column_stats = rainbow_csv#adjust_column_stats(column_stats) if !len(column_stats) echoerr 'Unable to allign: Internal Rainbow CSV Error' return endif + let has_edit = 0 - let is_first_line = 1 - for linenum in range(1, lastLineNo) + + let is_first_line = a:1 + for linenum in range(a:1, lastLineNo) if (progress_bucket_size && linenum % progress_bucket_size == 0) let s:align_progress_bar_position = s:align_progress_bar_position + 1 call s:display_progress_bar(s:align_progress_bar_position) @@ -883,11 +890,10 @@ func! rainbow_csv#csv_align() let is_first_line = 0 endfor if !has_edit - echoerr "File is already aligned" + echoerr "Range is already aligned" endif endfunc - func! rainbow_csv#csv_shrink() let [delim, policy, comment_prefix] = rainbow_csv#get_current_dialect() if policy == 'monocolumn' @@ -1119,7 +1125,7 @@ func! s:cell_jump_simple(direction, delim, policy, comment_prefix) if a:direction == 'down' || a:direction == 'up' let lastLineNo = line("$") let cur_line_num = anchor_line_num - while 1 + while 1 if a:direction == 'down' let cur_line_num += 1 else @@ -1181,7 +1187,7 @@ func! s:cell_jump_rfc(direction, delim, comment_prefix) let field_num += 1 elseif a:direction == 'left' let field_num -= 1 - elseif a:direction == 'down' + elseif a:direction == 'down' let relative_record_num += 1 elseif a:direction == 'up' let relative_record_num -= 1 diff --git a/plugin/rainbow_csv.vim b/plugin/rainbow_csv.vim index 01bd5da..7e41d2f 100644 --- a/plugin/rainbow_csv.vim +++ b/plugin/rainbow_csv.vim @@ -30,7 +30,8 @@ command! NoRainbowComment call rainbow_csv#manual_disable_comment_prefix() command! RainbowLint call rainbow_csv#csv_lint() command! CSVLint call rainbow_csv#csv_lint() -command! RainbowAlign call rainbow_csv#csv_align() +command! RainbowAlign call rainbow_csv#csv_align(1,line("$")) +command! -range RainbowAlignRange call rainbow_csv#csv_align(,) command! RainbowShrink call rainbow_csv#csv_shrink() command! RainbowQuery call rainbow_csv#start_or_finish_query_editing() From 074b1fa3340b523abbb6bf68ed6f030c3ae22fef Mon Sep 17 00:00:00 2001 From: Cary W Reams Date: Wed, 5 Jun 2024 09:51:49 -0500 Subject: [PATCH 2/7] issue #51: range-enabled csv_shrink; converts csv_shrink, csv_align to use either address or visual range --- autoload/rainbow_csv.vim | 38 ++++++++++++++++++++++++-------------- plugin/rainbow_csv.vim | 5 ++--- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/autoload/rainbow_csv.vim b/autoload/rainbow_csv.vim index e52f7d6..0acbf73 100644 --- a/autoload/rainbow_csv.vim +++ b/autoload/rainbow_csv.vim @@ -818,34 +818,36 @@ func! rainbow_csv#align_field(field, is_first_line, max_field_components_lens, i return repeat(' ', integer_delta_length) . clean_field . trailing_spaces endfunc -func! rainbow_csv#csv_align(...) +func! rainbow_csv#csv_align() range + let l:firstline = a:firstline + let l:lastline = a:lastline " The first (statistic) pass of the function takes about 40% of runtime, the second (actual align) pass around 60% of runtime. " Numeric-aware logic by itself adds about 50% runtime compared to the basic string-based field width alignment " If there are lot of numeric columns this can additionally increase runtime by another 50% or more. - let show_progress_bar = (a:2 - a:1) > 200000 + let show_progress_bar = (l:lastline - l:firstline) > 200000 let [delim, policy, comment_prefix] = rainbow_csv#get_current_dialect() if policy == 'monocolumn' - echoerr "RainbowAlign[Range] is available only for highlighted CSV files" + echoerr "RainbowAlign is available only for highlighted CSV files" return endif if policy == 'quoted_rfc' - echoerr 'RainbowAlign[Range] not available for "rfc_csv" filetypes, consider using "csv" instead' + echoerr 'RainbowAlign not available for "rfc_csv" filetypes, consider using "csv" instead' return endif - if (a:1 == 1 && a:2 == line("$") && (&ft == 'markdown' || &ft == 'rmd')) - echoerr "RainbowAlign is available only for highlighted CSV files; try RainbowAlignRange" + if (l:firstline == 1 && l:lastline == line("$") && (&ft == 'markdown' || &ft == 'rmd')) + echoerr "RainbowAlign requires an address range in markdown and rmd files" return endif - let lastLineNo = a:2 + let lastLineNo = l:lastline let progress_bucket_size = (lastLineNo * 2) / s:progress_bar_size " multiply by 2 because we have two passes. if !show_progress_bar || progress_bucket_size < 10 let progress_bucket_size = 0 endif let s:align_progress_bar_position = 0 - let [column_stats, first_failed_line] = s:calc_column_stats(delim, policy, comment_prefix, progress_bucket_size,a:1,a:2) + let [column_stats, first_failed_line] = s:calc_column_stats(delim, policy, comment_prefix, progress_bucket_size,l:firstline,l:lastline) if first_failed_line != 0 echoerr 'Unable to allign: Inconsistent double quotes at line ' . first_failed_line return @@ -859,8 +861,8 @@ func! rainbow_csv#csv_align(...) let has_edit = 0 - let is_first_line = a:1 - for linenum in range(a:1, lastLineNo) + let is_first_line = l:firstline + for linenum in range(l:firstline, lastLineNo) if (progress_bucket_size && linenum % progress_bucket_size == 0) let s:align_progress_bar_position = s:align_progress_bar_position + 1 call s:display_progress_bar(s:align_progress_bar_position) @@ -894,7 +896,9 @@ func! rainbow_csv#csv_align(...) endif endfunc -func! rainbow_csv#csv_shrink() +func! rainbow_csv#csv_shrink() range + let l:firstline = a:firstline + let l:lastline = a:lastline let [delim, policy, comment_prefix] = rainbow_csv#get_current_dialect() if policy == 'monocolumn' echoerr "RainbowShrink is available only for highlighted CSV files" @@ -904,15 +908,21 @@ func! rainbow_csv#csv_shrink() echoerr 'RainbowShrink not available for "rfc_csv" filetypes, consider using "csv" instead' return endif - let lastLineNo = line("$") + + if (l:firstline == 1 && l:lastline == line("$") && (&ft == 'markdown' || &ft == 'rmd')) + echoerr "RainbowShrink requires an address range in markdown and rmd files" + return + endif + + let lastLineNo = l:lastline let has_edit = 0 - let show_progress_bar = wordcount()['bytes'] > 200000 + let show_progress_bar = (l:lastline - l:firstline) > 200000 let progress_bucket_size = lastLineNo / s:progress_bar_size if !show_progress_bar || progress_bucket_size < 10 let progress_bucket_size = 0 endif let s:align_progress_bar_position = 0 - for linenum in range(1, lastLineNo) + for linenum in range(l:firstline, lastLineNo) if (progress_bucket_size && linenum % progress_bucket_size == 0) let s:align_progress_bar_position = s:align_progress_bar_position + 1 call s:display_progress_bar(s:align_progress_bar_position) diff --git a/plugin/rainbow_csv.vim b/plugin/rainbow_csv.vim index 7e41d2f..3e2e233 100644 --- a/plugin/rainbow_csv.vim +++ b/plugin/rainbow_csv.vim @@ -30,9 +30,8 @@ command! NoRainbowComment call rainbow_csv#manual_disable_comment_prefix() command! RainbowLint call rainbow_csv#csv_lint() command! CSVLint call rainbow_csv#csv_lint() -command! RainbowAlign call rainbow_csv#csv_align(1,line("$")) -command! -range RainbowAlignRange call rainbow_csv#csv_align(,) -command! RainbowShrink call rainbow_csv#csv_shrink() +command! -range=% RainbowAlign ,call rainbow_csv#csv_align() +command! -range=% RainbowShrink ,call rainbow_csv#csv_shrink() command! RainbowQuery call rainbow_csv#start_or_finish_query_editing() command! -nargs=+ Select call rainbow_csv#run_select_cmd_query() From 76963ccc14c1cd1cadb48c59ff5a85ba74c8df4c Mon Sep 17 00:00:00 2001 From: Cary W Reams Date: Wed, 5 Jun 2024 10:10:30 -0500 Subject: [PATCH 3/7] issue #51: adds documentation of md and rmd filetype acceptance as well as requirements for ranges when using align and shrink functions in those filetypes --- README.md | 274 ++++++++++++++++++++++---------------------- doc/rainbow_csv.txt | 22 ++-- 2 files changed, 151 insertions(+), 145 deletions(-) diff --git a/README.md b/README.md index 7480ea9..652e6ee 100644 --- a/README.md +++ b/README.md @@ -3,17 +3,17 @@ ![rainbow_csv](https://i.imgur.com/EhV2niB.png) ## Installation -Use your favorite package manager. +Use your favorite package manager. -Vundle: `Plugin 'mechatroner/rainbow_csv'` -VimPlug: `Plug 'mechatroner/rainbow_csv'` -dein: `call dein#add('mechatroner/rainbow_csv')` +Vundle: `Plugin 'mechatroner/rainbow_csv'` +VimPlug: `Plug 'mechatroner/rainbow_csv'` +dein: `call dein#add('mechatroner/rainbow_csv')` -No additional steps required - Rainbow CSV will work out of the box. +No additional steps required - Rainbow CSV will work out of the box. ## Overview -Main features: -* Highlight CSV columns in different rainbow colors. +Main features: +* Highlight CSV columns in different rainbow colors. * Provide info about column under the cursor * Provide `SELECT` and `UPDATE` queries in RBQL: SQL-like transprogramming query language. * Consistency check for csv files (CSVLint) @@ -21,55 +21,56 @@ Main features: * Cell-level cursor navigation There are 4 ways to enable csv columns highlighting: -1. CSV autodetection based on file content and/or extension -2. Manual CSV delimiter selection with `:RainbowDelim` command with cursor over the delimiter -3. Manual CSV delimiter selection with `:RainbowMultiDelim` for multi-character delimiters selecting them in "VISUAL" mode -4. Explicitly activate one of the built-in filetypes, e.g. `:set ft=csv` +1. CSV autodetection based on file content and/or extension +2. Manual CSV delimiter selection with `:RainbowDelim` command with cursor over the delimiter +3. Manual CSV delimiter selection with `:RainbowMultiDelim` for multi-character delimiters selecting them in "VISUAL" mode +4. Explicitly activate one of the built-in filetypes, e.g. `:set ft=csv` -The core functionality of the plugin is written in pure vimscript, no additional libraries required. +The core functionality of the plugin is written in pure vimscript, no additional libraries required. ![Rainbow CSV Screenshot](https://user-images.githubusercontent.com/5349737/190057249-8ec401f6-b76d-4420-a6af-053dd502f8a9.png) # Plugin description ### Built-in and autogenerated filetypes -Rainbow CSV has 7 built-in CSV filetypes and infinite number of autogenerated filetypes. -Each Rainbow CSV filetype is mapped to a separator and "policy" which describes additional properties e.g. if separator can be escaped inside double quoted field. -If you run `:RainbowDelim` or `:RainbowMultiDelim` to select a separator that doesn't map to one of the built-in filetypes, Rainbow CSV will dynamically generate the filetype syntax file and put it into the "syntax" folder. -List of built-in filetypes: - -|Filetype | Separator | Extension | Properties | -|---------------|---------------|-----------|-----------------------------------------------------| -|csv | , (comma) | .csv | Ignored inside double-quoted fields | -|tsv | \t (TAB) | .tsv .tab | | -|csv_semicolon | ; (semicolon) | | Ignored inside double-quoted fields | -|csv_whitespace | whitespace | | Consecutive whitespaces are merged | -|csv_pipe | | (pipe) | | | -|rfc_csv | , (comma) | | Same as "csv" but allows multiline fields | -|rfc_semicolon | ; (semicolon) | | Same as "csv_semicolon" but allows multiline fields | - +Rainbow CSV has 7 built-in CSV filetypes and infinite number of autogenerated filetypes. +Each Rainbow CSV filetype is mapped to a separator and "policy" which describes additional properties e.g. if separator can be escaped inside double quoted field. +If you run `:RainbowDelim` or `:RainbowMultiDelim` to select a separator that doesn't map to one of the built-in filetypes, Rainbow CSV will dynamically generate the filetype syntax file and put it into the "syntax" folder. +List of built-in filetypes: + +|Filetype |Separator |Extension |Properties | +|--------------- |--------------- |----------- |----------------------------------------------------- | +|csv |, (comma) |.csv |Ignored inside double-quoted fields | +|tsv |\t (TAB) |.tsv .tab | | +|csv_semicolon |; (semicolon) | |Ignored inside double-quoted fields | +|csv_whitespace |whitespace | |Consecutive whitespaces are merged | +|csv_pipe || (pipe) | | | +|rfc_csv |, (comma) | |Same as "csv" but allows multiline fields | +|rfc_semicolon |; (semicolon) | |Same as "csv_semicolon" but allows multiline fields | +|markdown || (pipe) |.md |align and shrink functions only operate on ranges | +|rmd || (pipe) |.rmd |align and shrink functions only operate on ranges | ### Associating file extensions with CSV dialects -In most cases the built-in autodetection algorithm should correctly detect correct CSV dialect for all CSV tables that you open in Vim, but if you have disabled the autodetection algorithm or don't want to rely on it for some reason, you can manually associate file extensions with available csv dialects. -Example: to associate ".dat" extension with "csv_pipe" dialect and ".csv" extension with "csv_semicolon" add the folowing lines to your .vimrc: +In most cases the built-in autodetection algorithm should correctly detect correct CSV dialect for all CSV tables that you open in Vim, but if you have disabled the autodetection algorithm or don't want to rely on it for some reason, you can manually associate file extensions with available csv dialects. +Example: to associate ".dat" extension with "csv_pipe" dialect and ".csv" extension with "csv_semicolon" add the folowing lines to your .vimrc: ``` autocmd BufNewFile,BufRead *.csv set filetype=csv_semicolon autocmd BufNewFile,BufRead *.dat set filetype=csv_pipe ``` ### Working with multiline CSV fields -In rare cases some CSV files can contain double-quoted fields spanning multiple lines. -To work with such files you can set filetype to either "rfc_csv" or "rfc_semicolon" e.g. `:set ft=rfc_csv`. -Syntax highlighting for rfc_csv and rfc_semicolon dialects can sometimes go out of sync with the file content, use `:syntax sync fromstart` command in that case. -rfc_csv and rfc_semicolon are fully supported by RBQL which among other things allows you to easily convert them to line-by-line CSV by replacing newlines in fields with sequences of 4 spaces or something like that. -rfc_csv and rfc_semicolon take their name from [RFC 4180](https://tools.ietf.org/html/rfc4180) memo with which they are fully compatible. +In rare cases some CSV files can contain double-quoted fields spanning multiple lines. +To work with such files you can set filetype to either "rfc_csv" or "rfc_semicolon" e.g. `:set ft=rfc_csv`. +Syntax highlighting for rfc_csv and rfc_semicolon dialects can sometimes go out of sync with the file content, use `:syntax sync fromstart` command in that case. +rfc_csv and rfc_semicolon are fully supported by RBQL which among other things allows you to easily convert them to line-by-line CSV by replacing newlines in fields with sequences of 4 spaces or something like that. +rfc_csv and rfc_semicolon take their name from [RFC 4180](https://tools.ietf.org/html/rfc4180) memo with which they are fully compatible. ### Built-in RBQL query language -Rainbow CSV comes with built-in lightweight RBQL SQL-like query engine. -To run an RBQL query either use `:Select` command e.g. `:Select a1, a2` or run `:RainbowQuery` command to enter query editing mode. +Rainbow CSV comes with built-in lightweight RBQL SQL-like query engine. +To run an RBQL query either use `:Select` command e.g. `:Select a1, a2` or run `:RainbowQuery` command to enter query editing mode. -Demonstration of rainbow_csv highlighting and RBQL queries +Demonstration of rainbow_csv highlighting and RBQL queries ![demo_screencast](https://i.imgur.com/4PIVvjc.gif) @@ -79,10 +80,10 @@ In this demo python expressions were used, but JavaScript is also available. ### Rainbow highlighting for non-table files -You can use rainbow highlighting and RBQL even for non-csv/tsv files. -E.g. you can highlight records in log files, one-line xmls and other delimited records. -You can even highlight function arguments in your programming language using comma or comma+whitespaces as a delimiter for `:RainbowDelim` or `:RainbowMultiDelim` commands. -And you can always turn off the rainbow highlighting using `:NoRainbowDelim` command. +You can use rainbow highlighting and RBQL even for non-csv/tsv files. +E.g. you can highlight records in log files, one-line xmls and other delimited records. +You can even highlight function arguments in your programming language using comma or comma+whitespaces as a delimiter for `:RainbowDelim` or `:RainbowMultiDelim` commands. +And you can always turn off the rainbow highlighting using `:NoRainbowDelim` command. ### Commands @@ -91,12 +92,12 @@ And you can always turn off the rainbow highlighting using `:NoRainbowDelim` com Mark current file as a table and highlight it's columns in rainbow colors. Character under the cursor will be used as a delimiter. The delimiter will be saved in a config file for future vim sessions. -You can also use this command for non-csv files, e.g. to highlight function arguments +You can also use this command for non-csv files, e.g. to highlight function arguments in source code in different colors. To return back to original syntax highlighting run `:NoRainbowDelim` #### :RainbowMultiDelim -Same as `:RainbowDelim`, but works with multicharacter separators. +Same as `:RainbowDelim`, but works with multicharacter separators. Visually select the multicharacter separator (e.g. `~#~`) and run `:RainbowMultiDelim` command. #### :NoRainbowDelim @@ -105,65 +106,67 @@ Disable rainbow columns highlighting for the current file. #### :RainbowCellGoUp -Move cursor one cell up. +Move cursor one cell up. Consider mapping this to Ctrl+[Up Arrow], see the "Key Mappings" section. #### :RainbowCellGoDown -Move cursor one cell down. +Move cursor one cell down. Consider mapping this to Ctrl+[Down Arrow], see the "Key Mappings" section. #### :RainbowCellGoLeft -Move cursor one cell left. +Move cursor one cell left. Consider mapping this to Ctrl+[Left Arrow], see the "Key Mappings" section. #### :RainbowCellGoRight -Move cursor one cell right. +Move cursor one cell right. Consider mapping this to Ctrl+[Right Arrow], see the "Key Mappings" section. #### :RainbowComment -Mark the character under the cursor as the comment prefix, e.g. `#`. By default Rainbow CSV doesn't highlight comments in CSV files. -You can also use `:RainbowCommentMulti` to mark a visual selection as a multicharacter comment prefix +Mark the character under the cursor as the comment prefix, e.g. `#`. By default Rainbow CSV doesn't highlight comments in CSV files. +You can also use `:RainbowCommentMulti` to mark a visual selection as a multicharacter comment prefix #### :NoRainbowComment -Disable all comments for the current CSV file. -This command is especially useful when you have set `g:rainbow_comment_prefix` variable and want to exclude just one particular file. +Disable all comments for the current CSV file. +This command is especially useful when you have set `g:rainbow_comment_prefix` variable and want to exclude just one particular file. #### :CSVLint -The linter checks the following: -* consistency of double quotes usage in CSV rows -* consistency of number of fields per CSV row +The linter checks the following: +* consistency of double quotes usage in CSV rows +* consistency of number of fields per CSV row #### :RainbowAlign -Align CSV columns with whitespaces. -Don't run this command if you treat leading and trailing whitespaces in fields as part of the data. -You can edit aligned CSV file in Vim column-edit mode (Ctrl+v). +Align CSV columns with whitespaces. +Don't run this command if you treat leading and trailing whitespaces in fields as part of the data. +You can edit aligned CSV file in Vim column-edit mode (Ctrl+v). +In markdown and r-markdown files, this function requires a range of addresses or a visual range selection. #### :RainbowShrink Remove leading and trailing whitespaces from all fields. Opposite to RainbowAlign +In markdown and r-markdown files, this function requires a range of addresses or a visual range selection. #### :Select ... -Allows to enter RBQL select query as vim command. +Allows to enter RBQL select query as vim command. E.g. `:Select a1, a2 order by a1` #### :Update ... -Allows to enter RBQL update query as vim command. +Allows to enter RBQL update query as vim command. E.g. `:Update a1 = a1 + " " + a2` #### :RainbowQuery -Enter RBQL Query editing mode. -When in the query editing mode, execute `:RainbowQuery` again to run the query. +Enter RBQL Query editing mode. +When in the query editing mode, execute `:RainbowQuery` again to run the query. Consider mapping `:RainbowQuery` to `` key i.e. `nnoremap :RainbowQuery` @@ -172,7 +175,7 @@ Consider mapping `:RainbowQuery` to `` key i.e. `nnoremap :RainbowQuery Assign any name to the current table. You can use this name in join operation instead of the table path. E.g. ``` JOIN customers ON a1 == b1 -``` +``` intead of: ``` JOIN /path/to/my/customers/table ON a1 == b1 @@ -180,13 +183,13 @@ JOIN /path/to/my/customers/table ON a1 == b1 #### :RainbowCopyBack -This command only applicable for RBQL output files. +This command only applicable for RBQL output files. Replace the content of the original file that was used to run the RBQL query with the query result set data. ### Key Mappings -Plugin does not create any new key mappings, but you can define your own in your .vimrc file. -All highlighted files have a special buffer variable `b:rbcsv` set to 1, so you can use this to define conditional csv-only key mappings. +Plugin does not create any new key mappings, but you can define your own in your .vimrc file. +All highlighted files have a special buffer variable `b:rbcsv` set to 1, so you can use this to define conditional csv-only key mappings. For example, to conditionally map Ctrl+Arrow keys to cell navigation commands you can use this snippet: ``` @@ -204,80 +207,80 @@ nnoremap :RainbowCellGoRight ### Configuration #### g:disable_rainbow_hover -Set to `1` to stop showing info about the column under the cursor in Vim command line -Example: +Set to `1` to stop showing info about the column under the cursor in Vim command line +Example: ``` let g:disable_rainbow_hover = 1 ``` #### g:rcsv_delimiters -Default: `["\t", ",", ";", "|"]` -List of separators to try for content-based autodetection +Default: `["\t", ",", ";", "|"]` +List of separators to try for content-based autodetection You can add or remove values from the list. Example: ``` let g:rcsv_delimiters = ["\t", ",", "^", "~#~"] ``` #### g:disable_rainbow_csv_autodetect -Set to `1` to disable CSV autodetection mechanism -Example: +Set to `1` to disable CSV autodetection mechanism +Example: ``` let g:disable_rainbow_csv_autodetect = 1 ``` -Manual delimiter selection would still be possible. -You can also manually associate specific file extensions with 'csv' or 'tsv' filetypes +Manual delimiter selection would still be possible. +You can also manually associate specific file extensions with 'csv' or 'tsv' filetypes #### g:rainbow_comment_prefix -Default: `''` -A string to use as a comment prefix for all CSV files you open in Vim. -This setting is helpful if you are dealing with lots of CSV files which consistently use the same comment prefix e.g. `'#'` or `'>>'` -If you want to enable comments on file-by-file basis, use the `:RainbowComment` or `:RainbowCommentMulti` commands instead. -To cancel the effect of `g:rainbow_comment_prefix` just for the current file use `:NoRainbowComment` command. +Default: `''` +A string to use as a comment prefix for all CSV files you open in Vim. +This setting is helpful if you are dealing with lots of CSV files which consistently use the same comment prefix e.g. `'#'` or `'>>'` +If you want to enable comments on file-by-file basis, use the `:RainbowComment` or `:RainbowCommentMulti` commands instead. +To cancel the effect of `g:rainbow_comment_prefix` just for the current file use `:NoRainbowComment` command. #### g:rcsv_max_columns -Default: `30` -Autodetection will fail if buffer has more than _g:rcsv\_max\_columns_ columns. +Default: `30` +Autodetection will fail if buffer has more than _g:rcsv\_max\_columns_ columns. You can increase or decrease this limit. #### g:rcsv_colorpairs -List of color name pairs to customize rainbow highlighting. -Each entry in the list is a pair of two colors: the first color is for terminal mode, the second one is for GUI mode. +List of color name pairs to customize rainbow highlighting. +Each entry in the list is a pair of two colors: the first color is for terminal mode, the second one is for GUI mode. Example: ``` let g:rcsv_colorpairs = [['red', 'red'], ['blue', 'blue'], ['green', 'green'], ['magenta', 'magenta'], ['NONE', 'NONE'], ['darkred', 'darkred'], ['darkblue', 'darkblue'], ['darkgreen', 'darkgreen'], ['darkmagenta', 'darkmagenta'], ['darkcyan', 'darkcyan']] ``` #### g:multiline_search_range -Default: `10` -This settings is only relevant for rfc_csv and rfc_semicolon dialects. -If some multiline records contain more lines that this value, hover info will not work correctly. It is not recommended to significantly increase this value because it will have negative impact on hover info performance +Default: `10` +This settings is only relevant for rfc_csv and rfc_semicolon dialects. +If some multiline records contain more lines that this value, hover info will not work correctly. It is not recommended to significantly increase this value because it will have negative impact on hover info performance #### g:rbql_backend_language -Default: `'python'` -Supported values: `'python'`, `'js'` +Default: `'python'` +Supported values: `'python'`, `'js'` Scripting language to use in RBQL expressions. #### g:rbql_encoding -Default: `utf-8` -Supported values: `'utf-8'`, `'latin-1'` +Default: `utf-8` +Supported values: `'utf-8'`, `'latin-1'` -CSV files encoding for RBQL. +CSV files encoding for RBQL. #### g:rbql_output_format -Default: `'input'` +Default: `'input'` Supported values: `'tsv'`, `'csv'`, `'input'` Format of RBQL result set tables. * input: same format as the input table -* tsv: doesn't allow quoted tabs inside fields. +* tsv: doesn't allow quoted tabs inside fields. * csv: is Excel-compatible and allows quoted commas. -Essentially format is a pair: delimiter + quoting policy. +Essentially format is a pair: delimiter + quoting policy. This setting for example can be used to convert files between tsv and csv format: * To convert _csv_ to _tsv_: **1.** open csv file. **2.** `:let g:rbql_output_format='tsv'` **3.** `:Select *` * To convert _tsv_ to _csv_: **1.** open tsv file. **2.** `:let g:rbql_output_format='csv'` **3.** `:Select *` @@ -288,16 +291,16 @@ Set to `1` to use system python interpreter for RBQL queries instead of the pyth #### g:rbql_with_headers -If most of the CSV files that you work with have headers, you can set this value to 1. In this case RBQL will treat first records in files as headers by default. -Example: `:let g:rbql_with_headers = 1` +If most of the CSV files that you work with have headers, you can set this value to 1. In this case RBQL will treat first records in files as headers by default. +Example: `:let g:rbql_with_headers = 1` You can also adjust (or override) this setting by adding `WITH (header)` or `WITH (noheader)` to the end of your RBQL queries. # RBQL (Rainbow Query Language) Description -RBQL is an eval-based SQL-like query engine for (not only) CSV file processing. It provides SQL-like language that supports SELECT queries with Python or JavaScript expressions. -RBQL is best suited for data transformation, data cleaning, and analytical queries. -RBQL is distributed with CLI apps, text editor plugins, Python and JS libraries. +RBQL is an eval-based SQL-like query engine for (not only) CSV file processing. It provides SQL-like language that supports SELECT queries with Python or JavaScript expressions. +RBQL is best suited for data transformation, data cleaning, and analytical queries. +RBQL is distributed with CLI apps, text editor plugins, Python and JS libraries. [Official Site](https://rbql.org/) @@ -331,29 +334,29 @@ RBQL is distributed with CLI apps, text editor plugins, Python and JS libraries. * LIMIT _N_ * AS -All keywords have the same meaning as in SQL queries. You can check them [online](https://www.w3schools.com/sql/default.asp) +All keywords have the same meaning as in SQL queries. You can check them [online](https://www.w3schools.com/sql/default.asp) ### RBQL variables RBQL for CSV files provides the following variables which you can use in your queries: -* _a1_, _a2_,..., _a{N}_ - Variable type: **string** - Description: value of i-th field in the current record in input table -* _b1_, _b2_,..., _b{N}_ - Variable type: **string** - Description: value of i-th field in the current record in join table B -* _NR_ - Variable type: **integer** - Description: Record number (1-based) -* _NF_ - Variable type: **integer** - Description: Number of fields in the current record -* _a.name_, _b.Person_age_, ... _a.{Good_alphanumeric_column_name}_ - Variable type: **string** - Description: Value of the field referenced by it's "name". You can use this notation if the field in the header has a "good" alphanumeric name -* _a["object id"]_, _a['9.12341234']_, _b["%$ !! 10 20"]_ ... _a["Arbitrary column name!"]_ - Variable type: **string** +* _a1_, _a2_,..., _a{N}_ + Variable type: **string** + Description: value of i-th field in the current record in input table +* _b1_, _b2_,..., _b{N}_ + Variable type: **string** + Description: value of i-th field in the current record in join table B +* _NR_ + Variable type: **integer** + Description: Record number (1-based) +* _NF_ + Variable type: **integer** + Description: Number of fields in the current record +* _a.name_, _b.Person_age_, ... _a.{Good_alphanumeric_column_name}_ + Variable type: **string** + Description: Value of the field referenced by it's "name". You can use this notation if the field in the header has a "good" alphanumeric name +* _a["object id"]_, _a['9.12341234']_, _b["%$ !! 10 20"]_ ... _a["Arbitrary column name!"]_ + Variable type: **string** Description: Value of the field referenced by it's "name". You can use this notation to reference fields by arbitrary values in the header @@ -363,31 +366,31 @@ _UPDATE_ query produces a new table where original values are replaced according ### Aggregate functions and queries -RBQL supports the following aggregate functions, which can also be used with _GROUP BY_ keyword: -_COUNT_, _ARRAY_AGG_, _MIN_, _MAX_, _SUM_, _AVG_, _VARIANCE_, _MEDIAN_ +RBQL supports the following aggregate functions, which can also be used with _GROUP BY_ keyword: +_COUNT_, _ARRAY_AGG_, _MIN_, _MAX_, _SUM_, _AVG_, _VARIANCE_, _MEDIAN_ -Limitation: aggregate functions inside Python (or JS) expressions are not supported. Although you can use expressions inside aggregate functions. -E.g. `MAX(float(a1) / 1000)` - valid; `MAX(a1) / 1000` - invalid. -There is a workaround for the limitation above for _ARRAY_AGG_ function which supports an optional parameter - a callback function that can do something with the aggregated array. Example: +Limitation: aggregate functions inside Python (or JS) expressions are not supported. Although you can use expressions inside aggregate functions. +E.g. `MAX(float(a1) / 1000)` - valid; `MAX(a1) / 1000` - invalid. +There is a workaround for the limitation above for _ARRAY_AGG_ function which supports an optional parameter - a callback function that can do something with the aggregated array. Example: `SELECT a2, ARRAY_AGG(a1, lambda v: sorted(v)[:5]) GROUP BY a2` - Python; `SELECT a2, ARRAY_AGG(a1, v => v.sort().slice(0, 5)) GROUP BY a2` - JS ### JOIN statements -Join table B can be referenced either by its file path or by its name - an arbitrary string which the user should provide before executing the JOIN query. -RBQL supports _STRICT LEFT JOIN_ which is like _LEFT JOIN_, but generates an error if any key in the left table "A" doesn't have exactly one matching key in the right table "B". -Table B path can be either relative to the working dir, relative to the main table or absolute. +Join table B can be referenced either by its file path or by its name - an arbitrary string which the user should provide before executing the JOIN query. +RBQL supports _STRICT LEFT JOIN_ which is like _LEFT JOIN_, but generates an error if any key in the left table "A" doesn't have exactly one matching key in the right table "B". +Table B path can be either relative to the working dir, relative to the main table or absolute. Limitation: _JOIN_ statements can't contain Python/JS expressions and must have the following form: _ (/path/to/table.tsv | table_name ) ON a... == b... [AND a... == b... [AND ... ]]_ ### SELECT EXCEPT statement -SELECT EXCEPT can be used to select everything except specific columns. E.g. to select everything but columns 2 and 4, run: `SELECT * EXCEPT a2, a4` +SELECT EXCEPT can be used to select everything except specific columns. E.g. to select everything but columns 2 and 4, run: `SELECT * EXCEPT a2, a4` Traditional SQL engines do not support this query mode. ### UNNEST() operator -UNNEST(list) takes a list/array as an argument and repeats the output record multiple times - one time for each value from the list argument. -Example: `SELECT a1, UNNEST(a2.split(';'))` +UNNEST(list) takes a list/array as an argument and repeats the output record multiple times - one time for each value from the list argument. +Example: `SELECT a1, UNNEST(a2.split(';'))` ### LIKE() function @@ -403,8 +406,8 @@ Example: `select top 5 NR, * with (header)` ### User Defined Functions (UDF) -RBQL supports User Defined Functions -You can define custom functions and/or import libraries in two special files: +RBQL supports User Defined Functions +You can define custom functions and/or import libraries in two special files: * `~/.rbql_init_source.py` - for Python * `~/.rbql_init_source.js` - for JavaScript @@ -443,12 +446,12 @@ You can define custom functions and/or import libraries in two special files: #### Advantages -* WYSIWYG -* Familiar editing environment of your favorite text editor -* Zero-cost abstraction: Syntax highlighting is essentially free, while graphical column alignment can be computationally expensive +* WYSIWYG +* Familiar editing environment of your favorite text editor +* Zero-cost abstraction: Syntax highlighting is essentially free, while graphical column alignment can be computationally expensive * High information density: Rainbow CSV shows more data per screen because it doesn't insert column-aligning whitespaces. * Works with non-table and semi-tabular files (text files that contain both table(s) and non-table data like text) -* Ability to visually associate two same-colored columns from two different windows. This is not possible with graphical column alignment +* Ability to visually associate two same-colored columns from two different windows. This is not possible with graphical column alignment #### Disadvantages @@ -477,8 +480,7 @@ You can define custom functions and/or import libraries in two special files: * Demo Google Colab IPython [notebook](https://colab.research.google.com/drive/1_cFPtnQUxILP0RE2_DBlqIfXaEzT-oZ6?usp=sharing) #### Related vim plugins: -Rainbow CSV name and original implementation was significantly influenced by [rainbow_parentheses](https://github.com/kien/rainbow_parentheses.vim) Vim plugin. +Rainbow CSV name and original implementation was significantly influenced by [rainbow_parentheses](https://github.com/kien/rainbow_parentheses.vim) Vim plugin. -There also exists an old vim syntax file [csv_color](https://vim.sourceforge.io/scripts/script.php?script_id=518) which, despite it's name, can highlight only *.tsv files. +There also exists an old vim syntax file [csv_color](https://vim.sourceforge.io/scripts/script.php?script_id=518) which, despite it's name, can highlight only *.tsv files. And, of course, there is [csv.vim](https://github.com/chrisbra/csv.vim) - diff --git a/doc/rainbow_csv.txt b/doc/rainbow_csv.txt index 782aea7..726ee90 100644 --- a/doc/rainbow_csv.txt +++ b/doc/rainbow_csv.txt @@ -32,7 +32,7 @@ There are 4 ways to enable csv columns highlighting: 3. Manual CSV delimiter selection with |:RainbowMultiDelim| for multi-character delimiters 4. Execute `:set ft=csv` or `:set ft=tsv` for CSV/TSV dialects. For other separators use |:RainbowDelim| -To run an RBQL query either use |:Select| command e.g. ":Select a1, a2" or run |:RainbowQuery| command to enter query editing mode. +To run an RBQL query either use |:Select| command e.g. ":Select a1, a2" or run |:RainbowQuery| command to enter query editing mode. As soon as you finish entering "select" (or update) and press whitespace, the plugin will show column names in the status line. The core functionality of the plugin is written in pure vimscript, no additional libraries required. @@ -75,6 +75,10 @@ In rare cases some CSV files can contain double-quoted fields spanning multiple To work with such files you can set filetype to either "rfc_csv" or "rfc_semicolon". Syntax highlighting for rfc_csv and rfc_semicolon dialects can go out of sync with the file content under specific conditions, use `:syntax sync fromstart` command in that case +Working with markdown and rmd files ~ +You can use |:RainbowAlign| and |:RainbowShrink| by providing an address range +or visual range to either function. + ============================================================================== 2. Commands *rainbow_csv-commands* @@ -102,22 +106,22 @@ Disable rainbow columns highlighting for the current file. *:RainbowCellGoUp* -Move cursor one cell up. +Move cursor one cell up. Consider mapping this to Ctrl+[Up Arrow], see the "Key Mappings" section. *:RainbowCellGoDown* -Move cursor one cell down. +Move cursor one cell down. Consider mapping this to Ctrl+[Down Arrow], see the "Key Mappings" section. *:RainbowCellGoLeft* -Move cursor one cell left. +Move cursor one cell left. Consider mapping this to Ctrl+[Left Arrow], see the "Key Mappings" section. *:RainbowCellGoRight* -Move cursor one cell right. +Move cursor one cell right. Consider mapping this to Ctrl+[Right Arrow], see the "Key Mappings" section. @@ -168,8 +172,8 @@ Example > *:RainbowQuery* -Enter RBQL Query editing mode. -When in the query editing mode, execute |:RainbowQuery| again to run the query. +Enter RBQL Query editing mode. +When in the query editing mode, execute |:RainbowQuery| again to run the query. Consider mapping |:RainbowQuery| to key i.e. > nnoremap :RainbowQuery @@ -194,8 +198,8 @@ Replace the content of the original file that was used to run the RBQL query wit ============================================================================== 3. Key Mappings *rainbow_csv-key_mappings* -Plugin does not create any new key mappings, but you can define your own in your .vimrc file. -All highlighted files have a special buffer variable |b:rbcsv| set to 1, so you can use this to define conditional csv-only key mappings. +Plugin does not create any new key mappings, but you can define your own in your .vimrc file. +All highlighted files have a special buffer variable |b:rbcsv| set to 1, so you can use this to define conditional csv-only key mappings. For example, to conditionally map Ctrl+Arrow keys to cell navigation commands you can use this snippet: > nnoremap get(b:, 'rbcsv', 0) == 1 ? ':RainbowCellGoLeft' : '' From c5c52c5ba1b3a48bd2ba8db559c35d9aba87c389 Mon Sep 17 00:00:00 2001 From: Cary W Reams Date: Thu, 6 Jun 2024 08:17:41 -0500 Subject: [PATCH 4/7] w/r/t #51, addressing PR #52 comments and concerns --- README.md | 25 +++++++++++----------- autoload/rainbow_csv.vim | 45 ++++++++++++++++++++++------------------ doc/rainbow_csv.txt | 3 ++- 3 files changed, 39 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 652e6ee..d6e579b 100644 --- a/README.md +++ b/README.md @@ -38,17 +38,16 @@ Each Rainbow CSV filetype is mapped to a separator and "policy" which describes If you run `:RainbowDelim` or `:RainbowMultiDelim` to select a separator that doesn't map to one of the built-in filetypes, Rainbow CSV will dynamically generate the filetype syntax file and put it into the "syntax" folder. List of built-in filetypes: -|Filetype |Separator |Extension |Properties | -|--------------- |--------------- |----------- |----------------------------------------------------- | -|csv |, (comma) |.csv |Ignored inside double-quoted fields | -|tsv |\t (TAB) |.tsv .tab | | -|csv_semicolon |; (semicolon) | |Ignored inside double-quoted fields | -|csv_whitespace |whitespace | |Consecutive whitespaces are merged | -|csv_pipe || (pipe) | | | -|rfc_csv |, (comma) | |Same as "csv" but allows multiline fields | -|rfc_semicolon |; (semicolon) | |Same as "csv_semicolon" but allows multiline fields | -|markdown || (pipe) |.md |align and shrink functions only operate on ranges | -|rmd || (pipe) |.rmd |align and shrink functions only operate on ranges | +|Filetype | Separator | Extension | Properties | +|---------------|---------------|-----------|-----------------------------------------------------| +|csv | , (comma) | .csv | Ignored inside double-quoted fields | +|tsv | \t (TAB) | .tsv .tab | | +|csv_semicolon | ; (semicolon) | | Ignored inside double-quoted fields | +|csv_whitespace | whitespace | | Consecutive whitespaces are merged | +|csv_pipe | | (pipe) | | | +|rfc_csv | , (comma) | | Same as "csv" but allows multiline fields | +|rfc_semicolon | ; (semicolon) | | Same as "csv_semicolon" but allows multiline fields | + ### Associating file extensions with CSV dialects In most cases the built-in autodetection algorithm should correctly detect correct CSV dialect for all CSV tables that you open in Vim, but if you have disabled the autodetection algorithm or don't want to rely on it for some reason, you can manually associate file extensions with available csv dialects. @@ -146,12 +145,12 @@ The linter checks the following: Align CSV columns with whitespaces. Don't run this command if you treat leading and trailing whitespaces in fields as part of the data. You can edit aligned CSV file in Vim column-edit mode (Ctrl+v). -In markdown and r-markdown files, this function requires a range of addresses or a visual range selection. +With markdown and rmd filetypes, the pipe separator is used by default. These filetypes also require that either an address range or visual selection be provided to the function. #### :RainbowShrink Remove leading and trailing whitespaces from all fields. Opposite to RainbowAlign -In markdown and r-markdown files, this function requires a range of addresses or a visual range selection. +With markdown and rmd filetypes, the pipe separator is used by default. These filetypes also require that either an address range or visual selection be provided to the function. #### :Select ... diff --git a/autoload/rainbow_csv.vim b/autoload/rainbow_csv.vim index 0acbf73..7652fd0 100644 --- a/autoload/rainbow_csv.vim +++ b/autoload/rainbow_csv.vim @@ -17,7 +17,7 @@ let s:system_python_interpreter = '' let s:magic_chars = '^*$.~/[]\' -let s:named_syntax_map = {'csv': [',', 'quoted', ''], 'csv_semicolon': [';', 'quoted', ''], 'tsv': ["\t", 'simple', ''], 'csv_pipe': ['|', 'simple', ''], 'csv_whitespace': [" ", 'whitespace', ''], 'rfc_csv': [',', 'quoted_rfc', ''], 'rfc_semicolon': [';', 'quoted_rfc', ''], 'markdown':['|', 'simple', ''], 'rmd':['|', 'simple', '']} +let s:named_syntax_map = {'csv': [',', 'quoted', ''], 'csv_semicolon': [';', 'quoted', ''], 'tsv': ["\t", 'simple', ''], 'csv_pipe': ['|', 'simple', ''], 'csv_whitespace': [" ", 'whitespace', ''], 'rfc_csv': [',', 'quoted_rfc', ''], 'rfc_semicolon': [';', 'quoted_rfc', '']} let s:autodetection_delims = exists('g:rcsv_delimiters') ? g:rcsv_delimiters : ["\t", ",", ";", "|"] @@ -819,13 +819,23 @@ func! rainbow_csv#align_field(field, is_first_line, max_field_components_lens, i endfunc func! rainbow_csv#csv_align() range - let l:firstline = a:firstline - let l:lastline = a:lastline + let first_line = a:firstline + let last_line = a:lastline " The first (statistic) pass of the function takes about 40% of runtime, the second (actual align) pass around 60% of runtime. " Numeric-aware logic by itself adds about 50% runtime compared to the basic string-based field width alignment " If there are lot of numeric columns this can additionally increase runtime by another 50% or more. - let show_progress_bar = (l:lastline - l:firstline) > 200000 + " no progress bar for markdown and rmd filetypes + let show_progress_bar = (wordcount()['bytes'] > 200000) && !(&ft == 'markdown' || &ft == 'rmd') let [delim, policy, comment_prefix] = rainbow_csv#get_current_dialect() + if (&ft == 'markdown' || &ft == 'rmd') + if (first_line == 1 && last_line == line("$")) + echoerr "RainbowAlign requires an address range in markdown and rmd files" + return + endif + policy = 'simple' + delim = '|' + comment_prefix = '' + endif if policy == 'monocolumn' echoerr "RainbowAlign is available only for highlighted CSV files" return @@ -835,19 +845,14 @@ func! rainbow_csv#csv_align() range return endif - if (l:firstline == 1 && l:lastline == line("$") && (&ft == 'markdown' || &ft == 'rmd')) - echoerr "RainbowAlign requires an address range in markdown and rmd files" - return - endif - - let lastLineNo = l:lastline + let lastLineNo = last_line let progress_bucket_size = (lastLineNo * 2) / s:progress_bar_size " multiply by 2 because we have two passes. if !show_progress_bar || progress_bucket_size < 10 let progress_bucket_size = 0 endif let s:align_progress_bar_position = 0 - let [column_stats, first_failed_line] = s:calc_column_stats(delim, policy, comment_prefix, progress_bucket_size,l:firstline,l:lastline) + let [column_stats, first_failed_line] = s:calc_column_stats(delim, policy, comment_prefix, progress_bucket_size,first_line,last_line) if first_failed_line != 0 echoerr 'Unable to allign: Inconsistent double quotes at line ' . first_failed_line return @@ -861,8 +866,8 @@ func! rainbow_csv#csv_align() range let has_edit = 0 - let is_first_line = l:firstline - for linenum in range(l:firstline, lastLineNo) + let is_first_line = first_line + for linenum in range(first_line, lastLineNo) if (progress_bucket_size && linenum % progress_bucket_size == 0) let s:align_progress_bar_position = s:align_progress_bar_position + 1 call s:display_progress_bar(s:align_progress_bar_position) @@ -892,13 +897,13 @@ func! rainbow_csv#csv_align() range let is_first_line = 0 endfor if !has_edit - echoerr "Range is already aligned" + echoerr "File is already aligned" endif endfunc func! rainbow_csv#csv_shrink() range - let l:firstline = a:firstline - let l:lastline = a:lastline + let first_line = a:firstline + let last_line = a:lastline let [delim, policy, comment_prefix] = rainbow_csv#get_current_dialect() if policy == 'monocolumn' echoerr "RainbowShrink is available only for highlighted CSV files" @@ -909,20 +914,20 @@ func! rainbow_csv#csv_shrink() range return endif - if (l:firstline == 1 && l:lastline == line("$") && (&ft == 'markdown' || &ft == 'rmd')) + if (first_line == 1 && last_line == line("$") && (&ft == 'markdown' || &ft == 'rmd')) echoerr "RainbowShrink requires an address range in markdown and rmd files" return endif - let lastLineNo = l:lastline + let lastLineNo = last_line let has_edit = 0 - let show_progress_bar = (l:lastline - l:firstline) > 200000 + let show_progress_bar = (last_line - first_line) > 200000 let progress_bucket_size = lastLineNo / s:progress_bar_size if !show_progress_bar || progress_bucket_size < 10 let progress_bucket_size = 0 endif let s:align_progress_bar_position = 0 - for linenum in range(l:firstline, lastLineNo) + for linenum in range(first_line, lastLineNo) if (progress_bucket_size && linenum % progress_bucket_size == 0) let s:align_progress_bar_position = s:align_progress_bar_position + 1 call s:display_progress_bar(s:align_progress_bar_position) diff --git a/doc/rainbow_csv.txt b/doc/rainbow_csv.txt index 726ee90..5f6052d 100644 --- a/doc/rainbow_csv.txt +++ b/doc/rainbow_csv.txt @@ -149,11 +149,12 @@ The linter checks the following: Align CSV columns with whitespaces. Don't run this command if you treat leading and trailing whitespaces in fields as part of the data. You can edit aligned CSV file in Vim column-edit mode (). - +With markdown and rmd filetypes, the pipe separator is used by default. These filetypes also require that either an address range or visual selection be provided to the function. *:RainbowShrink* Remove leading and trailing whitespaces from all fields. Opposite to RainbowAlign +With markdown and rmd filetypes, the pipe separator is used by default. These filetypes also require that either an address range or visual selection be provided to the function. *:Select* {...} From 0b618d172c1f1b4ccea9361c97c3c3792abfa1c5 Mon Sep 17 00:00:00 2001 From: Cary W Reams Date: Thu, 6 Jun 2024 08:33:19 -0500 Subject: [PATCH 5/7] w/r/t #51, addressing PR #52; restores whitespace to README.md --- README.md | 249 +++++++++++++++++++++++++++--------------------------- 1 file changed, 125 insertions(+), 124 deletions(-) diff --git a/README.md b/README.md index d6e579b..e699845 100644 --- a/README.md +++ b/README.md @@ -3,17 +3,17 @@ ![rainbow_csv](https://i.imgur.com/EhV2niB.png) ## Installation -Use your favorite package manager. +Use your favorite package manager. -Vundle: `Plugin 'mechatroner/rainbow_csv'` -VimPlug: `Plug 'mechatroner/rainbow_csv'` -dein: `call dein#add('mechatroner/rainbow_csv')` +Vundle: `Plugin 'mechatroner/rainbow_csv'` +VimPlug: `Plug 'mechatroner/rainbow_csv'` +dein: `call dein#add('mechatroner/rainbow_csv')` -No additional steps required - Rainbow CSV will work out of the box. +No additional steps required - Rainbow CSV will work out of the box. ## Overview -Main features: -* Highlight CSV columns in different rainbow colors. +Main features: +* Highlight CSV columns in different rainbow colors. * Provide info about column under the cursor * Provide `SELECT` and `UPDATE` queries in RBQL: SQL-like transprogramming query language. * Consistency check for csv files (CSVLint) @@ -21,22 +21,22 @@ Main features: * Cell-level cursor navigation There are 4 ways to enable csv columns highlighting: -1. CSV autodetection based on file content and/or extension -2. Manual CSV delimiter selection with `:RainbowDelim` command with cursor over the delimiter -3. Manual CSV delimiter selection with `:RainbowMultiDelim` for multi-character delimiters selecting them in "VISUAL" mode -4. Explicitly activate one of the built-in filetypes, e.g. `:set ft=csv` +1. CSV autodetection based on file content and/or extension +2. Manual CSV delimiter selection with `:RainbowDelim` command with cursor over the delimiter +3. Manual CSV delimiter selection with `:RainbowMultiDelim` for multi-character delimiters selecting them in "VISUAL" mode +4. Explicitly activate one of the built-in filetypes, e.g. `:set ft=csv` -The core functionality of the plugin is written in pure vimscript, no additional libraries required. +The core functionality of the plugin is written in pure vimscript, no additional libraries required. ![Rainbow CSV Screenshot](https://user-images.githubusercontent.com/5349737/190057249-8ec401f6-b76d-4420-a6af-053dd502f8a9.png) # Plugin description ### Built-in and autogenerated filetypes -Rainbow CSV has 7 built-in CSV filetypes and infinite number of autogenerated filetypes. -Each Rainbow CSV filetype is mapped to a separator and "policy" which describes additional properties e.g. if separator can be escaped inside double quoted field. -If you run `:RainbowDelim` or `:RainbowMultiDelim` to select a separator that doesn't map to one of the built-in filetypes, Rainbow CSV will dynamically generate the filetype syntax file and put it into the "syntax" folder. -List of built-in filetypes: +Rainbow CSV has 7 built-in CSV filetypes and infinite number of autogenerated filetypes. +Each Rainbow CSV filetype is mapped to a separator and "policy" which describes additional properties e.g. if separator can be escaped inside double quoted field. +If you run `:RainbowDelim` or `:RainbowMultiDelim` to select a separator that doesn't map to one of the built-in filetypes, Rainbow CSV will dynamically generate the filetype syntax file and put it into the "syntax" folder. +List of built-in filetypes: |Filetype | Separator | Extension | Properties | |---------------|---------------|-----------|-----------------------------------------------------| @@ -50,26 +50,26 @@ List of built-in filetypes: ### Associating file extensions with CSV dialects -In most cases the built-in autodetection algorithm should correctly detect correct CSV dialect for all CSV tables that you open in Vim, but if you have disabled the autodetection algorithm or don't want to rely on it for some reason, you can manually associate file extensions with available csv dialects. -Example: to associate ".dat" extension with "csv_pipe" dialect and ".csv" extension with "csv_semicolon" add the folowing lines to your .vimrc: +In most cases the built-in autodetection algorithm should correctly detect correct CSV dialect for all CSV tables that you open in Vim, but if you have disabled the autodetection algorithm or don't want to rely on it for some reason, you can manually associate file extensions with available csv dialects. +Example: to associate ".dat" extension with "csv_pipe" dialect and ".csv" extension with "csv_semicolon" add the folowing lines to your .vimrc: ``` autocmd BufNewFile,BufRead *.csv set filetype=csv_semicolon autocmd BufNewFile,BufRead *.dat set filetype=csv_pipe ``` ### Working with multiline CSV fields -In rare cases some CSV files can contain double-quoted fields spanning multiple lines. -To work with such files you can set filetype to either "rfc_csv" or "rfc_semicolon" e.g. `:set ft=rfc_csv`. -Syntax highlighting for rfc_csv and rfc_semicolon dialects can sometimes go out of sync with the file content, use `:syntax sync fromstart` command in that case. -rfc_csv and rfc_semicolon are fully supported by RBQL which among other things allows you to easily convert them to line-by-line CSV by replacing newlines in fields with sequences of 4 spaces or something like that. -rfc_csv and rfc_semicolon take their name from [RFC 4180](https://tools.ietf.org/html/rfc4180) memo with which they are fully compatible. +In rare cases some CSV files can contain double-quoted fields spanning multiple lines. +To work with such files you can set filetype to either "rfc_csv" or "rfc_semicolon" e.g. `:set ft=rfc_csv`. +Syntax highlighting for rfc_csv and rfc_semicolon dialects can sometimes go out of sync with the file content, use `:syntax sync fromstart` command in that case. +rfc_csv and rfc_semicolon are fully supported by RBQL which among other things allows you to easily convert them to line-by-line CSV by replacing newlines in fields with sequences of 4 spaces or something like that. +rfc_csv and rfc_semicolon take their name from [RFC 4180](https://tools.ietf.org/html/rfc4180) memo with which they are fully compatible. ### Built-in RBQL query language -Rainbow CSV comes with built-in lightweight RBQL SQL-like query engine. -To run an RBQL query either use `:Select` command e.g. `:Select a1, a2` or run `:RainbowQuery` command to enter query editing mode. +Rainbow CSV comes with built-in lightweight RBQL SQL-like query engine. +To run an RBQL query either use `:Select` command e.g. `:Select a1, a2` or run `:RainbowQuery` command to enter query editing mode. -Demonstration of rainbow_csv highlighting and RBQL queries +Demonstration of rainbow_csv highlighting and RBQL queries ![demo_screencast](https://i.imgur.com/4PIVvjc.gif) @@ -79,10 +79,10 @@ In this demo python expressions were used, but JavaScript is also available. ### Rainbow highlighting for non-table files -You can use rainbow highlighting and RBQL even for non-csv/tsv files. -E.g. you can highlight records in log files, one-line xmls and other delimited records. -You can even highlight function arguments in your programming language using comma or comma+whitespaces as a delimiter for `:RainbowDelim` or `:RainbowMultiDelim` commands. -And you can always turn off the rainbow highlighting using `:NoRainbowDelim` command. +You can use rainbow highlighting and RBQL even for non-csv/tsv files. +E.g. you can highlight records in log files, one-line xmls and other delimited records. +You can even highlight function arguments in your programming language using comma or comma+whitespaces as a delimiter for `:RainbowDelim` or `:RainbowMultiDelim` commands. +And you can always turn off the rainbow highlighting using `:NoRainbowDelim` command. ### Commands @@ -91,12 +91,12 @@ And you can always turn off the rainbow highlighting using `:NoRainbowDelim` com Mark current file as a table and highlight it's columns in rainbow colors. Character under the cursor will be used as a delimiter. The delimiter will be saved in a config file for future vim sessions. -You can also use this command for non-csv files, e.g. to highlight function arguments +You can also use this command for non-csv files, e.g. to highlight function arguments in source code in different colors. To return back to original syntax highlighting run `:NoRainbowDelim` #### :RainbowMultiDelim -Same as `:RainbowDelim`, but works with multicharacter separators. +Same as `:RainbowDelim`, but works with multicharacter separators. Visually select the multicharacter separator (e.g. `~#~`) and run `:RainbowMultiDelim` command. #### :NoRainbowDelim @@ -105,46 +105,46 @@ Disable rainbow columns highlighting for the current file. #### :RainbowCellGoUp -Move cursor one cell up. +Move cursor one cell up. Consider mapping this to Ctrl+[Up Arrow], see the "Key Mappings" section. #### :RainbowCellGoDown -Move cursor one cell down. +Move cursor one cell down. Consider mapping this to Ctrl+[Down Arrow], see the "Key Mappings" section. #### :RainbowCellGoLeft -Move cursor one cell left. +Move cursor one cell left. Consider mapping this to Ctrl+[Left Arrow], see the "Key Mappings" section. #### :RainbowCellGoRight -Move cursor one cell right. +Move cursor one cell right. Consider mapping this to Ctrl+[Right Arrow], see the "Key Mappings" section. #### :RainbowComment -Mark the character under the cursor as the comment prefix, e.g. `#`. By default Rainbow CSV doesn't highlight comments in CSV files. -You can also use `:RainbowCommentMulti` to mark a visual selection as a multicharacter comment prefix +Mark the character under the cursor as the comment prefix, e.g. `#`. By default Rainbow CSV doesn't highlight comments in CSV files. +You can also use `:RainbowCommentMulti` to mark a visual selection as a multicharacter comment prefix #### :NoRainbowComment -Disable all comments for the current CSV file. -This command is especially useful when you have set `g:rainbow_comment_prefix` variable and want to exclude just one particular file. +Disable all comments for the current CSV file. +This command is especially useful when you have set `g:rainbow_comment_prefix` variable and want to exclude just one particular file. #### :CSVLint -The linter checks the following: -* consistency of double quotes usage in CSV rows -* consistency of number of fields per CSV row +The linter checks the following: +* consistency of double quotes usage in CSV rows +* consistency of number of fields per CSV row #### :RainbowAlign -Align CSV columns with whitespaces. -Don't run this command if you treat leading and trailing whitespaces in fields as part of the data. -You can edit aligned CSV file in Vim column-edit mode (Ctrl+v). +Align CSV columns with whitespaces. +Don't run this command if you treat leading and trailing whitespaces in fields as part of the data. +You can edit aligned CSV file in Vim column-edit mode (Ctrl+v). With markdown and rmd filetypes, the pipe separator is used by default. These filetypes also require that either an address range or visual selection be provided to the function. #### :RainbowShrink @@ -154,18 +154,18 @@ With markdown and rmd filetypes, the pipe separator is used by default. These fi #### :Select ... -Allows to enter RBQL select query as vim command. +Allows to enter RBQL select query as vim command. E.g. `:Select a1, a2 order by a1` #### :Update ... -Allows to enter RBQL update query as vim command. +Allows to enter RBQL update query as vim command. E.g. `:Update a1 = a1 + " " + a2` #### :RainbowQuery -Enter RBQL Query editing mode. -When in the query editing mode, execute `:RainbowQuery` again to run the query. +Enter RBQL Query editing mode. +When in the query editing mode, execute `:RainbowQuery` again to run the query. Consider mapping `:RainbowQuery` to `` key i.e. `nnoremap :RainbowQuery` @@ -174,7 +174,7 @@ Consider mapping `:RainbowQuery` to `` key i.e. `nnoremap :RainbowQuery Assign any name to the current table. You can use this name in join operation instead of the table path. E.g. ``` JOIN customers ON a1 == b1 -``` +``` intead of: ``` JOIN /path/to/my/customers/table ON a1 == b1 @@ -182,13 +182,13 @@ JOIN /path/to/my/customers/table ON a1 == b1 #### :RainbowCopyBack -This command only applicable for RBQL output files. +This command only applicable for RBQL output files. Replace the content of the original file that was used to run the RBQL query with the query result set data. ### Key Mappings -Plugin does not create any new key mappings, but you can define your own in your .vimrc file. -All highlighted files have a special buffer variable `b:rbcsv` set to 1, so you can use this to define conditional csv-only key mappings. +Plugin does not create any new key mappings, but you can define your own in your .vimrc file. +All highlighted files have a special buffer variable `b:rbcsv` set to 1, so you can use this to define conditional csv-only key mappings. For example, to conditionally map Ctrl+Arrow keys to cell navigation commands you can use this snippet: ``` @@ -206,80 +206,80 @@ nnoremap :RainbowCellGoRight ### Configuration #### g:disable_rainbow_hover -Set to `1` to stop showing info about the column under the cursor in Vim command line -Example: +Set to `1` to stop showing info about the column under the cursor in Vim command line +Example: ``` let g:disable_rainbow_hover = 1 ``` #### g:rcsv_delimiters -Default: `["\t", ",", ";", "|"]` -List of separators to try for content-based autodetection +Default: `["\t", ",", ";", "|"]` +List of separators to try for content-based autodetection You can add or remove values from the list. Example: ``` let g:rcsv_delimiters = ["\t", ",", "^", "~#~"] ``` #### g:disable_rainbow_csv_autodetect -Set to `1` to disable CSV autodetection mechanism -Example: +Set to `1` to disable CSV autodetection mechanism +Example: ``` let g:disable_rainbow_csv_autodetect = 1 ``` -Manual delimiter selection would still be possible. -You can also manually associate specific file extensions with 'csv' or 'tsv' filetypes +Manual delimiter selection would still be possible. +You can also manually associate specific file extensions with 'csv' or 'tsv' filetypes #### g:rainbow_comment_prefix -Default: `''` -A string to use as a comment prefix for all CSV files you open in Vim. -This setting is helpful if you are dealing with lots of CSV files which consistently use the same comment prefix e.g. `'#'` or `'>>'` -If you want to enable comments on file-by-file basis, use the `:RainbowComment` or `:RainbowCommentMulti` commands instead. -To cancel the effect of `g:rainbow_comment_prefix` just for the current file use `:NoRainbowComment` command. +Default: `''` +A string to use as a comment prefix for all CSV files you open in Vim. +This setting is helpful if you are dealing with lots of CSV files which consistently use the same comment prefix e.g. `'#'` or `'>>'` +If you want to enable comments on file-by-file basis, use the `:RainbowComment` or `:RainbowCommentMulti` commands instead. +To cancel the effect of `g:rainbow_comment_prefix` just for the current file use `:NoRainbowComment` command. #### g:rcsv_max_columns -Default: `30` -Autodetection will fail if buffer has more than _g:rcsv\_max\_columns_ columns. +Default: `30` +Autodetection will fail if buffer has more than _g:rcsv\_max\_columns_ columns. You can increase or decrease this limit. #### g:rcsv_colorpairs -List of color name pairs to customize rainbow highlighting. -Each entry in the list is a pair of two colors: the first color is for terminal mode, the second one is for GUI mode. +List of color name pairs to customize rainbow highlighting. +Each entry in the list is a pair of two colors: the first color is for terminal mode, the second one is for GUI mode. Example: ``` let g:rcsv_colorpairs = [['red', 'red'], ['blue', 'blue'], ['green', 'green'], ['magenta', 'magenta'], ['NONE', 'NONE'], ['darkred', 'darkred'], ['darkblue', 'darkblue'], ['darkgreen', 'darkgreen'], ['darkmagenta', 'darkmagenta'], ['darkcyan', 'darkcyan']] ``` #### g:multiline_search_range -Default: `10` -This settings is only relevant for rfc_csv and rfc_semicolon dialects. -If some multiline records contain more lines that this value, hover info will not work correctly. It is not recommended to significantly increase this value because it will have negative impact on hover info performance +Default: `10` +This settings is only relevant for rfc_csv and rfc_semicolon dialects. +If some multiline records contain more lines that this value, hover info will not work correctly. It is not recommended to significantly increase this value because it will have negative impact on hover info performance #### g:rbql_backend_language -Default: `'python'` -Supported values: `'python'`, `'js'` +Default: `'python'` +Supported values: `'python'`, `'js'` Scripting language to use in RBQL expressions. #### g:rbql_encoding -Default: `utf-8` -Supported values: `'utf-8'`, `'latin-1'` +Default: `utf-8` +Supported values: `'utf-8'`, `'latin-1'` -CSV files encoding for RBQL. +CSV files encoding for RBQL. #### g:rbql_output_format -Default: `'input'` +Default: `'input'` Supported values: `'tsv'`, `'csv'`, `'input'` Format of RBQL result set tables. * input: same format as the input table -* tsv: doesn't allow quoted tabs inside fields. +* tsv: doesn't allow quoted tabs inside fields. * csv: is Excel-compatible and allows quoted commas. -Essentially format is a pair: delimiter + quoting policy. +Essentially format is a pair: delimiter + quoting policy. This setting for example can be used to convert files between tsv and csv format: * To convert _csv_ to _tsv_: **1.** open csv file. **2.** `:let g:rbql_output_format='tsv'` **3.** `:Select *` * To convert _tsv_ to _csv_: **1.** open tsv file. **2.** `:let g:rbql_output_format='csv'` **3.** `:Select *` @@ -290,16 +290,16 @@ Set to `1` to use system python interpreter for RBQL queries instead of the pyth #### g:rbql_with_headers -If most of the CSV files that you work with have headers, you can set this value to 1. In this case RBQL will treat first records in files as headers by default. -Example: `:let g:rbql_with_headers = 1` +If most of the CSV files that you work with have headers, you can set this value to 1. In this case RBQL will treat first records in files as headers by default. +Example: `:let g:rbql_with_headers = 1` You can also adjust (or override) this setting by adding `WITH (header)` or `WITH (noheader)` to the end of your RBQL queries. # RBQL (Rainbow Query Language) Description -RBQL is an eval-based SQL-like query engine for (not only) CSV file processing. It provides SQL-like language that supports SELECT queries with Python or JavaScript expressions. -RBQL is best suited for data transformation, data cleaning, and analytical queries. -RBQL is distributed with CLI apps, text editor plugins, Python and JS libraries. +RBQL is an eval-based SQL-like query engine for (not only) CSV file processing. It provides SQL-like language that supports SELECT queries with Python or JavaScript expressions. +RBQL is best suited for data transformation, data cleaning, and analytical queries. +RBQL is distributed with CLI apps, text editor plugins, Python and JS libraries. [Official Site](https://rbql.org/) @@ -333,29 +333,29 @@ RBQL is distributed with CLI apps, text editor plugins, Python and JS libraries. * LIMIT _N_ * AS -All keywords have the same meaning as in SQL queries. You can check them [online](https://www.w3schools.com/sql/default.asp) +All keywords have the same meaning as in SQL queries. You can check them [online](https://www.w3schools.com/sql/default.asp) ### RBQL variables RBQL for CSV files provides the following variables which you can use in your queries: -* _a1_, _a2_,..., _a{N}_ - Variable type: **string** - Description: value of i-th field in the current record in input table -* _b1_, _b2_,..., _b{N}_ - Variable type: **string** - Description: value of i-th field in the current record in join table B -* _NR_ - Variable type: **integer** - Description: Record number (1-based) -* _NF_ - Variable type: **integer** - Description: Number of fields in the current record -* _a.name_, _b.Person_age_, ... _a.{Good_alphanumeric_column_name}_ - Variable type: **string** - Description: Value of the field referenced by it's "name". You can use this notation if the field in the header has a "good" alphanumeric name -* _a["object id"]_, _a['9.12341234']_, _b["%$ !! 10 20"]_ ... _a["Arbitrary column name!"]_ - Variable type: **string** +* _a1_, _a2_,..., _a{N}_ + Variable type: **string** + Description: value of i-th field in the current record in input table +* _b1_, _b2_,..., _b{N}_ + Variable type: **string** + Description: value of i-th field in the current record in join table B +* _NR_ + Variable type: **integer** + Description: Record number (1-based) +* _NF_ + Variable type: **integer** + Description: Number of fields in the current record +* _a.name_, _b.Person_age_, ... _a.{Good_alphanumeric_column_name}_ + Variable type: **string** + Description: Value of the field referenced by it's "name". You can use this notation if the field in the header has a "good" alphanumeric name +* _a["object id"]_, _a['9.12341234']_, _b["%$ !! 10 20"]_ ... _a["Arbitrary column name!"]_ + Variable type: **string** Description: Value of the field referenced by it's "name". You can use this notation to reference fields by arbitrary values in the header @@ -365,31 +365,31 @@ _UPDATE_ query produces a new table where original values are replaced according ### Aggregate functions and queries -RBQL supports the following aggregate functions, which can also be used with _GROUP BY_ keyword: -_COUNT_, _ARRAY_AGG_, _MIN_, _MAX_, _SUM_, _AVG_, _VARIANCE_, _MEDIAN_ +RBQL supports the following aggregate functions, which can also be used with _GROUP BY_ keyword: +_COUNT_, _ARRAY_AGG_, _MIN_, _MAX_, _SUM_, _AVG_, _VARIANCE_, _MEDIAN_ -Limitation: aggregate functions inside Python (or JS) expressions are not supported. Although you can use expressions inside aggregate functions. -E.g. `MAX(float(a1) / 1000)` - valid; `MAX(a1) / 1000` - invalid. -There is a workaround for the limitation above for _ARRAY_AGG_ function which supports an optional parameter - a callback function that can do something with the aggregated array. Example: +Limitation: aggregate functions inside Python (or JS) expressions are not supported. Although you can use expressions inside aggregate functions. +E.g. `MAX(float(a1) / 1000)` - valid; `MAX(a1) / 1000` - invalid. +There is a workaround for the limitation above for _ARRAY_AGG_ function which supports an optional parameter - a callback function that can do something with the aggregated array. Example: `SELECT a2, ARRAY_AGG(a1, lambda v: sorted(v)[:5]) GROUP BY a2` - Python; `SELECT a2, ARRAY_AGG(a1, v => v.sort().slice(0, 5)) GROUP BY a2` - JS ### JOIN statements -Join table B can be referenced either by its file path or by its name - an arbitrary string which the user should provide before executing the JOIN query. -RBQL supports _STRICT LEFT JOIN_ which is like _LEFT JOIN_, but generates an error if any key in the left table "A" doesn't have exactly one matching key in the right table "B". -Table B path can be either relative to the working dir, relative to the main table or absolute. +Join table B can be referenced either by its file path or by its name - an arbitrary string which the user should provide before executing the JOIN query. +RBQL supports _STRICT LEFT JOIN_ which is like _LEFT JOIN_, but generates an error if any key in the left table "A" doesn't have exactly one matching key in the right table "B". +Table B path can be either relative to the working dir, relative to the main table or absolute. Limitation: _JOIN_ statements can't contain Python/JS expressions and must have the following form: _ (/path/to/table.tsv | table_name ) ON a... == b... [AND a... == b... [AND ... ]]_ ### SELECT EXCEPT statement -SELECT EXCEPT can be used to select everything except specific columns. E.g. to select everything but columns 2 and 4, run: `SELECT * EXCEPT a2, a4` +SELECT EXCEPT can be used to select everything except specific columns. E.g. to select everything but columns 2 and 4, run: `SELECT * EXCEPT a2, a4` Traditional SQL engines do not support this query mode. ### UNNEST() operator -UNNEST(list) takes a list/array as an argument and repeats the output record multiple times - one time for each value from the list argument. -Example: `SELECT a1, UNNEST(a2.split(';'))` +UNNEST(list) takes a list/array as an argument and repeats the output record multiple times - one time for each value from the list argument. +Example: `SELECT a1, UNNEST(a2.split(';'))` ### LIKE() function @@ -405,8 +405,8 @@ Example: `select top 5 NR, * with (header)` ### User Defined Functions (UDF) -RBQL supports User Defined Functions -You can define custom functions and/or import libraries in two special files: +RBQL supports User Defined Functions +You can define custom functions and/or import libraries in two special files: * `~/.rbql_init_source.py` - for Python * `~/.rbql_init_source.js` - for JavaScript @@ -445,12 +445,12 @@ You can define custom functions and/or import libraries in two special files: #### Advantages -* WYSIWYG -* Familiar editing environment of your favorite text editor -* Zero-cost abstraction: Syntax highlighting is essentially free, while graphical column alignment can be computationally expensive +* WYSIWYG +* Familiar editing environment of your favorite text editor +* Zero-cost abstraction: Syntax highlighting is essentially free, while graphical column alignment can be computationally expensive * High information density: Rainbow CSV shows more data per screen because it doesn't insert column-aligning whitespaces. * Works with non-table and semi-tabular files (text files that contain both table(s) and non-table data like text) -* Ability to visually associate two same-colored columns from two different windows. This is not possible with graphical column alignment +* Ability to visually associate two same-colored columns from two different windows. This is not possible with graphical column alignment #### Disadvantages @@ -479,7 +479,8 @@ You can define custom functions and/or import libraries in two special files: * Demo Google Colab IPython [notebook](https://colab.research.google.com/drive/1_cFPtnQUxILP0RE2_DBlqIfXaEzT-oZ6?usp=sharing) #### Related vim plugins: -Rainbow CSV name and original implementation was significantly influenced by [rainbow_parentheses](https://github.com/kien/rainbow_parentheses.vim) Vim plugin. +Rainbow CSV name and original implementation was significantly influenced by [rainbow_parentheses](https://github.com/kien/rainbow_parentheses.vim) Vim plugin. -There also exists an old vim syntax file [csv_color](https://vim.sourceforge.io/scripts/script.php?script_id=518) which, despite it's name, can highlight only *.tsv files. +There also exists an old vim syntax file [csv_color](https://vim.sourceforge.io/scripts/script.php?script_id=518) which, despite it's name, can highlight only *.tsv files. And, of course, there is [csv.vim](https://github.com/chrisbra/csv.vim) + From 6fcc0c7d0a3bf297a619c32555386b1adda72447 Mon Sep 17 00:00:00 2001 From: Cary W Reams Date: Thu, 6 Jun 2024 08:54:21 -0500 Subject: [PATCH 6/7] w/r/t #51, addressing PR #52; restores whitespace to autoload/.vim and doc/.txt --- autoload/rainbow_csv.vim | 41 +++++++++++++++++++++------------------- doc/rainbow_csv.txt | 23 ++++++++++------------ 2 files changed, 32 insertions(+), 32 deletions(-) diff --git a/autoload/rainbow_csv.vim b/autoload/rainbow_csv.vim index 7652fd0..a9d2edd 100644 --- a/autoload/rainbow_csv.vim +++ b/autoload/rainbow_csv.vim @@ -761,6 +761,7 @@ func! rainbow_csv#adjust_column_stats(column_stats) return adjusted_stats endfunc + func! s:calc_column_stats(delim, policy, comment_prefix, progress_bucket_size, first_line, last_line) " Result `column_stats` is a list of (max_total_len, max_int_part_len, max_fractional_part_len) tuples. let column_stats = [] @@ -791,6 +792,7 @@ func! s:calc_column_stats(delim, policy, comment_prefix, progress_bucket_size, f return [column_stats, 0] endfunc + func! rainbow_csv#align_field(field, is_first_line, max_field_components_lens, is_last_column) " Align field, use max() to avoid negative delta_length which can happen theorethically due to async doc edit. let extra_readability_whitespace_length = 1 @@ -818,23 +820,25 @@ func! rainbow_csv#align_field(field, is_first_line, max_field_components_lens, i return repeat(' ', integer_delta_length) . clean_field . trailing_spaces endfunc + func! rainbow_csv#csv_align() range let first_line = a:firstline let last_line = a:lastline " The first (statistic) pass of the function takes about 40% of runtime, the second (actual align) pass around 60% of runtime. " Numeric-aware logic by itself adds about 50% runtime compared to the basic string-based field width alignment " If there are lot of numeric columns this can additionally increase runtime by another 50% or more. - " no progress bar for markdown and rmd filetypes - let show_progress_bar = (wordcount()['bytes'] > 200000) && !(&ft == 'markdown' || &ft == 'rmd') + let show_progress_bar = wordcount()['bytes'] > 200000 let [delim, policy, comment_prefix] = rainbow_csv#get_current_dialect() if (&ft == 'markdown' || &ft == 'rmd') if (first_line == 1 && last_line == line("$")) echoerr "RainbowAlign requires an address range in markdown and rmd files" return endif - policy = 'simple' - delim = '|' - comment_prefix = '' + let policy = 'simple' + let delim = '|' + let comment_prefix = '' + " no progress bar for markdown and rmd filetypes + let show_progress_bar = 0 endif if policy == 'monocolumn' echoerr "RainbowAlign is available only for highlighted CSV files" @@ -844,28 +848,23 @@ func! rainbow_csv#csv_align() range echoerr 'RainbowAlign not available for "rfc_csv" filetypes, consider using "csv" instead' return endif - let lastLineNo = last_line let progress_bucket_size = (lastLineNo * 2) / s:progress_bar_size " multiply by 2 because we have two passes. if !show_progress_bar || progress_bucket_size < 10 let progress_bucket_size = 0 endif let s:align_progress_bar_position = 0 - let [column_stats, first_failed_line] = s:calc_column_stats(delim, policy, comment_prefix, progress_bucket_size,first_line,last_line) if first_failed_line != 0 echoerr 'Unable to allign: Inconsistent double quotes at line ' . first_failed_line return endif - let column_stats = rainbow_csv#adjust_column_stats(column_stats) if !len(column_stats) echoerr 'Unable to allign: Internal Rainbow CSV Error' return endif - let has_edit = 0 - let is_first_line = first_line for linenum in range(first_line, lastLineNo) if (progress_bucket_size && linenum % progress_bucket_size == 0) @@ -901,10 +900,21 @@ func! rainbow_csv#csv_align() range endif endfunc + func! rainbow_csv#csv_shrink() range let first_line = a:firstline let last_line = a:lastline + let show_progress_bar = wordcount()['bytes'] > 200000 let [delim, policy, comment_prefix] = rainbow_csv#get_current_dialect() + if (&ft == 'markdown' || &ft == 'rmd') + if (first_line == 1 && last_line == line("$")) + echoerr "RainbowShrink requires an address range in markdown and rmd files" + return + endif + let policy = 'simple' + let delim = '|' + let comment_prefix = '' + endif if policy == 'monocolumn' echoerr "RainbowShrink is available only for highlighted CSV files" return @@ -913,15 +923,8 @@ func! rainbow_csv#csv_shrink() range echoerr 'RainbowShrink not available for "rfc_csv" filetypes, consider using "csv" instead' return endif - - if (first_line == 1 && last_line == line("$") && (&ft == 'markdown' || &ft == 'rmd')) - echoerr "RainbowShrink requires an address range in markdown and rmd files" - return - endif - let lastLineNo = last_line let has_edit = 0 - let show_progress_bar = (last_line - first_line) > 200000 let progress_bucket_size = lastLineNo / s:progress_bar_size if !show_progress_bar || progress_bucket_size < 10 let progress_bucket_size = 0 @@ -1140,7 +1143,7 @@ func! s:cell_jump_simple(direction, delim, policy, comment_prefix) if a:direction == 'down' || a:direction == 'up' let lastLineNo = line("$") let cur_line_num = anchor_line_num - while 1 + while 1 if a:direction == 'down' let cur_line_num += 1 else @@ -1202,7 +1205,7 @@ func! s:cell_jump_rfc(direction, delim, comment_prefix) let field_num += 1 elseif a:direction == 'left' let field_num -= 1 - elseif a:direction == 'down' + elseif a:direction == 'down' let relative_record_num += 1 elseif a:direction == 'up' let relative_record_num -= 1 diff --git a/doc/rainbow_csv.txt b/doc/rainbow_csv.txt index 5f6052d..f67547b 100644 --- a/doc/rainbow_csv.txt +++ b/doc/rainbow_csv.txt @@ -32,7 +32,7 @@ There are 4 ways to enable csv columns highlighting: 3. Manual CSV delimiter selection with |:RainbowMultiDelim| for multi-character delimiters 4. Execute `:set ft=csv` or `:set ft=tsv` for CSV/TSV dialects. For other separators use |:RainbowDelim| -To run an RBQL query either use |:Select| command e.g. ":Select a1, a2" or run |:RainbowQuery| command to enter query editing mode. +To run an RBQL query either use |:Select| command e.g. ":Select a1, a2" or run |:RainbowQuery| command to enter query editing mode. As soon as you finish entering "select" (or update) and press whitespace, the plugin will show column names in the status line. The core functionality of the plugin is written in pure vimscript, no additional libraries required. @@ -75,10 +75,6 @@ In rare cases some CSV files can contain double-quoted fields spanning multiple To work with such files you can set filetype to either "rfc_csv" or "rfc_semicolon". Syntax highlighting for rfc_csv and rfc_semicolon dialects can go out of sync with the file content under specific conditions, use `:syntax sync fromstart` command in that case -Working with markdown and rmd files ~ -You can use |:RainbowAlign| and |:RainbowShrink| by providing an address range -or visual range to either function. - ============================================================================== 2. Commands *rainbow_csv-commands* @@ -106,22 +102,22 @@ Disable rainbow columns highlighting for the current file. *:RainbowCellGoUp* -Move cursor one cell up. +Move cursor one cell up. Consider mapping this to Ctrl+[Up Arrow], see the "Key Mappings" section. *:RainbowCellGoDown* -Move cursor one cell down. +Move cursor one cell down. Consider mapping this to Ctrl+[Down Arrow], see the "Key Mappings" section. *:RainbowCellGoLeft* -Move cursor one cell left. +Move cursor one cell left. Consider mapping this to Ctrl+[Left Arrow], see the "Key Mappings" section. *:RainbowCellGoRight* -Move cursor one cell right. +Move cursor one cell right. Consider mapping this to Ctrl+[Right Arrow], see the "Key Mappings" section. @@ -151,6 +147,7 @@ Don't run this command if you treat leading and trailing whitespaces in fields a You can edit aligned CSV file in Vim column-edit mode (). With markdown and rmd filetypes, the pipe separator is used by default. These filetypes also require that either an address range or visual selection be provided to the function. + *:RainbowShrink* Remove leading and trailing whitespaces from all fields. Opposite to RainbowAlign @@ -173,8 +170,8 @@ Example > *:RainbowQuery* -Enter RBQL Query editing mode. -When in the query editing mode, execute |:RainbowQuery| again to run the query. +Enter RBQL Query editing mode. +When in the query editing mode, execute |:RainbowQuery| again to run the query. Consider mapping |:RainbowQuery| to key i.e. > nnoremap :RainbowQuery @@ -199,8 +196,8 @@ Replace the content of the original file that was used to run the RBQL query wit ============================================================================== 3. Key Mappings *rainbow_csv-key_mappings* -Plugin does not create any new key mappings, but you can define your own in your .vimrc file. -All highlighted files have a special buffer variable |b:rbcsv| set to 1, so you can use this to define conditional csv-only key mappings. +Plugin does not create any new key mappings, but you can define your own in your .vimrc file. +All highlighted files have a special buffer variable |b:rbcsv| set to 1, so you can use this to define conditional csv-only key mappings. For example, to conditionally map Ctrl+Arrow keys to cell navigation commands you can use this snippet: > nnoremap get(b:, 'rbcsv', 0) == 1 ? ':RainbowCellGoLeft' : '' From 51cf551710fa8dcba19f652c081e1f22c87846b0 Mon Sep 17 00:00:00 2001 From: Cary W Reams Date: Fri, 7 Jun 2024 07:56:48 -0500 Subject: [PATCH 7/7] round II, PR #52 + is_last_line returned to treatment as bool + range-provided checks converted to use + predicates exception for markdown/rmd files on current dialect = monocolumn for both shrink and align functions + adds maps of exception filetypes; used in both shink and align functions; other variations of markdown may be supported in future --- autoload/rainbow_csv.vim | 18 ++++++++++-------- plugin/rainbow_csv.vim | 4 ++-- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/autoload/rainbow_csv.vim b/autoload/rainbow_csv.vim index a9d2edd..e8802e2 100644 --- a/autoload/rainbow_csv.vim +++ b/autoload/rainbow_csv.vim @@ -19,6 +19,8 @@ let s:magic_chars = '^*$.~/[]\' let s:named_syntax_map = {'csv': [',', 'quoted', ''], 'csv_semicolon': [';', 'quoted', ''], 'tsv': ["\t", 'simple', ''], 'csv_pipe': ['|', 'simple', ''], 'csv_whitespace': [" ", 'whitespace', ''], 'rfc_csv': [',', 'quoted_rfc', ''], 'rfc_semicolon': [';', 'quoted_rfc', '']} +let s:ft_exceptions_map = {'markdown':['|','simple',''], 'rmd':['|','simple','']} + let s:autodetection_delims = exists('g:rcsv_delimiters') ? g:rcsv_delimiters : ["\t", ",", ";", "|"] let s:number_regex = '^[0-9]\+\(\.[0-9]\+\)\?$' @@ -766,7 +768,7 @@ func! s:calc_column_stats(delim, policy, comment_prefix, progress_bucket_size, f " Result `column_stats` is a list of (max_total_len, max_int_part_len, max_fractional_part_len) tuples. let column_stats = [] let lastLineNo = a:last_line - let is_first_line = a:first_line + let is_first_line = 1 for linenum in range(a:first_line, lastLineNo) if (a:progress_bucket_size && linenum % a:progress_bucket_size == 0) let s:align_progress_bar_position = s:align_progress_bar_position + 1 @@ -821,7 +823,7 @@ func! rainbow_csv#align_field(field, is_first_line, max_field_components_lens, i endfunc -func! rainbow_csv#csv_align() range +func! rainbow_csv#csv_align(range_info) range let first_line = a:firstline let last_line = a:lastline " The first (statistic) pass of the function takes about 40% of runtime, the second (actual align) pass around 60% of runtime. @@ -829,8 +831,8 @@ func! rainbow_csv#csv_align() range " If there are lot of numeric columns this can additionally increase runtime by another 50% or more. let show_progress_bar = wordcount()['bytes'] > 200000 let [delim, policy, comment_prefix] = rainbow_csv#get_current_dialect() - if (&ft == 'markdown' || &ft == 'rmd') - if (first_line == 1 && last_line == line("$")) + if ((policy == 'monocolumn') && (has_key(s:ft_exceptions_map, &ft))) + if (a:range_info == 0) echoerr "RainbowAlign requires an address range in markdown and rmd files" return endif @@ -865,7 +867,7 @@ func! rainbow_csv#csv_align() range return endif let has_edit = 0 - let is_first_line = first_line + let is_first_line = 1 for linenum in range(first_line, lastLineNo) if (progress_bucket_size && linenum % progress_bucket_size == 0) let s:align_progress_bar_position = s:align_progress_bar_position + 1 @@ -901,13 +903,13 @@ func! rainbow_csv#csv_align() range endfunc -func! rainbow_csv#csv_shrink() range +func! rainbow_csv#csv_shrink(range_info) range let first_line = a:firstline let last_line = a:lastline let show_progress_bar = wordcount()['bytes'] > 200000 let [delim, policy, comment_prefix] = rainbow_csv#get_current_dialect() - if (&ft == 'markdown' || &ft == 'rmd') - if (first_line == 1 && last_line == line("$")) + if ((policy == 'monocolumn') && (has_key(s:ft_exceptions_map, &ft))) + if (a:range_info == 0) echoerr "RainbowShrink requires an address range in markdown and rmd files" return endif diff --git a/plugin/rainbow_csv.vim b/plugin/rainbow_csv.vim index 3e2e233..1c7bcbb 100644 --- a/plugin/rainbow_csv.vim +++ b/plugin/rainbow_csv.vim @@ -30,8 +30,8 @@ command! NoRainbowComment call rainbow_csv#manual_disable_comment_prefix() command! RainbowLint call rainbow_csv#csv_lint() command! CSVLint call rainbow_csv#csv_lint() -command! -range=% RainbowAlign ,call rainbow_csv#csv_align() -command! -range=% RainbowShrink ,call rainbow_csv#csv_shrink() +command! -range=% RainbowAlign ,call rainbow_csv#csv_align() +command! -range=% RainbowShrink ,call rainbow_csv#csv_shrink() command! RainbowQuery call rainbow_csv#start_or_finish_query_editing() command! -nargs=+ Select call rainbow_csv#run_select_cmd_query()