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

👍 Update components stabilities #43

Merged
merged 1 commit into from
Apr 1, 2022
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
37 changes: 8 additions & 29 deletions autoload/gin/component/branch.vim
Original file line number Diff line number Diff line change
@@ -1,32 +1,11 @@
let s:ascii = ''
let s:unicode = ''

function! gin#component#branch#ascii(...) abort
let cwd = a:0 ? a:1 : ''
call timer_start(0, { -> s:update_ascii(cwd) })
return s:ascii
endfunction

function! gin#component#branch#unicode(...) abort
let cwd = a:0 ? a:1 : ''
call timer_start(0, { -> s:update_unicode(cwd) })
return s:unicode
endfunction

function! s:update_ascii(cwd) abort
try
let s:ascii = denops#request('gin', 'component:branch:ascii', [a:cwd])
catch
let s:ascii = ''
endtry
call gin#util#debounce('doautocmd <nomodeline> User GinComponentPost')
function! gin#component#branch#ascii() abort
let component = 'component:batch:ascii'
call gin#internal#component#init(component)
return gin#internal#component#get(component)
endfunction

function! s:update_unicode(cwd) abort
try
let s:unicode = denops#request('gin', 'component:branch:unicode', [a:cwd])
catch
let s:unicode = ''
endtry
call gin#util#debounce('doautocmd <nomodeline> User GinComponentPost')
function! gin#component#branch#unicode() abort
let component = 'component:batch:unicode'
call gin#internal#component#init(component)
return gin#internal#component#get(component)
endfunction
37 changes: 8 additions & 29 deletions autoload/gin/component/traffic.vim
Original file line number Diff line number Diff line change
@@ -1,32 +1,11 @@
let s:ascii = ''
let s:unicode = ''

function! gin#component#traffic#ascii(...) abort
let cwd = a:0 ? a:1 : ''
call timer_start(0, { -> s:update_ascii(cwd) })
return s:ascii
endfunction

function! gin#component#traffic#unicode(...) abort
let cwd = a:0 ? a:1 : ''
call timer_start(0, { -> s:update_unicode(cwd) })
return s:unicode
endfunction

function! s:update_ascii(cwd) abort
try
let s:ascii = denops#request('gin', 'component:traffic:ascii', [a:cwd])
catch
let s:ascii = ''
endtry
call gin#util#debounce('doautocmd <nomodeline> User GinComponentPost')
function! gin#component#traffic#ascii() abort
let component = 'component:traffic:ascii'
call gin#internal#component#init(component)
return gin#internal#component#get(component)
endfunction

function! s:update_unicode(cwd) abort
try
let s:unicode = denops#request('gin', 'component:traffic:unicode', [a:cwd])
catch
let s:unicode = ''
endtry
call gin#util#debounce('doautocmd <nomodeline> User GinComponentPost')
function! gin#component#traffic#unicode() abort
let component = 'component:traffic:unicode'
call gin#internal#component#init(component)
return gin#internal#component#get(component)
endfunction
37 changes: 8 additions & 29 deletions autoload/gin/component/worktree.vim
Original file line number Diff line number Diff line change
@@ -1,32 +1,11 @@
let s:name = ''
let s:full = ''

function! gin#component#worktree#full(...) abort
let cwd = a:0 ? a:1 : ''
call timer_start(0, { -> s:update_full(cwd) })
return s:full
endfunction

function! gin#component#worktree#name(...) abort
let cwd = a:0 ? a:1 : ''
call timer_start(0, { -> s:update_name(cwd) })
return s:name
endfunction

function! s:update_name(cwd) abort
try
let s:name = denops#request('gin', 'component:worktree:name', [a:cwd])
catch
let s:name = ''
endtry
call gin#util#debounce('doautocmd <nomodeline> User GinComponentPost')
function! gin#component#worktree#name() abort
let component = 'component:worktree:name'
call gin#internal#component#init(component)
return gin#internal#component#get(component)
endfunction

function! s:update_full(cwd) abort
try
let s:full = denops#request('gin', 'component:worktree:full', [a:cwd])
catch
let s:full = ''
endtry
call gin#util#debounce('doautocmd <nomodeline> User GinComponentPost')
function! gin#component#worktree#full() abort
let component = 'component:worktree:full'
call gin#internal#component#init(component)
return gin#internal#component#get(component)
endfunction
31 changes: 31 additions & 0 deletions autoload/gin/internal/component.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
let s:cache = {}
let s:init = {}

function! gin#internal#component#init(component) abort
if has_key(s:init, a:component)
return
endif
let s:init[a:component] = 1
execute printf('augroup gin_internal_%s', substitute(a:component, '\W', '_', 'g'))
autocmd!
execute printf('autocmd BufEnter * call gin#internal#component#update("%s")', a:component)
execute printf('autocmd User GinCommandPost call gin#internal#component#update("%s")', a:component)
augroup END
endfunction

function! gin#internal#component#get(component) abort
return get(s:cache, a:component, '')
endfunction

function! gin#internal#component#update(component) abort
let previous = get(s:cache, a:component, '')
try
let value = denops#request('gin', a:component, [])
catch
let value = ''
endtry
let s:cache[a:component] = value
if value !=# previous
call gin#util#debounce('doautocmd <nomodeline> User GinComponentPost', 100)
endif
endfunction
21 changes: 2 additions & 19 deletions autoload/gin/util.vim
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,8 @@ function! gin#util#expand(expr) abort
return denops#request('gin', 'util:expand', [a:expr])
endfunction

function! gin#util#redraw_components() abort
silent! call timer_stop(s:redraw_component_timer)
let s:redraw_component_timer = timer_start(
\ g:gin#util#redraw_components_delay,
\ { -> execute('redrawstatus | redrawtabline') },
\)
endfunction

function! gin#util#debounce(expr, ...) abort
let options = extend({
\ 'delay': g:gin#util#debounce_delay,
\}, a:0 ? a:1 : {},
\)
function! gin#util#debounce(expr, delay) abort
let timer = get(s:debounce_timers, a:expr, 0)
silent! call timer_stop(timer)
let s:debounce_timers[a:expr] = timer_start(
\ options.delay,
\ { -> execute(a:expr) },
\)
let s:debounce_timers[a:expr] = timer_start(a:delay, { -> execute(a:expr) })
endfunction

let g:gin#util#debounce_delay = get(g:, 'gin#util#debounce_delay', 100)
15 changes: 6 additions & 9 deletions denops/gin/component/branch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Cache, Denops, unknownutil } from "../deps.ts";
import { Cache, Denops } from "../deps.ts";
import { decodeUtf8 } from "../util/text.ts";
import { getWorktree } from "../util/worktree.ts";
import { execute } from "../git/process.ts";
Expand All @@ -9,12 +9,11 @@ const cache = new Cache<string, Data>(100);

async function getData(
denops: Denops,
worktree: string,
): Promise<Data> {
if (cache.has("data")) {
return cache.get("data");
}
const cwd = worktree || await getWorktree(denops);
const cwd = await getWorktree(denops);
const result = await getBranches(cwd);
cache.set("data", result);
return result;
Expand All @@ -40,17 +39,15 @@ async function getBranches(
export function main(denops: Denops): void {
denops.dispatcher = {
...denops.dispatcher,
"component:branch:ascii": async (worktree) => {
unknownutil.assertString(worktree);
const [branch, upstream] = await getData(denops, worktree);
"component:branch:ascii": async () => {
const [branch, upstream] = await getData(denops);
if (branch && upstream) {
return `${branch} -> ${upstream}`;
}
return branch;
},
"component:branch:unicode": async (worktree) => {
unknownutil.assertString(worktree);
const [branch, upstream] = await getData(denops, worktree);
"component:branch:unicode": async () => {
const [branch, upstream] = await getData(denops);
if (branch && upstream) {
return `${branch} → ${upstream}`;
}
Expand Down
15 changes: 6 additions & 9 deletions denops/gin/component/traffic.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Cache, Denops, unknownutil } from "../deps.ts";
import { Cache, Denops } from "../deps.ts";
import { decodeUtf8 } from "../util/text.ts";
import { getWorktree } from "../util/worktree.ts";
import { execute } from "../git/process.ts";
Expand All @@ -9,12 +9,11 @@ const cache = new Cache<string, Data>(100);

async function getData(
denops: Denops,
worktree: string,
): Promise<Data> {
if (cache.has("data")) {
return cache.get("data");
}
const cwd = worktree || await getWorktree(denops);
const cwd = await getWorktree(denops);
const result = await Promise.all([
getAhead(cwd),
getBehind(cwd),
Expand Down Expand Up @@ -50,9 +49,8 @@ async function getBehind(cwd: string): Promise<number> {
export function main(denops: Denops): void {
denops.dispatcher = {
...denops.dispatcher,
"component:traffic:ascii": async (worktree) => {
unknownutil.assertString(worktree);
const [ahead, behind] = await getData(denops, worktree);
"component:traffic:ascii": async () => {
const [ahead, behind] = await getData(denops);
let component = "";
if (ahead) {
component += `^${ahead}`;
Expand All @@ -62,9 +60,8 @@ export function main(denops: Denops): void {
}
return component;
},
"component:traffic:unicode": async (worktree) => {
unknownutil.assertString(worktree);
const [ahead, behind] = await getData(denops, worktree);
"component:traffic:unicode": async () => {
const [ahead, behind] = await getData(denops);
let component = "";
if (ahead) {
component += `↑${ahead}`;
Expand Down
15 changes: 6 additions & 9 deletions denops/gin/component/worktree.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Cache, Denops, path, unknownutil } from "../deps.ts";
import { Cache, Denops, path } from "../deps.ts";
import { getWorktree } from "../util/worktree.ts";
import { find } from "../git/finder.ts";

Expand All @@ -8,12 +8,11 @@ const cache = new Cache<string, Data>(100);

async function getData(
denops: Denops,
worktree: string,
): Promise<Data> {
if (cache.has("data")) {
return cache.get("data");
}
const cwd = worktree || await getWorktree(denops);
const cwd = await getWorktree(denops);
const result = await find(cwd);
cache.set("data", result);
return result;
Expand All @@ -22,14 +21,12 @@ async function getData(
export function main(denops: Denops): void {
denops.dispatcher = {
...denops.dispatcher,
"component:worktree:full": async (worktree) => {
unknownutil.assertString(worktree);
const fullpath = await getData(denops, worktree);
"component:worktree:full": async () => {
const fullpath = await getData(denops);
return fullpath;
},
"component:worktree:name": async (worktree) => {
unknownutil.assertString(worktree);
const fullpath = await getData(denops, worktree);
"component:worktree:name": async () => {
const fullpath = await getData(denops);
return path.basename(fullpath);
},
};
Expand Down
47 changes: 21 additions & 26 deletions doc/gin.txt
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,11 @@ COMMANDS *gin-commands*
Gin supports several options starts with double plus signs (++) as:

++worktree={worktree}
Specify a particular git working tree. If this option is not
specified, gin will find the one from the current buffer or the
current working directory. It tries to find a git working tree from
Specify a particular git working tree.
*gin-worktree*
If this option is not specified, gin will find the one from the
current buffer or the current working directory. It tries to find a
git working tree from

1. A target working tree of gin associated buffers (e.g. gindiff://)
2. A parent directory of a symlink resolved path of the current buffer
Expand Down Expand Up @@ -318,6 +320,11 @@ gin#util#expand([{expr}])
Like |expand()| but it extract a target filename if the buffer is
gin's file-like buffer.

*gin#util#debounce()*
gin#util#debounce({expr}, {delay})
Execute {expr} after {delay} milliseconds and debounce all calls
within that {delay}.

*gin#action#fn()*
gin#action#fn({callback})
A wrapper function to make an action mapping.
Expand Down Expand Up @@ -352,44 +359,32 @@ gin#action#gather_candidates({range})
COMPONENTS *gin-components*

Components are |Function| that never fail and always return a string for
|statusline| and |tabline|.
If no {worktree} is specified or an empty string, gin tries to find one like
++worktree option explained in |gin-command-options|.
|statusline| and |tabline|. It returns information of a current working tree
(See |gin-worktree| for detail about how gin find a current working tree.)

Users can use it like:
Use component functions like:
>
set statusline+=\ %{gin#component#worktree#name()}
set statusline+=\ (%{gin#component#branch#ascii()})
set statusline+=\ [%{gin#component#traffic#ascii()}]
<
Note that components update actual values in background and always return
cached one. Use |User| GinComponentUpdated autocmd event to notice update ASAP
like:
>
augroup gin_component_fast_update
autocmd!
autocmd User GinComponentPost ++nested redrawstatusline
" Or if you prefer tabline, use below
"autocmd User GinComponentPost ++nested redrawtabline
augroup END
<
*gin#component#branch#ascii()*
*gin#component#branch#unicode()*
gin#component#branch#ascii([{worktree}])
gin#component#branch#unicode([{worktree}])
gin#component#branch#ascii()
gin#component#branch#unicode()
Return an indicator string of a current and upstream branches.

*gin#component#traffic#ascii()*
*gin#component#traffic#unicode()*
gin#component#traffic#ascii([{worktree}])
gin#component#traffic#unicode([{worktree}])
gin#component#traffic#ascii()
gin#component#traffic#unicode()
Return an indicator string of the number of ahead and behind commits.

*gin#component#worktree#full()*
*gin#component#worktree#name()*
gin#component#worktree#full([{worktree}])
gin#component#worktree#name([{worktree}])
Return an indicator string of the current worktree.
*gin#component#worktree#full()*
gin#component#worktree#name()
gin#component#worktree#full()
Return an indicator string of the current working tree.

-----------------------------------------------------------------------------
MAPPINGS *gin-mappings*
Expand Down