From 6bb2e2127092315b7cb6cbef941147b4740cb74e Mon Sep 17 00:00:00 2001 From: Qiming Zhao Date: Fri, 1 Sep 2023 13:37:36 +0800 Subject: [PATCH] refactor(inlayHint): use api buffer.setVirtualText --- autoload/coc/vtext.vim | 12 ++++++++---- src/__tests__/handler/inlayHint.test.ts | 4 ++-- src/handler/inlayHint/buffer.ts | 15 ++++----------- 3 files changed, 14 insertions(+), 17 deletions(-) diff --git a/autoload/coc/vtext.vim b/autoload/coc/vtext.vim index e9529336adf..6c08c3c6d71 100644 --- a/autoload/coc/vtext.vim +++ b/autoload/coc/vtext.vim @@ -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. @@ -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 = ' ' @@ -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 @@ -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 diff --git a/src/__tests__/handler/inlayHint.test.ts b/src/__tests__/handler/inlayHint.test.ts index 6a640b75740..24c7b9227fc 100644 --- a/src/__tests__/handler/inlayHint.test.ts +++ b/src/__tests__/handler/inlayHint.test.ts @@ -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), @@ -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 diff --git a/src/handler/inlayHint/buffer.ts b/src/handler/inlayHint/buffer.ts index 8d403686e2a..506c7717e45 100644 --- a/src/handler/inlayHint/buffer.ts +++ b/src/handler/inlayHint/buffer.ts @@ -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 } @@ -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 @@ -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()