From 4929841a7a1ebd362309f822d79f4d7941d50219 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Mon, 20 Jan 2014 01:30:31 +0900 Subject: [PATCH] Remove +float dependency --- autoload/easy_align.vim | 40 +++++++++++++++++++++++++--------------- plugin/easy_align.vim | 6 +++++- 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/autoload/easy_align.vim b/autoload/easy_align.vim index 98661b9..d5a2576 100644 --- a/autoload/easy_align.vim +++ b/autoload/easy_align.vim @@ -77,6 +77,14 @@ else endfunction endif +function! s:ceil2(v) + return a:v % 2 == 0 ? a:v : a:v + 1 +endfunction + +function! s:floor2(v) + return a:v % 2 == 0 ? a:v : a:v - 1 +endfunction + function! s:highlighted_as(line, col, groups) if empty(a:groups) | return 0 | endif let hl = synIDattr(synID(a:line, a:col, 0), 'name') @@ -335,7 +343,7 @@ function! s:do_align(todo, modes, all_tokens, all_delims, fl, ll, fc, lc, nth, r let mode = a:modes[0] let lines = {} let min_indent = -1 - let max = { 'pivot_len': str2float('0.0'), 'token_len': 0, 'just_len': 0, 'delim_len': 0, + let max = { 'pivot_len2': 0, 'token_len': 0, 'just_len': 0, 'delim_len': 0, \ 'indent': 0, 'tokens': 0, 'strip_len': 0 } let d = a:dict let [f, fx] = s:parse_filter(d.filter) @@ -413,8 +421,9 @@ function! s:do_align(todo, modes, all_tokens, all_delims, fl, ll, fc, lc, nth, r let max.delim_len = max([max.delim_len, s:strwidth(delim)]) if mode ==? 'c' - if max.pivot_len < pw + tw / 2.0 - let max.pivot_len = pw + tw / 2.0 + let pivot_len2 = pw * 2 + tw + if max.pivot_len2 < pivot_len2 + let max.pivot_len2 = pivot_len2 endif let max.strip_len = max([max.strip_len, s:strwidth(s:trim(token))]) endif @@ -435,9 +444,9 @@ function! s:do_align(todo, modes, all_tokens, all_delims, fl, ll, fc, lc, nth, r end if idt !=? 'k' - let max.just_len = 0 - let max.token_len = 0 - let max.pivot_len = 0 + let max.just_len = 0 + let max.token_len = 0 + let max.pivot_len2 = 0 for [line, elems] in items(lines) let [nth, prefix, token, delim] = elems @@ -463,8 +472,9 @@ function! s:do_align(todo, modes, all_tokens, all_delims, fl, ll, fc, lc, nth, r let max.token_len = max([max.token_len, tw]) let max.just_len = max([max.just_len, pw + tw]) if mode ==? 'c' - if max.pivot_len < pw + tw / 2.0 - let max.pivot_len = pw + tw / 2.0 + let pivot_len2 = pw * 2 + tw + if max.pivot_len2 < pivot_len2 + let max.pivot_len2 = pivot_len2 endif endif @@ -499,15 +509,15 @@ function! s:do_align(todo, modes, all_tokens, all_delims, fl, ll, fc, lc, nth, r let indent = matchstr(token, '^\s*') let token = indent . pad . s:ltrim(token) elseif mode ==? 'c' - let p1 = max.pivot_len - (pw + tw / 2.0) - let p2 = (max.token_len - tw) / 2.0 - let pf1 = floor(p1) - if pf1 < p1 | let p2 = ceil(p2) - else | let p2 = floor(p2) + let p1 = max.pivot_len2 - (pw * 2 + tw) + let p2 = max.token_len - tw + let pf1 = s:floor2(p1) + if pf1 < p1 | let p2 = s:ceil2(p2) + else | let p2 = s:floor2(p2) endif - let strip = float2nr(ceil((max.token_len - max.strip_len) / 2.0)) + let strip = s:ceil2(max.token_len - max.strip_len) / 2 let indent = matchstr(token, '^\s*') - let token = indent. repeat(' ', float2nr(pf1)) .s:ltrim(token). repeat(' ', float2nr(p2)) + let token = indent. repeat(' ', pf1 / 2) .s:ltrim(token). repeat(' ', p2 / 2) let token = substitute(token, repeat(' ', strip) . '$', '', '') if d.stick_to_left diff --git a/plugin/easy_align.vim b/plugin/easy_align.vim index b5bd79d..9c498da 100644 --- a/plugin/easy_align.vim +++ b/plugin/easy_align.vim @@ -31,8 +31,12 @@ command! -nargs=* -range -bang LiveEasyAlign ,call easy_align#alig let s:last_command = 'EasyAlign' +function! s:abs(v) + return a:v >= 0 ? a:v : - a:v +endfunction + function! s:remember_visual(mode) - let s:last_visual = [a:mode, abs(line("'>") - line("'<")), abs(col("'>") - col("'<"))] + let s:last_visual = [a:mode, s:abs(line("'>") - line("'<")), s:abs(col("'>") - col("'<"))] endfunction function! s:repeat_visual()