Skip to content

Commit

Permalink
add custom style for pmenu and fuzzy matching chars
Browse files Browse the repository at this point in the history
  • Loading branch information
jayli committed Jan 8, 2024
1 parent e90e00c commit 3c5fb05
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 28 deletions.
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,13 +257,21 @@ Disable TabNine inline suggestion: `let g:easycomplete_tabnine_suggestion = 0`.

---------------------

## Beautify the vim completion menu
## Beautify completion menu

If you just want to use default nerdfonts configuration, you can simplily config `g:easycomplete_nerd_font = 1`
Set `g:easycomplete_nerd_font = 1` to enable default nerdfonts configuration.

If you want to customize the kind icon, you can modify the configuration with <https://nerdfonts.com> installed. [Examples](custom-config.md).

You can add custom fuzzy matching charactors your own highlight group via `g:easycomplete_fuzzymatch_hlgroup = "MyGroup"`.
You can add custom Pmenu styles by defining these highlight groups:

- `EasyFuzzyMatch`: highlight fuzzy matching character. It links to "Constant" by default if it's not defined.
- `EasyPmenu`: Pmenu style. It links to "Pmenu" by default.
- `EasyPmenuKind`: PmenuKind style. It links to "PmenuKind" by default.
- `EasyPmenuExtra`: PmenuExtra style. It links to "PmenuExtra" by default.
- `EasyFunction`: Function kind icon style. links to "Conditional" by default.
- `EasySnippet`: Snippet kind icon style. links to "Number" by default.
- `EasyTabNine`: TabNine kind icon style. links to "Character" by default.

More examples here: [full custom config](custom-config.md)

Expand Down
64 changes: 40 additions & 24 deletions autoload/easycomplete/pum.vim
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ let s:pum_direction = ""

" pum 高亮所需的临时样式 match id
let g:easycomplete_match_id = 0
" 高亮exec所需的字符串
let s:easycomplete_hl_exec_cmd = ""

" scrollthumb vars
let s:scrollthumb_window = 0
Expand Down Expand Up @@ -67,6 +69,10 @@ function! easycomplete#pum#complete(startcol, items)
call s:OpenPum(a:startcol, s:NormalizeItems(s:curr_items))
endfunction

function! s:HLExists(group)
return easycomplete#ui#HighlightGroupExists(a:group)
endfunction

" 基础的三类样式用到的 Conceal 字符:
" EazyFuzzyMatch: "§", abbr 中匹配 fuzzymatch 的字符高亮,只配置 fg
" EasyKind: "|", 继承 PmenuKind
Expand All @@ -79,29 +85,39 @@ endfunction
" EasyTabNine: ";", TabNine
" EasyNormal: ":", Buf/Text/dict - Pmenu 默认色
function! s:hl()
let hl_group = empty(g:easycomplete_fuzzymatch_hlgroup) ? "Constant" : g:easycomplete_fuzzymatch_hlgroup
if easycomplete#util#IsGui()
let dev = "gui"
else
let dev = "cterm"
endif
let exec_cmd = [
\ 'syntax region EasyFuzzyMatch matchgroup=Conceal start=/\%(§§\)\@!§/ matchgroup=Conceal end=/\%(§§\)\@!§/ concealends oneline keepend',
\ 'syntax region EasyExtra matchgroup=Conceal start=/\%(‰‰\)\@!‰/ matchgroup=Conceal end=/\%(‰‰\)\@!‰/ concealends oneline',
\ 'syntax region EasyKind matchgroup=Conceal start=/|\([^|]|\)\@=/ matchgroup=Conceal end=/\(|[^|]\)\@<=|/ concealends oneline',
\ 'syntax region EasyFunction matchgroup=Conceal start=/%\([^%]%\)\@=/ matchgroup=Conceal end=/\(%[^%]\)\@<=%/ concealends oneline',
\ 'syntax region EasySnippet matchgroup=Conceal start=/&\([^&]&\)\@=/ matchgroup=Conceal end=/\(&[^&]\)\@<=&/ concealends oneline',
\ 'syntax region EasyTabNine matchgroup=Conceal start=/;\([^;];\)\@=/ matchgroup=Conceal end=/\(;[^;]\)\@<=;/ concealends oneline',
\ 'syntax region EasyNormal matchgroup=Conceal start=/:\([^:]:\)\@=/ matchgroup=Conceal end=/\(:[^:]\)\@<=:/ concealends oneline',
\ "hi EasyFuzzyMatch " . dev . "fg=" . easycomplete#ui#GetFgColor(hl_group),
\ "hi link EasyKind PmenuKind",
\ "hi link EasyExtra PmenuExtra",
\ "hi link EasyFunction Conditional",
\ "hi link EasySnippet Number",
\ "hi link EasyTabNine Character",
\ "hi link EasyNormal Pmenu",
\ ]
call win_execute(s:pum_window, join(exec_cmd, "\n"))
if empty(s:easycomplete_hl_exec_cmd)
if easycomplete#util#IsGui()
let dev = "gui"
else
let dev = "cterm"
endif
let fuzzymatch_hl_group = s:HLExists("EasyFuzzyMatch") ? "EasyFuzzyMatch" : "Constant"
let pmenu_kind_hl_group = s:HLExists("EasyPmenuKind") ? "EasyPmenuKind" : "PmenuKind"
let pmenu_extra_hl_group = s:HLExists("EasyPmenuExtra") ? "EasyPmenuExtra" : "PmenuExtra"
let function_hl_group = s:HLExists("EasyFunction") ? "EasyFunction" : "Conditional"
let snippet_hl_group = s:HLExists("EasySnippet") ? "EasySnippet" : "Number"
let tabnine_hl_group = s:HLExists("EasyTabNine") ? "EasyTabNine" : "Character"
let pmenu_hl_group = s:HLExists("EasyPmenu") ? "EasyPmenu" : "Pmenu"

let s:easycomplete_hl_exec_cmd = [
\ 'syntax region CustomFuzzyMatch matchgroup=Conceal start=/\%(§§\)\@!§/ matchgroup=Conceal end=/\%(§§\)\@!§/ concealends oneline keepend',
\ 'syntax region CustomExtra matchgroup=Conceal start=/\%(‰‰\)\@!‰/ matchgroup=Conceal end=/\%(‰‰\)\@!‰/ concealends oneline',
\ 'syntax region CustomKind matchgroup=Conceal start=/|\([^|]|\)\@=/ matchgroup=Conceal end=/\(|[^|]\)\@<=|/ concealends oneline',
\ 'syntax region CustomFunction matchgroup=Conceal start=/%\([^%]%\)\@=/ matchgroup=Conceal end=/\(%[^%]\)\@<=%/ concealends oneline',
\ 'syntax region CustomSnippet matchgroup=Conceal start=/&\([^&]&\)\@=/ matchgroup=Conceal end=/\(&[^&]\)\@<=&/ concealends oneline',
\ 'syntax region CustomTabNine matchgroup=Conceal start=/;\([^;];\)\@=/ matchgroup=Conceal end=/\(;[^;]\)\@<=;/ concealends oneline',
\ 'syntax region CustomNormal matchgroup=Conceal start=/:\([^:]:\)\@=/ matchgroup=Conceal end=/\(:[^:]\)\@<=:/ concealends oneline',
\ "hi CustomFuzzyMatch " . dev . "fg=" . easycomplete#ui#GetFgColor(fuzzymatch_hl_group),
\ "hi link CustomKind " . pmenu_kind_hl_group,
\ "hi link CustomExtra " . pmenu_extra_hl_group,
\ "hi link CustomFunction " . function_hl_group,
\ "hi link CustomSnippet " . snippet_hl_group,
\ "hi link CustomTabNine " . tabnine_hl_group,
\ "hi link CustomNormal " . pmenu_hl_group,
\ ]
" \ "hi Search guibg=NONE guifg=NONE ctermbg=NONE ctermfg=NONE",
endif
call win_execute(s:pum_window, join(s:easycomplete_hl_exec_cmd, "\n"))
endfunction

function! s:OpenPum(startcol, lines)
Expand Down Expand Up @@ -336,7 +352,7 @@ function! s:select(line_index)
else
let prefix_length = 2
endif
call s:HLCursordFuzzyChar("EasyFuzzyMatch", prefix_length)
call s:HLCursordFuzzyChar("CustomFuzzyMatch", prefix_length)
endif
endfunction

Expand Down
9 changes: 9 additions & 0 deletions autoload/easycomplete/ui.vim
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ function! easycomplete#ui#ApplyMarkdownSyntax(winid)
endtry
endfunction " }}}

function! easycomplete#ui#HighlightGroupExists(group)
try
call execute("hi " . a:group)
catch /411/
return v:false
endtry
return v:true
endfunction

" Get back ground color form a GroupName {{{
function! easycomplete#ui#GetBgColor(name)
return easycomplete#ui#GetHiColor(a:name, "bg")
Expand Down
4 changes: 3 additions & 1 deletion custom-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ vim.g.easycomplete_kind_icons = {
vim.g.easycomplete_pum_format = {"kind", "abbr", "menu"}

-- Define highlight group for fuzzy matched charactors.
vim.g.easycomplete_fuzzymatch_hlgroup = "MyGroup"
vim.cmd[[
hi EasyFuzzyMatch guifg=lightblue
]]

-- Enable Tabnine, default is on, install tabnine lsp via `:InstallLspServer tn`
vim.g.easycomplete_tabnine_enable
Expand Down

0 comments on commit 3c5fb05

Please sign in to comment.