Skip to content

Commit

Permalink
feat(inlayHint): inline display for nvim (#4648)
Browse files Browse the repository at this point in the history
  • Loading branch information
fannheyward committed Aug 31, 2023
1 parent 1038082 commit d81fde9
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 85 deletions.
18 changes: 0 additions & 18 deletions data/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1307,30 +1307,12 @@
"type": "string"
}
},
"inlayHint.parameterSeparator": {
"type": "string",
"scope": "language-overridable",
"default": "",
"description": "Separator for parameter inlay hint, neovim only."
},
"inlayHint.refreshOnInsertMode": {
"type": "boolean",
"default": false,
"scope": "language-overridable",
"description": "Refresh inlayHints on insert mode."
},
"inlayHint.subSeparator": {
"type": "string",
"scope": "language-overridable",
"default": " ",
"description": "Separator for chained inlay hints, neovim only."
},
"inlayHint.typeSeparator": {
"type": "string",
"scope": "language-overridable",
"default": "",
"description": "Separator for type inlay hint, neovim only."
},
"links.enable": {
"type": "boolean",
"scope": "language-overridable",
Expand Down
18 changes: 0 additions & 18 deletions doc/coc-config.txt
Original file line number Diff line number Diff line change
Expand Up @@ -539,30 +539,12 @@ InlayHint~

Scope: `language-overridable`, default: `true`

"inlayHint.parameterSeparator" *coc-config-inlayHint-parameterSeparator*

Separator for parameter inlay hint, neovim only.

Scope: `language-overridable`, default: `""`

"inlayHint.refreshOnInsertMode" *coc-config-inlayHint-refreshOnInsertMode*

Refresh inlayHints on insert mode.

Scope: `language-overridable`, default: `false`

"inlayHint.subSeparator" *coc-config-inlayHint-subSeparator*

Separator for chained inlay hints, neovim only.

Scope: `language-overridable`, default: `" "`

"inlayHint.typeSeparator" *coc-config-inlayHint-typeSeparator*

Separator for type inlay hint, neovim only.

Scope: `language-overridable`, default: `""`

------------------------------------------------------------------------------
Links~
*coc-config-links*
Expand Down
11 changes: 0 additions & 11 deletions src/__tests__/handler/inlayHint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,17 +226,6 @@ describe('InlayHint', () => {
let markers = await doc.buffer.getExtMarks(ns, 0, -1, { details: true })
expect(markers.length).toBe(1)
})

it('should use custom subseparator', async () => {
helper.updateConfiguration('inlayHint.subSeparator', '|')
let doc = await helper.createDocument()
let disposable = await registerProvider('foo bar')
disposables.push(disposable)
await waitRefresh(doc.bufnr)
let markers = await doc.buffer.getExtMarks(ns, 0, -1, { details: true })
let virt_text = markers[0][3].virt_text
expect(virt_text[1]).toEqual(['|', 'CocInlayHintType'])
})
})

describe('toggle inlayHint', () => {
Expand Down
58 changes: 20 additions & 38 deletions src/handler/inlayHint/buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ export interface InlayHintConfig {
filetypes: string[]
refreshOnInsertMode: boolean
enableParameter: boolean
typeSeparator: string
parameterSeparator: string
subSeparator: string
}

let srcId: number | undefined
Expand Down Expand Up @@ -74,9 +71,6 @@ export default class InlayHintBuffer implements SyncItem {
filetypes: config.get<string[]>('filetypes'),
refreshOnInsertMode: config.get<boolean>('refreshOnInsertMode'),
enableParameter: config.get<boolean>('enableParameter'),
typeSeparator: config.get<string>('typeSeparator', ''),
parameterSeparator: config.get<string>('parameterSeparator', ''),
subSeparator: config.get<string>('subSeparator', ' ')
}
if (changed) {
let { enable, display } = this._config
Expand Down Expand Up @@ -190,42 +184,30 @@ export default class InlayHintBuffer implements SyncItem {
public setVirtualText(range: Range, inlayHints: InlayHintWithProvider[], isVim: boolean): void {
let { nvim, doc } = this
let buffer = doc.buffer
let { subSeparator, parameterSeparator, typeSeparator } = this.config
const chunksMap: Map<number, [string, string][]> = new Map()
if (!isVim) {
for (const item of inlayHints) {
let { line } = item.position
const chunks: [string, string][] = chunksMap.get(line) ?? []
if (chunks.length > 0) {
chunks.push([subSeparator, subSeparator === ' ' ? 'Normal' : getHighlightGroup(item.kind)])
}
let sep = item.kind === InlayHintKind.Parameter ? parameterSeparator : typeSeparator
chunks.push([sep + getLabel(item), getHighlightGroup(item.kind)])
chunksMap.set(line, chunks)
}
}

nvim.pauseNotification()
buffer.clearNamespace(srcId, range.start.line, range.end.line + 1)
if (isVim) {
for (const item of inlayHints) {
const chunks: [string, string][] = []
let { position } = item
let line = this.doc.getline(position.line)
let col = byteIndex(line, position.character) + 1
if (item.paddingLeft) {
chunks.push([' ', 'Normal'])
}
chunks.push([getLabel(item), getHighlightGroup(item.kind)])
if (item.paddingRight) {
chunks.push([' ', 'Normal'])
}
buffer.setVirtualText(srcId, position.line, chunks, { col })
for (const item of inlayHints) {
const chunks: [string, string][] = []
let { position } = item
let line = this.doc.getline(position.line)
let col = byteIndex(line, position.character) + 1
if (item.paddingLeft) {
chunks.push([' ', 'Normal'])
}
} else {
for (let [line, chunks] of chunksMap.entries()) {
buffer.setExtMark(srcId, line, 0, {
chunks.push([getLabel(item), getHighlightGroup(item.kind)])
if (item.paddingRight) {
chunks.push([' ', 'Normal'])
}
if (isVim) {
buffer.setVirtualText(srcId, position.line, chunks, { col })
} else {
buffer.setExtMark(srcId, position.line, col - 1, {
virt_text: chunks,
virt_text_pos: 'eol',
// TODO: needs @chemzqm/neovim to support virt_text_pos inline, disable the error alert for now
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
virt_text_pos: 'inline',
hl_mode: 'combine'
})
}
Expand Down

0 comments on commit d81fde9

Please sign in to comment.