Compare commits

..

14 Commits

Author SHA1 Message Date
Dhruva Sagar
0c9cd8fb50 Releasing v4.3.0 2014-04-25 15:02:27 +05:30
Dhruva Sagar
da39088fd8 Merge branch 'feature/gfm_column_alignment' 2014-04-25 14:55:22 +05:30
Dhruva Sagar
a0fcb89f43 Fixed #19
Updated tablemode#table#IsRow() to allow hiphens within the row.
2014-04-25 14:54:43 +05:30
Dhruva Sagar
5007e5f728 Added support for column alignment #23
Now we can define gfm-style aligment by adding them to the header column
2014-04-25 14:42:02 +05:30
Dhruva Sagar
71051e2c22 Merge branch 'master' into feature/gfm_column_alignment
Conflicts:
	autoload/tablemode/table.vim
2014-04-25 14:31:33 +05:30
Dhruva Sagar
43d0c0ff6f Cosmetic refactoring
* Changed the API calls to be more sensible & simple and free of
  redundancy.
2014-04-25 11:09:14 +05:30
Dhruva Sagar
00c54e8e02 Refactoring alignment 2014-04-24 15:48:57 +05:30
Dhruva Sagar
cfbac6979d Fixed #25
Minor refactoring & corrected variable names
2014-04-23 11:27:25 +05:30
Dhruva Sagar
7e00cea7f5 Improved table.vim tests. Fixed #24. 2014-04-22 00:11:40 +05:30
Dhruva Sagar
79166b3d4e Fixing #24
Exposed a public api for IsTableModeActive and using it instead.
2014-04-21 14:58:02 +05:30
Dhruva Sagar
6862a8b7e4 Update CHANGELOG.md 2014-04-09 14:40:31 +05:30
Dhruva Sagar
d889c342d3 Fixed the tests with new changes 2014-04-09 11:45:02 +05:30
Dhruva Sagar
8a54ef28ab Removed CellTextObject() from spreadsheet.vim 2014-04-09 11:34:54 +05:30
Dhruva Sagar
a2492fbf08 Removed old TableMotion from autoload/tablemode/table.vim 2014-04-09 11:32:03 +05:30
16 changed files with 367 additions and 344 deletions

View File

@@ -1,4 +1,9 @@
# Change Log # Change Log
## Version 4.3.0
* Refactored some more
* Fixed issue #19, hiphens in the table broke alignment
* Added feature #26, you can now define column alignments in the table header
## Version 4.2.0 ## Version 4.2.0
* Refactored cells logic out to autoload/tablemode/spreadsheet/cell.vim * Refactored cells logic out to autoload/tablemode/spreadsheet/cell.vim
* Refactored formula logic out to autoload/tablemode/spreadsheet/formula.vim * Refactored formula logic out to autoload/tablemode/spreadsheet/formula.vim
@@ -11,7 +16,7 @@
* Major refactoring of the codebase. * Major refactoring of the codebase.
* Improved modular tests. * Improved modular tests.
* Fixed long standing unicode character alignment issue. * Fixed long standing unicode character alignment issue.
* Moved to providing <Plug> mappings rather than configuration based mappings * Moved to providing \<Plug\> mappings rather than configuration based mappings
which can be more easily overriden by end user. which can be more easily overriden by end user.
## Version 3.3.2 ## Version 3.3.2

View File

@@ -1,4 +1,4 @@
# VIM Table Mode v4.2.0 [![Build Status](https://travis-ci.org/dhruvasagar/vim-table-mode.png?branch=master)](https://travis-ci.org/dhruvasagar/vim-table-mode) # VIM Table Mode v4.3.0 [![Build Status](https://travis-ci.org/dhruvasagar/vim-table-mode.png?branch=master)](https://travis-ci.org/dhruvasagar/vim-table-mode)
An awesome automatic table creator & formatter allowing one to create neat An awesome automatic table creator & formatter allowing one to create neat
tables as you type. tables as you type.

View File

@@ -32,13 +32,6 @@ function! s:SetBufferOptDefault(opt, val) "{{{2
endif endif
endfunction endfunction
function! s:IsTableModeActive() "{{{2
if g:table_mode_always_active | return 1 | endif
call s:SetBufferOptDefault('table_mode_active', 0)
return b:table_mode_active
endfunction
function! s:ToggleMapping() "{{{2 function! s:ToggleMapping() "{{{2
if exists('b:table_mode_active') && b:table_mode_active if exists('b:table_mode_active') && b:table_mode_active
call s:SetBufferOptDefault('table_mode_separator_map', g:table_mode_separator) call s:SetBufferOptDefault('table_mode_separator_map', g:table_mode_separator)
@@ -97,28 +90,35 @@ function! tablemode#scope() "{{{2
return s: return s:
endfunction endfunction
function! tablemode#IsActive() "{{{2
if g:table_mode_always_active | return 1 | endif
call s:SetBufferOptDefault('table_mode_active', 0)
return b:table_mode_active
endfunction
function! tablemode#TableizeInsertMode() "{{{2 function! tablemode#TableizeInsertMode() "{{{2
if s:IsTableModeActive() && getline('.') =~# (tablemode#table#StartExpr() . g:table_mode_separator . g:table_mode_separator) if tablemode#IsActive() && getline('.') =~# (tablemode#table#StartExpr() . g:table_mode_separator . g:table_mode_separator)
call tablemode#table#AddHeaderBorder('.') call tablemode#table#AddHeaderBorder('.')
normal! A normal! A
elseif s:IsTableModeActive() && getline('.') =~# (tablemode#table#StartExpr() . g:table_mode_separator) elseif tablemode#IsActive() && getline('.') =~# (tablemode#table#StartExpr() . g:table_mode_separator)
let column = tablemode#utils#strlen(substitute(getline('.')[0:col('.')], '[^' . g:table_mode_separator . ']', '', 'g')) let column = tablemode#utils#strlen(substitute(getline('.')[0:col('.')], '[^' . g:table_mode_separator . ']', '', 'g'))
let position = tablemode#utils#strlen(matchstr(getline('.')[0:col('.')], '.*' . g:table_mode_separator . '\s*\zs.*')) let position = tablemode#utils#strlen(matchstr(getline('.')[0:col('.')], '.*' . g:table_mode_separator . '\s*\zs.*'))
call tablemode#table#TableRealign('.') call tablemode#table#Realign('.')
normal! 0 normal! 0
call search(repeat('[^' . g:table_mode_separator . ']*' . g:table_mode_separator, column) . '\s\{-\}' . repeat('.', position), 'ce', line('.')) call search(repeat('[^' . g:table_mode_separator . ']*' . g:table_mode_separator, column) . '\s\{-\}' . repeat('.', position), 'ce', line('.'))
endif endif
endfunction endfunction
function! tablemode#TableModeEnable() "{{{2 function! tablemode#Enable() "{{{2
call s:SetActive(1) call s:SetActive(1)
endfunction endfunction
function! tablemode#TableModeDisable() "{{{2 function! tablemode#Disable() "{{{2
call s:SetActive(0) call s:SetActive(0)
endfunction endfunction
function! tablemode#TableModeToggle() "{{{2 function! tablemode#Toggle() "{{{2
if g:table_mode_always_active if g:table_mode_always_active
return 1 return 1
endif endif
@@ -135,7 +135,7 @@ function! tablemode#TableizeRange(...) range "{{{2
let lnum += 1 let lnum += 1
endwhile endwhile
call tablemode#table#TableRealign(lnum - 1) call tablemode#table#Realign(lnum - 1)
endfunction endfunction
function! tablemode#TableizeByDelimiter() "{{{2 function! tablemode#TableizeByDelimiter() "{{{2

View File

@@ -64,11 +64,21 @@ function! s:Padding(string, length, where) "{{{3
endif endif
endfunction endfunction
" function! s:Split() - Split a string into fields and delimiters {{{2 " Public Functions {{{1
function! tablemode#align#sid() "{{{2
return maparg('<sid>', 'n')
endfunction
nnoremap <sid> <sid>
function! tablemode#align#scope() "{{{2
return s:
endfunction
" function! tablemode#align#Split() - Split a string into fields and delimiters {{{2
" Like split(), but include the delimiters as elements " Like split(), but include the delimiters as elements
" All odd numbered elements are delimiters " All odd numbered elements are delimiters
" All even numbered elements are non-delimiters (including zero) " All even numbered elements are non-delimiters (including zero)
function! s:Split(string, delim) function! tablemode#align#Split(string, delim)
let rv = [] let rv = []
let beg = 0 let beg = 0
@@ -107,59 +117,61 @@ function! s:Split(string, delim)
return rv return rv
endfunction endfunction
" Public Functions {{{1 function! tablemode#align#alignments(lnum, ncols) "{{{2
function! tablemode#align#sid() "{{{2 let alignments = repeat(['l'], a:ncols) " For each column
return maparg('<sid>', 'n') if tablemode#table#IsHeader(a:lnum+1)
endfunction let hcols = tablemode#align#Split(getline(a:lnum+1), '[' . g:table_mode_corner . g:table_mode_corner_corner . ']')
nnoremap <sid> <sid> for idx in range(len(hcols))
" Right align if header
function! tablemode#align#scope() "{{{2 if hcols[idx] =~# g:table_mode_align_char . '$' | let alignments[idx] = 'r' | endif
return s: endfor
end
return alignments
endfunction endfunction
function! tablemode#align#Align(lines) "{{{2 function! tablemode#align#Align(lines) "{{{2
let lines = map(a:lines, 's:Split(v:val, g:table_mode_separator)') let lines = map(a:lines, 'map(v:val, "v:key =~# \"text\" ? tablemode#align#Split(v:val, g:table_mode_separator) : v:val")')
for line in lines for line in lines
if len(line) <= 1 | continue | endif let stext = line.text
if len(stext) <= 1 | continue | endif
if line[0] !~ tablemode#table#StartExpr() if stext[0] !~ tablemode#table#StartExpr()
let line[0] = s:StripTrailingSpaces(line[0]) let stext[0] = s:StripTrailingSpaces(stext[0])
endif endif
if len(line) >= 2 if len(stext) >= 2
for i in range(1, len(line)-1) for i in range(1, len(stext)-1)
let line[i] = tablemode#utils#strip(line[i]) let stext[i] = tablemode#utils#strip(stext[i])
endfor endfor
endif endif
endfor endfor
let maxes = [] let maxes = []
for line in lines for line in lines
if len(line) <= 1 | continue | endif let stext = line.text
for i in range(len(line)) if len(stext) <= 1 | continue | endif
for i in range(len(stext))
if i == len(maxes) if i == len(maxes)
let maxes += [ s:Strlen(line[i]) ] let maxes += [ s:Strlen(stext[i]) ]
else else
let maxes[i] = max([ maxes[i], s:Strlen(line[i]) ]) let maxes[i] = max([ maxes[i], s:Strlen(stext[i]) ])
endif endif
endfor endfor
endfor endfor
let alignments = tablemode#align#alignments(lines[0].lnum, len(lines[0].text))
for idx in range(len(lines)) for idx in range(len(lines))
let line = lines[idx] let tlnum = lines[idx].lnum
let tline = lines[idx].text
if len(line) <= 1 | continue | endif if len(tline) <= 1 | continue | endif
for i in range(len(line)) for i in range(len(tline))
if line[i] !~# '[^0-9\.]' let field = s:Padding(tline[i], maxes[i], alignments[i])
let field = s:Padding(line[i], maxes[i], 'r') let tline[i] = field . (i == 0 || i == len(tline) ? '' : ' ')
else
let field = s:Padding(line[i], maxes[i], 'l')
endif
let line[i] = field . (i == 0 || i == len(line) ? '' : ' ')
endfor endfor
let lines[idx] = s:StripTrailingSpaces(join(line, '')) let lines[idx].text = s:StripTrailingSpaces(join(tline, ''))
endfor endfor
return lines return lines

View File

@@ -47,50 +47,50 @@ function! tablemode#spreadsheet#scope() "{{{2
endfunction endfunction
function! tablemode#spreadsheet#GetFirstRow(line) "{{{2 function! tablemode#spreadsheet#GetFirstRow(line) "{{{2
if tablemode#table#IsATableRow(a:line) if tablemode#table#IsRow(a:line)
let line = tablemode#utils#line(a:line) let line = tablemode#utils#line(a:line)
while tablemode#table#IsATableRow(line - 1) || tablemode#table#IsATableHeader(line - 1) while tablemode#table#IsRow(line - 1) || tablemode#table#IsHeader(line - 1)
let line -= 1 let line -= 1
endwhile endwhile
if tablemode#table#IsATableHeader(line) | let line += 1 | endif if tablemode#table#IsHeader(line) | let line += 1 | endif
return line return line
endif endif
endfunction endfunction
function! tablemode#spreadsheet#MoveToFirstRow() "{{{2 function! tablemode#spreadsheet#MoveToFirstRow() "{{{2
if tablemode#table#IsATableRow('.') if tablemode#table#IsRow('.')
call cursor(tablemode#spreadsheet#GetFirstRow('.'), col('.')) call cursor(tablemode#spreadsheet#GetFirstRow('.'), col('.'))
endif endif
endfunction endfunction
function! tablemode#spreadsheet#GetLastRow(line) "{{{2 function! tablemode#spreadsheet#GetLastRow(line) "{{{2
if tablemode#table#IsATableRow(a:line) if tablemode#table#IsRow(a:line)
let line = tablemode#utils#line(a:line) let line = tablemode#utils#line(a:line)
while tablemode#table#IsATableRow(line + 1) || tablemode#table#IsATableHeader(line + 1) while tablemode#table#IsRow(line + 1) || tablemode#table#IsHeader(line + 1)
let line += 1 let line += 1
endwhile endwhile
if tablemode#table#IsATableHeader(line) | let line -= 1 | endif if tablemode#table#IsHeader(line) | let line -= 1 | endif
return line return line
endif endif
endfunction endfunction
function! tablemode#spreadsheet#MoveToLastRow() "{{{2 function! tablemode#spreadsheet#MoveToLastRow() "{{{2
if tablemode#table#IsATableRow('.') if tablemode#table#IsRow('.')
call cursor(tablemode#spreadsheet#GetLastRow('.'), col('.')) call cursor(tablemode#spreadsheet#GetLastRow('.'), col('.'))
endif endif
endfunction endfunction
function! tablemode#spreadsheet#LineNr(row) "{{{2 function! tablemode#spreadsheet#LineNr(row) "{{{2
if tablemode#table#IsATableRow('.') if tablemode#table#IsRow('.')
let line = tablemode#spreadsheet#GetFirstRow('.') let line = tablemode#spreadsheet#GetFirstRow('.')
let row_nr = 0 let row_nr = 0
while tablemode#table#IsATableRow(line + 1) || tablemode#table#IsATableHeader(line + 1) while tablemode#table#IsRow(line + 1) || tablemode#table#IsHeader(line + 1)
if tablemode#table#IsATableRow(line) if tablemode#table#IsRow(line)
let row_nr += 1 let row_nr += 1
if row ==# row_nr | break | endif if row ==# row_nr | break | endif
endif endif
@@ -105,8 +105,8 @@ function! tablemode#spreadsheet#RowNr(line) "{{{2
let line = tablemode#utils#line(a:line) let line = tablemode#utils#line(a:line)
let rowNr = 0 let rowNr = 0
while tablemode#table#IsATableRow(line) || tablemode#table#IsATableHeader(line) while tablemode#table#IsRow(line) || tablemode#table#IsHeader(line)
if tablemode#table#IsATableRow(line) | let rowNr += 1 | endif if tablemode#table#IsRow(line) | let rowNr += 1 | endif
let line -= 1 let line -= 1
endwhile endwhile
@@ -117,14 +117,14 @@ function! tablemode#spreadsheet#RowCount(line) "{{{2
let line = tablemode#utils#line(a:line) let line = tablemode#utils#line(a:line)
let [tline, totalRowCount] = [line, 0] let [tline, totalRowCount] = [line, 0]
while tablemode#table#IsATableRow(tline) || tablemode#table#IsATableHeader(tline) while tablemode#table#IsRow(tline) || tablemode#table#IsHeader(tline)
if tablemode#table#IsATableRow(tline) | let totalRowCount += 1 | endif if tablemode#table#IsRow(tline) | let totalRowCount += 1 | endif
let tline -= 1 let tline -= 1
endwhile endwhile
let tline = line + 1 let tline = line + 1
while tablemode#table#IsATableRow(tline) || tablemode#table#IsATableHeader(tline) while tablemode#table#IsRow(tline) || tablemode#table#IsHeader(tline)
if tablemode#table#IsATableRow(tline) | let totalRowCount += 1 | endif if tablemode#table#IsRow(tline) | let totalRowCount += 1 | endif
let tline += 1 let tline += 1
endwhile endwhile
@@ -167,33 +167,33 @@ function! tablemode#spreadsheet#MoveToStartOfCell() "{{{2
endfunction endfunction
function! tablemode#spreadsheet#GetLastRow(line) "{{{2 function! tablemode#spreadsheet#GetLastRow(line) "{{{2
if tablemode#table#IsATableRow(a:line) if tablemode#table#IsRow(a:line)
let line = tablemode#utils#line(a:line) let line = tablemode#utils#line(a:line)
while tablemode#table#IsATableRow(line + 1) || tablemode#table#IsATableHeader(line + 1) while tablemode#table#IsRow(line + 1) || tablemode#table#IsHeader(line + 1)
let line += 1 let line += 1
endwhile endwhile
if tablemode#table#IsATableHeader(line) | let line -= 1 | endif if tablemode#table#IsHeader(line) | let line -= 1 | endif
return line return line
endif endif
endfunction endfunction
function! tablemode#spreadsheet#GetFirstRow(line) "{{{2 function! tablemode#spreadsheet#GetFirstRow(line) "{{{2
if tablemode#table#IsATableRow(a:line) if tablemode#table#IsRow(a:line)
let line = tablemode#utils#line(a:line) let line = tablemode#utils#line(a:line)
while tablemode#table#IsATableRow(line - 1) || tablemode#table#IsATableHeader(line - 1) while tablemode#table#IsRow(line - 1) || tablemode#table#IsHeader(line - 1)
let line -= 1 let line -= 1
endwhile endwhile
if tablemode#table#IsATableHeader(line) | let line += 1 | endif if tablemode#table#IsHeader(line) | let line += 1 | endif
return line return line
endif endif
endfunction endfunction
function! tablemode#spreadsheet#DeleteColumn() "{{{2 function! tablemode#spreadsheet#DeleteColumn() "{{{2
if tablemode#table#IsATableRow('.') if tablemode#table#IsRow('.')
for i in range(v:count1) for i in range(v:count1)
call tablemode#spreadsheet#MoveToStartOfCell() call tablemode#spreadsheet#MoveToStartOfCell()
call tablemode#spreadsheet#MoveToFirstRow() call tablemode#spreadsheet#MoveToFirstRow()
@@ -202,23 +202,23 @@ function! tablemode#spreadsheet#DeleteColumn() "{{{2
normal! d normal! d
endfor endfor
call tablemode#table#TableRealign('.') call tablemode#table#Realign('.')
endif endif
endfunction endfunction
function! tablemode#spreadsheet#DeleteRow() "{{{2 function! tablemode#spreadsheet#DeleteRow() "{{{2
if tablemode#table#IsATableRow('.') if tablemode#table#IsRow('.')
for i in range(v:count1) for i in range(v:count1)
if tablemode#table#IsATableRow('.') if tablemode#table#IsRow('.')
normal! dd normal! dd
endif endif
if !tablemode#table#IsATableRow('.') if !tablemode#table#IsRow('.')
normal! k normal! k
endif endif
endfor endfor
call tablemode#table#TableRealign('.') call tablemode#table#Realign('.')
endif endif
endfunction endfunction
@@ -234,14 +234,3 @@ function! tablemode#spreadsheet#Average(range, ...) abort "{{{2
return s:Average(call('tablemode#spreadsheet#cell#GetCellRange', args)) return s:Average(call('tablemode#spreadsheet#cell#GetCellRange', args))
endfunction endfunction
function! tablemode#spreadsheet#CellTextObject(inner) "{{{2
if tablemode#table#IsATableRow('.')
call tablemode#spreadsheet#MoveToStartOfCell()
if a:inner
normal! v
call search('[^' . g:table_mode_separator . ']\ze\s*' . g:table_mode_separator)
else
execute 'normal! vf' . g:table_mode_separator . 'l'
endif
endif
endfunction

View File

@@ -71,7 +71,7 @@ endfunction
function! tablemode#spreadsheet#cell#GetCells(line, ...) abort function! tablemode#spreadsheet#cell#GetCells(line, ...) abort
let line = tablemode#utils#line(a:line) let line = tablemode#utils#line(a:line)
if tablemode#table#IsATableRow(line) if tablemode#table#IsRow(line)
if a:0 < 1 if a:0 < 1
let [row, colm] = [line, 0] let [row, colm] = [line, 0]
elseif a:0 < 2 elseif a:0 < 2
@@ -84,8 +84,8 @@ function! tablemode#spreadsheet#cell#GetCells(line, ...) abort
if row == 0 if row == 0
let values = [] let values = []
let line = first_row let line = first_row
while tablemode#table#IsATableRow(line) || tablemode#table#IsATableHeader(line) while tablemode#table#IsRow(line) || tablemode#table#IsHeader(line)
if tablemode#table#IsATableRow(line) if tablemode#table#IsRow(line)
let row_line = getline(line)[stridx(getline(line), g:table_mode_separator):strridx(getline(line), g:table_mode_separator)] let row_line = getline(line)[stridx(getline(line), g:table_mode_separator):strridx(getline(line), g:table_mode_separator)]
call add(values, tablemode#utils#strip(get(split(row_line, g:table_mode_separator), colm>0?colm-1:colm, ''))) call add(values, tablemode#utils#strip(get(split(row_line, g:table_mode_separator), colm>0?colm-1:colm, '')))
endif endif
@@ -95,8 +95,8 @@ function! tablemode#spreadsheet#cell#GetCells(line, ...) abort
else else
let row_nr = 0 let row_nr = 0
let line = first_row let line = first_row
while tablemode#table#IsATableRow(line) || tablemode#table#IsATableHeader(line) while tablemode#table#IsRow(line) || tablemode#table#IsHeader(line)
if tablemode#table#IsATableRow(line) if tablemode#table#IsRow(line)
let row_nr += 1 let row_nr += 1
if row ==# row_nr | break | endif if row ==# row_nr | break | endif
endif endif
@@ -142,7 +142,7 @@ function! tablemode#spreadsheet#cell#GetCellRange(range, ...) abort "{{{2
let values = [] let values = []
if tablemode#table#IsATableRow(line) if tablemode#table#IsRow(line)
let [row1, col1, row2, col2] = s:ParseRange(a:range, colm) let [row1, col1, row2, col2] = s:ParseRange(a:range, colm)
if row1 == row2 if row1 == row2
@@ -193,7 +193,7 @@ function! tablemode#spreadsheet#cell#SetCell(val, ...) "{{{2
let [line, row, colm] = a:000 let [line, row, colm] = a:000
endif endif
if tablemode#table#IsATableRow(line) if tablemode#table#IsRow(line)
let line = tablemode#utils#line(line) + (row - tablemode#spreadsheet#RowNr(line)) * 1 let line = tablemode#utils#line(line) + (row - tablemode#spreadsheet#RowNr(line)) * 1
let line_val = getline(line) let line_val = getline(line)
let cstartexpr = tablemode#table#StartCommentExpr() let cstartexpr = tablemode#table#StartCommentExpr()
@@ -207,11 +207,11 @@ function! tablemode#spreadsheet#cell#SetCell(val, ...) "{{{2
let line_value = sce . line_value . ece let line_value = sce . line_value . ece
endif endif
call setline(line, line_value) call setline(line, line_value)
call tablemode#table#TableRealign(line) call tablemode#table#Realign(line)
endif endif
endfunction endfunction
function! tablemode#spreadsheet#cell#TextObject(inner) "{{{2 function! tablemode#spreadsheet#cell#TextObject(inner) "{{{2
if tablemode#table#IsATableRow('.') if tablemode#table#IsRow('.')
call tablemode#spreadsheet#MoveToStartOfCell() call tablemode#spreadsheet#MoveToStartOfCell()
if a:inner if a:inner
normal! v normal! v
@@ -223,11 +223,11 @@ function! tablemode#spreadsheet#cell#TextObject(inner) "{{{2
endfunction endfunction
function! tablemode#spreadsheet#cell#Motion(direction, ...) "{{{2 function! tablemode#spreadsheet#cell#Motion(direction, ...) "{{{2
let l:count = a:0 ? a:1 : v:count1 let l:count = a:0 ? a:1 : v:count1
if tablemode#table#IsATableRow('.') if tablemode#table#IsRow('.')
for ii in range(l:count) for ii in range(l:count)
if a:direction ==# 'l' if a:direction ==# 'l'
if tablemode#spreadsheet#IsLastCell() if tablemode#spreadsheet#IsLastCell()
if !tablemode#table#IsATableRow(line('.') + 1) && (tablemode#table#IsATableHeader(line('.') + 1) && !tablemode#table#IsATableRow(line('.') + 2)) if !tablemode#table#IsRow(line('.') + 1) && (tablemode#table#IsHeader(line('.') + 1) && !tablemode#table#IsRow(line('.') + 2))
return return
endif endif
call tablemode#spreadsheet#cell#Motion('j', 1) call tablemode#spreadsheet#cell#Motion('j', 1)
@@ -242,7 +242,7 @@ function! tablemode#spreadsheet#cell#Motion(direction, ...) "{{{2
endif endif
elseif a:direction ==# 'h' elseif a:direction ==# 'h'
if tablemode#spreadsheet#IsFirstCell() if tablemode#spreadsheet#IsFirstCell()
if !tablemode#table#IsATableRow(line('.') - 1) && (tablemode#table#IsATableHeader(line('.') - 1) && !tablemode#table#IsATableRow(line('.') - 2)) if !tablemode#table#IsRow(line('.') - 1) && (tablemode#table#IsHeader(line('.') - 1) && !tablemode#table#IsRow(line('.') - 2))
return return
endif endif
call tablemode#spreadsheet#cell#Motion('k', 1) call tablemode#spreadsheet#cell#Motion('k', 1)
@@ -256,18 +256,18 @@ function! tablemode#spreadsheet#cell#Motion(direction, ...) "{{{2
execute 'normal! 2F' . g:table_mode_separator . '2l' execute 'normal! 2F' . g:table_mode_separator . '2l'
endif endif
elseif a:direction ==# 'j' elseif a:direction ==# 'j'
if tablemode#table#IsATableRow(line('.') + 1) if tablemode#table#IsRow(line('.') + 1)
" execute 'normal! ' . 1 . 'j' " execute 'normal! ' . 1 . 'j'
normal! j normal! j
elseif tablemode#table#IsATableHeader(line('.') + 1) && tablemode#table#IsATableRow(line('.') + 2) elseif tablemode#table#IsHeader(line('.') + 1) && tablemode#table#IsRow(line('.') + 2)
" execute 'normal! ' . 2 . 'j' " execute 'normal! ' . 2 . 'j'
normal! 2j normal! 2j
endif endif
elseif a:direction ==# 'k' elseif a:direction ==# 'k'
if tablemode#table#IsATableRow(line('.') - 1) if tablemode#table#IsRow(line('.') - 1)
" execute 'normal! ' . 1 . 'k' " execute 'normal! ' . 1 . 'k'
normal! k normal! k
elseif tablemode#table#IsATableHeader(line('.') - 1) && tablemode#table#IsATableRow(line('.') - 2) elseif tablemode#table#IsHeader(line('.') - 1) && tablemode#table#IsRow(line('.') - 2)
" execute 'normal! ' . (1 + 1) . 'k' " execute 'normal! ' . (1 + 1) . 'k'
normal! 2k normal! 2k
endif endif

View File

@@ -39,7 +39,7 @@ function! tablemode#spreadsheet#formula#Add(...) "{{{2
if fr !=# '' if fr !=# ''
let fr = '$' . row . ',' . colm . '=' . fr let fr = '$' . row . ',' . colm . '=' . fr
let fline = tablemode#spreadsheet#GetLastRow('.') + 1 let fline = tablemode#spreadsheet#GetLastRow('.') + 1
if tablemode#table#IsATableHeader(fline) | let fline += 1 | endif if tablemode#table#IsHeader(fline) | let fline += 1 | endif
let cursor_pos = [line('.'), col('.')] let cursor_pos = [line('.'), col('.')]
if getline(fline) =~# 'tmf: ' if getline(fline) =~# 'tmf: '
" Comment line correctly " Comment line correctly
@@ -98,7 +98,7 @@ function! tablemode#spreadsheet#formula#EvaluateExpr(expr, line) abort "{{{2
call tablemode#spreadsheet#cell#SetCell(eval(expr), line, row, colm) call tablemode#spreadsheet#cell#SetCell(eval(expr), line, row, colm)
else else
let [row, line] = [1, tablemode#spreadsheet#GetFirstRow(line)] let [row, line] = [1, tablemode#spreadsheet#GetFirstRow(line)]
while tablemode#table#IsATableRow(line) while tablemode#table#IsRow(line)
let texpr = expr let texpr = expr
if expr =~# '\$' if expr =~# '\$'
let texpr = substitute(texpr, '\$\(\d\+\)', let texpr = substitute(texpr, '\$\(\d\+\)',
@@ -126,17 +126,17 @@ function! tablemode#spreadsheet#formula#EvaluateFormulaLine() abort "{{{2
else else
let matchexpr = '^\s* tmf: \zs.*$' let matchexpr = '^\s* tmf: \zs.*$'
endif endif
if tablemode#table#IsATableRow('.') " We're inside the table if tablemode#table#IsRow('.') " We're inside the table
let line = tablemode#spreadsheet#GetLastRow('.') let line = tablemode#spreadsheet#GetLastRow('.')
let fline = line + 1 let fline = line + 1
if tablemode#table#IsATableHeader(fline) | let fline += 1 | endif if tablemode#table#IsHeader(fline) | let fline += 1 | endif
if getline(fline) =~# 'tmf: ' if getline(fline) =~# 'tmf: '
let exprs = split(matchstr(getline(fline), matchexpr), ';') let exprs = split(matchstr(getline(fline), matchexpr), ';')
endif endif
elseif getline('.') =~# 'tmf: ' " We're on the formula line elseif getline('.') =~# 'tmf: ' " We're on the formula line
let line = line('.') - 1 let line = line('.') - 1
if tablemode#table#IsATableHeader(line) | let line -= 1 | endif if tablemode#table#IsHeader(line) | let line -= 1 | endif
if tablemode#table#IsATableRow(line) if tablemode#table#IsRow(line)
let exprs = split(matchstr(getline('.'), matchexpr), ';') let exprs = split(matchstr(getline('.'), matchexpr), ';')
endif endif
endif endif

View File

@@ -21,13 +21,13 @@
function! s:HeaderBorderExpr() "{{{2 function! s:HeaderBorderExpr() "{{{2
return tablemode#table#StartExpr() . return tablemode#table#StartExpr() .
\ '[' . g:table_mode_corner . g:table_mode_corner_corner . ']' . \ '[' . g:table_mode_corner . g:table_mode_corner_corner . ']' .
\ '[' . g:table_mode_fillchar . g:table_mode_corner . ']*' . \ '[' . g:table_mode_fillchar . g:table_mode_corner . g:table_mode_align_char . ']*' .
\ '[' . g:table_mode_corner . g:table_mode_corner_corner . ']' . \ '[' . g:table_mode_corner . g:table_mode_corner_corner . ']' .
\ tablemode#table#EndExpr() \ tablemode#table#EndExpr()
endfunction endfunction
function! s:DefaultHeaderBorder() "{{{2 function! s:DefaultHeaderBorder() "{{{2
if s:IsTableModeActive() if tablemode#IsActive()
return g:table_mode_corner_corner . g:table_mode_fillchar . g:table_mode_corner . g:table_mode_fillchar . g:table_mode_corner_corner return g:table_mode_corner_corner . g:table_mode_fillchar . g:table_mode_corner . g:table_mode_fillchar . g:table_mode_corner_corner
else else
return '' return ''
@@ -36,19 +36,38 @@ endfunction
function! s:GenerateHeaderBorder(line) "{{{2 function! s:GenerateHeaderBorder(line) "{{{2
let line = tablemode#utils#line(a:line) let line = tablemode#utils#line(a:line)
if tablemode#table#IsATableRow(line - 1) || tablemode#table#IsATableRow(line + 1) if tablemode#table#IsRow(line - 1) || tablemode#table#IsRow(line + 1)
let line_val = '' let line_val = ''
if tablemode#table#IsATableRow(line + 1) if tablemode#table#IsRow(line + 1)
let line_val = getline(line + 1) let line_val = getline(line + 1)
endif endif
if tablemode#table#IsATableRow(line - 1) && tablemode#utils#strlen(line_val) < tablemode#utils#strlen(getline(line - 1)) if tablemode#table#IsRow(line - 1) && tablemode#utils#strlen(line_val) < tablemode#utils#strlen(getline(line - 1))
let line_val = getline(line - 1) let line_val = getline(line - 1)
endif endif
if tablemode#utils#strlen(line_val) <= 1 | return s:DefaultHeaderBorder() | endif if tablemode#utils#strlen(line_val) <= 1 | return s:DefaultHeaderBorder() | endif
let border = substitute(line_val[stridx(line_val, g:table_mode_separator):strridx(line_val, g:table_mode_separator)], g:table_mode_separator, g:table_mode_corner, 'g') let border = substitute(line_val[stridx(line_val, g:table_mode_separator):strridx(line_val, g:table_mode_separator)], g:table_mode_separator, g:table_mode_corner, 'g')
let border = substitute(border, '[^' . g:table_mode_corner . ']', g:table_mode_fillchar, 'g') let border = substitute(border, '[^' . g:table_mode_corner . ']', g:table_mode_fillchar, 'g')
let border = substitute(border, '^' . g:table_mode_corner . '\(.*\)' . g:table_mode_corner . '$', g:table_mode_corner_corner . '\1' . g:table_mode_corner_corner, '') let border = substitute(border, '^' . g:table_mode_corner . '\(.*\)' . g:table_mode_corner . '$', g:table_mode_corner_corner . '\1' . g:table_mode_corner_corner, '')
" Incorporate header alignment chars
if getline(line) =~# g:table_mode_align_char
let pat = '[' . g:table_mode_corner_corner . g:table_mode_corner . ']'
let hcols = tablemode#align#Split(getline(line), pat)
let gcols = tablemode#align#Split(border, pat)
for idx in range(len(hcols))
if hcols[idx] =~# g:table_mode_align_char
if hcols[idx] =~# g:table_mode_align_char . '$'
let gcols[idx] = gcols[idx][:-2] . g:table_mode_align_char
else
let gcols[idx] = g:table_mode_align_char . gcols[idx][1:]
endif
endif
endfor
let border = join(gcols, '')
endif
let cstartexpr = tablemode#table#StartCommentExpr() let cstartexpr = tablemode#table#StartCommentExpr()
if tablemode#utils#strlen(cstartexpr) > 0 && getline(line) =~# cstartexpr if tablemode#utils#strlen(cstartexpr) > 0 && getline(line) =~# cstartexpr
let sce = matchstr(line_val, tablemode#table#StartCommentExpr()) let sce = matchstr(line_val, tablemode#table#StartCommentExpr())
@@ -134,109 +153,51 @@ function! tablemode#table#EndExpr() "{{{2
endif endif
endfunction endfunction
function! tablemode#table#IsATableRow(line) "{{{2 function! tablemode#table#IsHeader(line) "{{{2
return getline(a:line) =~# (tablemode#table#StartExpr() . g:table_mode_separator . '[^' . return getline(a:line) =~# s:HeaderBorderExpr()
\ g:table_mode_fillchar . ']*[^' . g:table_mode_corner . ']*$')
endfunction endfunction
function! tablemode#table#IsATableHeader(line) "{{{2 function! tablemode#table#IsRow(line) "{{{2
return getline(a:line) =~# s:HeaderBorderExpr() return !tablemode#table#IsHeader(a:line) && getline(a:line) =~# (tablemode#table#StartExpr() . g:table_mode_separator)
endfunction endfunction
function! tablemode#table#AddHeaderBorder(line) "{{{2 function! tablemode#table#AddHeaderBorder(line) "{{{2
call setline(a:line, s:GenerateHeaderBorder(a:line)) call setline(a:line, s:GenerateHeaderBorder(a:line))
endfunction endfunction
function! tablemode#table#TableRealign(line) "{{{2 function! tablemode#table#Realign(line) "{{{2
let line = tablemode#utils#line(a:line) let line = tablemode#utils#line(a:line)
let [lnums, lines] = [[], []] let lines = []
let [tline, blines] = [line, []] let [lnum, blines] = [line, []]
while tablemode#table#IsATableRow(tline) || tablemode#table#IsATableHeader(tline) while tablemode#table#IsRow(lnum) || tablemode#table#IsHeader(lnum)
if tablemode#table#IsATableHeader(tline) if tablemode#table#IsHeader(lnum)
call insert(blines, tline) call insert(blines, lnum)
let tline -= 1 let lnum -= 1
continue continue
endif endif
call insert(lnums, tline) call insert(lines, {'lnum': lnum, 'text': getline(lnum)})
call insert(lines, getline(tline)) let lnum -= 1
let tline -= 1
endwhile endwhile
let tline = line + 1 let lnum = line + 1
while tablemode#table#IsRow(lnum) || tablemode#table#IsHeader(lnum)
while tablemode#table#IsATableRow(tline) || tablemode#table#IsATableHeader(tline) if tablemode#table#IsHeader(lnum)
if tablemode#table#IsATableHeader(tline) call add(blines, lnum)
call insert(blines, tline) let lnum += 1
let tline += 1
continue continue
endif endif
call add(lnums, tline) call add(lines, {'lnum': lnum, 'text': getline(lnum)})
call add(lines, getline(tline)) let lnum += 1
let tline += 1
endwhile endwhile
let lines = tablemode#align#Align(lines) let lines = tablemode#align#Align(lines)
for lnum in lnums for aline in lines
let index = index(lnums, lnum) call setline(aline.lnum, aline.text)
call setline(lnum, lines[index])
endfor endfor
for bline in blines for bline in blines
call tablemode#table#AddHeaderBorder(bline) call tablemode#table#AddHeaderBorder(bline)
endfor endfor
endfunction endfunction
function! tablemode#table#TableMotion(direction, ...) "{{{2
let l:count = a:0 ? a:1 : v:count1
if tablemode#table#IsATableRow('.')
for ii in range(l:count)
if a:direction ==# 'l'
if tablemode#spreadsheet#IsLastCell()
if !tablemode#table#IsATableRow(line('.') + 1) && (tablemode#table#IsATableHeader(line('.') + 1) && !tablemode#table#IsATableRow(line('.') + 2 * 1))
return
endif
call tablemode#table#TableMotion('j', 1)
normal! 0
endif
" If line starts with g:table_mode_separator
if getline('.')[col('.')-1] ==# g:table_mode_separator
normal! 2l
else
execute 'normal! f' . g:table_mode_separator . '2l'
endif
elseif a:direction ==# 'h'
if tablemode#spreadsheet#IsFirstCell()
if !tablemode#table#IsATableRow(line('.') - 1) && (tablemode#table#IsATableHeader(line('.') - 1) && !tablemode#table#IsATableRow(line('.') - 2 * 1))
return
endif
call tablemode#table#TableMotion('k', 1)
normal! $
endif
" If line ends with g:table_mode_separator
if getline('.')[col('.')-1] ==# g:table_mode_separator
execute 'normal! F' . g:table_mode_separator . '2l'
else
execute 'normal! 2F' . g:table_mode_separator . '2l'
endif
elseif a:direction ==# 'j'
if tablemode#table#IsATableRow(line('.') + 1)
execute 'normal! ' . 1 . 'j'
elseif tablemode#table#IsATableHeader(line('.') + 1) && tablemode#table#IsATableRow(line('.') + 2)
execute 'normal! ' . (1 + 1) . 'j'
endif
elseif a:direction ==# 'k'
if tablemode#table#IsATableRow(line('.') - 1)
execute 'normal! ' . 1 . 'k'
elseif tablemode#table#IsATableHeader(line('.') - 1) && tablemode#table#IsATableRow(line('.') - 2)
execute 'normal! ' . (1 + 1) . 'k'
endif
endif
endfor
endif
endfunction
" vim: sw=2 sts=2 fdl=0 fdm=marker

View File

@@ -1,7 +1,7 @@
*table-mode.txt* Table Mode for easy table formatting. v4.2.0 *table-mode.txt* Table Mode for easy table formatting. v4.3.0
=============================================================================== ===============================================================================
Table Mode, THE AWESOME AUTOMATIC TABLE CREATOR & FORMATTER Table Mode, THE AWESOME AUTOMATIC TABLE CREATOR & FORMATTER
VERSION 4.2.0 VERSION 4.3.0
Author: Dhruva Sagar <http://dhruvasagar.com/> Author: Dhruva Sagar <http://dhruvasagar.com/>
License: MIT <http://opensource.org/licenses/MIT/> License: MIT <http://opensource.org/licenses/MIT/>
@@ -34,6 +34,11 @@ Create Table on the fly:
each additional separator you add, it aligns the table properly, each additional separator you add, it aligns the table properly,
without having to do anything else. without having to do anything else.
Table Mode now supports gfm style tables and allows to define column
alignments with the aid of placing ':' (configured using
|g:table_mode_align_char|) on the table header border appropriately to
align content left or right.
The table mode is disabled by default and you can enter table mode The table mode is disabled by default and you can enter table mode
using |table-mode-toggle-map| or you can also enable it permanently using |table-mode-toggle-map| or you can also enable it permanently
using |table-mode-always-active| if you wish though not recommended. using |table-mode-always-active| if you wish though not recommended.
@@ -139,6 +144,10 @@ Overview:
|table-mode-delimiter| .......... Set the delimiter for Tableize. |table-mode-delimiter| .......... Set the delimiter for Tableize.
|table-mode-corner-corner| ...... Set the character to be used for |table-mode-corner-corner| ...... Set the character to be used for
extreme corners of the table border. extreme corners of the table border.
|table-mode-align-char|.......... Set the alignment character which
can be added to the table header
border to control alignment of that
column.
g:loaded_table_mode *table-mode-loaded* g:loaded_table_mode *table-mode-loaded*
Use this option to disable the plugin: > Use this option to disable the plugin: >
@@ -196,6 +205,11 @@ g:table_mode_corner_corner *table-mode-corner-corner*
corners of the table border. > corners of the table border. >
let g:table_mode_corner_corner = '|' let g:table_mode_corner_corner = '|'
< <
g:table_mode_align_char *table-mode-align-char*
Use this option to define the character to be used for defining
alignments for columns in the table header border. >
let g:table_mode_align_char = ':'
<
=============================================================================== ===============================================================================
MAPPINGS *table-mode-mappings* MAPPINGS *table-mode-mappings*

View File

@@ -42,9 +42,10 @@ call s:SetGlobalOptDefault('table_mode_toggle_map', 'm')
call s:SetGlobalOptDefault('table_mode_always_active', 0) call s:SetGlobalOptDefault('table_mode_always_active', 0)
call s:SetGlobalOptDefault('table_mode_delimiter', ',') call s:SetGlobalOptDefault('table_mode_delimiter', ',')
call s:SetGlobalOptDefault('table_mode_corner_corner', '|') call s:SetGlobalOptDefault('table_mode_corner_corner', '|')
call s:SetGlobalOptDefault('table_mode_align_char', ':')
function! s:TableEchoCell() "{{{1 function! s:TableEchoCell() "{{{1
if tablemode#table#IsATableRow('.') if tablemode#table#IsRow('.')
echomsg '$' . tablemode#spreadsheet#RowNr('.') . ',' . tablemode#spreadsheet#ColumnNr('.') echomsg '$' . tablemode#spreadsheet#RowNr('.') . ',' . tablemode#spreadsheet#ColumnNr('.')
endif endif
endfunction endfunction
@@ -52,10 +53,10 @@ endfunction
" Define Commands & Mappings {{{1 " Define Commands & Mappings {{{1
if !g:table_mode_always_active "{{{2 if !g:table_mode_always_active "{{{2
exec "nnoremap <silent> " . g:table_mode_map_prefix . g:table_mode_toggle_map . exec "nnoremap <silent> " . g:table_mode_map_prefix . g:table_mode_toggle_map .
\ " <Esc>:call tablemode#TableModeToggle()<CR>" \ " <Esc>:call tablemode#Toggle()<CR>"
command! -nargs=0 TableModeToggle call tablemode#TableModeToggle() command! -nargs=0 TableModeToggle call tablemode#Toggle()
command! -nargs=0 TableModeEnable call tablemode#TableModeEnable() command! -nargs=0 TableModeEnable call tablemode#Enable()
command! -nargs=0 TableModeDisable call tablemode#TableModeDisable() command! -nargs=0 TableModeDisable call tablemode#Disable()
else else
let table_mode_separator_map = g:table_mode_separator let table_mode_separator_map = g:table_mode_separator
" '|' is a special character, we need to map <Bar> instead " '|' is a special character, we need to map <Bar> instead
@@ -67,16 +68,16 @@ else
endif endif
" }}}2 " }}}2
command! -nargs=? -range Tableize <line1>,<line2>call tablemode#table#TableizeRange(<q-args>) command! -nargs=? -range Tableize <line1>,<line2>call tablemode#TableizeRange(<q-args>)
command! TableAddFormula call tablemode#spreadsheet#formula#Add() command! TableAddFormula call tablemode#spreadsheet#formula#Add()
command! TableModeRealign call tablemode#spreadsheet#TableRealign('.') command! TableModeRealign call tablemode#table#Realign('.')
command! TableEvalFormulaLine call tablemode#spreadsheet#formula#EvaluateFormulaLine() command! TableEvalFormulaLine call tablemode#spreadsheet#formula#EvaluateFormulaLine()
nnoremap <silent> <Plug>(table-mode-tableize) :Tableize<CR> nnoremap <silent> <Plug>(table-mode-tableize) :Tableize<CR>
xnoremap <silent> <Plug>(table-mode-tableize) :Tableize<CR> xnoremap <silent> <Plug>(table-mode-tableize) :Tableize<CR>
xnoremap <silent> <Plug>(table-mode-tableize-delimiter) :<C-U>call tablemode#TableizeByDelimiter()<CR> xnoremap <silent> <Plug>(table-mode-tableize-delimiter) :<C-U>call tablemode#TableizeByDelimiter()<CR>
nnoremap <silent> <Plug>(table-mode-realign) :call tablemode#table#TableRealign('.')<CR> nnoremap <silent> <Plug>(table-mode-realign) :call tablemode#table#Realign('.')<CR>
nnoremap <silent> <Plug>(table-mode-motion-up) :<C-U>call tablemode#spreadsheet#cell#Motion('k')<CR> nnoremap <silent> <Plug>(table-mode-motion-up) :<C-U>call tablemode#spreadsheet#cell#Motion('k')<CR>
nnoremap <silent> <Plug>(table-mode-motion-down) :<C-U>call tablemode#spreadsheet#cell#Motion('j')<CR> nnoremap <silent> <Plug>(table-mode-motion-down) :<C-U>call tablemode#spreadsheet#cell#Motion('j')<CR>

View File

@@ -3,12 +3,20 @@ source t/config/options.vim
call vspec#hint({'scope': 'tablemode#align#scope()', 'sid': 'tablemode#align#sid()'}) call vspec#hint({'scope': 'tablemode#align#scope()', 'sid': 'tablemode#align#sid()'})
function! ConvertLines2Dict(lines)
let lines = []
for idx in range(len(a:lines))
call insert(lines, {"lnum": idx+1, "text": a:lines[idx]})
endfor
return lines
endfunction
describe 'Align' describe 'Align'
it 'should align table content correctly' it 'should align table content correctly'
Expect tablemode#align#Align(readfile('t/fixtures/align/simple_before.txt')) == readfile('t/fixtures/align/simple_after.txt') Expect tablemode#align#Align(ConvertLines2Dict(readfile('t/fixtures/align/simple_before.txt'))) == ConvertLines2Dict(readfile('t/fixtures/align/simple_after.txt'))
end end
it 'should align table content with unicode characters correctly' it 'should align table content with unicode characters correctly'
Expect tablemode#align#Align(readfile('t/fixtures/align/unicode_before.txt')) == readfile('t/fixtures/align/unicode_after.txt') Expect tablemode#align#Align(ConvertLines2Dict(readfile('t/fixtures/align/unicode_before.txt'))) == ConvertLines2Dict(readfile('t/fixtures/align/unicode_after.txt'))
end end
end end

View File

@@ -3,47 +3,127 @@ source t/config/options.vim
call vspec#hint({'scope': 'tablemode#spreadsheet#cell#scope()', 'sid': 'tablemode#spreadsheet#cell#sid()'}) call vspec#hint({'scope': 'tablemode#spreadsheet#cell#scope()', 'sid': 'tablemode#spreadsheet#cell#sid()'})
describe 'cell API' describe 'cell'
before describe 'API'
new before
read t/fixtures/sample.txt new
read t/fixtures/sample.txt
end
it 'should return the cells'
Expect tablemode#spreadsheet#cell#GetCells(2, 1, 1) ==# 'test11'
" Get Rows
Expect tablemode#spreadsheet#cell#GetCells(2, 1) == ['test11', 'test12']
Expect tablemode#spreadsheet#cell#GetCells(2, 2) == ['test21', 'test22']
" Get Columns
Expect tablemode#spreadsheet#cell#GetCells(2, 0, 1) == ['test11', 'test21']
Expect tablemode#spreadsheet#cell#GetCells(2, 0, 2) == ['test12', 'test22']
end
it 'should return the cells in a range'
" Entire table as range
Expect tablemode#spreadsheet#cell#GetCellRange('1,1:2,2', 2, 1) == [['test11', 'test21'], ['test12', 'test22']]
" Get Rows given different seed lines and columns
Expect tablemode#spreadsheet#cell#GetCellRange('1,1:1,2', 2, 1) == ['test11', 'test12']
Expect tablemode#spreadsheet#cell#GetCellRange('1,1:1,2', 2, 2) == ['test11', 'test12']
Expect tablemode#spreadsheet#cell#GetCellRange('1,1:1,2', 3, 1) == ['test11', 'test12']
Expect tablemode#spreadsheet#cell#GetCellRange('1,1:1,2', 3, 2) == ['test11', 'test12']
Expect tablemode#spreadsheet#cell#GetCellRange('2,1:2,2', 2, 1) == ['test21', 'test22']
Expect tablemode#spreadsheet#cell#GetCellRange('2,1:2,2', 2, 2) == ['test21', 'test22']
Expect tablemode#spreadsheet#cell#GetCellRange('2,1:2,2', 3, 1) == ['test21', 'test22']
Expect tablemode#spreadsheet#cell#GetCellRange('2,1:2,2', 3, 2) == ['test21', 'test22']
" Get Columns given different seed lines and column
Expect tablemode#spreadsheet#cell#GetCellRange('1:2', 2, 1) == ['test11', 'test21']
Expect tablemode#spreadsheet#cell#GetCellRange('1:2', 2, 2) == ['test12', 'test22']
Expect tablemode#spreadsheet#cell#GetCellRange('1:2', 3, 1) == ['test11', 'test21']
Expect tablemode#spreadsheet#cell#GetCellRange('1:2', 3, 2) == ['test12', 'test22']
" Get Column given negative values in range for representing rows from
" the end, -1 being the second last row.
Expect tablemode#spreadsheet#cell#GetCellRange('1:-1', 2, 1) == ['test11']
Expect tablemode#spreadsheet#cell#GetCellRange('1:-1', 3, 1) == ['test11']
Expect tablemode#spreadsheet#cell#GetCellRange('1:-1', 2, 2) == ['test12']
Expect tablemode#spreadsheet#cell#GetCellRange('1:-1', 3, 2) == ['test12']
end
end end
it 'should return the cells' describe 'Motions'
Expect tablemode#spreadsheet#cell#GetCells(2, 1, 1) ==# 'test11' describe 'left or right'
" Get Rows before
Expect tablemode#spreadsheet#cell#GetCells(2, 1) == ['test11', 'test12'] new
Expect tablemode#spreadsheet#cell#GetCells(2, 2) == ['test21', 'test22'] normal! ggdG
" Get Columns call tablemode#Enable()
Expect tablemode#spreadsheet#cell#GetCells(2, 0, 1) == ['test11', 'test21'] normal i|test11|test12|
Expect tablemode#spreadsheet#cell#GetCells(2, 0, 2) == ['test12', 'test22'] |test21|test22|
end call cursor(1, 3)
end end
it 'should return the cells in a range'
" Entire table as range it 'should move left when not on first column'
Expect tablemode#spreadsheet#cell#GetCellRange('1,1:2,2', 2, 1) == [['test11', 'test21'], ['test12', 'test22']] call cursor(1, 12)
Expect tablemode#spreadsheet#ColumnNr('.') == 2
call tablemode#spreadsheet#cell#Motion('h')
Expect tablemode#spreadsheet#ColumnNr('.') == 1
end end
" Get Rows given different seed lines and columns
Expect tablemode#spreadsheet#cell#GetCellRange('1,1:1,2', 2, 1) == ['test11', 'test12'] it 'should move to the previous row last column if it exists when on first column'
Expect tablemode#spreadsheet#cell#GetCellRange('1,1:1,2', 2, 2) == ['test11', 'test12'] call cursor(2, 3)
Expect tablemode#spreadsheet#cell#GetCellRange('1,1:1,2', 3, 1) == ['test11', 'test12'] Expect tablemode#spreadsheet#RowNr('.') == 2
Expect tablemode#spreadsheet#cell#GetCellRange('1,1:1,2', 3, 2) == ['test11', 'test12'] Expect tablemode#spreadsheet#ColumnNr('.') == 1
Expect tablemode#spreadsheet#cell#GetCellRange('2,1:2,2', 2, 1) == ['test21', 'test22'] call tablemode#spreadsheet#cell#Motion('h')
Expect tablemode#spreadsheet#cell#GetCellRange('2,1:2,2', 2, 2) == ['test21', 'test22'] Expect tablemode#spreadsheet#RowNr('.') == 1
Expect tablemode#spreadsheet#cell#GetCellRange('2,1:2,2', 3, 1) == ['test21', 'test22'] Expect tablemode#spreadsheet#ColumnNr('.') == 2
Expect tablemode#spreadsheet#cell#GetCellRange('2,1:2,2', 3, 2) == ['test21', 'test22']
end end
" Get Columns given different seed lines and column
Expect tablemode#spreadsheet#cell#GetCellRange('1:2', 2, 1) == ['test11', 'test21'] it 'should move right when not on last column'
Expect tablemode#spreadsheet#cell#GetCellRange('1:2', 2, 2) == ['test12', 'test22'] Expect tablemode#spreadsheet#ColumnNr('.') == 1
Expect tablemode#spreadsheet#cell#GetCellRange('1:2', 3, 1) == ['test11', 'test21'] call tablemode#spreadsheet#cell#Motion('l')
Expect tablemode#spreadsheet#cell#GetCellRange('1:2', 3, 2) == ['test12', 'test22'] Expect tablemode#spreadsheet#ColumnNr('.') == 2
end end
" Get Column given negative values in range for representing rows from
" the end, -1 being the second last row. it 'should move to the next row first column if it exists when on last column'
Expect tablemode#spreadsheet#cell#GetCellRange('1:-1', 2, 1) == ['test11'] call cursor(1, 12)
Expect tablemode#spreadsheet#cell#GetCellRange('1:-1', 3, 1) == ['test11'] Expect tablemode#spreadsheet#RowNr('.') == 1
Expect tablemode#spreadsheet#cell#GetCellRange('1:-1', 2, 2) == ['test12'] Expect tablemode#spreadsheet#ColumnNr('.') == 2
Expect tablemode#spreadsheet#cell#GetCellRange('1:-1', 3, 2) == ['test12'] call tablemode#spreadsheet#cell#Motion('l')
Expect tablemode#spreadsheet#RowNr('.') == 2
Expect tablemode#spreadsheet#ColumnNr('.') == 1
end
end
describe 'up or down'
before
new
normal! ggdG
call tablemode#Enable()
normal i|test11|test12|
|test21|test22|
call cursor(1, 3)
end
it 'should move a row up unless on first row'
call cursor(2, 3)
Expect tablemode#spreadsheet#RowNr('.') == 2
call tablemode#spreadsheet#cell#Motion('k')
Expect tablemode#spreadsheet#RowNr('.') == 1
end
it 'should remain on first row when trying to move up'
Expect tablemode#spreadsheet#RowNr('.') == 1
call tablemode#spreadsheet#cell#Motion('k')
Expect tablemode#spreadsheet#RowNr('.') == 1
end
it 'should move a row down unless on last row'
Expect tablemode#spreadsheet#RowNr('.') == 1
call tablemode#spreadsheet#cell#Motion('j')
Expect tablemode#spreadsheet#RowNr('.') == 2
end
it 'should remain on last row when trying to move down'
Expect tablemode#spreadsheet#RowNr('.') == 1
call tablemode#spreadsheet#cell#Motion('k')
Expect tablemode#spreadsheet#RowNr('.') == 1
end end
end end

View File

@@ -6,3 +6,4 @@ let g:table_mode_toggle_map = 'm'
let g:table_mode_always_active = 0 let g:table_mode_always_active = 0
let g:table_mode_delimiter = ',' let g:table_mode_delimiter = ','
let g:table_mode_corner_corner = '|' let g:table_mode_corner_corner = '|'
let g:table_mode_align_char = ':'

View File

@@ -0,0 +1,7 @@
| Title | Message |
|--------+---------|
| test11 | test12 |
| test21 | test22 |
|--------+---------|

View File

@@ -5,115 +5,39 @@ call vspec#hint({'scope': 'tablemode#table#scope()', 'sid': 'tablemode#table#sid
describe 'table' describe 'table'
describe 'API' describe 'API'
before describe 'IsRow'
new
read t/fixtures/sample.txt
end
it 'should return true when inside a table'
Expect tablemode#table#IsATableRow(2) to_be_true
end
it 'should return false when outside a table'
Expect tablemode#table#IsATableRow(4) to_be_false
end
end
describe 'Tableize'
before
new
read t/fixtures/tableize.txt
end
it 'should tableize with default delimiter'
:2,3call tablemode#TableizeRange('')
Expect tablemode#table#IsATableRow(2) to_be_true
Expect tablemode#spreadsheet#RowCount(2) == 2
Expect tablemode#spreadsheet#ColumnCount(2) == 3
end
it 'should tableize with given delimiter'
:2,3call tablemode#TableizeRange('/;')
Expect tablemode#table#IsATableRow(2) to_be_true
Expect tablemode#spreadsheet#RowCount(2) == 2
Expect tablemode#spreadsheet#ColumnCount(2) == 2
end
end
describe 'Motions'
describe 'left or right'
before before
new new
normal! ggdG read t/fixtures/sample.txt
call tablemode#TableModeEnable()
normal i|test11|test12|
|test21|test22|
end end
it 'should return true when inside a table'
it 'should move left when not on first column' Expect tablemode#table#IsRow(2) to_be_true
call cursor(1, 12) Expect tablemode#table#IsRow(3) to_be_true
Expect tablemode#spreadsheet#ColumnNr('.') == 2
call tablemode#table#TableMotion('h')
end end
it 'should return false when outside a table'
it 'should move to the previous row last column if it exists when on first column' Expect tablemode#table#IsRow(1) to_be_false
call cursor(2, 3) Expect tablemode#table#IsRow(4) to_be_false
Expect tablemode#spreadsheet#RowNr('.') == 2
Expect tablemode#spreadsheet#ColumnNr('.') == 1
call tablemode#table#TableMotion('h')
Expect tablemode#spreadsheet#RowNr('.') == 1
Expect tablemode#spreadsheet#ColumnNr('.') == 2
end
it 'should move right when not on last column'
Expect tablemode#spreadsheet#ColumnNr('.') == 1
call tablemode#table#TableMotion('l')
Expect tablemode#spreadsheet#ColumnNr('.') == 2
end
it 'should move to the next row first column if it exists when on last column'
call cursor(1, 12)
Expect tablemode#spreadsheet#RowNr('.') == 1
Expect tablemode#spreadsheet#ColumnNr('.') == 2
call tablemode#table#TableMotion('l')
Expect tablemode#spreadsheet#RowNr('.') == 2
end end
end end
describe 'IsHeader'
before before
new new
new read t/fixtures/sample_with_header.txt
normal! ggdG
call tablemode#TableModeEnable()
normal i|test11|test12|
end end
end it 'should return true when on a table header'
Expect tablemode#table#IsHeader(3) to_be_true
it 'should move a row up unless on first row' Expect tablemode#table#IsHeader(6) to_be_true
call cursor(2, 3)
Expect tablemode#spreadsheet#RowNr('.') == 2
end end
end it 'should return false when not on a table header'
Expect tablemode#table#IsHeader(1) to_be_false
it 'should remain on first row when trying to move up' Expect tablemode#table#IsHeader(2) to_be_false
Expect tablemode#spreadsheet#RowNr('.') == 1 Expect tablemode#table#IsHeader(4) to_be_false
call tablemode#table#TableMotion('k') Expect tablemode#table#IsHeader(5) to_be_false
Expect tablemode#spreadsheet#RowNr('.') == 1
end
it 'should move a row down unless on last row'
Expect tablemode#spreadsheet#RowNr('.') == 1
call tablemode#table#TableMotion('j')
Expect tablemode#spreadsheet#RowNr('.') == 2
end
it 'should remain on last row when trying to move down'
Expect tablemode#spreadsheet#RowNr('.') == 1
end end
end end
end end

View File

@@ -5,9 +5,9 @@ call vspec#hint({'scope': 'tablemode#scope()', 'sid': 'tablemode#sid()'})
describe 'tablemode' describe 'tablemode'
describe 'Activation' describe 'Activation'
describe 'tablemode#TableModeEnable()' describe 'tablemode#Enable()'
before before
call tablemode#TableModeEnable() call tablemode#Enable()
end end
it 'should enable table mode' it 'should enable table mode'
@@ -15,9 +15,9 @@ describe 'tablemode'
end end
end end
describe 'tablemode#TableModeDisable()' describe 'tablemode#Disable()'
before before
call tablemode#TableModeDisable() call tablemode#Disable()
end end
it 'should disable table mode' it 'should disable table mode'
@@ -25,13 +25,34 @@ describe 'tablemode'
end end
end end
describe 'tablemode#TableModeToggle()' describe 'tablemode#Toggle()'
it 'should toggle table mode' it 'should toggle table mode'
call tablemode#TableModeToggle() call tablemode#Toggle()
Expect b:table_mode_active to_be_true Expect b:table_mode_active to_be_true
call tablemode#TableModeToggle() call tablemode#Toggle()
Expect b:table_mode_active to_be_false Expect b:table_mode_active to_be_false
end end
end end
end end
describe 'Tableize'
before
new
read t/fixtures/tableize.txt
end
it 'should tableize with default delimiter'
:2,3call tablemode#TableizeRange('')
Expect tablemode#table#IsRow(2) to_be_true
Expect tablemode#spreadsheet#RowCount(2) == 2
Expect tablemode#spreadsheet#ColumnCount(2) == 3
end
it 'should tableize with given delimiter'
:2,3call tablemode#TableizeRange('/;')
Expect tablemode#table#IsRow(2) to_be_true
Expect tablemode#spreadsheet#RowCount(2) == 2
Expect tablemode#spreadsheet#ColumnCount(2) == 2
end
end
end end