Skip to content

Commit

Permalink
feat(configuration): rework configuration
Browse files Browse the repository at this point in the history
* Change root on file create
* Support configuration of scoped uri
* Move parse code to configurations model
* Add coc.preferences.openResourceCommand
  • Loading branch information
chemzqm committed Oct 18, 2018
1 parent ad65d4c commit 39bed90
Show file tree
Hide file tree
Showing 14 changed files with 306 additions and 255 deletions.
2 changes: 0 additions & 2 deletions autoload/coc/rpc.vim
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ function! coc#rpc#start_server()
if s:server_running | return | endif
let cmd = coc#util#job_command()
let $VIMCONFIG = coc#util#get_config_home()
let $VERSION = coc#util#version()
let $ROOTS = join(get(g:, 'rooter_patterns', []), ',')
if empty(cmd) | return | endif
if s:is_vim
let s:job = job_start(cmd, {
Expand Down
6 changes: 6 additions & 0 deletions data/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,12 @@
"type": "string"
}
},
"coc.preferences.openResourceCommand": {
"type": "string",
"description": "Command used for open resource.",
"default": "edit",
"enum": ["edit", "split", "vsplit", "tabe"]
},
"coc.preferences.jumpCommand": {
"type": "string",
"description": "Command used for location jump, like goto definition, goto references etc.",
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/completion/sources.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ describe('native sources', () => {

it('should works for omni source', async () => {
let buf = await helper.edit('omni.vim')
await helper.wait(100)
await helper.wait(200)
await nvim.input('icomm')
let opt = await buf.getOption('omnifunc') as string
expect(opt).toBe('syntaxcomplete#Complete')
await helper.wait(100)
await helper.wait(200)
let res = await helper.visible('command', 'omni')
expect(res).toBe(true)
})
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ export class Helper extends Emitter {

public async edit(file: string): Promise<Buffer> {
await this.nvim.command(`exe 'edit ' . fnameescape('${file}')`)
let buf = await this.nvim.buffer
await this.wait(50)
let buf = await this.nvim.buffer
let m = await this.nvim.mode
if (m.blocking) {
console.log('blocking') // tslint:disable-line
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/modules/document.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('document model properties', () => {
it('should parse iskeyword', async () => {
let doc = await helper.createDocument('foo')
await nvim.setLine('foo bar')
await helper.wait(100)
await helper.wait(200)
let words = doc.words
expect(words).toEqual(['foo', 'bar'])
})
Expand Down
12 changes: 0 additions & 12 deletions src/__tests__/modules/terminal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,4 @@ describe('terminal', () => {
let res = await t.resolveModule('typescript')
expect(typeof res).toBe('string')
})

test('terminal.installModule()', async () => {
let t = new Terminal(nvim)
; (workspace as any).nvim = nvim
if (platform.isMacintosh) {
let p = t.installModule('uid')
await wait(30)
await nvim.input('1<enter>')
let res = await p
expect(res).toMatch('uid')
}
}, 30000)
})
30 changes: 7 additions & 23 deletions src/__tests__/modules/workspace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ describe('workspace methods', () => {
file = workspace.getConfigFile(ConfigurationTarget.User)
expect(file).toBeTruthy()
file = workspace.getConfigFile(ConfigurationTarget.Workspace)
expect(file).toBeTruthy()
expect(file).toBeFalsy()
})

it('should create file watcher', async () => {
Expand Down Expand Up @@ -459,18 +459,6 @@ describe('workspace utility', () => {
expect(filepath).toMatch('tsconfig.json')
})

it('should show errors', async () => {
let uri = URI.file('/tmp/foo').toString()
let content = 'bar'
let errors = []
for (let i = 1; i < 17; i++) {
errors.push({ error: i, offset: 0, length: 1 })
}
await (workspace as any).showErrors(uri, content, errors)
let list = await nvim.call('getqflist', { title: 1 })
expect(list.title).toMatch('Errors of coc config')
})

it('should choose quickpick', async () => {
let p = workspace.showQuickpick(['a', 'b'])
await helper.wait(100)
Expand Down Expand Up @@ -572,10 +560,14 @@ describe('workspace events', () => {

it('should fire onDidChangeConfiguration', async () => {
let fn = jest.fn()
workspace.onDidChangeConfiguration(fn, null, disposables)
workspace.onDidChangeConfiguration(e => {
expect(e.affectsConfiguration('tsserver')).toBe(true)
expect(e.affectsConfiguration('tslint')).toBe(false)
fn()
}, null, disposables)
let config = workspace.getConfiguration('tsserver')
config.update('enable', false)
await helper.wait(500)
await helper.wait(2000)
expect(fn).toHaveBeenCalledTimes(1)
config.update('enable', undefined)
})
Expand Down Expand Up @@ -615,14 +607,6 @@ describe('workspace private', () => {
expect(doc.getline(0)).toMatch('abcdef')
})

it('should parse config with errors', async () => {
let uri = URI.file('/tmp/foo').toString()
await (workspace as any).parseConfig(uri, 'abc')
await helper.wait(100)
let list = await nvim.call('getqflist', { title: 1 })
expect(list.title).toMatch('Errors of coc config')
})

it('should detach buffers', async () => {
let buf = await helper.edit('foo')
expect(buf.isAttached).toBe(true)
Expand Down
6 changes: 3 additions & 3 deletions src/attach.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default function(opts: Attach): Plugin {
switch (method) {
case 'VimEnter':
plugin.init().catch(e => {
logger.error(e.message)
logger.error(e)
})
return
case 'CocAutocmd':
Expand All @@ -21,7 +21,7 @@ export default function(opts: Attach): Plugin {
case 'CocInstalled':
for (let name of args) {
extensions.onExtensionInstall(name).catch(e => {
logger.error(e.message)
logger.error(e)
})
}
return
Expand Down Expand Up @@ -57,7 +57,7 @@ export default function(opts: Attach): Plugin {
await nvim.setVar('coc_node_channel_id', channelId)
let entered = await nvim.getVvar('vim_did_enter')
if (entered) plugin.init().catch(e => {
logger.error(e.message)
logger.error(e)
})
}).catch(e => {
logger.error(e)
Expand Down
5 changes: 2 additions & 3 deletions src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,7 @@ export default class Handler {
return links
}

public async openLink(cmd: string): Promise<boolean> {
cmd = cmd || 'edit'
public async openLink(): Promise<boolean> {
let { document, position } = await workspace.getCurrentState()
let links = await languages.getDocumentLinks(document)
if (!links || links.length == 0) return false
Expand All @@ -395,7 +394,7 @@ export default class Handler {
target = link.target
}
if (target) {
await workspace.openResource(target, cmd)
await workspace.openResource(target)
return true
}
return false
Expand Down
10 changes: 4 additions & 6 deletions src/model/configurationShape.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
import { Neovim } from '@chemzqm/neovim'
import fs from 'fs'
import { applyEdits, modify } from 'jsonc-parser'
import { FormattingOptions } from 'vscode-languageserver-protocol'
import Uri from 'vscode-uri'
import { ConfigurationShape, ConfigurationTarget, IWorkspace } from '../types'
import { echoErr } from '../util'
const logger = require('../util/logger')('model-ConfigurationShape')

export default class ConfigurationProxy implements ConfigurationShape {
private formattingOptions: FormattingOptions

constructor(private workspace: IWorkspace) {
this.formattingOptions = { tabSize: 2, insertSpaces: true }
}

private get nvim(): Neovim {
return this.workspace.nvim
}

private async modifyConfiguration(target: ConfigurationTarget, key: string, value?: any): Promise<void> {

let { nvim } = this
let file = this.workspace.getConfigFile(target)
let content = ''
if (file) content = await this.workspace.readFile(Uri.file(file).toString())
let { formattingOptions } = this
if (!file) return
let formattingOptions = await this.workspace.getFormatOptions()
let content = await this.workspace.readFile(Uri.file(file).toString())
value = value == null ? undefined : value
let edits = modify(content, [key], value, { formattingOptions })
content = applyEdits(content, edits)
Expand Down
Loading

0 comments on commit 39bed90

Please sign in to comment.