Last active
May 29, 2021 00:05
-
-
Save chemzqm/e22ddf489ec24c7d21bbcb047f4b3f86 to your computer and use it in GitHub Desktop.
repl with coc.nvim
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Save the file to ~/.vim/coc-extensions | |
// Usage: xmap <silent> <TAB> <Plug>(coc-repl-sendtext) | |
const {commands, workspace} = require('coc.nvim') | |
exports.activate = context => { | |
let {nvim} = workspace | |
let terminal | |
context.subscriptions.push(commands.registerCommand('repl.openTerminal', async () => { | |
let filetype = await nvim.eval('&filetype') | |
let prog = '' | |
if (filetype == 'javascript') { | |
prog = 'node' | |
} else if (filetype == 'typescript') { | |
prog = 'ts-node' | |
} else if (filetype == 'python') { | |
prog = 'python' | |
} | |
terminal = await workspace.createTerminal({ | |
name: prog || 'terminal' | |
}) | |
if (prog) { | |
terminal.sendText(prog, true) | |
} | |
})) | |
context.subscriptions.push(commands.registerCommand('repl.showTerminal', async () => { | |
if (terminal) { | |
terminal.show() | |
} | |
})) | |
context.subscriptions.push(commands.registerCommand('repl.disposeTerminal', async () => { | |
if (terminal) { | |
terminal.dispose() | |
} | |
})) | |
context.subscriptions.push(workspace.registerKeymap(['x'], 'repl-sendtext', async () => { | |
await nvim.call('eval', 'feedkeys("\\<esc>", "in")') | |
let doc = workspace.getDocument(workspace.bufnr) | |
if (!doc) return | |
let visualmode = await nvim.call('visualmode') | |
let range = await workspace.getSelectedRange(visualmode, doc) | |
if (!range) return | |
let text = doc.textDocument.getText(range) | |
if (text && terminal) terminal.sendText(text, true) | |
}, {sync: false, silent: true})) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No idea, the log is not complete.