Skip to content
This repository has been archived by the owner on Aug 20, 2022. It is now read-only.

Commit

Permalink
Refactoring return values of each #call function
Browse files Browse the repository at this point in the history
  • Loading branch information
lambdalisue committed Jan 25, 2016
1 parent 027ebe0 commit c6638c1
Show file tree
Hide file tree
Showing 16 changed files with 140 additions and 92 deletions.
31 changes: 18 additions & 13 deletions autoload/gista/command/browse.vim
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ function! gista#command#browse#call(...) abort
\ 'gistid': '',
\ 'filename': '',
\}, get(a:000, 0, {}))
let gistid = ''
let filename = ''
try
let gistid = gista#resource#local#get_valid_gistid(empty(options.gist)
\ ? options.gistid
Expand All @@ -25,31 +23,38 @@ function! gista#command#browse#call(...) abort
let filename = empty(options.filename)
\ ? ''
\ : gista#resource#local#get_valid_filename(gist, options.filename)
return [s:create_url(gist.html_url, filename), gistid, filename]
let url = s:create_url(gist.html_url, filename)
let result = {
\ 'url': url,
\ 'gist': gist,
\ 'gistid': gistid,
\ 'filename': filename,
\}
return result
catch /^vim-gista:/
call gista#util#handle_exception(v:exception)
return ['', gistid, filename]
return {}
endtry
endfunction
function! gista#command#browse#open(...) abort
let options = extend({}, get(a:000, 0, {}))
let url = gista#command#browse#call(options)[0]
if !empty(url)
call s:File.open(url)
let result = gista#command#browse#call(options)
if !empty(result)
call s:File.open(result.url)
endif
endfunction
function! gista#command#browse#yank(...) abort
let options = extend({}, get(a:000, 0, {}))
let url = gista#command#browse#call(options)[0]
if !empty(url)
call gista#util#clip(url)
let result = gista#command#browse#call(options)
if !empty(result)
call gista#util#clip(result.url)
endif
endfunction
function! gista#command#browse#echo(...) abort
let options = extend({}, get(a:000, 0, {}))
let url = gista#command#browse#call(options)[0]
if !empty(url)
echo url
let result = gista#command#browse#call(options)
if !empty(result)
echo result.url
endif
endfunction

Expand Down
28 changes: 16 additions & 12 deletions autoload/gista/command/commits.vim
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ function! gista#command#commits#call(...) abort
\ 'gist': {},
\ 'gistid': '',
\}, get(a:000, 0, {}))
let gistid = ''
try
let gistid = gista#resource#local#get_valid_gistid(empty(options.gist)
\ ? options.gistid
Expand All @@ -154,7 +153,7 @@ function! gista#command#commits#call(...) abort
let commits = gista#resource#remote#commits(gistid, options)
catch /^vim-gista:/
call gista#util#handle_exception(v:exception)
return [{}, gistid]
return {}
endtry
" Convert commits to entries
let entries = []
Expand All @@ -167,14 +166,19 @@ function! gista#command#commits#call(...) abort
\})
call add(entries, entry)
endfor
return [entries, gistid]
let result = {
\ 'gistid': gistid,
\ 'entries': entries,
\ 'commits': commits,
\}
return result
endfunction
function! gista#command#commits#open(...) abort
let options = extend({
\ 'opener': '',
\}, get(a:000, 0, {}))
let [entries, gistid] = gista#command#commits#call(options)
if empty(entries)
let result = gista#command#commits#call(options)
if empty(result)
return
endif
let client = gista#client#get()
Expand All @@ -184,7 +188,7 @@ function! gista#command#commits#open(...) abort
\ ? g:gista#command#commits#default_opener
\ : options.opener
let bufname = printf('gista-commits:%s:%s',
\ client.apiname, gistid,
\ client.apiname, result.gistid,
\)
call gista#util#buffer#open(bufname, {
\ 'opener': opener . '!',
Expand All @@ -194,8 +198,8 @@ function! gista#command#commits#open(...) abort
\ 'winwidth': winwidth(0),
\ 'apiname': apiname,
\ 'username': username,
\ 'gistid': gistid,
\ 'entries': entries,
\ 'gistid': result.gistid,
\ 'entries': result.entries,
\ 'options': options,
\ 'content_type': 'commits',
\}
Expand Down Expand Up @@ -226,8 +230,8 @@ function! gista#command#commits#update(...) abort
let options = extend(options, {
\ 'gistid': b:gista.gistid,
\})
let [entries, gistid] = gista#command#commits#call(options)
if empty(entries)
let result = gista#command#commits#call(options)
if empty(result)
return
endif
let client = gista#client#get()
Expand All @@ -237,8 +241,8 @@ function! gista#command#commits#update(...) abort
\ 'winwidth': winwidth(0),
\ 'apiname': apiname,
\ 'username': username,
\ 'gistid': gistid,
\ 'entries': entries,
\ 'gistid': result.gistid,
\ 'entries': result.entries,
\ 'options': options,
\ 'content_type': 'commits',
\}
Expand Down
8 changes: 5 additions & 3 deletions autoload/gista/command/delete.vim
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ function! gista#command#delete#call(...) abort
\ 'force': 0,
\ 'confirm': 1,
\}, get(a:000, 0, {}))
let gistid = ''
try
let client = gista#client#get()
let gistid = gista#resource#local#get_valid_gistid(empty(options.gist)
Expand All @@ -29,10 +28,13 @@ function! gista#command#delete#call(...) abort
\ 'A gist %s is deleted from %s',
\ gistid, client.apiname,
\))
return [gistid]
let result = {
\ 'gistid': gistid,
\}
return result
catch /^vim-gista:/
call gista#util#handle_exception(v:exception)
return [gistid]
return {}
endtry
endfunction

Expand Down
9 changes: 6 additions & 3 deletions autoload/gista/command/fork.vim
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ function! gista#command#fork#call(...) abort
let options = extend({
\ 'gistid': '',
\}, get(a:000, 0, {}))
let gistid = ''
try
let gistid = gista#resource#local#get_valid_gistid(empty(options.gist)
\ ? options.gistid
Expand All @@ -18,10 +17,14 @@ function! gista#command#fork#call(...) abort
\ 'A gist %s in %s is forked to %s',
\ gistid, client.apiname, gist.id,
\))
return [gist, gistid]
let result = {
\ 'gist': gist,
\ 'gistid': gistid,
\}
return result
catch /^vim-gista:/
call gista#util#handle_exception(v:exception)
return [{}, gistid]
return {}
endtry
endfunction

Expand Down
23 changes: 13 additions & 10 deletions autoload/gista/command/json.vim
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,39 @@ function! gista#command#json#call(...) abort
\ 'gist': {},
\ 'gistid': '',
\}, get(a:000, 0, {}))
let gistid = ''
try
let gistid = gista#resource#local#get_valid_gistid(empty(options.gist)
\ ? options.gistid
\ : options.gist.id
\)
let gist = gista#resource#remote#get(gistid, options)
return [gist, gistid]
let result = {
\ 'gist': gist,
\ 'gistid': gistid,
\}
return result
catch /^vim-gista:/
call gista#util#handle_exception(v:exception)
return [{}, gistid]
return {}
endtry
endfunction
function! gista#command#json#read(...) abort
silent doautocmd FileReadPre
let options = extend({}, get(a:000, 0, {}))
let gist = gista#command#json#call(options)[0]
if empty(gist)
let result = gista#command#json#call(options)
if empty(result)
return
endif
let content = split(s:JSON.encode(gist, { 'indent': 2 }), "\r\\?\n")
let content = split(s:JSON.encode(result.gist, { 'indent': 2 }), "\r\\?\n")
call gista#util#buffer#read_content(content)
silent doautocmd FileReadPost
silent call gista#util#doautocmd('CacheUpdatePost')
endfunction
function! gista#command#json#edit(...) abort
silent doautocmd BufReadPre
let options = extend({}, get(a:000, 0, {}))
let [gist, gistid] = gista#command#json#call(options)
if empty(gist)
let result = gista#command#json#call(options)
if empty(result)
return
endif
let client = gista#client#get()
Expand All @@ -45,10 +48,10 @@ function! gista#command#json#edit(...) abort
let b:gista = {
\ 'apiname': apiname,
\ 'username': username,
\ 'gistid': gistid,
\ 'gistid': result.gistid,
\ 'content_type': 'json',
\}
let content = split(s:JSON.encode(gist, { 'indent': 2 }), "\r\\?\n")
let content = split(s:JSON.encode(result.gist, { 'indent': 2 }), "\r\\?\n")
call gista#util#buffer#edit_content(content)
setlocal buftype=nowrite
setlocal nomodifiable
Expand Down
27 changes: 15 additions & 12 deletions autoload/gista/command/list.vim
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,12 @@ function! gista#command#list#call(...) abort
\ 'lookup': '',
\ 'cache': 1,
\}, get(a:000, 0, {}))
let lookup = ''
try
let lookup = gista#resource#local#get_valid_lookup(options.lookup)
let index = gista#resource#remote#list(lookup, options)
catch /^vim-gista:/
call gista#util#handle_exception(v:exception)
return [{}, lookup]
return {}
endtry
" apply 'is_starred' field
let client = gista#client#get()
Expand All @@ -256,15 +255,19 @@ function! gista#command#list#call(...) abort
\ 'extend(v:val, { "is_starred": get(starred, v:val.id) })',
\)
endif
return [index, lookup]
let result = {
\ 'index': index,
\ 'lookup': lookup,
\}
return result
endfunction
function! gista#command#list#open(...) abort
let options = extend({
\ 'opener': '',
\ 'cache': 1,
\}, get(a:000, 0, {}))
let [index, lookup] = gista#command#list#call(options)
if empty(index)
let result = gista#command#list#call(options)
if empty(result)
return
endif
let client = gista#client#get()
Expand All @@ -274,7 +277,7 @@ function! gista#command#list#open(...) abort
\ ? g:gista#command#list#default_opener
\ : options.opener
let bufname = printf('gista-list:%s:%s',
\ client.apiname, lookup,
\ client.apiname, result.lookup,
\)
call gista#util#buffer#open(bufname, {
\ 'opener': opener . (options.cache ? '' : '!'),
Expand All @@ -284,8 +287,8 @@ function! gista#command#list#open(...) abort
\ 'winwidth': winwidth(0),
\ 'apiname': apiname,
\ 'username': username,
\ 'lookup': lookup,
\ 'entries': s:sort_entries(index.entries),
\ 'lookup': result.lookup,
\ 'entries': s:sort_entries(result.index.entries),
\ 'options': s:Dict.omit(options, ['cache']),
\ 'content_type': 'list',
\}
Expand Down Expand Up @@ -316,8 +319,8 @@ function! gista#command#list#update(...) abort
let options = extend(options, {
\ 'lookup': b:gista.lookup,
\})
let [index, lookup] = gista#command#list#call(options)
if empty(index)
let result = gista#command#list#call(options)
if empty(result)
return
endif
let client = gista#client#get()
Expand All @@ -327,8 +330,8 @@ function! gista#command#list#update(...) abort
\ 'winwidth': winwidth(0),
\ 'apiname': apiname,
\ 'username': username,
\ 'lookup': lookup,
\ 'entries': s:sort_entries(index.entries),
\ 'lookup': result.lookup,
\ 'entries': s:sort_entries(result.index.entries),
\ 'options': options,
\ 'content_type': 'list',
\}
Expand Down
10 changes: 6 additions & 4 deletions autoload/gista/command/login.vim
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ function! gista#command#login#call(...) abort
\ 'apiname': '',
\ 'username': '',
\}, get(a:000, 0, {}))
let apiname = ''
let username = ''
try
let apiname = gista#client#get_valid_apiname(options.apiname)
let username = gista#client#get_valid_username(apiname, options.username)
Expand All @@ -20,10 +18,14 @@ function! gista#command#login#call(...) abort
\ client.apiname,
\ client.get_authorized_username(),
\)
return [apiname, username]
let result = {
\ 'apiname': apiname,
\ 'username': username,
\}
return result
catch /^vim-gista:/
call gista#util#handle_exception(v:exception)
return [apiname, username]
return {}
endtry
endfunction

Expand Down
8 changes: 5 additions & 3 deletions autoload/gista/command/logout.vim
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@ function! gista#command#logout#call(...) abort
let options = extend({
\ 'apiname': '',
\}, get(a:000, 0, {}))
let apiname = ''
try
let apiname = gista#client#get_valid_apiname(options.apiname)
call gista#client#set(apiname, { 'username': '' })
let client = gista#client#get()
echo printf('Logout from %s', client.apiname)
return [apiname]
let result = {
\ 'apiname': apiname,
\}
return result
catch /^vim-gista:/
call gista#util#handle_exception(v:exception)
return [apiname]
return {}
endtry
endfunction

Expand Down
Loading

0 comments on commit c6638c1

Please sign in to comment.