accept md and rmd files; refactor align, shrink, and calc_column_stats to work on address/visual range#52
Conversation
…csv_align to use either address or visual range
…tance as well as requirements for ranges when using align and shrink functions in those filetypes
|
addresses #51 |
|
Awesome! Thank you! Looking at it |
mechatroner
left a comment
There was a problem hiding this comment.
Thank you for sending this PR!
There are some adjustments that have to be made before I can merge it.
There was a problem hiding this comment.
Please revert all whitespace-related changes, they are meaningful in markdown - removing them breaks the doc formatting when it is rendered.
There was a problem hiding this comment.
everything else good to go. hunting down where, in my vim settings, I have trailing whitespaces getting trimmed automatically.
As I've never seen it before, what's the effect of trailing whitespaces on markdown documents ? or, what kind of rendering is being done that relies on the trailing whitespaces ?
As soon as I've addressed that, I will resubmit.
There was a problem hiding this comment.
Two whitespaces at the end create a linebreak in GFM (Github-Flavored Markdown). There are multiple competing markdown dialects actually: https://en.wikipedia.org/wiki/Markdown but since this repo is hosted on github we should obviously follow the github spec.
README.md
Outdated
| |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 | |
There was a problem hiding this comment.
I don't think we should add markdown filetypes to this list since they are being handled in a special way only for the purpose of the Align/Shrink procedures. Instead, you can write more about this special case in the docs for Align and Shrink commands. This is because the main feature of the extension is highlighting and users could wrongly assume that Rainbow CSV somehow adjusts markdown highlighting too.
README.md
Outdated
| 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. |
There was a problem hiding this comment.
I would extend this comment by adding that in markdown files the RainbowAlign command uses pipe separator by default and only works on ranges and/or visual range selection or something like this.
autoload/rainbow_csv.vim
Outdated
| 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', '']} |
There was a problem hiding this comment.
Please don't change this list since it can be used by other logic.
autoload/rainbow_csv.vim
Outdated
| " 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 = (l:lastline - l:firstline) > 200000 |
There was a problem hiding this comment.
Please revert, you replaced byte count with line count
| let show_progress_bar = (l:lastline - l:firstline) > 200000 | ||
| let [delim, policy, comment_prefix] = rainbow_csv#get_current_dialect() | ||
| if policy == 'monocolumn' | ||
| echoerr "RainbowAlign is available only for highlighted CSV files" |
There was a problem hiding this comment.
Instead of immediately returning you can adjust delim and policy to '|' and simple for markdown files - this would be the special case at Align level only. same for the shrink. You can also check here that range is provided instead of checking it below.
autoload/rainbow_csv.vim
Outdated
| endif | ||
| let lastLineNo = line("$") | ||
|
|
||
| if (l:firstline == 1 && l:lastline == line("$") && (&ft == 'markdown' || &ft == 'rmd')) |
There was a problem hiding this comment.
Please move this check higher, see my other comment.
autoload/rainbow_csv.vim
Outdated
|
|
||
| func! rainbow_csv#csv_align() | ||
| func! rainbow_csv#csv_align() range | ||
| let l:firstline = a:firstline |
There was a problem hiding this comment.
Please don't use l: prefix for consistency with other code.
autoload/rainbow_csv.vim
Outdated
| endfor | ||
| if !has_edit | ||
| echoerr "File is already aligned" | ||
| echoerr "Range is already aligned" |
There was a problem hiding this comment.
Please keep the original comment since it is a more common use case, and seeing the range might be confusing.
autoload/rainbow_csv.vim
Outdated
|
|
||
|
|
||
| func! rainbow_csv#csv_shrink() | ||
| func! rainbow_csv#csv_shrink() range |
There was a problem hiding this comment.
All my comments regarding the align function also apply to Shrink function.
There was a problem hiding this comment.
done.
moved the show_progress_bar setting to location similar to that found in _align, so that the exception processing block for md/rmd files could remain in a single location, as in _align.
…ce to README.md
…ce to autoload/.vim and doc/.txt
|
I believe that will handle the current set of comments. Let me know if there's more we can do. |
autoload/rainbow_csv.vim
Outdated
| let is_first_line = 1 | ||
| for linenum in range(1, lastLineNo) | ||
| let lastLineNo = a:last_line | ||
| let is_first_line = a:first_line |
There was a problem hiding this comment.
This is a boolean var that affects how numeric-only columns with a non-numeric header are aligned.
There was a problem hiding this comment.
sorry about that . complete miss on my part while converting to ranges
autoload/rainbow_csv.vim
Outdated
| " 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') |
There was a problem hiding this comment.
This condition should only override "monocolumn" policy, otherwise get_current_dialect() policy should have priority
There was a problem hiding this comment.
Actually, this might not be a big deal, I can fix it myself, later, so you can leave it as is if you want. "monocolumn" is just a fall back which means that no better dialect was found.
There was a problem hiding this comment.
after reading through the rest of the file, wondering if &ft is correct choice over &syntax. Input?
autoload/rainbow_csv.vim
Outdated
| let has_edit = 0 | ||
| let is_first_line = 1 | ||
| for linenum in range(1, lastLineNo) | ||
| let is_first_line = first_line |
There was a problem hiding this comment.
Please fix, this is a boolean variable.
autoload/rainbow_csv.vim
Outdated
| 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("$")) |
There was a problem hiding this comment.
I think this check might be unreliable, please use this idiom instead: https://vi.stackexchange.com/questions/24793/how-to-check-whether-a-command-is-run-with-range-or-not
There was a problem hiding this comment.
Actually, it is fine to keep your implementation if you prefer, since this is just a warning which won't be able to produce a serious bug.
There was a problem hiding this comment.
Two whitespaces at the end create a linebreak in GFM (Github-Flavored Markdown). There are multiple competing markdown dialects actually: https://en.wikipedia.org/wiki/Markdown but since this repo is hosted on github we should obviously follow the github spec.
+ is_last_line returned to treatment as bool
+ range-provided checks converted to use <range>
+ 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
|
Thank you for your adjustments! I will take it from here, would still need to test it and maybe make some additional changes. I currently have some other competing priorities but when I have some free time on my hands I will finalize the integration. Stay tuned! |
-range=%