Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactored duplicate offset functions #762

Merged
merged 3 commits into from
Mar 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 4 additions & 20 deletions autoload/go/complete.vim
Original file line number Diff line number Diff line change
Expand Up @@ -87,32 +87,21 @@ 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

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
Expand Down Expand Up @@ -148,11 +137,6 @@ function! go#complete#GetInfoFromOffset(offset)
return ""
endfunction

function! go#complete#GetInfo()
let offset = go#complete#gocodeCursor()+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()
Expand Down
19 changes: 3 additions & 16 deletions autoload/go/def.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -65,18 +63,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()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should remove s:getOffset() and change def.vim to make it that it uses this directly if possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will do.

endfunction


Expand Down
15 changes: 3 additions & 12 deletions autoload/go/oracle.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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)
Expand Down
11 changes: 1 addition & 10 deletions autoload/go/rename.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
"
16 changes: 16 additions & 0 deletions autoload/go/util.vim
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,22 @@ function! go#util#Shelllist(arglist, ...)
endtry
endfunction

" Returns the byte offset for line and column
function! go#util#Offset(line, col)
if &encoding != 'utf-8'
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:line) + (a:col-2)
endfunction
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the newline ending are different for fileformat dos. For example in complete.vim's offset implementation there was a line:

 let sep = &l:fileformat == 'dos' ? "\r\n" : "\n"   

that would check for that. Can you please go over the offset functions and also add these kind of edge cases into this funciton ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot, my bad. We have a function in util.vim just for this.

"
" 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

Expand Down