Minor refactor & improved test coverage

This commit is contained in:
Dhruva Sagar
2014-05-01 16:35:25 +05:30
parent fa568fe91b
commit dbd79f2c1e
17 changed files with 239 additions and 99 deletions

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

@@ -93,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

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

@@ -96,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
@@ -123,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
@@ -137,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
@@ -155,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
@@ -172,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
@@ -183,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
@@ -200,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

@@ -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']]
@@ -95,7 +105,7 @@ describe 'cell'
describe 'up or down'
before
new
normal! ggdG
normal! ggdG
call tablemode#TableModeEnable()
normal i|test11|test12|
|test21|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,4 @@
| Title | Message |
|------:+--------:|
| t1 | msg1 |
| t2 | msg2 |

View File

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

View File

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

View File

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

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,6 +55,33 @@ 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'

View File

@@ -4,40 +4,96 @@ 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
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
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
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
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
end
describe 'Realign'
describe 'without header alignments'
before
new
read t/fixtures/sample.txt
read t/fixtures/table/sample_realign_before.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
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
it 'should be aligned properly'
call tablemode#table#Realign(1)
Expect getline(1,'$') == readfile('t/fixtures/table/sample_realign_after.txt')
end
end
describe 'IsHeader'
describe 'with header alignments'
before
new
read t/fixtures/sample_with_header.txt
read t/fixtures/table/sample_header_realign_before.txt
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
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_after.txt')
end
end
end