From a03389504a4173636d67966c34fdffa01db36d9b Mon Sep 17 00:00:00 2001 From: Weirong Xu Date: Sat, 8 Jun 2024 21:51:23 +0800 Subject: [PATCH] feat: custom select chars, fix #415 --- autoload/coc_explorer/select_wins.vim | 22 +++++++++++++--------- package.json | 5 +++++ readme.md | 4 ++++ src/config.ts | 1 + src/types/pkg-config.d.ts | 4 ++++ src/util/ui.ts | 4 +++- 6 files changed, 30 insertions(+), 10 deletions(-) diff --git a/autoload/coc_explorer/select_wins.vim b/autoload/coc_explorer/select_wins.vim index e7754d7c..101184a4 100644 --- a/autoload/coc_explorer/select_wins.vim +++ b/autoload/coc_explorer/select_wins.vim @@ -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 @@ -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 @@ -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 diff --git a/package.json b/package.json index ae36c177..741f5de4 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/readme.md b/readme.md index 9aca2e72..18a10be9 100644 --- a/readme.md +++ b/readme.md @@ -899,6 +899,10 @@ Type:
BufferFilter & {
 }
+explorer.openAction.select.chars: Chars for select strategy. +Type:
string
Default:
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+
+
explorer.openAction.for.directory: The action when you open a directory of file source. Type:
MappingAction | MappingActionExp[]
Default:
"cd"
diff --git a/src/config.ts b/src/config.ts index 3b15c6bb..403ae7e1 100644 --- a/src/config.ts +++ b/src/config.ts @@ -26,6 +26,7 @@ export interface ExplorerConfig { get( section: 'openAction.select.filter', ): NonNullable; + get(section: 'openAction.select.chars'): string; get( section: 'previewAction.onHover', ): NonNullable; diff --git a/src/types/pkg-config.d.ts b/src/types/pkg-config.d.ts index f87b5eb5..9dd7d795 100644 --- a/src/types/pkg-config.d.ts +++ b/src/types/pkg-config.d.ts @@ -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 */ diff --git a/src/util/ui.ts b/src/util/ui.ts index 556e3ff3..3ff23f4b 100644 --- a/src/util/ui.ts +++ b/src/util/ui.ts @@ -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 | undefined; @@ -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 @@ -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,