{"id":419092,"date":"2021-03-22T18:34:51","date_gmt":"2021-03-22T10:34:51","guid":{"rendered":"http:\/\/4563.org\/?p=419092"},"modified":"2021-03-22T18:34:51","modified_gmt":"2021-03-22T10:34:51","slug":"%e6%96%87%e4%bb%b6%e8%87%aa%e5%8a%a8%e6%89%a7%e8%a1%8c%e6%8f%92%e4%bb%b6","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=419092","title":{"rendered":"\u6587\u4ef6\u81ea\u52a8\u6267\u884c\u63d2\u4ef6"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u6587\u4ef6\u81ea\u52a8\u6267\u884c\u63d2\u4ef6               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : chemzqm <\/span>  <span><i><\/i> 1<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>coc.nvim \u63d2\u4ef6\uff0c\u653e\u5728 <code>.vim\/coc-extensions<\/code> \u76ee\u5f55\u4e0b\u5373\u53ef\u4f7f\u7528\u3002<\/p>\n<p>\u53f3\u4fa7\u6253\u5f00\u7a97\u53e3\u663e\u793a\u6267\u884c\u4ee3\u7801\u7684\u7ed3\u679c\uff0c\u4fdd\u5b58\u540e\u81ea\u52a8\u91cd\u65b0\u6267\u884c\u3002<\/p>\n<ul>\n<li>\u4f7f\u7528\u5f02\u6b65 job \u6267\u884c <\/li>\n<li>\u652f\u6301 vim \u548c neovim<\/li>\n<li>\u9519\u8bef\u9ad8\u4eae\u63d0\u793a<\/li>\n<li>\u72b6\u6001\u680f\u652f\u6301<\/li>\n<\/ul>\n<p>stderr \u548c stdout \u51fa\u73b0\u987a\u5e8f\u53ef\u80fd\u4f1a\u9519\uff0c\u6682\u65f6\u4e0d\u652f\u6301 ansi \u89e3\u6790\uff0c\u53ef\u901a\u8fc7\u4f7f\u7528 strip-ansi \u6a21\u5757\u53bb\u6389\u3002<\/p>\n<pre><code>const {Uri, commands, workspace, window, 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 =&gt; {   const {nvim, cwd} = workspace   \/\/ bufnr =&gt; Task   const taskMap = new Map()    let statusItem = window.createStatusBarItem(0, {progress: true})   context.subscriptions.push(statusItem)    const executeFile = async (doc, create) =&gt; {     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 &amp;&amp; !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 =&gt; {       statusItem.hide()       taskMap.delete(doc.bufnr)       if (code != 0) {         window.showErrorMessage(`${cmd} exit with code: ${code}`)       }     })     let empty = true     let appendLines = async lines =&gt; {       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 =&gt; {       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 =&gt; {       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 () =&gt; {     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 =&gt; {     let doc = workspace.getDocument(e.uri)     if (!taskMap.has(doc.bufnr)) return     await executeFile(doc, false)   }))    context.subscriptions.push({     dispose: () =&gt; {       for (let task of taskMap.values()) {         task.dispose()       }     }   })    context.subscriptions.push(     commands.registerCommand('execute.currentFile', execute)   ) } <\/code><\/pre>\n<p>\u4ec5\u4f9b\u53c2\u8003<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>3<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5592929\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : IgniteWhite <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7ed9\u5927\u4f6c\u70b9\u8d5e                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5592930\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : chemzqm <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u591a\u4e86\u4e00\u884c `if (!taskMap.has(doc.bufnr)) return`                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5592931\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : yuuko <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6211\u8fd9\u91cc<\/p>\n<p>nvim.pauseNotification()<br \/>nvim.command(`belowright vs ${bufname}`)<\/p>\n<p>\u8981\u6539\u6210<\/p>\n<p>await nvim.command(`belowright vs ${bufname}`)<br \/>nvim.pauseNotification()                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u4ef6\u81ea\u52a8\u6267\u884c\u63d2\u4ef6 \u8cc7\u6df1\u5927\u4f6c : c&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/419092"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=419092"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/419092\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=419092"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=419092"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=419092"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}