Skip to content

Commit

Permalink
refactor(inlayHint): use api buffer.setVirtualText
Browse files Browse the repository at this point in the history
  • Loading branch information
chemzqm committed Sep 1, 2023
1 parent 0c2c64c commit 6bb2e21
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 17 deletions.
12 changes: 8 additions & 4 deletions autoload/coc/vtext.vim
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ let s:is_vim = !has('nvim')
let s:virtual_text_support = has('nvim-0.5.0') || has('patch-9.0.0067')
let s:text_options = has('patch-9.0.0121') || has('nvim-0.6.0')
let s:vim_above = has('patch-9.0.0438')
let s:n10 = has('nvim-0.10.0')

" This function is called by buffer.setVirtualText
" opts.hl_mode default to 'combine'.
" opts.col vim only, no support on neovim, default to 0.
" opts.col vim & nvim > 0.10.0, default to 0.
" opts.virt_text_win_col neovim only.
" opts.text_align could be 'after' 'right' 'below' 'above', converted on neovim.
" opts.text_wrap could be 'wrap' and 'truncate', vim9 only.
Expand All @@ -15,12 +16,12 @@ function! coc#vtext#add(bufnr, src_id, line, blocks, opts) abort
return
endif
let align = get(a:opts, 'text_align', 'after')
let column = get(a:opts, 'col', 0)
let indent = ''
if get(a:opts, 'indent', 0)
let indent = matchstr(getline(a:line + 1), '^\s\+')
endif
if s:is_vim
let column = get(a:opts, 'col', 0)
if !has_key(a:opts, 'col') && align ==# 'after'
" add a whitespace, same as neovim.
let indent = ' '
Expand Down Expand Up @@ -55,7 +56,9 @@ function! coc#vtext#add(bufnr, src_id, line, blocks, opts) abort
endif
else
let opts['virt_text'] = a:blocks
if align ==# 'right'
if s:n10 && column != 0
let opts['virt_text_pos'] = 'inline'
elseif align ==# 'right'
let opts['virt_text_pos'] = 'right_align'
else
if type(get(a:opts, 'virt_text_win_col', v:null)) == 0
Expand All @@ -73,7 +76,8 @@ function! coc#vtext#add(bufnr, src_id, line, blocks, opts) abort
let opts['virt_text_pos'] = 'overlay'
endif
endif
call nvim_buf_set_extmark(a:bufnr, a:src_id, a:line, 0, opts)
let col = s:n10 ? column - 1 : 0
call nvim_buf_set_extmark(a:bufnr, a:src_id, a:line, col, opts)
endif
endfunction

Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/handler/inlayHint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ describe('InlayHint', () => {
await nvim.setLine('foo bar')
let item = handler.getItem(doc.bufnr)
let r = Range.create(0, 0, 1, 0)
item.setVirtualText(r, [], true)
item.setVirtualText(r, [])
let hint: InlayHintWithProvider = {
label: 'string',
position: Position.create(0, 0),
Expand All @@ -283,7 +283,7 @@ describe('InlayHint', () => {
paddingLeft: true,
paddingRight: true
}
item.setVirtualText(r, [hint, paddingHint], true)
item.setVirtualText(r, [hint, paddingHint])
await helper.waitValue(async () => {
let markers = await doc.buffer.getExtMarks(ns, 0, -1, { details: true })
return markers.length
Expand Down
15 changes: 4 additions & 11 deletions src/handler/inlayHint/buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export default class InlayHintBuffer implements SyncItem {

public get enabled(): boolean {
if (!this.config.display || !this.configEnabled) return false
if (workspace.isNvim && !workspace.has('nvim-0.10.0') && !global.__TEST__) return false
return this.hasProvider
}

Expand Down Expand Up @@ -178,10 +179,10 @@ export default class InlayHintBuffer implements SyncItem {
}
this.currentHints = this.currentHints.filter(o => positionInRange(o.position, range) !== 0)
this.currentHints.push(...inlayHints)
this.setVirtualText(range, inlayHints, workspace.env.isVim)
this.setVirtualText(range, inlayHints)
}

public setVirtualText(range: Range, inlayHints: InlayHintWithProvider[], isVim: boolean): void {
public setVirtualText(range: Range, inlayHints: InlayHintWithProvider[]): void {
let { nvim, doc } = this
let buffer = doc.buffer

Expand All @@ -199,15 +200,7 @@ export default class InlayHintBuffer implements SyncItem {
if (item.paddingRight) {
chunks.push([' ', 'Normal'])
}
if (isVim) {
buffer.setVirtualText(srcId, position.line, chunks, { col })
} else if (workspace.has('nvim-0.10.0')) {
buffer.setExtMark(srcId, position.line, col - 1, {
virt_text: chunks,
virt_text_pos: 'inline',
hl_mode: 'combine'
})
}
buffer.setVirtualText(srcId, position.line, chunks, { col })
}
nvim.resumeNotification(true, true)
this._onDidRefresh.fire()
Expand Down

0 comments on commit 6bb2e21

Please sign in to comment.