文件自动执行插件 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
chemzqm
V2EX    Vim

文件自动执行插件

  •  2
     
  •   chemzqm 2021-03-28 20:52:12 +08:00 2425 次点击
    这是一个创建于 1663 天前的主题,其中的信息可能已经有所发展或是发生改变。

    coc.nvim 插件,放在 .vim/coc-extensions 目录下即可使用。

    右侧打开窗口显示执行代码的结果,保存后自动重新执行。

    • 使用异步 job 执行
    • 支持 vim 和 neovim
    • 错误高亮提示
    • 状态栏支持

    stderr 和 stdout 出现顺序可能会错,暂时不支持 ansi 解析,可通过使用 strip-ansi 模块去掉。

    const {Uri, commands, workspace, windw, Mutex} = require('coc.nvim') const path = require('path') const programMap = { Javascript: 'node', typescript: 'ts-node', python: 'python' } let global_id = 0 exports.activate = async cOntext=> { const {nvim, cwd} = workspace // bufnr => Task const taskMap = new Map() let statusItem = window.createStatusBarItem(0, {progress: true}) context.subscriptions.push(statusItem) const executeFile = async (doc, create) => { let uri = doc.uri let relPath = path.relative(cwd, Uri.parse(uri).fsPath) let bufname = `__coc_execute_${doc.bufnr}__` let task = taskMap.get(doc.bufnr) if (task) { task.dispose() taskMap.delete(doc.bufnr) } statusItem.hide() let winnr = await nvim.call('bufwinnr', [bufname]) if (winnr == -1 && !create) return if (winnr == -1) { nvim.pauseNotification() nvim.command(`belowright vs ${bufname}`) nvim.command(`setl buftype=nofile`) nvim.command(`setl cOnceallevel=0`) nvim.command(`setl norelativenumber`) await nvim.resumeNotification() winnr = await nvim.call('winnr', []) await nvim.command('wincmd p') } else { // clear buffer await nvim.command(`silent call deletebufline('${bufname}', 1, '$')`) } let bufId = await nvim.call('bufnr', [bufname]) let buf = nvim.createBuffer(bufId) let t = workspace.createTask(`execute-${global_id}`) global_id = global_id + 1 let cmd = programMap[doc.filetype] // start with options let succeed = await t.start({cwd, cmd, args: [relPath]}) if (!succeed) { window.showErrorMessage(`Command failed to start: ${cmd} ${relPath}`) return } statusItem.text = `${cmd} ${relPath}` statusItem.show() taskMap.set(doc.bufnr, t) t.onExit(code => { statusItem.hide() taskMap.delete(doc.bufnr) if (code != 0) { window.showErrorMessage(`${cmd} exit with code: ${code}`) } }) let empty = true let appendLines = async lines => { if (empty) { empty = false await buf.setLines(lines, {start: 0, end: -1, strictIndexing: false}) } else { await nvim.call('appendbufline', [buf.id, '$', lines]) } } let mutex = new Mutex() t.onStderr(async lines => { let replace = empty let release = await mutex.acquire() try { let len = await buf.length await appendLines(lines) await buf.highlightRanges('coc-execute', 'WarningMsg', [{ start: {line: (replace ? len - 1 : len), character: 0}, end: {line: len + lines.length, character: 0} }]) if (workspace.isVim) nvim.command('redraw', true) } catch (e) { window.showErrorMessage(e.message) } release() }) t.onStdout(async lines => { let release = await mutex.acquire() try { await appendLines(lines) if (workspace.isVim) nvim.command('redraw', true) } catch (e) { window.showErrorMessage(e.message) } release() }) } const execute = async () => { let doc = await workspace.document let program = programMap[doc.filetype] if (!program) { window.showErrorMessage(`filetype not supported`) return } await executeFile(doc, true) } context.subscriptions.push(workspace.onDidSaveTextDocument(async e => { let doc = workspace.getDocument(e.uri) if (!taskMap.has(doc.bufnr)) return await executeFile(doc, false) })) context.subscriptions.push({ dispose: () => { for (let task of taskMap.values()) { task.dispose() } } }) context.subscriptions.push( commands.registerCommand('execute.currentFile', execute) ) } 

    仅供参考

    3 条回复    2021-03-29 17:20:37 +08:00
    IgniteWhite
        1
    IgniteWhite  
       2021-03-28 21:26:59 +08:00
    给大佬点赞
    chemzqm
        2
    chemzqm  
    OP
       2021-03-29 00:07:08 +08:00
    多了一行 `if (!taskMap.has(doc.bufnr)) return`
    yuuko
        3
    yuuko  
       2021-03-29 17:20:37 +08:00
    我这里

    nvim.pauseNotification()
    nvim.command(`belowright vs ${bufname}`)

    要改成

    await nvim.command(`belowright vs ${bufname}`)
    nvim.pauseNotification()
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     972 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 26ms UTC 22:42 PVG 06:42 LAX 15:42 JFK 18:42
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86