Skip to content

Commit

Permalink
feat(workspace): add workspace.registerKeymap
Browse files Browse the repository at this point in the history
  • Loading branch information
chemzqm committed Dec 11, 2018
1 parent d33f0d8 commit 18b62c1
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 2 deletions.
18 changes: 18 additions & 0 deletions src/__tests__/modules/workspace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,24 @@ describe('workspace private', () => {
})
})

describe('workspace keymaps', () => {
it('should regist keymap', async () => {
let fn = jest.fn()
await nvim.command('nmap go <Plug>(coc-echo)')
let disposable = workspace.registerKeymap(['n', 'v'], 'echo', fn)
await helper.wait(30)
let { mode } = await nvim.mode
expect(mode).toBe('n')
await nvim.call('feedkeys', ['go', 'i'])
await helper.wait(100)
expect(fn).toBeCalledTimes(1)
disposable.dispose()
await nvim.call('feedkeys', ['go', 'i'])
await helper.wait(100)
expect(fn).toBeCalledTimes(1)
})
})

describe('workspace textDocument content provider', () => {

it('should regist document content provider', async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/model/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export default class Document {
this._changedtick = opts.changedtick
this.eol = opts.eol == 1
let bufname = buftype == 'nofile' || opts.bufname == '' ? opts.bufname : opts.fullpath
let uri = getUri(bufname, buffer.id, buftype)
let uri = Uri.parse(getUri(bufname, buffer.id, buftype)).toString()
if (this.shouldAttach(buftype)) {
if (!this.env.isVim) {
let res = await this.attach()
Expand Down
6 changes: 6 additions & 0 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@ export default class Plugin extends EventEmitter {
return commandManager.commandList.map(o => o.id)
}

public async doKeymap(key: string): Promise<string[]> {
let fn = workspace.keymaps.get(key)
if (!fn) return
await Promise.resolve(fn())
}

public async cocInstalled(...names: string[]): Promise<void> {
for (let name of names) {
await extensions.onExtensionInstall(name)
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ export interface SnippetManager {

export type ModuleResolve = () => Promise<string>

export type MapMode = 'n' | 'i' | 'v' | 'x' | 's'

export enum SourceType {
Native,
Remote,
Expand Down
17 changes: 16 additions & 1 deletion src/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Resolver from './model/resolver'
import StatusLine from './model/status'
import WillSaveUntilHandler from './model/willSaveHandler'
import { TextDocumentContentProvider } from './provider'
import { ConfigurationChangeEvent, ConfigurationTarget, EditerState, Env, ErrorItem, IWorkspace, MessageLevel, MsgTypes, OutputChannel, QuickfixItem, StatusBarItem, StatusItemOption, TerminalResult, TextDocumentWillSaveEvent, WorkspaceConfiguration } from './types'
import { ConfigurationChangeEvent, ConfigurationTarget, EditerState, Env, ErrorItem, IWorkspace, MessageLevel, MsgTypes, OutputChannel, QuickfixItem, StatusBarItem, StatusItemOption, TerminalResult, TextDocumentWillSaveEvent, WorkspaceConfiguration, MapMode } from './types'
import { isFile, mkdirAsync, readFile, renameAsync, resolveRoot, statAsync, writeFile, readFileLine } from './util/fs'
import { disposeAll, echoErr, echoMessage, echoWarning, runCommand, wait } from './util/index'
import { score } from './util/match'
Expand All @@ -38,6 +38,7 @@ export class Workspace implements IWorkspace {
public readonly nvim: Neovim
public readonly version: string
public bufnr: number
public readonly keymaps: Map<string, Function> = new Map()
private resolver: Resolver = new Resolver()

private messageLevel: MessageLevel
Expand Down Expand Up @@ -745,6 +746,20 @@ export class Workspace implements IWorkspace {
})
}

public registerKeymap(modes: MapMode[], key: string, fn: Function): Disposable {
if (this.keymaps.has(key)) return
this.keymaps.set(key, fn)
for (let m of modes) {
this.nvim.command(`${m}noremap <Plug>(coc-${key}) :<C-u>call coc#rpc#notify('doKeymap', ['${key}'])<cr>`, true)
}
return Disposable.create(() => {
this.keymaps.delete(key)
for (let m of modes) {
this.nvim.command(`${m}unmap <Plug>(coc-${key})`, true)
}
})
}

public createStatusBarItem(priority = 0, opt: StatusItemOption = {}): StatusBarItem {
return this.statusLine.createStatusBarItem(priority, opt.progress || false)
}
Expand Down

0 comments on commit 18b62c1

Please sign in to comment.