Skip to content

Commit 64423a4

Browse files
authored
Support tree-sitter highlight for provider preview (#1019)
* Try identifying the buffer language from filetype * Skip serde if Vec::is_empty * Minor refactoring: merge syntax and fname into VimSyntaxInfo * provider: implement tree-sitter highlight for preview Close #532 * Remove todo!() * Remove coloreyre * Minor refactorings * Add more language support * clippy fixes * Update to rust 1.74 * clippy fixes * Rename to sublime-syntax-color-scheme * Skip serialization if VimSyntaxInfo::is_empty() * Add TODO * Fix typo * Define g:clap_provider_clap_actions if g:clap_plugin_experimental is on * Nits * docs * Add ClapAction command
1 parent 9fa56f9 commit 64423a4

File tree

23 files changed

+568
-248
lines changed

23 files changed

+568
-248
lines changed

Cargo.lock

Lines changed: 66 additions & 60 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ tokio = { version = "1.31", features = ["rt"] }
1818

1919
cli = { path = "crates/cli" }
2020
upgrade = { path = "crates/upgrade" }
21-
color-eyre = "0.6.2"
2221

2322
[build-dependencies]
2423
built = { package = "built", version = "0.6", features = ["git2"] }

autoload/clap/helper.vim

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,22 @@ function! clap#helper#complete(ArgLead, CmdLine, P) abort
4848
if !exists('s:autoload_providers')
4949
let s:autoload_providers = map(split(globpath(&runtimepath, 'autoload/clap/provider/*.vim'), "\n"), 'fnamemodify(v:val, ":t:r")')
5050
endif
51-
return filter(uniq(sort(s:autoload_providers + keys(g:clap#provider_alias) + registered)), 'v:val =~# "^".a:ArgLead')
51+
if !exists('s:user_providers')
52+
let s:user_providers = map(clap#provider#providers#get_user_defined(), 'split(v:val, ":")[0]')
53+
endif
54+
return filter(uniq(sort(s:autoload_providers + s:user_providers + keys(g:clap#provider_alias) + registered)), 'v:val =~# "^".a:ArgLead')
55+
endfunction
56+
57+
function! clap#helper#complete_actions(A, L, P) abort
58+
if !exists('g:clap_actions')
59+
echoerr '`g:clap_actions` not found'
60+
return []
61+
endif
62+
if empty(a:A)
63+
return g:clap_actions
64+
else
65+
return filter(g:clap_actions, printf('v:val =~ "^%s"', a:A))
66+
endif
5267
endfunction
5368

5469
function! clap#helper#echo_message(msg) abort

autoload/clap/highlighter.vim

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,12 @@ else
8484

8585
" lnum is 0-based.
8686
function! s:add_ts_highlight_at(bufnr, lnum, col, length, hl_group) abort
87-
call prop_add(a:lnum+1, a:col+1, {'length': a:length, 'type': a:hl_group, 'bufnr': a:bufnr})
87+
try
88+
call prop_add(a:lnum+1, a:col+1, {'length': a:length, 'type': a:hl_group, 'bufnr': a:bufnr})
89+
catch
90+
" Not sure why, but I keep run into error: Invalid line number, neovim
91+
" does not have this issue.
92+
endtry
8893
endfunction
8994

9095
function! s:add_display_highlights(hl_lines) abort
@@ -148,6 +153,21 @@ function! clap#highlighter#highlight_line(bufnr, lnum, token_highlights) abort
148153
endfor
149154
endfunction
150155

156+
" Highlight a list of lines.
157+
function! clap#highlighter#add_sublime_highlights(bufnr, line_highlights) abort
158+
for [lnum, line_highlight] in a:line_highlights
159+
call clap#highlighter#highlight_line(a:bufnr, lnum, line_highlight)
160+
endfor
161+
endfunction
162+
163+
function! clap#highlighter#disable_tree_sitter(bufnr) abort
164+
if has('nvim')
165+
call nvim_buf_clear_namespace(a:bufnr, s:tree_sitter_ns_id, 0, -1)
166+
elseif !empty(s:ts_types)
167+
call prop_remove({ 'types': s:ts_types, 'all': v:true, 'bufnr': a:bufnr } )
168+
endif
169+
endfunction
170+
151171
function! clap#highlighter#add_ts_highlights(bufnr, to_replace_line_ranges, highlights) abort
152172
if has('nvim')
153173
" All old highlights need to be replaced.
@@ -163,7 +183,7 @@ function! clap#highlighter#add_ts_highlights(bufnr, to_replace_line_ranges, high
163183
call prop_remove({ 'types': s:ts_types, 'all': v:true, 'bufnr': a:bufnr } )
164184
else
165185
for [start, end] in a:to_replace_line_ranges
166-
call prop_remove({ 'types': s:ts_types, 'all': v:true, 'bufnr': a:bufnr }, start + 1, end - 1)
186+
call prop_remove({ 'types': s:ts_types, 'all': v:true, 'bufnr': a:bufnr }, start, end)
167187
endfor
168188
endif
169189
endif
@@ -181,12 +201,5 @@ function! clap#highlighter#add_ts_highlights(bufnr, to_replace_line_ranges, high
181201
endfor
182202
endfunction
183203

184-
" Highlight a list of lines.
185-
function! clap#highlighter#highlight_lines(bufnr, line_highlights) abort
186-
for [lnum, line_highlight] in a:line_highlights
187-
call clap#highlighter#highlight_line(a:bufnr, lnum, line_highlight)
188-
endfor
189-
endfunction
190-
191204
let &cpoptions = s:save_cpo
192205
unlet s:save_cpo

autoload/clap/provider/providers.vim

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,28 @@ function! s:providers.sink(selected) abort
1212
call timer_start(0, {-> clap#_for(provider)})
1313
endfunction
1414

15+
function! clap#provider#providers#get_user_defined() abort
16+
let user_providers = []
17+
" `description` is required, otherwise we can't distinguish whether the variable name
18+
" like `g:clap_provider_yanks_history` is a name of some provider or merely a control
19+
" variable of a provider.
20+
let maybe_user_var_providers = filter(keys(g:), 'v:val =~# "^clap_provider_"')
21+
for maybe_var_provider in maybe_user_var_providers
22+
try
23+
let evaled = eval('g:'.maybe_var_provider)
24+
if type(evaled) == v:t_dict
25+
let provider_id = matchstr(maybe_var_provider, 'clap_provider_\zs\(.*\)')
26+
let description = get(evaled, 'description', '')
27+
call add(user_providers, provider_id.': '.description)
28+
endif
29+
catch
30+
echom 'Failed to fetch user defined clap providers: '.v:exception
31+
return user_providers
32+
endtry
33+
endfor
34+
return user_providers
35+
endfunction
36+
1537
function! s:providers.source() abort
1638
if !exists('s:global_source')
1739
let s:global_source = []
@@ -31,23 +53,7 @@ function! s:providers.source() abort
3153
endif
3254
endfor
3355

34-
" `description` is required, otherwise we can't distinguish whether the variable name
35-
" like `g:clap_provider_yanks_history` is a name of some provider or merely a control
36-
" variable of a provider.
37-
let maybe_user_var_providers = filter(keys(g:), 'v:val =~# "^clap_provider_"')
38-
for maybe_var_provider in maybe_user_var_providers
39-
try
40-
let evaled = eval('g:'.maybe_var_provider)
41-
if type(evaled) == v:t_dict
42-
let provider_id = matchstr(maybe_var_provider, 'clap_provider_\zs\(.*\)')
43-
let description = get(evaled, 'description', '')
44-
call add(s:global_source, provider_id.': '.description)
45-
endif
46-
catch
47-
echom 'Failed to fetch user defined clap providers: 'v:exception
48-
return g:global_source
49-
endtry
50-
endfor
56+
call extend(s:global_source, clap#provider#providers#get_user_defined())
5157
endif
5258
return s:global_source
5359
endfunction

autoload/clap/state.vim

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,23 @@ function! clap#state#render_preview(preview) abort
114114
call g:clap.preview.show(['Error occurred while showing the preview:', v:exception, '', string(a:preview.lines)])
115115
return
116116
endtry
117-
if has_key(a:preview, 'syntax')
118-
call g:clap.preview.set_syntax(a:preview.syntax)
119-
elseif has_key(a:preview, 'fname')
120-
call g:clap.preview.set_syntax(clap#ext#into_filetype(a:preview.fname))
121-
elseif has_key(a:preview, 'syntax_highlights')
122-
for [lnum, line_highlight] in a:preview.syntax_highlights
117+
if has_key(a:preview, 'sublime_syntax_highlights')
118+
for [lnum, line_highlight] in a:preview.sublime_syntax_highlights
123119
try
124120
call clap#highlighter#highlight_line(g:clap.preview.bufnr, lnum, line_highlight)
125121
catch
126122
" Ignore any potential errors as the line might be truncated.
127123
endtry
128124
endfor
125+
elseif has_key(a:preview, 'tree_sitter_highlights')
126+
call clap#highlighter#add_ts_highlights(g:clap.preview.bufnr, [], a:preview.tree_sitter_highlights)
127+
elseif has_key(a:preview, 'vim_syntax_info')
128+
let vim_syntax_info = a:preview.vim_syntax_info
129+
if !empty(vim_syntax_info.syntax)
130+
call g:clap.preview.set_syntax(vim_syntax_info.syntax)
131+
elseif !empty(vim_syntax_info.fname)
132+
call g:clap.preview.set_syntax(clap#ext#into_filetype(vim_syntax_info.fname))
133+
endif
129134
endif
130135
call clap#preview#highlight_header()
131136

crates/maple_core/src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ pub struct ProviderConfig {
223223
///
224224
/// If the theme is not found and the engine is [`HighlightEngine::SublimeSyntax`],
225225
/// the default theme (`Visual Studio Dark+`) will be used.
226-
pub preview_color_scheme: Option<String>,
226+
pub sublime_syntax_color_scheme: Option<String>,
227227

228228
/// Whether to share the input history of each provider.
229229
pub share_input_history: bool,

0 commit comments

Comments
 (0)