Skip to content

Commit

Permalink
feat(handler): support codeLens.position
Browse files Browse the repository at this point in the history
Closes #3565
  • Loading branch information
chemzqm committed Jan 15, 2022
1 parent 82b6f14 commit ad3a116
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 15 deletions.
6 changes: 6 additions & 0 deletions data/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,12 @@
"description": "Enable codeLens feature, require neovim with set virtual text feature.",
"default": false
},
"codeLens.position": {
"type": "string",
"enum": ["top", "eol", "right_align"],
"description": "Position of codeLens, requires nvim >= 0.6.0",
"default": "top"
},
"codeLens.separator": {
"type": "string",
"description": "Separator text for codeLens in virtual text",
Expand Down
5 changes: 5 additions & 0 deletions doc/coc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,11 @@ Built-in configurations:~
Enable `codeLens` feature. Requires Neovim with virtual text feature,
default: `false`.

"codeLens.position":~

Position of codeLens, requires nvim >= 0.6.0, valid options ["top",
"eol", "right_align"], default: `top`.

"codeLens.separator":~

Separator text for `codeLens` in virtual text, default: `""`.
Expand Down
4 changes: 4 additions & 0 deletions history.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 2022-01-16

- Add configuration 'codeLens.position'.

# 2022-01-14

- Add configuration 'suggest.selection'.
Expand Down
41 changes: 36 additions & 5 deletions src/__tests__/handler/codelens.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ afterAll(async () => {
afterEach(async () => {
await helper.reset()
disposeAll(disposables)
disposables = []
})

describe('codeLenes featrue', () => {
Expand Down Expand Up @@ -60,6 +59,37 @@ describe('codeLenes featrue', () => {
expect(markers.length).toBe(2)
})

it('should change codeLenes position', async () => {
let fn = jest.fn()
helper.updateConfiguration('codeLens.position', 'eol')
disposables.push({
dispose: () => {
helper.updateConfiguration('codeLens.position', 'top')
}
})
disposables.push(commands.registerCommand('__save', (...args) => {
fn(...args)
}))
disposables.push(languages.registerCodeLensProvider([{ language: 'javascript' }], {
provideCodeLenses: () => {
return [{
range: Range.create(0, 0, 0, 1)
}]
},
resolveCodeLens: codeLens => {
codeLens.command = Command.create('save', '__save', 1, 2, 3)
return codeLens
}
}))
let doc = await helper.createDocument('example.js')
await nvim.call('setline', [1, ['a', 'b', 'c']])
await codeLens.checkProvider()
let res = await doc.buffer.getExtMarks(srcId, 0, -1, { details: true })
expect(res.length).toBeGreaterThan(0)
let arr = res[0][3]['virt_text']
expect(arr[0][0]).toBe('save')
})

it('should refresh codeLens on CursorHold', async () => {
disposables.push(languages.registerCodeLensProvider([{ language: 'javascript' }], {
provideCodeLenses: document => {
Expand Down Expand Up @@ -247,6 +277,11 @@ describe('codeLenes featrue', () => {
})

it('should refresh on configuration change', async () => {
disposables.push({
dispose: () => {
helper.updateConfiguration('codeLens.enable', true)
}
})
disposables.push(languages.registerCodeLensProvider([{ language: '*' }], {
provideCodeLenses: () => {
return [{
Expand All @@ -262,9 +297,5 @@ describe('codeLenes featrue', () => {
await helper.wait(10)
let markers = await helper.getMarkers(buffer.id, srcId)
expect(markers.length).toBe(0)
helper.updateConfiguration('codeLens.enable', true)
await helper.wait(300)
markers = await helper.getMarkers(buffer.id, srcId)
expect(markers.length).toBeGreaterThan(0)
})
})
22 changes: 14 additions & 8 deletions src/handler/codelens/buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface CodeLensInfo {
}

export interface CodeLensConfig {
position: 'top' | 'eol' | 'right_align'
enabled: boolean
separator: string
subseparator: string
Expand Down Expand Up @@ -138,6 +139,7 @@ export default class CodeLensBuffer implements BufferSyncItem {
private setVirtualText(codeLenses: CodeLens[]): void {
if (codeLenses.length == 0) return
let list: Map<number, CodeLens[]> = new Map()
let { position } = this.config
for (let codeLens of codeLenses) {
let { range, command } = codeLens
if (!command) continue
Expand Down Expand Up @@ -167,16 +169,20 @@ export default class CodeLensBuffer implements BufferSyncItem {
chunks.unshift([`${this.config.separator} `, 'CocCodeLens'])
}
if (workspace.has('nvim-0.6.0')) {
// get indent
let textLine = textDocument.lineAt(lnum)
let col = getIndentCols(textLine.text)
if (col) {
chunks.unshift([(new Array(col)).fill(' ').join(''), 'CocCodeLens'])
if (position == 'top') {
let col = getIndentCols(textLine.text)
if (col) chunks.unshift([(new Array(col)).fill(' ').join(''), 'CocCodeLens'])
buf.setExtMark(this.srcId, lnum, 0, {
virt_lines: [chunks],
virt_lines_above: true
})
} else {
buf.setExtMark(this.srcId, lnum, 0, {
virt_text: chunks,
virt_text_pos: position
})
}
buf.setExtMark(this.srcId, lnum, 0, {
virt_lines: [chunks],
virt_lines_above: true
})
} else {
this.nvim.call('nvim_buf_set_virtual_text', [this.bufnr, this.srcId, lnum, chunks, {}], true)
}
Expand Down
5 changes: 3 additions & 2 deletions src/handler/codelens/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ export default class CodeLensManager {
if (!workspace.isNvim) return
workspace.onDidChangeConfiguration(e => {
this.setConfiguration(e)
})
}, null, this.disposables)
this.buffers = workspace.registerBufferSync(doc => {
if (doc.buftype != '') return undefined
return new CodeLensBuffer(nvim, doc.bufnr, this.config)
})
this.disposables.push(this.buffers)
this.listen()
}

Expand Down Expand Up @@ -70,6 +71,7 @@ export default class CodeLensManager {
}
this.config = Object.assign(this.config || {}, {
enabled: enable,
position: config.get<'top' | 'eol'>('position', 'top'),
separator: config.get<string>('separator', '‣'),
subseparator: config.get<string>('subseparator', ' ')
})
Expand All @@ -82,7 +84,6 @@ export default class CodeLensManager {
}

public dispose(): void {
this.buffers.dispose()
disposeAll(this.disposables)
}
}

0 comments on commit ad3a116

Please sign in to comment.