Compare commits

...

11 Commits

Author SHA1 Message Date
Dhruva Sagar
8d3c4912a2 Added syntax for matching tables 2014-05-03 02:19:19 +05:30
Dhruva Sagar
13e1a20002 Fixing tests 2014-05-01 21:29:31 +05:30
Dhruva Sagar
d73236f964 Fixing ruby version 2014-05-01 21:21:31 +05:30
Dhruva Sagar
c5efbe1ad7 Bumped ruby version for travis 2014-05-01 21:16:56 +05:30
Dhruva Sagar
8970d5ffbb Improved tests 2014-05-01 21:14:49 +05:30
Dhruva Sagar
859eb42856 Added more tests for Realigning unicode content 2014-05-01 21:07:53 +05:30
Dhruva Sagar
d7ad97f099 Fixing tests 2014-05-01 16:56:11 +05:30
Dhruva Sagar
dbd79f2c1e Minor refactor & improved test coverage 2014-05-01 16:35:25 +05:30
Dhruva Sagar
fa568fe91b Improved test for utils 2014-04-30 23:05:19 +05:30
Dhruva Sagar
53da3fb4c2 Fixed #28
Altered border generation code to incorporate true display width of
characters, hence generate more accurate borders.
2014-04-30 21:30:44 +05:30
Dhruva Sagar
6007953f0e Fixed typo in docs 2014-04-28 14:14:26 +05:30
29 changed files with 429 additions and 157 deletions

View File

@@ -1,4 +1,4 @@
language: ruby
rvm:
- 1.9.3
- 2.1
script: rake ci

View File

@@ -1,4 +1,7 @@
# Change Log
## Version 4.4.1
* Added syntax for matching tables
## Version 4.4.0
* Minor bug fixes
* Added feature to allow using negative indices within formulas to access rows,

View File

@@ -1,4 +1,4 @@
# VIM Table Mode v4.4.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.4.1 [![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
tables as you type.
@@ -151,7 +151,7 @@ $ git submodule add git@github.com:dhruvasagar/vim-table-mode.git bundle/table-m
mode also provides 2 special functions `Sum` and `Average`. Both these
functions take a range as input. A range can be of two forms :
- `r1,r2`: This represents cells in the current column from row `r1`
- `r1:r2`: This represents cells in the current column from row `r1`
through `r2`. If `r2` is negative it represents `r2` rows above the
current row (of the target cell).

View File

@@ -101,7 +101,7 @@ endfunction
function! tablemode#TableizeInsertMode() "{{{2
if tablemode#IsActive() && getline('.') =~# (tablemode#table#StartExpr() . g:table_mode_separator . g:table_mode_separator)
call tablemode#table#AddHeaderBorder('.')
call tablemode#table#AddBorder('.')
normal! A
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'))

View File

@@ -19,32 +19,6 @@
" Borrowed from Tabular
" Private Functions {{{1
" Return the number of bytes in a string after expanding tabs to spaces. {{{2
" This expansion is done based on the current value of 'tabstop'
if exists('*strdisplaywidth')
" Needs vim 7.3
let s:Strlen = function("strdisplaywidth")
else
function! s:Strlen(string)
" Implement the tab handling part of strdisplaywidth for vim 7.2 and
" earlier - not much that can be done about handling doublewidth
" characters.
let rv = 0
let i = 0
for char in split(a:string, '\zs')
if char == "\t"
let rv += &ts - i
let i = 0
else
let rv += 1
let i = (i + 1) % &ts
endif
endfor
return rv
endfunction
endif
" function! s:StripTrailingSpaces(string) - Remove all trailing spaces {{{2
" from a string.
function! s:StripTrailingSpaces(string)
@@ -52,7 +26,7 @@ function! s:StripTrailingSpaces(string)
endfunction
function! s:Padding(string, length, where) "{{{3
let gap_length = a:length - s:Strlen(a:string)
let gap_length = a:length - tablemode#utils#StrDisplayWidth(a:string)
if a:where =~# 'l'
return a:string . repeat(" ", gap_length)
elseif a:where =~# 'r'
@@ -119,7 +93,7 @@ endfunction
function! tablemode#align#alignments(lnum, ncols) "{{{2
let alignments = []
if tablemode#table#IsHeader(a:lnum+1)
if tablemode#table#IsBorder(a:lnum+1)
let hcols = tablemode#align#Split(getline(a:lnum+1), '[' . g:table_mode_corner . g:table_mode_corner_corner . ']')
for idx in range(len(hcols))
" Right align if header
@@ -154,9 +128,9 @@ function! tablemode#align#Align(lines) "{{{2
if len(stext) <= 1 | continue | endif
for i in range(len(stext))
if i == len(maxes)
let maxes += [ s:Strlen(stext[i]) ]
let maxes += [ tablemode#utils#StrDisplayWidth(stext[i]) ]
else
let maxes[i] = max([ maxes[i], s:Strlen(stext[i]) ])
let maxes[i] = max([ maxes[i], tablemode#utils#StrDisplayWidth(stext[i]) ])
endif
endfor
endfor

View File

@@ -50,10 +50,10 @@ function! tablemode#spreadsheet#GetFirstRow(line) "{{{2
if tablemode#table#IsRow(a:line)
let line = tablemode#utils#line(a:line)
while tablemode#table#IsRow(line - 1) || tablemode#table#IsHeader(line - 1)
while tablemode#table#IsRow(line - 1) || tablemode#table#IsBorder(line - 1)
let line -= 1
endwhile
if tablemode#table#IsHeader(line) | let line += 1 | endif
if tablemode#table#IsBorder(line) | let line += 1 | endif
return line
endif
@@ -69,10 +69,10 @@ function! tablemode#spreadsheet#GetLastRow(line) "{{{2
if tablemode#table#IsRow(a:line)
let line = tablemode#utils#line(a:line)
while tablemode#table#IsRow(line + 1) || tablemode#table#IsHeader(line + 1)
while tablemode#table#IsRow(line + 1) || tablemode#table#IsBorder(line + 1)
let line += 1
endwhile
if tablemode#table#IsHeader(line) | let line -= 1 | endif
if tablemode#table#IsBorder(line) | let line -= 1 | endif
return line
endif
@@ -89,7 +89,7 @@ function! tablemode#spreadsheet#LineNr(row) "{{{2
let line = tablemode#spreadsheet#GetFirstRow('.')
let row_nr = 0
while tablemode#table#IsRow(line + 1) || tablemode#table#IsHeader(line + 1)
while tablemode#table#IsRow(line + 1) || tablemode#table#IsBorder(line + 1)
if tablemode#table#IsRow(line)
let row_nr += 1
if row ==# row_nr | break | endif
@@ -105,7 +105,7 @@ function! tablemode#spreadsheet#RowNr(line) "{{{2
let line = tablemode#utils#line(a:line)
let rowNr = 0
while tablemode#table#IsRow(line) || tablemode#table#IsHeader(line)
while tablemode#table#IsRow(line) || tablemode#table#IsBorder(line)
if tablemode#table#IsRow(line) | let rowNr += 1 | endif
let line -= 1
endwhile
@@ -117,13 +117,13 @@ function! tablemode#spreadsheet#RowCount(line) "{{{2
let line = tablemode#utils#line(a:line)
let [tline, totalRowCount] = [line, 0]
while tablemode#table#IsRow(tline) || tablemode#table#IsHeader(tline)
while tablemode#table#IsRow(tline) || tablemode#table#IsBorder(tline)
if tablemode#table#IsRow(tline) | let totalRowCount += 1 | endif
let tline -= 1
endwhile
let tline = line + 1
while tablemode#table#IsRow(tline) || tablemode#table#IsHeader(tline)
while tablemode#table#IsRow(tline) || tablemode#table#IsBorder(tline)
if tablemode#table#IsRow(tline) | let totalRowCount += 1 | endif
let tline += 1
endwhile
@@ -166,27 +166,27 @@ function! tablemode#spreadsheet#MoveToStartOfCell() "{{{2
endif
endfunction
function! tablemode#spreadsheet#GetLastRow(line) "{{{2
function! tablemode#spreadsheet#GetFirstRow(line) "{{{2
if tablemode#table#IsRow(a:line)
let line = tablemode#utils#line(a:line)
while tablemode#table#IsRow(line + 1) || tablemode#table#IsHeader(line + 1)
let line += 1
while tablemode#table#IsRow(line - 1) || tablemode#table#IsBorder(line - 1)
let line -= 1
endwhile
if tablemode#table#IsHeader(line) | let line -= 1 | endif
if tablemode#table#IsBorder(line) | let line += 1 | endif
return line
endif
endfunction
function! tablemode#spreadsheet#GetFirstRow(line) "{{{2
function! tablemode#spreadsheet#GetLastRow(line) "{{{2
if tablemode#table#IsRow(a:line)
let line = tablemode#utils#line(a:line)
while tablemode#table#IsRow(line - 1) || tablemode#table#IsHeader(line - 1)
let line -= 1
while tablemode#table#IsRow(line + 1) || tablemode#table#IsBorder(line + 1)
let line += 1
endwhile
if tablemode#table#IsHeader(line) | let line += 1 | endif
if tablemode#table#IsBorder(line) | let line -= 1 | endif
return line
endif

View File

@@ -85,7 +85,7 @@ function! tablemode#spreadsheet#cell#GetCells(line, ...) abort
if row == 0
let values = []
let line = first_row
while tablemode#table#IsRow(line) || tablemode#table#IsHeader(line)
while tablemode#table#IsRow(line) || tablemode#table#IsBorder(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)]
call add(values, tablemode#utils#strip(get(split(row_line, g:table_mode_separator), colm>0?colm-1:colm, '')))
@@ -97,7 +97,7 @@ function! tablemode#spreadsheet#cell#GetCells(line, ...) abort
let row_nr = 0
let row_diff = row > 0 ? 1 : -1
let line = row > 0 ? first_row : last_row
while tablemode#table#IsRow(line) || tablemode#table#IsHeader(line)
while tablemode#table#IsRow(line) || tablemode#table#IsBorder(line)
if tablemode#table#IsRow(line)
let row_nr += row_diff
if row ==# row_nr | break | endif
@@ -127,10 +127,27 @@ function! tablemode#spreadsheet#cell#GetCell(...) "{{{2
endfunction
function! tablemode#spreadsheet#cell#GetRow(row, ...) abort "{{{2
let line = a:0 < 1 ? '.' : a:1
let line = a:0 ? a:1 : '.'
return tablemode#spreadsheet#cell#GetCells(line, a:row)
endfunction
function! tablemode#spreadsheet#cell#GetRowColumn(col, ...) abort "{{{2
let line = a:0 ? a:1 : '.'
let row = tablemode#spreadsheet#RowNr('.')
return tablemode#spreadsheet#cell#GetCells(line, row, a:col)
endfunction
function! tablemode#spreadsheet#cell#GetColumn(col, ...) abort "{{{2
let line = a:0 ? a:1 : '.'
return tablemode#spreadsheet#cell#GetCells(line, 0, a:col)
endfunction
function! tablemode#spreadsheet#cell#GetColumnRow(row, ...) abort "{{{2
let line = a:0 ? a:1 : '.'
let col = tablemode#spreadsheet#ColumnNr('.')
return tablemode#spreadsheet#cell#GetCells(line, a:row, col)
endfunction
function! tablemode#spreadsheet#cell#GetCellRange(range, ...) abort "{{{2
if a:0 < 1
let [line, colm] = ['.', tablemode#spreadsheet#ColumnNr('.')]
@@ -169,23 +186,6 @@ function! tablemode#spreadsheet#cell#GetCellRange(range, ...) abort "{{{2
return values
endfunction
function! tablemode#spreadsheet#cell#GetRowColumn(col, ...) abort "{{{2
let line = a:0 < 1 ? '.' : a:1
let row = tablemode#spreadsheet#RowNr('.')
return tablemode#spreadsheet#cell#GetCells(line, row, a:col)
endfunction
function! tablemode#spreadsheet#cell#GetColumn(col, ...) abort "{{{2
let line = a:0 < 1 ? '.' : a:1
return tablemode#spreadsheet#cell#GetCells(line, 0, a:col)
endfunction
function! tablemode#spreadsheet#cell#GetColumnRow(row, ...) abort "{{{2
let line = a:0 < 1 ? '.' : a:1
let col = tablemode#spreadsheet#ColumnNr('.')
return tablemode#spreadsheet#cell#GetCells(line, a:row, col)
endfunction
function! tablemode#spreadsheet#cell#SetCell(val, ...) "{{{2
if a:0 == 0
let [line, row, colm] = ['.', tablemode#spreadsheet#RowNr('.'), tablemode#spreadsheet#ColumnNr('.')]
@@ -233,7 +233,7 @@ function! tablemode#spreadsheet#cell#Motion(direction, ...) "{{{2
for ii in range(l:count)
if a:direction ==# 'l'
if tablemode#spreadsheet#IsLastCell()
if !tablemode#table#IsRow(line('.') + 1) && (tablemode#table#IsHeader(line('.') + 1) && !tablemode#table#IsRow(line('.') + 2))
if !tablemode#table#IsRow(line('.') + 1) && (tablemode#table#IsBorder(line('.') + 1) && !tablemode#table#IsRow(line('.') + 2))
return
endif
call tablemode#spreadsheet#cell#Motion('j', 1)
@@ -248,7 +248,7 @@ function! tablemode#spreadsheet#cell#Motion(direction, ...) "{{{2
endif
elseif a:direction ==# 'h'
if tablemode#spreadsheet#IsFirstCell()
if !tablemode#table#IsRow(line('.') - 1) && (tablemode#table#IsHeader(line('.') - 1) && !tablemode#table#IsRow(line('.') - 2))
if !tablemode#table#IsRow(line('.') - 1) && (tablemode#table#IsBorder(line('.') - 1) && !tablemode#table#IsRow(line('.') - 2))
return
endif
call tablemode#spreadsheet#cell#Motion('k', 1)
@@ -265,7 +265,7 @@ function! tablemode#spreadsheet#cell#Motion(direction, ...) "{{{2
if tablemode#table#IsRow(line('.') + 1)
" execute 'normal! ' . 1 . 'j'
normal! j
elseif tablemode#table#IsHeader(line('.') + 1) && tablemode#table#IsRow(line('.') + 2)
elseif tablemode#table#IsBorder(line('.') + 1) && tablemode#table#IsRow(line('.') + 2)
" execute 'normal! ' . 2 . 'j'
normal! 2j
endif
@@ -273,7 +273,7 @@ function! tablemode#spreadsheet#cell#Motion(direction, ...) "{{{2
if tablemode#table#IsRow(line('.') - 1)
" execute 'normal! ' . 1 . 'k'
normal! k
elseif tablemode#table#IsHeader(line('.') - 1) && tablemode#table#IsRow(line('.') - 2)
elseif tablemode#table#IsBorder(line('.') - 1) && tablemode#table#IsRow(line('.') - 2)
" execute 'normal! ' . (1 + 1) . 'k'
normal! 2k
endif

View File

@@ -39,7 +39,7 @@ function! tablemode#spreadsheet#formula#Add(...) "{{{2
if fr !=# ''
let fr = '$' . row . ',' . colm . '=' . fr
let fline = tablemode#spreadsheet#GetLastRow('.') + 1
if tablemode#table#IsHeader(fline) | let fline += 1 | endif
if tablemode#table#IsBorder(fline) | let fline += 1 | endif
let cursor_pos = [line('.'), col('.')]
if getline(fline) =~# 'tmf: '
" Comment line correctly
@@ -129,13 +129,13 @@ function! tablemode#spreadsheet#formula#EvaluateFormulaLine() abort "{{{2
if tablemode#table#IsRow('.') " We're inside the table
let line = tablemode#spreadsheet#GetLastRow('.')
let fline = line + 1
if tablemode#table#IsHeader(fline) | let fline += 1 | endif
if tablemode#table#IsBorder(fline) | let fline += 1 | endif
if getline(fline) =~# 'tmf: '
let exprs = split(matchstr(getline(fline), matchexpr), ';')
endif
elseif getline('.') =~# 'tmf: ' " We're on the formula line
let line = line('.') - 1
if tablemode#table#IsHeader(line) | let line -= 1 | endif
if tablemode#table#IsBorder(line) | let line -= 1 | endif
if tablemode#table#IsRow(line)
let exprs = split(matchstr(getline('.'), matchexpr), ';')
endif

View File

@@ -47,7 +47,9 @@ function! s:GenerateHeaderBorder(line) "{{{2
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(border, '[^' . g:table_mode_corner . ']', g:table_mode_fillchar, 'g')
" To accurately deal with unicode double width characters
let fill_columns = map(split(border, g:table_mode_corner), 'repeat(g:table_mode_fillchar, tablemode#utils#StrDisplayWidth(v:val))')
let border = g:table_mode_corner . join(fill_columns, g:table_mode_corner) . g:table_mode_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
@@ -94,24 +96,6 @@ function! tablemode#table#scope() "{{{2
return s:
endfunction
function! tablemode#table#StartCommentExpr() "{{{2
let cstartexpr = tablemode#table#GetCommentStart()
if tablemode#utils#strlen(cstartexpr) > 0
return '^\s*' . cstartexpr . '\s*'
else
return ''
endif
endfunction
function! tablemode#table#EndCommentExpr() "{{{2
let cendexpr = tablemode#table#GetCommentEnd()
if tablemode#utils#strlen(cendexpr) > 0
return '.*\zs\s\+' . cendexpr . '\s*$'
else
return ''
endif
endfunction
function! tablemode#table#GetCommentStart() "{{{2
let cstring = &commentstring
if tablemode#utils#strlen(cstring) > 0
@@ -121,6 +105,15 @@ function! tablemode#table#GetCommentStart() "{{{2
endif
endfunction
function! tablemode#table#StartCommentExpr() "{{{2
let cstartexpr = tablemode#table#GetCommentStart()
if tablemode#utils#strlen(cstartexpr) > 0
return '^\s*' . cstartexpr . '\s*'
else
return ''
endif
endfunction
function! tablemode#table#GetCommentEnd() "{{{2
let cstring = &commentstring
if tablemode#utils#strlen(cstring) > 0
@@ -135,6 +128,15 @@ function! tablemode#table#GetCommentEnd() "{{{2
endif
endfunction
function! tablemode#table#EndCommentExpr() "{{{2
let cendexpr = tablemode#table#GetCommentEnd()
if tablemode#utils#strlen(cendexpr) > 0
return '.*\zs\s\+' . cendexpr . '\s*$'
else
return ''
endif
endfunction
function! tablemode#table#StartExpr() "{{{2
let cstart = tablemode#table#GetCommentStart()
if tablemode#utils#strlen(cstart) > 0
@@ -153,15 +155,20 @@ function! tablemode#table#EndExpr() "{{{2
endif
endfunction
function! tablemode#table#IsHeader(line) "{{{2
function! tablemode#table#IsBorder(line) "{{{2
return getline(a:line) =~# s:HeaderBorderExpr()
endfunction
function! tablemode#table#IsRow(line) "{{{2
return !tablemode#table#IsHeader(a:line) && getline(a:line) =~# (tablemode#table#StartExpr() . g:table_mode_separator)
function! tablemode#table#IsHeader(line) "{{{2
let line = tablemode#utils#line(a:line)
return tablemode#table#IsBorder(line+1) && !tablemode#table#IsRow(line-1) && !tablemode#table#IsRow(line-2)
endfunction
function! tablemode#table#AddHeaderBorder(line) "{{{2
function! tablemode#table#IsRow(line) "{{{2
return !tablemode#table#IsBorder(a:line) && getline(a:line) =~# (tablemode#table#StartExpr() . g:table_mode_separator)
endfunction
function! tablemode#table#AddBorder(line) "{{{2
call setline(a:line, s:GenerateHeaderBorder(a:line))
endfunction
@@ -170,8 +177,8 @@ function! tablemode#table#Realign(line) "{{{2
let lines = []
let [lnum, blines] = [line, []]
while tablemode#table#IsRow(lnum) || tablemode#table#IsHeader(lnum)
if tablemode#table#IsHeader(lnum)
while tablemode#table#IsRow(lnum) || tablemode#table#IsBorder(lnum)
if tablemode#table#IsBorder(lnum)
call insert(blines, lnum)
let lnum -= 1
continue
@@ -181,8 +188,8 @@ function! tablemode#table#Realign(line) "{{{2
endwhile
let lnum = line + 1
while tablemode#table#IsRow(lnum) || tablemode#table#IsHeader(lnum)
if tablemode#table#IsHeader(lnum)
while tablemode#table#IsRow(lnum) || tablemode#table#IsBorder(lnum)
if tablemode#table#IsBorder(lnum)
call add(blines, lnum)
let lnum += 1
continue
@@ -198,6 +205,6 @@ function! tablemode#table#Realign(line) "{{{2
endfor
for bline in blines
call tablemode#table#AddHeaderBorder(bline)
call tablemode#table#AddBorder(bline)
endfor
endfunction

View File

@@ -51,3 +51,27 @@ endfunction
function! tablemode#utils#strlen(text)
return strlen(substitute(a:text, '.', 'x', 'g'))
endfunction
function! tablemode#utils#StrDisplayWidth(string) "{{{2
if exists('*strdisplaywidth')
return strdisplaywidth(a:string)
else
" Implement the tab handling part of strdisplaywidth for vim 7.2 and
" earlier - not much that can be done about handling doublewidth
" characters.
let rv = 0
let i = 0
for char in split(a:string, '\zs')
if char == "\t"
let rv += &ts - i
let i = 0
else
let rv += 1
let i = (i + 1) % &ts
endif
endfor
return rv
endif
endfunction

View File

@@ -1,7 +1,7 @@
*table-mode.txt* Table Mode for easy table formatting. v4.4.0
*table-mode.txt* Table Mode for easy table formatting. v4.4.1
===============================================================================
Table Mode, THE AWESOME AUTOMATIC TABLE CREATOR & FORMATTER
VERSION 4.4.0
VERSION 4.4.1
Author: Dhruva Sagar <http://dhruvasagar.com/>
License: MIT <http://opensource.org/licenses/MIT/>
@@ -119,7 +119,7 @@ Formula Expressions :
special functions 'Sum' and 'Average'. Both these functions take a
range as input. A range can be of two forms :
'n,m': This represents cells in the current column from row
'n:m': This represents cells in the current column from row
'n' through 'm'. If 'm' is negative it represents 'm' row
above the current row (of the target cell).

View File

@@ -50,6 +50,23 @@ function! s:TableEchoCell() "{{{1
endif
endfunction
function! s:EnableTableSyntax() "{{{1
syntax match Table /^\s*|.\+|\s*$/ contains=TableBorder,TableSeparator,TableColumnAlign containedin=ALL
syntax match TableSeparator /|/ contained
syntax match TableColumnAlign /:/ contained
syntax match TableBorder /[\-+]\+/ contained
endfunction
augroup TableMode
au!
autocmd Syntax * call <SID>EnableTableSyntax()
augroup END
hi! link TableBorder Delimiter
hi! link TableSeparator Delimiter
hi! link TableColumnAlign Type
" Define Commands & Mappings {{{1
if !g:table_mode_always_active "{{{2
exec "nnoremap <silent> " . g:table_mode_map_prefix . g:table_mode_toggle_map .

View File

@@ -10,7 +10,7 @@ describe 'cell'
read t/fixtures/sample.txt
end
it 'should return the cells'
it 'should return the cells with GetCells'
Expect tablemode#spreadsheet#cell#GetCells(2, 1, 1) ==# 'test11'
" Get Rows
Expect tablemode#spreadsheet#cell#GetCells(2, 1) == ['test11', 'test12']
@@ -20,7 +20,17 @@ describe 'cell'
Expect tablemode#spreadsheet#cell#GetCells(2, 0, 2) == ['test12', 'test22']
end
it 'should return the cells in a range'
it 'should return the row with GetRow'
Expect tablemode#spreadsheet#cell#GetRow(1, 2) == ['test11', 'test12']
Expect tablemode#spreadsheet#cell#GetRow(2, 2) == ['test21', 'test22']
end
it 'should return the column with GetColumn'
Expect tablemode#spreadsheet#cell#GetColumn(1, 2) == ['test11', 'test21']
Expect tablemode#spreadsheet#cell#GetColumn(2, 2) == ['test12', 'test22']
end
it 'should return the cells in a range with GetCellRange'
" Entire table as range
Expect tablemode#spreadsheet#cell#GetCellRange('1,1:2,2', 2, 1) == [['test11', 'test21'], ['test12', 'test22']]

View File

@@ -0,0 +1,2 @@
| 1 | 2 |
| 3 | 4 |

View File

@@ -0,0 +1,4 @@
| test11 | test12 |
| test21 | test22 |

View File

@@ -0,0 +1,3 @@
| test11 | test12 |
| test21 | test22 |

View File

@@ -0,0 +1,9 @@
| abc | 测试长度 | 长测试 |
| 长 | 测试测试测试测试 | 测试测试 |
| 测试测试 | 测试 | 测试测测试 |
| 测试测试测试 | 测试测试 | 测试 |

View File

@@ -0,0 +1,4 @@
| Title | Message |
|------:+--------:|
| t1 | msg1 |
| t2 | msg2 |

View File

@@ -0,0 +1,4 @@
| Title | Message |
|------:+--------:|
| t1 | msg1 |
| t2 | msg2 |

View File

@@ -0,0 +1,8 @@
|--------------+------------------+------------|
| 测试测试 | 测试长度 | 长测试 |
|--------------+------------------+-----------:|
| abc | 测试长度 | 长测试 |
| 长 | 测试测试测试测试 | 测试测试 |
| 测试测试 | 测试 | 测试测测试 |
| 测试测试测试 | 测试测试 | 测试 |
|--------------+------------------+------------|

View File

@@ -0,0 +1,8 @@
|--------+--------+------|
|测试测试|测试长度|长测试|
|--------+--------+-----:|
|abc|测试长度|长测试|
|长|测试测试测试测试|测试测试|
|测试测试|测试|测试测测试|
|测试测试测试|测试测试|测试|
|------------+--------+----|

View File

@@ -0,0 +1,2 @@
| test11 | test12 |
| test21 | test22 |

View File

@@ -0,0 +1,2 @@
|test11|test12|
|test21|test22|

View File

@@ -0,0 +1,4 @@
| abc | 测试长度 | 长测试 |
| 长 | 测试测试测试测试 | 测试测试 |
| 测试测试 | 测试 | 测试测测试 |
| 测试测试测试 | 测试测试 | 测试 |

View File

@@ -0,0 +1,4 @@
|abc|测试长度|长测试|
|长|测试测试测试测试|测试测试|
|测试测试|测试|测试测测试|
|测试测试测试|测试测试|测试|

View File

@@ -1,4 +1,4 @@
|--------+---------|
| Title | Message |
|--------+---------|
| test11 | test12 |

View File

@@ -25,6 +25,27 @@ describe 'spreadsheet'
Expect tablemode#spreadsheet#ColumnCount(3) == 2
end
it 'should return the column number'
call cursor(2,3)
Expect tablemode#spreadsheet#ColumnNr('.') == 1
call cursor(2,12)
Expect tablemode#spreadsheet#ColumnNr('.') == 2
end
it 'should return true when in the first cell'
call cursor(2,3)
Expect tablemode#spreadsheet#IsFirstCell() to_be_true
call cursor(2,12)
Expect tablemode#spreadsheet#IsFirstCell() to_be_false
end
it 'should return true when in the last cell'
call cursor(2,3)
Expect tablemode#spreadsheet#IsLastCell() to_be_false
call cursor(2,12)
Expect tablemode#spreadsheet#IsLastCell() to_be_true
end
it 'should return the line number of the first row'
Expect tablemode#spreadsheet#GetFirstRow(2) == 2
Expect tablemode#spreadsheet#GetFirstRow(3) == 2
@@ -34,11 +55,40 @@ describe 'spreadsheet'
Expect tablemode#spreadsheet#GetLastRow(2) == 3
Expect tablemode#spreadsheet#GetLastRow(3) == 3
end
describe 'Math'
before
new
read t/fixtures/cell/sample.txt
end
it 'should return the sum of cell range'
call cursor(1,3)
Expect tablemode#spreadsheet#Sum('1:2') == 4.0
Expect tablemode#spreadsheet#Sum('1,1:1,2') == 3.0
Expect tablemode#spreadsheet#Sum('1,1:2,2') == 10.0
call cursor(2,7)
Expect tablemode#spreadsheet#Sum('1:2') == 6.0
Expect tablemode#spreadsheet#Sum('2,1:2,2') == 7.0
end
it 'should return the average of cell range'
call cursor(1,3)
Expect tablemode#spreadsheet#Average('1:2') == 2.0
Expect tablemode#spreadsheet#Average('1,1:1,2') == 1.5
Expect tablemode#spreadsheet#Average('1,1:2,2') == 5.0
call cursor(2,7)
Expect tablemode#spreadsheet#Average('1:2') == 3.0
Expect tablemode#spreadsheet#Average('2,1:2,2') == 3.5
end
end
end
describe 'Manipulations'
before
new
normal! ggdG
call tablemode#Enable()
normal i|test11|test12|
|test21|test22|
call cursor(1, 3)

View File

@@ -4,40 +4,159 @@ source t/config/options.vim
call vspec#hint({'scope': 'tablemode#table#scope()', 'sid': 'tablemode#table#sid()'})
describe 'table'
describe 'API'
describe 'IsRow'
describe 'IsRow'
before
new
normal! ggdG
read t/fixtures/table/sample.txt
end
it 'should be true when on a table row'
Expect tablemode#table#IsRow(2) to_be_true
Expect tablemode#table#IsRow(3) to_be_true
end
it 'should be false when not on a table row'
Expect tablemode#table#IsRow(1) to_be_false
Expect tablemode#table#IsRow(4) to_be_false
end
end
describe 'IsBorder'
before
new
normal! ggdG
read t/fixtures/table/sample_with_header.txt
end
it 'should be true on a table border'
Expect tablemode#table#IsBorder(1) to_be_true
Expect tablemode#table#IsBorder(3) to_be_true
Expect tablemode#table#IsBorder(6) to_be_true
end
it 'should be false when not on a table border'
Expect tablemode#table#IsBorder(2) to_be_false
Expect tablemode#table#IsBorder(4) to_be_false
Expect tablemode#table#IsBorder(5) to_be_false
end
end
describe 'IsHeader'
before
new
normal! ggdG
read t/fixtures/table/sample_with_header.txt
end
it 'should be true on the table header'
Expect tablemode#table#IsHeader(2) to_be_true
end
it 'should be false anywhere else'
Expect tablemode#table#IsHeader(1) to_be_false
Expect tablemode#table#IsHeader(4) to_be_false
Expect tablemode#table#IsHeader(5) to_be_false
Expect tablemode#table#IsHeader(6) to_be_false
Expect tablemode#table#IsHeader(7) to_be_false
end
end
describe 'AddBorder'
before
new
normal! ggdG
read t/fixtures/table/sample_for_header.txt
end
it 'should add border to line'
call tablemode#table#AddBorder(2)
Expect tablemode#table#IsHeader(1) to_be_true
Expect tablemode#table#IsBorder(2) to_be_true
end
describe 'for unicode'
before
new
read t/fixtures/sample.txt
normal! ggdG
read t/fixtures/table/sample_for_header_unicode.txt
end
it 'should return true when inside a table'
Expect tablemode#table#IsRow(2) to_be_true
Expect tablemode#table#IsRow(3) to_be_true
it 'should add border to line'
call tablemode#table#AddBorder(1)
call tablemode#table#AddBorder(3)
call tablemode#table#AddBorder(5)
call tablemode#table#AddBorder(7)
call tablemode#table#AddBorder(9)
Expect tablemode#table#IsBorder(1) to_be_true
Expect tablemode#utils#StrDisplayWidth(getline(1)) == tablemode#utils#StrDisplayWidth(getline(2))
Expect tablemode#table#IsBorder(3) to_be_true
Expect tablemode#utils#StrDisplayWidth(getline(3)) == tablemode#utils#StrDisplayWidth(getline(4))
Expect tablemode#table#IsBorder(5) to_be_true
Expect tablemode#utils#StrDisplayWidth(getline(5)) == tablemode#utils#StrDisplayWidth(getline(6))
Expect tablemode#table#IsBorder(7) to_be_true
Expect tablemode#utils#StrDisplayWidth(getline(7)) == tablemode#utils#StrDisplayWidth(getline(8))
Expect tablemode#table#IsBorder(9) to_be_true
Expect tablemode#utils#StrDisplayWidth(getline(9)) == tablemode#utils#StrDisplayWidth(getline(8))
end
end
end
describe 'Realign'
describe 'without header alignments'
describe 'for simple'
before
new
normal! ggdG
read t/fixtures/table/sample_realign_before.txt
end
it 'should be aligned properly'
call tablemode#table#Realign(1)
Expect getline(1,'$') == readfile('t/fixtures/table/sample_realign_after.txt')
end
end
it 'should return false when outside a table'
Expect tablemode#table#IsRow(1) to_be_false
Expect tablemode#table#IsRow(4) to_be_false
describe 'for unicode'
before
new
normal! ggdG
read t/fixtures/table/sample_realign_unicode_before.txt
end
it 'should be aligned properly'
call tablemode#table#Realign(1)
Expect getline(1,'$') == readfile('t/fixtures/table/sample_realign_unicode_after.txt')
end
end
end
describe 'IsHeader'
before
new
read t/fixtures/sample_with_header.txt
describe 'with header alignments'
describe 'for simple'
before
new
normal! ggdG
read t/fixtures/table/sample_header_realign_before.txt
end
it 'should be aligned properly'
call tablemode#table#Realign(1)
Expect getline(1,'$') == readfile('t/fixtures/table/sample_header_realign_after.txt')
end
end
it 'should return true when on a table header'
Expect tablemode#table#IsHeader(3) to_be_true
Expect tablemode#table#IsHeader(6) to_be_true
end
describe 'for unicode'
before
new
normal! ggdG
read t/fixtures/table/sample_header_realign_unicode_before.txt
end
it 'should return false when not on a table header'
Expect tablemode#table#IsHeader(1) to_be_false
Expect tablemode#table#IsHeader(2) to_be_false
Expect tablemode#table#IsHeader(4) to_be_false
Expect tablemode#table#IsHeader(5) to_be_false
it 'should be aligned properly'
call tablemode#table#Realign(1)
Expect getline(1,'$') == readfile('t/fixtures/table/sample_header_realign_unicode_after.txt')
end
end
end
end

View File

@@ -3,31 +3,45 @@ source t/config/options.vim
call vspec#hint({'scope': 'tablemode#utils#scope()', 'sid': 'tablemode#utils#sid()'})
describe 'line'
it 'should return the current line number'
Expect tablemode#utils#line('.') == line('.')
describe 'utils'
describe 'line'
it 'should return the current line number'
Expect tablemode#utils#line('.') == line('.')
end
it 'should return the line number itself if it is a number'
Expect tablemode#utils#line(1) == 1
end
end
it 'should return the line number itself if it is a number'
Expect tablemode#utils#line(1) == 1
end
end
describe 'strip'
it 'should strip all initial or trailing whitespace from a string'
let string = ' This is awesome '
Expect tablemode#utils#strip(string) == 'This is awesome'
end
end
describe 'strlen'
it 'should return the length of a string correctly'
let string = 'this is a test'
Expect tablemode#utils#strlen(string) == 14
end
it 'should return the length of a unicode string correctly'
let string = '測試 is good.'
Expect tablemode#utils#strlen(string) == 11
describe 'strip'
it 'should strip all initial or trailing whitespace from a string'
let string = ' This is awesome '
Expect tablemode#utils#strip(string) == 'This is awesome'
end
end
describe 'strlen'
it 'should return the length of a string correctly'
let string = 'this is a test'
Expect tablemode#utils#strlen(string) == 14
end
it 'should return the length of a unicode string correctly'
let string = '測試 is good.'
Expect tablemode#utils#strlen(string) == 11
end
end
describe 'strdisplaywidth'
it 'should return the display width of a string correctly'
let string = 'this is a test'
Expect tablemode#utils#StrDisplayWidth(string) == 14
end
it 'should return the display width of a unicode string correctly'
let string = '測試 is good.'
Expect tablemode#utils#StrDisplayWidth(string) == 13
end
end
end