Fixing tabular align for unicode characters

This commit is contained in:
Dhruva Sagar
2014-04-07 15:00:34 +05:30
parent de2039dd24
commit 70744f308c
3 changed files with 80 additions and 3 deletions

View File

@@ -1,5 +1,31 @@
" Borrowed from Tabular " Borrowed from Tabular
" Private Functions {{{1 " 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 " function! s:StripTrailingSpaces(string) - Remove all trailing spaces {{{2
" from a string. " from a string.
function! s:StripTrailingSpaces(string) function! s:StripTrailingSpaces(string)
@@ -7,7 +33,7 @@ function! s:StripTrailingSpaces(string)
endfunction endfunction
function! s:Padding(string, length, where) "{{{3 function! s:Padding(string, length, where) "{{{3
let gap_length = a:length - tablemode#utils#strlen(a:string) let gap_length = a:length - s:Strlen(a:string)
if a:where =~# 'l' if a:where =~# 'l'
return a:string . repeat(" ", gap_length) return a:string . repeat(" ", gap_length)
elseif a:where =~# 'r' elseif a:where =~# 'r'
@@ -93,9 +119,9 @@ function! tablemode#align#Align(lines) "{{{2
if len(line) <= 1 | continue | endif if len(line) <= 1 | continue | endif
for i in range(len(line)) for i in range(len(line))
if i == len(maxes) if i == len(maxes)
let maxes += [ tablemode#utils#strlen(line[i]) ] let maxes += [ s:Strlen(line[i]) ]
else else
let maxes[i] = max([ maxes[i], tablemode#utils#strlen(line[i]) ]) let maxes[i] = max([ maxes[i], s:Strlen(line[i]) ])
endif endif
endfor endfor
endfor endfor

18
t/align.vim Normal file
View File

@@ -0,0 +1,18 @@
" vim: fdm=indent
source t/config.vim
call vspec#hint({'scope': 'tablemode#align#scope()', 'sid': 'tablemode#align#sid()'})
describe 'Align'
it 'should align table content correctly'
let lines = ['| This | is a | table |', '| This | is also | a table |']
let result = ['| This | is a | table |', '| This | is also | a table |']
Expect tablemode#align#Align(lines) == result
end
it 'should align table content with unicode characters correctly'
let lines = ['| This | is 測試 | table |', '| This | is also | a table |']
let result = ['| This | is 測試 | table |', '| This | is also | a table |']
Expect tablemode#align#Align(lines) == result
end
end

33
t/utils.vim Normal file
View File

@@ -0,0 +1,33 @@
" vim: fdm=indent
source t/config.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('.')
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
end
end