Skip to content

Commit e287d24

Browse files
authored
Use separate namespaces for document and diagnostic highlights (#1145)
1 parent 17a579d commit e287d24

File tree

3 files changed

+23
-24
lines changed

3 files changed

+23
-24
lines changed

autoload/LanguageClient.vim

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -337,10 +337,11 @@ function! s:ApplySemanticHighlights(bufnr, ns_id, clears, highlights) abort
337337
endfunction
338338

339339
" Batch version of nvim_buf_add_highlight
340-
function! s:AddHighlights(namespace_id, highlights) abort
340+
function! s:AddHighlights(namespace, highlights) abort
341341
if has('nvim')
342+
let l:namespace_id = nvim_create_namespace(a:namespace)
342343
for hl in a:highlights
343-
call nvim_buf_add_highlight(0, a:namespace_id, hl.group, hl.line, hl.character_start, hl.character_end)
344+
call nvim_buf_add_highlight(0, l:namespace_id, hl.group, hl.line, hl.character_start, hl.character_end)
344345
endfor
345346
else
346347
let match_ids = []
@@ -349,32 +350,27 @@ function! s:AddHighlights(namespace_id, highlights) abort
349350
let match_ids = add(match_ids, match_id)
350351
endfor
351352

352-
call setbufvar(bufname(), 'document_highlight_match_ids', match_ids)
353+
call setbufvar(bufname(), a:namespace . '_IDS', match_ids)
353354
endif
354355
endfunction
355356

356-
function! s:SetHighlights(highlights) abort
357-
call s:ClearHighlights()
358-
if has('nvim')
359-
let s:namespace_id = nvim_create_namespace('__LCN_DOCUMENT_HIGHLIGHTS__')
360-
call s:AddHighlights(s:namespace_id, a:highlights)
361-
else
362-
call s:AddHighlights(0, a:highlights)
363-
endif
357+
function! s:SetHighlights(highlights, namespace) abort
358+
call s:ClearHighlights(a:namespace)
359+
call s:AddHighlights(a:namespace, a:highlights)
364360
endfunction
365361

366-
function! s:ClearHighlights() abort
362+
function! s:ClearHighlights(namespace) abort
367363
if has('nvim')
368-
let s:namespace_id = nvim_create_namespace('__LCN_DOCUMENT_HIGHLIGHTS__')
369-
call nvim_buf_clear_namespace(0, s:namespace_id, 0, -1)
364+
let l:namespace_id = nvim_create_namespace(a:namespace)
365+
call nvim_buf_clear_namespace(0, l:namespace_id, 0, -1)
370366
else
371-
let match_ids = get(b:, 'document_highlight_match_ids', [])
367+
let match_ids = get(b:, a:namespace . '_IDS', [])
372368
for mid in match_ids
373369
" call inside a try/catch to avoid error for manually cleared matches
374370
try | call matchdelete(mid) | catch
375371
endtry
376372
endfor
377-
call setbufvar(bufname(), 'document_highlight_match_ids', [])
373+
call setbufvar(bufname(), a:namespace . '_IDS', [])
378374
endif
379375
endfunction
380376

src/language_server_protocol.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -510,15 +510,16 @@ impl LanguageClient {
510510
})
511511
.collect::<Result<Vec<_>>>()?;
512512

513-
self.vim()?.set_highlights(&highlights)?;
513+
self.vim()?
514+
.set_highlights(&highlights, "__LCN_DOCUMENT_HIGHLIGHT__")?;
514515
}
515516

516517
Ok(result)
517518
}
518519

519520
#[tracing::instrument(level = "info", skip(self))]
520521
pub fn clear_document_highlight(&self, _params: &Value) -> Result<()> {
521-
self.vim()?.clear_highlights()
522+
self.vim()?.clear_highlights("__LCN_DOCUMENT_HIGHLIGHT__")
522523
}
523524

524525
#[tracing::instrument(level = "info", skip(self))]
@@ -3117,7 +3118,8 @@ impl LanguageClient {
31173118
.collect())
31183119
})?;
31193120

3120-
self.vim()?.set_highlights(&highlights)?;
3121+
self.vim()?
3122+
.set_highlights(&highlights, "__LCN_DIAGNOSTIC_HIGHLIGHT__")?;
31213123
self.draw_virtual_texts(&params)?;
31223124

31233125
Ok(())

src/vim.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -239,18 +239,19 @@ impl Vim {
239239
}
240240

241241
/// clears all highlights in the current buffer.
242-
pub fn clear_highlights(&self) -> Result<()> {
243-
self.rpcclient.notify("s:ClearHighlights", json!([]))
242+
pub fn clear_highlights(&self, namespace: &str) -> Result<()> {
243+
self.rpcclient
244+
.notify("s:ClearHighlights", json!([namespace]))
244245
}
245246

246247
/// replaces the highlights of the current document with the passed highlights.
247-
pub fn set_highlights(&self, highlights: &[Highlight]) -> Result<()> {
248+
pub fn set_highlights(&self, highlights: &[Highlight], namespace: &str) -> Result<()> {
248249
if highlights.is_empty() {
249-
return Ok(());
250+
return self.clear_highlights(namespace);
250251
}
251252

252253
self.rpcclient
253-
.notify("s:SetHighlights", json!([highlights]))
254+
.notify("s:SetHighlights", json!([highlights, namespace]))
254255
}
255256

256257
pub fn create_namespace(&self, name: &str) -> Result<i64> {

0 commit comments

Comments
 (0)