Skip to content

Commit

Permalink
refactor ruby support
Browse files Browse the repository at this point in the history
  • Loading branch information
carakan committed Oct 17, 2018
1 parent 7e662e4 commit abf0fe6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 62 deletions.
72 changes: 10 additions & 62 deletions autoload/pmv/ruby/rubygems.vim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function! pmv#ruby#rubygems#allReleases(package_name)
let l:gem_name = s:gem_name_from_current_line()
function! pmv#ruby#rubygems#allReleases(packageName)
let l:gem_name = pmv#ruby#utils#getPackageName(a:packageName)
if empty(l:gem_name)
return
endif
Expand All @@ -9,7 +9,7 @@ function! pmv#ruby#rubygems#allReleases(package_name)
endfunction

function! pmv#ruby#rubygems#lastRelease()
let l:gem_name = s:gem_name_from_current_line()
let l:gem_name = pmv#ruby#utils#getPackageName('')
if empty(l:gem_name)
return
endif
Expand All @@ -18,8 +18,8 @@ function! pmv#ruby#rubygems#lastRelease()
echo l:output
endfunction

function! pmv#ruby#rubygems#packageInfo(package_name)
let gem_name = s:gem_name_from_current_line()
function! pmv#ruby#rubygems#packageInfo(packageName)
let gem_name = pmv#ruby#utils#getPackageName(a:packageName)
if empty(gem_name)
return
endif
Expand All @@ -35,7 +35,7 @@ function! pmv#ruby#rubygems#packageInfo(package_name)
endfunction

function! pmv#ruby#rubygems#appendRelease()
let gem_name = s:gem_name_from_current_line()
let gem_name = pmv#ruby#utils#getPackageName('')
if empty(gem_name)
return
endif
Expand All @@ -45,15 +45,15 @@ function! pmv#ruby#rubygems#appendRelease()
execute "normal! A, '~> ".gem_version."'"
endfunction

function! pmv#ruby#rubygems#openDocs(package_name)
let l:gemName = s:gem_name_from_current_line()
function! pmv#ruby#rubygems#openDocs(packageName)
let l:gemName = pmv#ruby#utils#getPackageName(a:packageName)
if !empty(l:gemName)
call pmv#ruby#utils#openPage(l:gemName, 'documentation_uri')
endif
endfunction

function! pmv#ruby#rubygems#openGithub(package_name)
let l:gemName = s:gem_name_from_current_line()
function! pmv#ruby#rubygems#openGithub(packageName)
let l:gemName = pmv#ruby#utils#getPackageName(a:packageName)
if !empty(l:gemName)
call pmv#ruby#utils#openPage(l:gemName, 'homepage_uri')
endif
Expand All @@ -73,58 +73,6 @@ function! rubygems#clean_signs()
sign unplace *
endfunction

function! rubygems#GemfileCheck()
sign unplace *
normal! gg
call s:highlight_signs()
let lines = getbufline(bufname('%'), 0, line('$'))
let index = 0
for line in lines
let index += 1
call s:update_cursor_position(index)
let gem_name = s:extract_gem_name(line)
let current_gem_version = s:extract_gem_version(line)

if strlen(gem_name) < 2 || strlen(current_gem_version) < 2
continue
endif

call s:hi_line(index, 'rubygem_checking')
let gem_info = pmv#ruby#utils#getApiPackage(gem_name)
if s:compare_versions(current_gem_version, gem_info.version)
call s:hi_line(index, 'rubygem_warning',)
else
exe 'sign unplace ' . index
endif
endfor
endfunction

function! s:gem_name_from_current_line()
let l:gemName = s:extract_gem_name(getline('.'))
return l:gemName
endfunction

function! s:is_gem_definition(str_arr)
let l:gem_def_prefixes = [
\ 'gem',
\ 'spec.add_development_dependency',
\ 'spec.add_runtime_dependency'
\ ]

return index(l:gem_def_prefixes, a:str_arr[0]) >= 0
endfunction

function! s:extract_gem_name(str)
let str = split(a:str, ' ')
if len(str) > 1 && s:is_gem_definition(str)
let gem_name = tolower(str[1])
let gem_name = matchstr(gem_name, '[0-9A-z-_]\+')
return gem_name
else
return
endif
endfunction

function! s:extract_gem_version(str)
let str = split(a:str, ' ')
if len(str) > 2 && s:is_gem_definition(str)
Expand Down
37 changes: 37 additions & 0 deletions autoload/pmv/ruby/utils.vim
Original file line number Diff line number Diff line change
@@ -1,3 +1,40 @@
function! pmv#ruby#utils#getPackageName(qarg)
if empty(a:qarg)
let l:package = s:scanForPackage()
if pmv#utils#packageNotFound(l:package)
return
endif
return l:package
endif
return a:qarg
endfunction

function! s:scanForPackage()
let l:gemName = s:extract_gem_name(getline('.'))
return l:gemName
endfunction

function! s:is_gem_definition(str_arr)
let l:gem_def_prefixes = [
\ 'gem',
\ 'spec.add_development_dependency',
\ 'spec.add_runtime_dependency'
\ ]

return index(l:gem_def_prefixes, a:str_arr[0]) >= 0
endfunction

function! s:extract_gem_name(str)
let str = split(a:str, ' ')
if len(str) > 1 && s:is_gem_definition(str)
let gem_name = tolower(str[1])
let gem_name = matchstr(gem_name, '[0-9A-z-_]\+')
return gem_name
else
return
endif
endfunction

function! pmv#ruby#utils#getApiPackage(gemName) abort
let l:uri = 'https://rubygems.org/api/v1/gems/' . a:gemName . '.json'
let l:content = pmv#utils#fetchApiPackage(l:uri)
Expand Down

0 comments on commit abf0fe6

Please sign in to comment.