使用 coc.nvim 整合自动构建工具

2019-06-25 11:59:28 +08:00
 chemzqm

优点

实现

以 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')

友情提示

4215 次点击
所在节点    Vim
6 条回复
weixiangzhe
2019-06-25 12:13:34 +08:00
支持下楼主,coc 解决好多问题,
yuuko
2019-06-25 12:16:35 +08:00
滋瓷,是不是可以搞成一个通用的列出 package.json 的 script command
chemzqm
2019-06-25 13:41:10 +08:00
@yuuko 当然可以,只是我不了解这方面的其它需求,这个够我用了
lancelock
2019-06-25 16:42:44 +08:00
vim 可以打断点 debug 吗? gdb 之类的用不来
ivechan
2019-06-25 18:13:11 +08:00
@lancelock

:packadd termdebug
:h :Termdebug

使用 GUI 版本的 vim,比如 gvim,应该是可以使用鼠标的。
Taigacute
2019-06-27 12:40:38 +08:00
👍

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/577212

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX