Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6e7b735

Browse files
committed
refactor(highlights): avoid unnecessary try catch
1 parent 66c6f9d commit 6e7b735

2 files changed

Lines changed: 11 additions & 16 deletions

File tree

src/__tests__/handler/highlights.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Neovim } from '@chemzqm/neovim'
2-
import { Disposable, Position, Range } from 'vscode-languageserver-protocol'
2+
import { Disposable, DocumentHighlightKind, Position, Range } from 'vscode-languageserver-protocol'
33
import Highlights from '../../handler/highlights'
44
import languages from '../../languages'
55
import workspace from '../../workspace'
@@ -33,11 +33,12 @@ function registProvider(): void {
3333
// let word = document.get
3434
let matches = Array.from((document.getText() as any).matchAll(/\w+/g)) as any[]
3535
let filtered = matches.filter(o => o[0] == word)
36-
return filtered.map(o => {
36+
return filtered.map((o, i) => {
3737
let start = document.positionAt(o.index)
3838
let end = document.positionAt(o.index + o[0].length)
3939
return {
40-
range: Range.create(start, end)
40+
range: Range.create(start, end),
41+
kind: i % 2 == 0 ? DocumentHighlightKind.Read : DocumentHighlightKind.Write
4142
}
4243
})
4344
}
@@ -121,7 +122,7 @@ describe('document highlights', () => {
121122
expect(err).toBeUndefined()
122123
})
123124

124-
it('should not throw when provide not found', async () => {
125+
it('should not throw when provider not found', async () => {
125126
disposeAll(disposables)
126127
await helper.createDocument()
127128
await nvim.setLine(' oo')

src/handler/highlights.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { CancellationTokenSource, Disposable, DocumentHighlight, DocumentHighlig
33
import events from '../events'
44
import languages from '../languages'
55
import Document from '../model/document'
6-
import { HandlerDelegate, CurrentState } from '../types'
6+
import { HandlerDelegate } from '../types'
77
import { disposeAll } from '../util'
88
import workspace from '../workspace'
99
const logger = require('../util/logger')('documentHighlight')
@@ -35,17 +35,11 @@ export default class Highlights {
3535
public async highlight(): Promise<void> {
3636
let { nvim } = this
3737
this.cancel()
38-
let state: CurrentState
39-
try {
40-
state = await this.handler.getCurrentState()
41-
this.handler.checkProvier('documentHighlight', state.doc.textDocument)
42-
} catch (e) {
43-
return
44-
}
45-
let { doc, winid, position } = state
46-
let cursors = await nvim.eval(`get(b:,'coc_cursors_activated',0)`) as number
47-
if (cursors) return
48-
let highlights = await this.getHighlights(doc, position)
38+
let [bufnr, winid, pos, cursors] = await nvim.eval(`[bufnr("%"),win_getid(),coc#util#cursor(),get(b:,'coc_cursors_activated',0)]`) as [number, number, [number, number], number]
39+
let doc = workspace.getDocument(bufnr)
40+
if (!doc || !doc.attached || cursors) return
41+
if (!languages.hasProvider('documentHighlight', doc.textDocument)) return
42+
let highlights = await this.getHighlights(doc, Position.create(pos[0], pos[1]))
4943
if (!highlights) return
5044
let groups: { [index: string]: Range[] } = {}
5145
for (let hl of highlights) {

0 commit comments

Comments
 (0)