From 77903d08407e2c0ed5ab7ac77ed3234bf6ce6756 Mon Sep 17 00:00:00 2001 From: Alisue Date: Sat, 2 Apr 2022 03:02:54 +0900 Subject: [PATCH] :+1: Update components stabilities --- autoload/gin/component/branch.vim | 37 +++++------------------ autoload/gin/component/traffic.vim | 37 +++++------------------ autoload/gin/component/worktree.vim | 37 +++++------------------ autoload/gin/internal/component.vim | 31 +++++++++++++++++++ autoload/gin/util.vim | 21 ++----------- denops/gin/component/branch.ts | 15 ++++----- denops/gin/component/traffic.ts | 15 ++++----- denops/gin/component/worktree.ts | 15 ++++----- doc/gin.txt | 47 +++++++++++++---------------- 9 files changed, 96 insertions(+), 159 deletions(-) create mode 100644 autoload/gin/internal/component.vim diff --git a/autoload/gin/component/branch.vim b/autoload/gin/component/branch.vim index 43a4673a..db51ee3e 100644 --- a/autoload/gin/component/branch.vim +++ b/autoload/gin/component/branch.vim @@ -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 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 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 diff --git a/autoload/gin/component/traffic.vim b/autoload/gin/component/traffic.vim index af8eef77..6fbb35e8 100644 --- a/autoload/gin/component/traffic.vim +++ b/autoload/gin/component/traffic.vim @@ -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 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 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 diff --git a/autoload/gin/component/worktree.vim b/autoload/gin/component/worktree.vim index 3ee660c4..1afa5b4e 100644 --- a/autoload/gin/component/worktree.vim +++ b/autoload/gin/component/worktree.vim @@ -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 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 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 diff --git a/autoload/gin/internal/component.vim b/autoload/gin/internal/component.vim new file mode 100644 index 00000000..a27d0d9a --- /dev/null +++ b/autoload/gin/internal/component.vim @@ -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 User GinComponentPost', 100) + endif +endfunction diff --git a/autoload/gin/util.vim b/autoload/gin/util.vim index 8c43003d..8f329701 100644 --- a/autoload/gin/util.vim +++ b/autoload/gin/util.vim @@ -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) diff --git a/denops/gin/component/branch.ts b/denops/gin/component/branch.ts index 5b2cb379..0d54cab1 100644 --- a/denops/gin/component/branch.ts +++ b/denops/gin/component/branch.ts @@ -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"; @@ -9,12 +9,11 @@ const cache = new Cache(100); async function getData( denops: Denops, - worktree: string, ): Promise { 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; @@ -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}`; } diff --git a/denops/gin/component/traffic.ts b/denops/gin/component/traffic.ts index a97130a0..a7e076b0 100644 --- a/denops/gin/component/traffic.ts +++ b/denops/gin/component/traffic.ts @@ -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"; @@ -9,12 +9,11 @@ const cache = new Cache(100); async function getData( denops: Denops, - worktree: string, ): Promise { 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), @@ -50,9 +49,8 @@ async function getBehind(cwd: string): Promise { 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}`; @@ -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}`; diff --git a/denops/gin/component/worktree.ts b/denops/gin/component/worktree.ts index 9aa68383..701514ed 100644 --- a/denops/gin/component/worktree.ts +++ b/denops/gin/component/worktree.ts @@ -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"; @@ -8,12 +8,11 @@ const cache = new Cache(100); async function getData( denops: Denops, - worktree: string, ): Promise { 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; @@ -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); }, }; diff --git a/doc/gin.txt b/doc/gin.txt index 082af453..dfbd162a 100644 --- a/doc/gin.txt +++ b/doc/gin.txt @@ -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 @@ -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. @@ -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*