以 webpack 为例:
const {commands, workspace, Disposable} = require('coc.nvim')
const path = require('path')
const patternWebpack = /ERROR\sin\s(?<filename>\S+)\s(?<line>\d+):(?<col>\d+)/
const errorPattern = /ERROR\sin\s(?<filename>[^(]+)\((?<line>\d+),(?<col>\d+)\)/
exports.activate = context => {
let {nvim} = workspace
let statusItem = workspace.createStatusBarItem(1, {progress: true})
let task = workspace.createTask('WEBPACK')
let cwd
async function check() {
let running = await task.running
if (running) {
statusItem.isProgress = false
statusItem.text = '?'
statusItem.show()
} else {
statusItem.hide()
}
}
check().catch(_e => {
// noop
})
task.onExit(code => {
if (code != 0) {
workspace.showMessage(`Webpack exit with code ${code}`, 'warning')
}
statusItem.hide()
})
task.onStdout(lines => {
let i = 0
let items = []
for (let line of lines) {
if (line.indexOf('ERROR') !== -1) {
let res = patternWebpack.exec(line)
if (res == null) {
res = errorPattern.exec(line)
}
if (res != null) {
let {filename} = res.groups
if (!path.isAbsolute(filename)) {
filename = path.join(cwd, filename)
}
items.push({
filename,
lnum: parseInt(res.groups.line),
col: parseInt(res.groups.col),
text: lines[i + 1].trim(),
type: 'E'
})
}
}
i++
}
nvim.call('setqflist', [items], true)
statusItem.text = items.length == 0 ? '✓' : '✗'
statusItem.isProgress = false
})
task.onStderr(lines => {
for (let line of lines) {
if (line.match(/webpack\sis\swatching/)) {
statusItem.text = 'watching'
}
}
})
context.subscriptions.push(Disposable.create(() => {
task.dispose()
}))
context.subscriptions.push(commands.registerCommand('webpack.watch', async () => {
cwd = workspace.cwd
task.start({
cmd: 'webpack',
args: ['--watch', '--no-color'],
cwd: workspace.cwd
})
statusItem.show()
}))
}
将文件保存为 $VIMCONFIG/coc-extensions/webpack.js
,设置自定义 Webpack 命令 ( vim 中执行 :echo $VIMCONFIG
查看 $VIMCONFIG )
command! -nargs=0 Webpack :call CocAction('runCommand', 'webpack.watch')
webpack --watch
, 而不是保存完重现构建,效率高一些tsserver.watchBuild
命令执行 tsc --watch
构建这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.