Skip to content

Commit

Permalink
feat: custom select chars, fix #415
Browse files Browse the repository at this point in the history
  • Loading branch information
weirongxu committed Jun 8, 2024
1 parent 9dbfa3e commit a033895
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 10 deletions.
22 changes: 13 additions & 9 deletions autoload/coc_explorer/select_wins.vim
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ endfunction
" -1 - User cancelled
" 0 - No window selected
" > 0 - Selected winnr
function! coc_explorer#select_wins#start(buftypes, filetypes, floating_windows) abort
function! coc_explorer#select_wins#start(chars, buftypes, filetypes, floating_windows) abort
let store = {}
let char_idx_mapto_winnr = {}
let char_idx = 0
let char_mapto_winnr = {}
let stored_laststatus = &laststatus
let char_idx = 0
for winnr in range(1, winnr('$'))
let bufnr = winbufnr(winnr)
if index(a:buftypes, getbufvar(bufnr, '&buftype')) >= 0
Expand All @@ -36,19 +37,22 @@ function! coc_explorer#select_wins#start(buftypes, filetypes, floating_windows)
continue
endif
call s:store_statusline(store, winnr)
let char_idx_mapto_winnr[char_idx] = winnr
let char = s:select_wins_chars[char_idx]
let statusline = printf('%%#CocExplorerSelectUI#%s %s', repeat(' ', winwidth(winnr)/2-1), char)
let char = tolower(a:chars[char_idx])
if empty(char)
break
endif
let char_mapto_winnr[char] = winnr
let statusline = printf('%%#CocExplorerSelectUI#%s %s', repeat(' ', winwidth(winnr)/2-1), toupper(char))
call setwinvar(winnr, '&statusline', statusline)
let char_idx += 1
endfor

if len(char_idx_mapto_winnr) == 0
if len(char_mapto_winnr) == 0
call s:restore_statuslines(store)
return 0
elseif len(char_idx_mapto_winnr) == 1
elseif len(char_mapto_winnr) == 1
call s:restore_statuslines(store)
return char_idx_mapto_winnr[0]
return items(char_mapto_winnr)[0][1]
else
if stored_laststatus != 2
let &laststatus = 2
Expand All @@ -61,7 +65,7 @@ function! coc_explorer#select_wins#start(buftypes, filetypes, floating_windows)
if nr == 27 " ESC
break
else
let select_winnr = get(char_idx_mapto_winnr, string(nr - char2nr('a')), -1)
let select_winnr = get(char_mapto_winnr, tolower(nr2char(nr)), -1)
if select_winnr != -1
break
endif
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,11 @@
}
}
},
"explorer.openAction.select.chars": {
"description": "Chars for select strategy",
"type": "string",
"default": "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
},
"explorer.openAction.for.directory": {
"description": "The action when you open a directory of file source",
"$ref": "#/definitions/mapping.actionExp",
Expand Down
4 changes: 4 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,10 @@ Type: <pre><code>BufferFilter & {
}</code></pre>
</details>
<details>
<summary><code>explorer.openAction.select.chars</code>: Chars for select strategy.</summary>
Type: <pre><code>string</code></pre>Default: <pre><code>"ABCDEFGHIJKLMNOPQRSTUVWXYZ"</code></pre>
</details>
<details>
<summary><code>explorer.openAction.for.directory</code>: The action when you open a directory of file source.</summary>
Type: <pre><code>MappingAction | MappingActionExp[]</code></pre>Default: <pre><code>"cd"</code></pre>
</details>
Expand Down
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface ExplorerConfig {
get(
section: 'openAction.select.filter',
): NonNullable<Explorer['explorer.openAction.select.filter']>;
get(section: 'openAction.select.chars'): string;
get(
section: 'previewAction.onHover',
): NonNullable<Explorer['explorer.previewAction.onHover']>;
Expand Down
4 changes: 4 additions & 0 deletions src/types/pkg-config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,10 @@ export interface Explorer {
};
[k: string]: unknown | undefined;
};
/**
* Chars for select strategy
*/
'explorer.openAction.select.chars'?: string;
/**
* The action when you open a directory of file source
*/
Expand Down
4 changes: 3 additions & 1 deletion src/util/ui.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { FloatInputType } from 'coc-floatinput';
import { extensions, workspace, type Extension } from 'coc.nvim';
import { config, type ExplorerConfig } from '../config';
import type { LiteralUnion } from 'type-fest';
import { config, type ExplorerConfig } from '../config';
import type { BufferFilter } from '../types/pkg-config';

let floatInputExt: Extension<FloatInputType> | undefined;
Expand Down Expand Up @@ -168,6 +168,7 @@ export async function selectWindowsUI(
},
) {
let filterOption = config.get('openAction.select.filter');
const chars = config.get('openAction.select.chars');
if (filterOption.sources) {
const sourceFilterOption = filterOption.sources[sourceType] as
| BufferFilter
Expand All @@ -180,6 +181,7 @@ export async function selectWindowsUI(
}
}
const winnr = await workspace.nvim.call('coc_explorer#select_wins#start', [
chars,
filterOption.buftypes ?? [],
filterOption.filetypes ?? [],
filterOption.floatingWindows ?? true,
Expand Down

0 comments on commit a033895

Please sign in to comment.