Add csv plugin, closes #239

This commit is contained in:
Adam Stankiewicz 2019-03-10 21:16:44 +01:00
parent 571f76e6b6
commit 5005f1e27a
7 changed files with 3429 additions and 1 deletions

View File

@ -8,7 +8,7 @@ A collection of language packs for Vim.
> One to rule them all, one to find them, one to bring them all and in the darkness bind them.
- It **won't affect your startup time**, as scripts are loaded only on demand\*.
- It **installs and updates 100+ times faster** than the <!--Package Count-->129<!--/Package Count--> packages it consists of.
- It **installs and updates 100+ times faster** than the <!--Package Count-->130<!--/Package Count--> packages it consists of.
- Solid syntax and indentation support (other features skipped). Only the best language packs.
- All unnecessary files are ignored (like enormous documentation from php support).
- No support for esoteric languages, only most popular ones (modern too, like `slim`).
@ -61,6 +61,7 @@ If you need full functionality of any plugin, please use it directly with your p
- [cql](https://github.com/elubow/cql-vim) (syntax)
- [cryptol](https://github.com/victoredwardocallaghan/cryptol.vim) (syntax, compiler, ftplugin)
- [crystal](https://github.com/rhysd/vim-crystal) (syntax, indent, autoload, ftplugin)
- [csv](https://github.com/chrisbra/csv.vim) (syntax, autoload, ftplugin)
- [cucumber](https://github.com/tpope/vim-cucumber) (syntax, indent, compiler, ftplugin)
- [cue](https://github.com/mgrabovsky/vim-cuesheet) (syntax)
- [dart](https://github.com/dart-lang/dart-vim-plugin) (syntax, indent, autoload, ftplugin)

3191
autoload/csv.vim Normal file

File diff suppressed because it is too large Load Diff

1
build
View File

@ -171,6 +171,7 @@ PACKS="
cql:elubow/cql-vim
cryptol:victoredwardocallaghan/cryptol.vim
crystal:rhysd/vim-crystal
csv:chrisbra/csv.vim
cucumber:tpope/vim-cucumber
cue:mgrabovsky/vim-cuesheet
dart:dart-lang/dart-vim-plugin

View File

@ -3,6 +3,14 @@ if !exists('g:jsx_ext_required')
let g:jsx_ext_required = 0
endif
" Make csv loading faster
if !exists('g:csv_start')
let g:csv_start = 1
endif
if !exists('g:csv_end')
let g:csv_end = 2
endif
" Disable json concealing by default
if !exists('g:vim_json_syntax_conceal')
let g:vim_json_syntax_conceal = 0

View File

@ -3,6 +3,14 @@ if !exists('g:jsx_ext_required')
let g:jsx_ext_required = 0
endif
" Make csv loading faster
if !exists('g:csv_start')
let g:csv_start = 1
endif
if !exists('g:csv_end')
let g:csv_end = 2
endif
" Disable json concealing by default
if !exists('g:vim_json_syntax_conceal')
let g:vim_json_syntax_conceal = 0
@ -241,6 +249,14 @@ autocmd BufNewFile,BufReadPost *.ecr setlocal filetype=eruby
augroup end
endif
if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'csv') == -1
augroup filetypedetect
" csv, from csv.vim in chrisbra/csv.vim
" Install Filetype detection for CSV files
au BufRead,BufNewFile *.csv,*.dat,*.tsv,*.tab set filetype=csv
augroup end
endif
if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'cucumber') == -1
augroup filetypedetect
" cucumber, from cucumber.vim in tpope/vim-cucumber

38
ftplugin/csv.vim Normal file
View File

@ -0,0 +1,38 @@
if exists('g:polyglot_disabled') && index(g:polyglot_disabled, 'csv') != -1
finish
endif
" Filetype plugin for editing CSV files. "{{{1
" Author: Christian Brabandt <cb@256bit.org>
" Version: 0.31
" Script: http://www.vim.org/scripts/script.php?script_id=2830
" License: VIM License
" Last Change: Thu, 15 Jan 2015 21:05:10 +0100
" Documentation: see :help ft-csv.txt
" GetLatestVimScripts: 2830 30 :AutoInstall: csv.vim
"
" Some ideas are taken from the wiki http://vim.wikia.com/wiki/VimTip667
" though, implementation differs.
" Plugin folklore "{{{1
if v:version < 700 || exists('b:did_ftplugin')
finish
endif
let b:did_ftplugin = 1
let s:cpo_save = &cpo
set cpo&vim
" Initialize Plugin "{{{2
" useful for configuring how many lines to analyze,
" set if you notice a slowdown
let b:csv_start = get(g:, 'csv_start', 1)
let b:csv_end = get(g:, 'csv_end', line('$'))
let b:csv_result = ''
call csv#Init(b:csv_start, b:csv_end)
let &cpo = s:cpo_save
unlet s:cpo_save
" Vim Modeline " {{{2
" vim: set foldmethod=marker et sw=0 sts=-1 ts=4:

173
syntax/csv.vim Normal file
View File

@ -0,0 +1,173 @@
if exists('g:polyglot_disabled') && index(g:polyglot_disabled, 'csv') != -1
finish
endif
" A simple syntax highlighting, simply alternate colors between two
" adjacent columns
" Init {{{2
let s:cpo_save = &cpo
set cpo&vim
scriptencoding utf8
if version < 600
syn clear
elseif exists("b:current_syntax")
finish
endif
" Helper functions "{{{2
fu! <sid>Warning(msg) "{{{3
" Don't redraw, so we are not overwriting messages from the ftplugin
" script
echohl WarningMsg
echomsg "CSV Syntax:" . a:msg
echohl Normal
endfu
fu! <sid>Esc(val, char) "{{{3
if empty(a:val)
return a:val
endif
return '\V'.escape(a:val, '\\'.a:char).'\m'
endfu
fu! <sid>CheckSaneSearchPattern() "{{{3
let s:del_def = ','
let s:col_def = '\%([^' . s:del_def . ']*' . s:del_def . '\|$\)'
let s:col_def_end = '\%([^' . s:del_def . ']*' . s:del_def . '\)'
" First:
" Check for filetype plugin. This syntax script relies on the filetype
" plugin, else, it won't work properly.
redir => s:a |sil filetype | redir end
let s:a=split(s:a, "\n")[0]
if match(s:a, '\cplugin:off') > 0
call <sid>Warning("No filetype support, only simple highlighting using"
\ . s:del_def . " as delimiter! See :h csv-installation")
endif
" Check Comment setting
if !exists("g:csv_comment")
let b:csv_cmt = split(&cms, '%s')
elseif match(g:csv_comment, '%s') >= 0
let b:csv_cmt = split(g:csv_comment, '%s')
else
let b:csv_cmt = [g:csv_comment]
endif
" Second: Check for sane defaults for the column pattern
" Not necessary to check for fixed width columns
if exists("b:csv_fixed_width_cols")
return
endif
" Try a simple highlighting, if the defaults from the ftplugin
" don't exist
let s:col = get(b:, 'col', s:col_def)
let s:col_end = get(b:, 'col_end', s:col_def_end)
let s:del = get(b:, 'delimiter', s:del_def)
let s:cmts = b:csv_cmt[0]
let s:cmte = len(b:csv_cmt) == 2 ? b:csv_cmt[1] : ''
" Make the file start at the first actual CSV record (issue #71)
if !exists("b:csv_headerline")
let cmts = <sid>Esc(s:cmts, '')
let pattern = '\%^\(\%('.cmts.'.*\n\)\|\%(\s*\n\)\)\+'
let start = search(pattern, 'nWe', 10)
" don't do it, on an empty file
if start > 0 && !empty(getline(start))
let b:csv_headerline = start+1
endif
endif
" escape '/' for syn match command
let s:cmts=<sid>Esc(s:cmts, '/')
let s:cmte=<sid>Esc(s:cmte, '/')
if line('$') > 1 && (!exists("b:col") || empty(b:col))
" check for invalid pattern, ftplugin hasn't been loaded yet
call <sid>Warning("Invalid column pattern, using default pattern " . s:col_def)
endif
endfu
" Syntax rules {{{2
fu! <sid>DoHighlight() "{{{3
if has("conceal") && !exists("g:csv_no_conceal") &&
\ !exists("b:csv_fixed_width_cols")
exe "syn match CSVDelimiter /" . s:col_end .
\ '/ms=e,me=e contained conceal cchar=' .
\ (&enc == "utf-8" ? "│" : '|')
hi def link CSVDelimiter Conceal
elseif !exists("b:csv_fixed_width_cols")
" The \%(.\)\@<= makes sure, the last char won't be concealed,
" if it isn't a delimiter
exe "syn match CSVDelimiter /" . s:col_end . '/ms=e,me=e contained'
if has("conceal")
hi def link CSVDelimiter Conceal
else
hi def link CSVDelimiter Ignore
endif
endif " There is no delimiter for csv fixed width columns
if !exists("b:csv_fixed_width_cols")
exe 'syn match CSVColumnEven nextgroup=CSVColumnOdd /'
\ . s:col . '/ contains=CSVDelimiter'
exe 'syn match CSVColumnOdd nextgroup=CSVColumnEven /'
\ . s:col . '/ contains=CSVDelimiter'
exe 'syn match CSVColumnHeaderEven nextgroup=CSVColumnHeaderOdd /\%<'. (get(b:, 'csv_headerline', 1)+1).'l'
\. s:col . '/ contains=CSVDelimiter'
exe 'syn match CSVColumnHeaderOdd nextgroup=CSVColumnHeaderEven /\%<'. (get(b:, 'csv_headerline', 1)+1).'l'
\. s:col . '/ contains=CSVDelimiter'
else
for i in range(len(b:csv_fixed_width_cols))
let pat = '/\%' . b:csv_fixed_width_cols[i] . 'v.*' .
\ ((i == len(b:csv_fixed_width_cols)-1) ? '/' :
\ '\%' . b:csv_fixed_width_cols[i+1] . 'v/')
let group = "CSVColumn" . (i%2 ? "Odd" : "Even" )
let ngroup = "CSVColumn" . (i%2 ? "Even" : "Odd" )
exe "syn match " group pat " nextgroup=" . ngroup
endfor
endif
" Comment regions
exe 'syn match CSVComment /'. s:cmts. '.*'.
\ (!empty(s:cmte) ? '\%('. s:cmte. '\)\?'
\: ''). '/'
hi def link CSVComment Comment
endfun
fu! <sid>HiLink(name, target) "{{{3
if !hlexists(a:name)
exe "hi def link" a:name a:target
endif
endfu
fu! <sid>DoSyntaxDefinitions() "{{{3
syn spell toplevel
" Not really needed
syn case ignore
call <sid>HiLink("CSVColumnHeaderOdd", "WarningMsg")
call <sid>HiLink("CSVColumnHeaderEven", "WarningMsg")
if get(g:, 'csv_no_column_highlight', 0)
call <sid>HiLink("CSVColumnOdd", "Normal")
call <sid>HiLink("CSVColumnEven", "Normal")
else
call <sid>HiLink("CSVColumnOdd", "String")
call <sid>HiLink("CSVColumnEven","Statement")
endif
endfun
" Main: {{{2
" Make sure, we are using a sane, valid pattern for syntax
" highlighting
call <sid>CheckSaneSearchPattern()
" Define all necessary syntax groups
call <sid>DoSyntaxDefinitions()
" Highlight the file
call <sid>DoHighlight()
" Set the syntax variable {{{2
let b:current_syntax="csv"
let &cpo = s:cpo_save
unlet s:cpo_save
" vim: set foldmethod=marker et sw=0 sts=-1 ts=4: