vimrc/sources_non_forked/vim-go/autoload/go/util.vim

216 lines
5.6 KiB
VimL
Raw Normal View History

2015-07-13 10:22:46 +00:00
" PathSep returns the appropriate OS specific path separator.
function! go#util#PathSep()
2016-06-26 11:12:36 +00:00
if go#util#IsWin()
return '\'
endif
return '/'
2015-07-13 10:22:46 +00:00
endfunction
" PathListSep returns the appropriate OS specific path list separator.
function! go#util#PathListSep()
2016-06-26 11:12:36 +00:00
if go#util#IsWin()
return ";"
endif
return ":"
2015-07-13 10:22:46 +00:00
endfunction
" LineEnding returns the correct line ending, based on the current fileformat
function! go#util#LineEnding()
2016-06-26 11:12:36 +00:00
if &fileformat == 'dos'
return "\r\n"
elseif &fileformat == 'mac'
return "\r"
endif
2015-07-13 10:22:46 +00:00
2016-06-26 11:12:36 +00:00
return "\n"
2015-07-13 10:22:46 +00:00
endfunction
2016-07-03 11:53:59 +00:00
" Join joins any number of path elements into a single path, adding a
" Separator if necessary and returns the result
function! go#util#Join(...)
return join(a:000, go#util#PathSep())
endfunction
2015-07-13 10:22:46 +00:00
" IsWin returns 1 if current OS is Windows or 0 otherwise
function! go#util#IsWin()
2016-06-26 11:12:36 +00:00
let win = ['win16', 'win32', 'win64', 'win95']
for w in win
if (has(w))
return 1
endif
endfor
2016-02-20 13:13:10 +00:00
2016-06-26 11:12:36 +00:00
return 0
2015-07-13 10:22:46 +00:00
endfunction
2016-05-14 11:57:54 +00:00
function! go#util#GOARCH()
2016-06-26 11:12:36 +00:00
return substitute(go#util#System('go env GOARCH'), '\n', '', 'g')
2016-05-14 11:57:54 +00:00
endfunction
function! go#util#GOOS()
2016-06-26 11:12:36 +00:00
return substitute(go#util#System('go env GOOS'), '\n', '', 'g')
2016-05-14 11:57:54 +00:00
endfunction
function! go#util#GOROOT()
2016-06-26 11:12:36 +00:00
return substitute(go#util#System('go env GOROOT'), '\n', '', 'g')
2016-05-14 11:57:54 +00:00
endfunction
function! go#util#GOPATH()
2016-06-26 11:12:36 +00:00
return substitute(go#util#System('go env GOPATH'), '\n', '', 'g')
2016-05-14 11:57:54 +00:00
endfunction
function! go#util#OSARCH()
2016-06-26 11:12:36 +00:00
return go#util#GOOS() . '_' . go#util#GOARCH()
2016-05-14 11:57:54 +00:00
endfunction
"Check if has vimproc
function! s:has_vimproc()
2016-06-26 11:12:36 +00:00
if !exists('g:go#use_vimproc')
if go#util#IsWin()
try
call vimproc#version()
let exists_vimproc = 1
catch
let exists_vimproc = 0
endtry
else
let exists_vimproc = 0
2016-05-14 11:57:54 +00:00
endif
2016-06-26 11:12:36 +00:00
let g:go#use_vimproc = exists_vimproc
endif
return g:go#use_vimproc
2016-05-14 11:57:54 +00:00
endfunction
if s:has_vimproc()
2016-06-26 11:12:36 +00:00
let s:vim_system = get(g:, 'gocomplete#system_function', 'vimproc#system2')
let s:vim_shell_error = get(g:, 'gocomplete#shell_error_function', 'vimproc#get_last_status')
2016-05-14 11:57:54 +00:00
else
2016-06-26 11:12:36 +00:00
let s:vim_system = get(g:, 'gocomplete#system_function', 'system')
let s:vim_shell_error = ''
2016-05-14 11:57:54 +00:00
endif
function! go#util#System(str, ...)
2016-06-26 11:12:36 +00:00
return call(s:vim_system, [a:str] + a:000)
2016-05-14 11:57:54 +00:00
endfunction
function! go#util#ShellError()
2016-06-26 11:12:36 +00:00
if empty(s:vim_shell_error)
return v:shell_error
endif
return call(s:vim_shell_error, [])
2016-05-14 11:57:54 +00:00
endfunction
2015-07-13 10:22:46 +00:00
" StripPath strips the path's last character if it's a path separator.
" example: '/foo/bar/' -> '/foo/bar'
function! go#util#StripPathSep(path)
2016-06-26 11:12:36 +00:00
let last_char = strlen(a:path) - 1
if a:path[last_char] == go#util#PathSep()
return strpart(a:path, 0, last_char)
endif
2015-07-13 10:22:46 +00:00
2016-06-26 11:12:36 +00:00
return a:path
2015-07-13 10:22:46 +00:00
endfunction
2016-03-14 10:04:57 +00:00
" StripTrailingSlash strips the trailing slash from the given path list.
" example: ['/foo/bar/'] -> ['/foo/bar']
function! go#util#StripTrailingSlash(paths)
return map(copy(a:paths), 'go#util#StripPathSep(v:val)')
endfunction
2015-12-08 13:20:04 +00:00
" Shelljoin returns a shell-safe string representation of arglist. The
" {special} argument of shellescape() may optionally be passed.
function! go#util#Shelljoin(arglist, ...)
2016-06-26 11:12:36 +00:00
try
let ssl_save = &shellslash
set noshellslash
if a:0
return join(map(copy(a:arglist), 'shellescape(v:val, ' . a:1 . ')'), ' ')
endif
2016-02-20 13:13:10 +00:00
return join(map(copy(a:arglist), 'shellescape(v:val)'), ' ')
finally
let &shellslash = ssl_save
endtry
2015-12-16 13:53:53 +00:00
endfunction
2016-05-14 11:57:54 +00:00
fu! go#util#Shellescape(arg)
if s:has_vimproc()
return vimproc#shellescape(a:arg)
endif
try
let ssl_save = &shellslash
set noshellslash
return shellescape(a:arg)
finally
let &shellslash = ssl_save
endtry
endf
2016-02-20 13:13:10 +00:00
" Shelllist returns a shell-safe representation of the items in the given
2015-12-16 13:53:53 +00:00
" arglist. The {special} argument of shellescape() may optionally be passed.
function! go#util#Shelllist(arglist, ...)
2016-02-20 13:13:10 +00:00
try
let ssl_save = &shellslash
set noshellslash
if a:0
return map(copy(a:arglist), 'shellescape(v:val, ' . a:1 . ')')
endif
return map(copy(a:arglist), 'shellescape(v:val)')
finally
let &shellslash = ssl_save
endtry
2015-12-16 13:53:53 +00:00
endfunction
2016-03-20 18:01:44 +00:00
" 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
"
" Returns the byte offset for the cursor
function! go#util#OffsetCursor()
return go#util#Offset(line('.'), col('.'))
endfunction
2016-04-12 08:31:09 +00:00
" Windo is like the built-in :windo, only it returns to the window the command
" was issued from
function! go#util#Windo(command)
let s:currentWindow = winnr()
try
execute "windo " . a:command
finally
execute s:currentWindow. "wincmd w"
unlet s:currentWindow
endtry
endfunction
2015-12-16 13:53:53 +00:00
" TODO(arslan): I couldn't parameterize the highlight types. Check if we can
" simplify the following functions
function! go#util#EchoSuccess(msg)
redraws! | echon "vim-go: " | echohl Function | echon a:msg | echohl None
2015-12-08 13:20:04 +00:00
endfunction
2015-12-16 13:53:53 +00:00
function! go#util#EchoError(msg)
redraws! | echon "vim-go: " | echohl ErrorMsg | echon a:msg | echohl None
endfunction
function! go#util#EchoWarning(msg)
redraws! | echon "vim-go: " | echohl WarningMsg | echon a:msg | echohl None
endfunction
function! go#util#EchoProgress(msg)
redraws! | echon "vim-go: " | echohl Identifier | echon a:msg | echohl None
endfunction
2016-06-26 11:12:36 +00:00
" vim: sw=2 ts=2 et