Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

accept md and rmd files; refactor align, shrink, and calc_column_stats to work on address/visual range #52

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

carywreams
Copy link

  • plugin/rainbow_csv.vim converts RainbowAlign and RainbowShrink to use -range=%
  • autoload/rainbow_csv.vim#csv_align
    • accepts either address or visual range
    • changes operational loops to have specific start/stop lines
    • emits error if an entire markdown or rmd file is submitted
  • autoload/rainbow_csv.vim#csv_shrink
    • accepts either address or visual range
    • changes operational loops to have specific start/stop lines
    • emits error if an entire markdown or rmd file is submitted
  • autoload/rainbow_csv.vim#calc_column_stats
    • accepts two new parameters, firstline and lastline
    • changes operational loops to have specific start/stop lines
  • autoload/rainbow_csv.vim#s:named_syntax_map adds markdown and rmd filetype entries
  • proposed README.md changes
  • proposed doc/rainbow_csv.txt changes

…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
@carywreams
Copy link
Author

addresses #51

@mechatroner
Copy link
Owner

Awesome! Thank you! Looking at it

Copy link
Owner

@mechatroner mechatroner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for sending this PR!
There are some adjustments that have to be made before I can merge it.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert all whitespace-related changes, they are meaningful in markdown - removing them breaks the doc formatting when it is rendered.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 |
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

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.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

@@ -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', '']}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't change this list since it can be used by other logic.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

" 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 = (l:lastline - l:firstline) > 200000
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert, you replaced byte count with line count

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

" 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 = (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"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -835,25 +834,35 @@ func! rainbow_csv#csv_align()
echoerr 'RainbowAlign 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'))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move this check higher, see my other comment.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


func! rainbow_csv#csv_align()
func! rainbow_csv#csv_align() range
let l:firstline = a:firstline
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't use l: prefix for consistency with other code.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

@@ -883,12 +892,13 @@ func! rainbow_csv#csv_align()
let is_first_line = 0
endfor
if !has_edit
echoerr "File is already aligned"
echoerr "Range is already aligned"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please keep the original comment since it is a more common use case, and seeing the range might be confusing.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

endif
endfunc


func! rainbow_csv#csv_shrink()
func! rainbow_csv#csv_shrink() range
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All my comments regarding the align function also apply to Shrink function.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@carywreams
Copy link
Author

I believe that will handle the current set of comments. Let me know if there's more we can do.

let is_first_line = 1
for linenum in range(1, lastLineNo)
let lastLineNo = a:last_line
let is_first_line = a:first_line
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a boolean var that affects how numeric-only columns with a non-numeric header are aligned.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry about that . complete miss on my part while converting to ranges

" 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 [delim, policy, comment_prefix] = rainbow_csv#get_current_dialect()
if (&ft == 'markdown' || &ft == 'rmd')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This condition should only override "monocolumn" policy, otherwise get_current_dialect() policy should have priority

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

after reading through the rest of the file, wondering if &ft is correct choice over &syntax. Input?

@@ -852,8 +865,8 @@ func! rainbow_csv#csv_align()
return
endif
let has_edit = 0
let is_first_line = 1
for linenum in range(1, lastLineNo)
let is_first_line = first_line
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix, this is a boolean variable.

" 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 [delim, policy, comment_prefix] = rainbow_csv#get_current_dialect()
if (&ft == 'markdown' || &ft == 'rmd')
if (first_line == 1 && last_line == line("$"))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
@mechatroner
Copy link
Owner

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants