From 6c3c3cb22b13d7a9d08d2877149c9ec59135506a Mon Sep 17 00:00:00 2001 From: Anmol Sethi Date: Wed, 16 Mar 2016 02:54:57 -0400 Subject: [PATCH 1/3] refactored duplicate offset functions Closes #719 --- autoload/go/complete.vim | 16 ++-------------- autoload/go/def.vim | 13 +------------ autoload/go/oracle.vim | 15 +++------------ autoload/go/rename.vim | 11 +---------- autoload/go/util.vim | 15 +++++++++++++++ 5 files changed, 22 insertions(+), 48 deletions(-) diff --git a/autoload/go/complete.vim b/autoload/go/complete.vim index 097422cdc4..f93cd463b8 100644 --- a/autoload/go/complete.vim +++ b/autoload/go/complete.vim @@ -87,23 +87,11 @@ fu! s:gocodeCurrentBufferOpt(filename) return '-in=' . a:filename endf -fu! go#complete#gocodeCursor() - if &encoding != 'utf-8' - let sep = &l:fileformat == 'dos' ? "\r\n" : "\n" - let c = col('.') - let buf = line('.') == 1 ? "" : (join(getline(1, line('.')-1), sep) . sep) - let buf .= c == 1 ? "" : getline('.')[:c-2] - return printf('%d', len(iconv(buf, &encoding, "utf-8"))) - endif - - return printf('%d', line2byte(line('.')) + (col('.')-2)) -endf - fu! s:gocodeAutocomplete() let filename = s:gocodeCurrentBuffer() let result = s:gocodeCommand('autocomplete', \ [s:gocodeCurrentBufferOpt(filename), '-f=vim'], - \ [expand('%:p'), go#complete#gocodeCursor()]) + \ [expand('%:p'), go#util#OffsetCursor()]) call delete(filename) return result endf @@ -149,7 +137,7 @@ function! go#complete#GetInfoFromOffset(offset) endfunction function! go#complete#GetInfo() - let offset = go#complete#gocodeCursor()+1 + let offset = go#util#OffsetCursor()+1 return go#complete#GetInfoFromOffset(offset) endfunction diff --git a/autoload/go/def.vim b/autoload/go/def.vim index b9bda3d503..8fadc840a4 100644 --- a/autoload/go/def.vim +++ b/autoload/go/def.vim @@ -65,18 +65,7 @@ endfunction function! s:getOffset() - let pos = getpos(".")[1:2] - if &encoding == 'utf-8' - let offs = line2byte(pos[0]) + pos[1] - 2 - else - let c = pos[1] - let buf = line('.') == 1 ? "" : (join(getline(1, pos[0] - 1), go#util#LineEnding()) . go#util#LineEnding()) - let buf .= c == 1 ? "" : getline(pos[0])[:c-2] - let offs = len(iconv(buf, &encoding, "utf-8")) - endif - - let argOff = "-o=" . offs - return argOff + return "-o=" . go#util#OffsetCursor() endfunction diff --git a/autoload/go/oracle.vim b/autoload/go/oracle.vim index 6ba0b9580f..490b6b4f49 100644 --- a/autoload/go/oracle.vim +++ b/autoload/go/oracle.vim @@ -62,15 +62,6 @@ func! s:loclistSecond(output) call go#list#Window("locationlist", len(errors)) endfun -func! s:getpos(l, c) - if &encoding != 'utf-8' - let buf = a:l == 1 ? '' : (join(getline(1, a:l-1), "\n") . "\n") - let buf .= a:c == 1 ? '' : getline('.')[:a:c-2] - return len(iconv(buf, &encoding, 'utf-8')) - endif - return line2byte(a:l) + (a:c-2) -endfun - func! s:RunOracle(mode, selected, needs_package) range abort let fname = expand('%:p') let dname = expand('%:p:h') @@ -102,13 +93,13 @@ func! s:RunOracle(mode, selected, needs_package) range abort endif if a:selected != -1 - let pos1 = s:getpos(line("'<"), col("'<")) - let pos2 = s:getpos(line("'>"), col("'>")) + let pos1 = go#util#Offset(line("'<"), col("'<")) + let pos2 = go#util#Offset(line("'>"), col("'>")) let cmd = printf('%s -format plain -pos=%s:#%d,#%d -tags=%s %s', \ bin_path, \ shellescape(fname), pos1, pos2, tags, a:mode) else - let pos = s:getpos(line('.'), col('.')) + let pos = go#util#OffsetCursor() let cmd = printf('%s -format plain -pos=%s:#%d -tags=%s %s', \ bin_path, \ shellescape(fname), pos, tags, a:mode) diff --git a/autoload/go/rename.vim b/autoload/go/rename.vim index f5afef6060..98c5fa3ec6 100644 --- a/autoload/go/rename.vim +++ b/autoload/go/rename.vim @@ -31,7 +31,7 @@ function! go#rename#Rename(bang, ...) endif let fname = expand('%:p') - let pos = s:getpos(line('.'), col('.')) + let pos = go#util#OffsetCursor() let cmd = printf('%s -offset %s -to %s', shellescape(bin_path), shellescape(printf('%s:#%d', fname, pos)), shellescape(to)) let out = go#tool#ExecuteInDir(cmd) @@ -65,14 +65,5 @@ function! go#rename#Rename(bang, ...) silent execute ":e" endfunction -func! s:getpos(l, c) - if &encoding != 'utf-8' - let buf = a:l == 1 ? '' : (join(getline(1, a:l-1), "\n") . "\n") - let buf .= a:c == 1 ? '' : getline('.')[:a:c-2] - return len(iconv(buf, &encoding, 'utf-8')) - endif - return line2byte(a:l) + (a:c-2) -endfun - " vim:ts=4:sw=4:et " diff --git a/autoload/go/util.vim b/autoload/go/util.vim index 9aae995a82..eb7764d1f8 100644 --- a/autoload/go/util.vim +++ b/autoload/go/util.vim @@ -85,6 +85,21 @@ function! go#util#Shelllist(arglist, ...) endtry endfunction +" Returns the byte offset for line a:l and column a:c +function! go#util#Offset(l, c) + if &encoding != 'utf-8' + let buf = a:l == 1 ? '' : (join(getline(1, a:l-1), "\n") . "\n") + let buf .= a:c == 1 ? '' : getline('.')[:a:c-2] + return len(iconv(buf, &encoding, 'utf-8')) + endif + return line2byte(a:l) + (a:c-2) +endfunction +" +" Returns the byte offset for the cursor +function! go#util#OffsetCursor() + return go#util#Offset(line('.'), col('.')) +endfunction + " TODO(arslan): I couldn't parameterize the highlight types. Check if we can " simplify the following functions From 9318e9fb87cb53ce9833e944dab45a660c56b3af Mon Sep 17 00:00:00 2001 From: Anmol Sethi Date: Wed, 16 Mar 2016 03:06:53 -0400 Subject: [PATCH 2/3] refactored go#complete#GetInfoFromOffset() --- autoload/go/complete.vim | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/autoload/go/complete.vim b/autoload/go/complete.vim index f93cd463b8..9937e5d24c 100644 --- a/autoload/go/complete.vim +++ b/autoload/go/complete.vim @@ -96,11 +96,12 @@ fu! s:gocodeAutocomplete() return result endf -function! go#complete#GetInfoFromOffset(offset) +function! go#complete#GetInfo() + let offset = go#util#OffsetCursor()+1 let filename = s:gocodeCurrentBuffer() let result = s:gocodeCommand('autocomplete', \ [s:gocodeCurrentBufferOpt(filename), '-f=godit'], - \ [expand('%:p'), a:offset]) + \ [expand('%:p'), offset]) call delete(filename) " first line is: Charcount,,NumberOfCandidates, i.e: 8,,1 @@ -136,11 +137,6 @@ function! go#complete#GetInfoFromOffset(offset) return "" endfunction -function! go#complete#GetInfo() - let offset = go#util#OffsetCursor()+1 - return go#complete#GetInfoFromOffset(offset) -endfunction - function! go#complete#Info(auto) " auto is true if we were called by g:go_auto_type_info's autocmd let result = go#complete#GetInfo() From 98487a7dbde2cffb426f9615293a2a3df63e02bf Mon Sep 17 00:00:00 2001 From: Anmol Sethi Date: Wed, 16 Mar 2016 04:12:25 -0400 Subject: [PATCH 3/3] Adding go#util#LineEnding to go#util#Offset --- autoload/go/def.vim | 6 ++---- autoload/go/util.vim | 11 ++++++----- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/autoload/go/def.vim b/autoload/go/def.vim index 8fadc840a4..092870220b 100644 --- a/autoload/go/def.vim +++ b/autoload/go/def.vim @@ -15,9 +15,7 @@ endf " modified and improved version of vim-godef function! go#def#Jump(...) if !len(a:000) - " gives us the offset of the word, basicall the position of the word under - " he cursor - let arg = s:getOffset() + let arg = "-o=" . go#util#OffsetCursor() else let arg = a:1 endif @@ -43,7 +41,7 @@ endfunction function! go#def#JumpMode(mode) - let arg = s:getOffset() + let arg = "-o=" . go#util#OffsetCursor() let bin_path = go#path#CheckBinPath(g:go_godef_bin) if empty(bin_path) diff --git a/autoload/go/util.vim b/autoload/go/util.vim index eb7764d1f8..24aa2545cb 100644 --- a/autoload/go/util.vim +++ b/autoload/go/util.vim @@ -85,14 +85,15 @@ function! go#util#Shelllist(arglist, ...) endtry endfunction -" Returns the byte offset for line a:l and column a:c -function! go#util#Offset(l, c) +" Returns the byte offset for line and column +function! go#util#Offset(line, col) if &encoding != 'utf-8' - let buf = a:l == 1 ? '' : (join(getline(1, a:l-1), "\n") . "\n") - let buf .= a:c == 1 ? '' : getline('.')[:a:c-2] + let sep = go#util#LineEnding() + let buf = a:line == 1 ? '' : (join(getline(1, a:line-1), sep) . sep) + let buf .= a:col == 1 ? '' : getline('.')[:a:col-2] return len(iconv(buf, &encoding, 'utf-8')) endif - return line2byte(a:l) + (a:c-2) + return line2byte(a:line) + (a:col-2) endfunction " " Returns the byte offset for the cursor