Skip to content

Commit

Permalink
feat(list): refresh diagnostics list on diagnostics change
Browse files Browse the repository at this point in the history
Add more tests
  • Loading branch information
chemzqm committed Jan 25, 2022
1 parent 93aa965 commit ca8ca2a
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 32 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ jobs:
run: |
sudo apt-get install ripgrep -y
rg --version
sudo apt-get install ctags -y
curl -LO https://github.com/neovim/neovim/releases/download/v0.6.0/nvim-linux64.tar.gz
tar xzf nvim-linux64.tar.gz
export PATH="${PATH}:node_modules/.bin:$(pwd)/nvim-linux64/bin"
Expand Down
172 changes: 153 additions & 19 deletions src/__tests__/list/sources.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { Neovim } from '@chemzqm/neovim'
import { ListContext, ListItem, ListArgument } from '../../types'
import manager from '../../list/manager'
import { CancellationToken, Emitter } from 'vscode-jsonrpc'
import { Diagnostic, DiagnosticSeverity, Disposable, Location, Range } from 'vscode-languageserver-protocol'
import diagnosticManager from '../../diagnostic/manager'
import languages from '../../languages'
import helper from '../helper'
import workspace from '../../workspace'
import { CancellationToken } from 'vscode-jsonrpc'
import { Location, Range } from 'vscode-languageserver-types'
import BasicList from '../../list/basic'
import manager from '../../list/manager'
import Document from '../../model/document'
import services, { IServiceProvider } from '../../services'
import { ListArgument, ListContext, ListItem, ServiceStat } from '../../types'
import { disposeAll } from '../../util'
import workspace from '../../workspace'
import helper from '../helper'

let listItems: ListItem[] = []
class OptionList extends BasicList {
Expand All @@ -27,8 +31,8 @@ class OptionList extends BasicList {
return Promise.resolve(listItems)
}
}
jest.setTimeout(3000)

let disposables: Disposable[] = []
let nvim: Neovim
beforeAll(async () => {
await helper.setup()
Expand All @@ -41,9 +45,9 @@ afterAll(async () => {
})

afterEach(async () => {
disposeAll(disposables)
manager.reset()
await helper.reset()
await helper.wait(100)
})

describe('BasicList', () => {
Expand Down Expand Up @@ -101,31 +105,98 @@ describe('list sources', () => {
describe('commands', () => {
it('should load commands source', async () => {
await manager.start(['commands'])
await helper.wait(100)
await manager.session?.ui.ready
expect(manager.isActivated).toBe(true)
})

it('should do run action', async () => {
await manager.start(['commands'])
await helper.wait(100)
await manager.session?.ui.ready
await manager.doAction()
})
})

describe('diagnostics', () => {

function createDiagnostic(msg: string, range?: Range, severity?: DiagnosticSeverity, code?: number): Diagnostic {
range = range ? range : Range.create(0, 0, 0, 1)
return Diagnostic.create(range, msg, severity || DiagnosticSeverity.Error, code)
}

async function createDocument(name?: string): Promise<Document> {
let doc = await helper.createDocument(name)
let collection = diagnosticManager.create('test')
disposables.push({
dispose: () => {
collection.clear()
collection.dispose()
}
})
let diagnostics: Diagnostic[] = []
await doc.buffer.setLines(['foo bar foo bar', 'foo bar', 'foo', 'bar'], {
start: 0,
end: -1,
strictIndexing: false
})
diagnostics.push(createDiagnostic('error', Range.create(0, 2, 0, 4), DiagnosticSeverity.Error, 1001))
diagnostics.push(createDiagnostic('warning', Range.create(0, 5, 0, 6), DiagnosticSeverity.Warning, 1002))
diagnostics.push(createDiagnostic('information', Range.create(1, 0, 1, 1), DiagnosticSeverity.Information, 1003))
diagnostics.push(createDiagnostic('hint', Range.create(1, 2, 1, 3), DiagnosticSeverity.Hint, 1004))
diagnostics.push(createDiagnostic('error', Range.create(2, 0, 2, 2), DiagnosticSeverity.Error, 1005))
collection.set(doc.uri, diagnostics)
doc.forceSync()
return doc
}

it('should load diagnostics source', async () => {
await createDocument('a')
await manager.start(['diagnostics'])
await manager.session?.ui.ready
expect(manager.isActivated).toBe(true)
})

it('should not include code', async () => {
let fn = helper.updateConfiguration('list.source.diagnostics.includeCode', false)
disposables.push({ dispose: fn })
await createDocument('a')
await manager.start(['diagnostics'])
await manager.session?.ui.ready
expect(manager.isActivated).toBe(true)
let line = await nvim.line
expect(line.match(/100/)).toBeNull()
})

it('should hide file path', async () => {
helper.updateConfiguration('list.source.diagnostics.pathFormat', 'hidden')
await createDocument('foo')
await manager.start(['diagnostics'])
await manager.session?.ui.ready
await helper.wait(100)
expect(manager.isActivated).toBe(true)
let line = await nvim.line
expect(line.match(/foo/)).toBeNull()
helper.updateConfiguration('list.source.diagnostics.pathFormat', 'full')
})

it('should refresh on diagnostics refresh', async () => {
let doc = await createDocument('bar')
await manager.start(['diagnostics'])
await manager.session?.ui.ready
expect(manager.isActivated).toBe(true)
let diagnostics: Diagnostic[] = []
let collection = diagnosticManager.create('test')
diagnostics.push(createDiagnostic('error', Range.create(2, 0, 2, 2), DiagnosticSeverity.Error, 1009))
collection.set(doc.uri, diagnostics)
await helper.wait(50)
let buf = await nvim.buffer
let lines = await buf.lines
expect(lines.length).toBe(1)
})
})

describe('extensions', () => {
it('should load extensions source', async () => {
await manager.start(['extensions'])
await manager.session?.ui.ready
await helper.wait(100)
expect(manager.isActivated).toBe(true)
})
})
Expand All @@ -134,7 +205,6 @@ describe('list sources', () => {
it('should load folders source', async () => {
await manager.start(['folders'])
await manager.session?.ui.ready
await helper.wait(100)
expect(manager.isActivated).toBe(true)
})
})
Expand All @@ -143,35 +213,101 @@ describe('list sources', () => {
it('should load lists source', async () => {
await manager.start(['lists'])
await manager.session?.ui.ready
await helper.wait(100)
expect(manager.isActivated).toBe(true)
await helper.listInput('<cr>')
let s = manager.getSession()
expect(s.name != 'lists').toBe(true)
})
})

describe('outline', () => {
it('should load outline source', async () => {
await manager.start(['outline'])
await manager.session?.ui.ready
await helper.wait(100)
expect(manager.isActivated).toBe(true)
})
})

describe('services', () => {
function createService(name: string): IServiceProvider {
let _onServcieReady = new Emitter<void>()
// public readonly onServcieReady: Event<void> = this.
let service: IServiceProvider = {
id: name,
name,
selector: [{ language: 'vim' }],
state: ServiceStat.Initial,
start(): Promise<void> {
service.state = ServiceStat.Running
_onServcieReady.fire()
return Promise.resolve()
},
dispose(): void {
service.state = ServiceStat.Stopped
},
stop(): void {
service.state = ServiceStat.Stopped
},
restart(): void {
service.state = ServiceStat.Running
_onServcieReady.fire()
},
onServiceReady: _onServcieReady.event
}
disposables.push(services.regist(service))
return service
}

it('should load services source', async () => {
createService('foo')
createService('bar')
await manager.start(['services'])
await manager.session?.ui.ready
expect(manager.isActivated).toBe(true)
let lines = await nvim.call('getline', [1, '$']) as string[]
expect(lines.length).toBe(2)
})

it('should toggle service state', async () => {
let service = createService('foo')
await service.start()
await manager.start(['services'])
await manager.session?.ui.ready
await helper.wait(100)
expect(manager.isActivated).toBe(true)
let ses = manager.session
expect(ses.name).toBe('services')
await ses.doAction('toggle')
expect(service.state).toBe(ServiceStat.Stopped)
await ses.doAction('toggle')
})
})

describe('sources', () => {
it('should load sources source', async () => {
await manager.start(['sources'])
await manager.session?.ui.ready
await helper.wait(100)
expect(manager.isActivated).toBe(true)
let session = manager.getSession()
await session.doAction('open')
let bufname = await nvim.call('bufname', '%')
expect(bufname).toMatch(/native/)
})

it('should toggle source state', async () => {
await manager.start(['sources'])
await manager.session?.ui.ready
expect(manager.isActivated).toBe(true)
let session = manager.getSession()
await session.doAction('toggle')
await session.doAction('toggle')
})

it('should refresh source', async () => {
await manager.start(['sources'])
await manager.session?.ui.ready
expect(manager.isActivated).toBe(true)
let session = manager.getSession()
await session.doAction('refresh')
})
})

Expand All @@ -182,7 +318,6 @@ describe('list sources', () => {
})
await manager.start(['symbols'])
await manager.session?.ui.ready
await helper.wait(100)
expect(manager.isActivated).toBe(true)
disposable.dispose()
})
Expand All @@ -195,7 +330,6 @@ describe('list sources', () => {
})
await manager.start(['links'])
await manager.session?.ui.ready
await helper.wait(100)
expect(manager.isActivated).toBe(true)
disposable.dispose()
})
Expand Down
4 changes: 2 additions & 2 deletions src/list/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class ListManager implements Disposable {
this.registerList(new OutlineList(nvim))
this.registerList(new CommandsList(nvim))
this.registerList(new ExtensionList(nvim))
this.registerList(new DiagnosticsList(nvim))
this.registerList(new DiagnosticsList(nvim, this))
this.registerList(new SourcesList(nvim))
this.registerList(new ServicesList(nvim))
this.registerList(new ListsList(nvim, this.listMap))
Expand Down Expand Up @@ -163,7 +163,7 @@ export class ListManager implements Disposable {
if (s) await s.next()
}

private getSession(name?: string): ListSession {
public getSession(name?: string): ListSession {
if (!name) return this.session
return this.sessionsMap.get(name)
}
Expand Down
11 changes: 10 additions & 1 deletion src/list/source/diagnostics.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
import { Neovim } from '@chemzqm/neovim'
import path from 'path'
import diagnosticManager from '../../diagnostic/manager'
import { ListContext, ListItem } from '../../types'
import LocationList from './location'
import { isParentFolder } from '../../util/fs'
import { formatListItems, formatPath, PathFormatting, UnformattedListItem } from '../formatting'
import { ListManager } from '../manager'
import LocationList from './location'
const logger = require('../../util/logger')('list-symbols')

export default class DiagnosticsList extends LocationList {
public readonly defaultAction = 'open'
public readonly description = 'diagnostics of current workspace'
public name = 'diagnostics'
public constructor(nvim: Neovim, manager: ListManager) {
super(nvim)
diagnosticManager.onDidRefresh(async () => {
let session = manager.getSession('diagnostics')
if (session) await session.reloadItems()
}, null, this.disposables)
}

public async loadItems(context: ListContext): Promise<ListItem[]> {
let list = diagnosticManager.getDiagnosticList()
Expand Down
6 changes: 2 additions & 4 deletions src/list/source/lists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import BasicList from '../basic'
import Mru from '../../model/mru'
import { formatListItems, UnformattedListItem } from '../formatting'

export default class LinksList extends BasicList {
export default class ListsList extends BasicList {
public readonly name = 'lists'
public readonly defaultAction = 'open'
public readonly description = 'registered lists of coc.nvim'
Expand Down Expand Up @@ -43,9 +43,7 @@ export default class LinksList extends BasicList {
nvim.pauseNotification()
nvim.command('syntax match CocListsDesc /\\t.*$/ contained containedin=CocListsLine', true)
nvim.command('highlight default link CocListsDesc Comment', true)
nvim.resumeNotification().catch(_e => {
// noop
})
void nvim.resumeNotification()
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/list/source/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ export default class ServicesList extends BasicList {
nvim.command('highlight default link CocServicesName Type', true)
nvim.command('highlight default link CocServicesStat Statement', true)
nvim.command('highlight default link CocServicesLanguages Comment', true)
nvim.resumeNotification().catch(_e => {
// noop
})
void nvim.resumeNotification()
}
}
4 changes: 1 addition & 3 deletions src/list/source/symbols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,6 @@ export default class Symbols extends LocationList {
nvim.command('highlight default link CocSymbolsName Normal', true)
nvim.command('highlight default link CocSymbolsKind Typedef', true)
nvim.command('highlight default link CocSymbolsFile Comment', true)
nvim.resumeNotification().catch(_e => {
// noop
})
void nvim.resumeNotification()
}
}

0 comments on commit ca8ca2a

Please sign in to comment.