Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Author

Choose a reason for hiding this comment

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

done

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

Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +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).
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 ...

Expand Down
48 changes: 37 additions & 11 deletions autoload/rainbow_csv.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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]\+\)\?$'
Expand Down Expand Up @@ -762,12 +764,12 @@ func! rainbow_csv#adjust_column_stats(column_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 lastLineNo = a:last_line
let is_first_line = 1
for linenum in range(1, lastLineNo)
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)
Expand Down Expand Up @@ -821,12 +823,25 @@ func! rainbow_csv#align_field(field, is_first_line, max_field_components_lens, i
endfunc


func! rainbow_csv#csv_align()
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.
" 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 ((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
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"
Copy link
Copy Markdown
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
Copy Markdown
Author

Choose a reason for hiding this comment

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

done

return
Expand All @@ -835,13 +850,13 @@ func! rainbow_csv#csv_align()
echoerr 'RainbowAlign not available for "rfc_csv" filetypes, consider using "csv" instead'
return
endif
let lastLineNo = line("$")
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)
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
Expand All @@ -853,7 +868,7 @@ func! rainbow_csv#csv_align()
endif
let has_edit = 0
let is_first_line = 1
for linenum in range(1, 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)
Expand Down Expand Up @@ -888,8 +903,20 @@ func! rainbow_csv#csv_align()
endfunc


func! rainbow_csv#csv_shrink()
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 ((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
let policy = 'simple'
let delim = '|'
let comment_prefix = ''
endif
if policy == 'monocolumn'
echoerr "RainbowShrink is available only for highlighted CSV files"
return
Expand All @@ -898,15 +925,14 @@ func! rainbow_csv#csv_shrink()
echoerr 'RainbowShrink not available for "rfc_csv" filetypes, consider using "csv" instead'
return
endif
let lastLineNo = line("$")
let lastLineNo = last_line
let has_edit = 0
let show_progress_bar = wordcount()['bytes'] > 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(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)
Expand Down
2 changes: 2 additions & 0 deletions doc/rainbow_csv.txt
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,13 @@ 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>).
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* {...}
Expand Down
4 changes: 2 additions & 2 deletions plugin/rainbow_csv.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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! RainbowAlign call rainbow_csv#csv_align()
command! RainbowShrink call rainbow_csv#csv_shrink()
command! -range=% RainbowAlign <line1>,<line2>call rainbow_csv#csv_align(<range>)
command! -range=% RainbowShrink <line1>,<line2>call rainbow_csv#csv_shrink(<range>)

command! RainbowQuery call rainbow_csv#start_or_finish_query_editing()
command! -nargs=+ Select call rainbow_csv#run_select_cmd_query(<q-args>)
Expand Down