概要
Visual Studio Code(以下VSCode)ではタスク管理に.vscode/tasks.jsonを、起動やデバッグに.vscode/launch.jsonへ書いて実行することが多いと思います。当然ですがNeovimではこれらのタスクをそのまま実行することはできませんので、Neovimを使う際はmakefileやシェルスクリプトなどにタスクを書いているのではないでしょうか。
VSCodeとNeovim(拡張機能でない)を同時に使用している筆者のような場合や共同プロジェクトの場合、NeovimからVSCodeのタスクランナーが使用できれば管理しやすいのではないかと思います。
今回は、NeovimからVSCode用のタスクを実行する方法を紹介します。
使用するプラグインなど
- "mfussenegger/nvim-dap"
Debug Adapter Protocolでデバッグを可能にします。拡張機能で.vscode/launch.jsonを読み込めます。 - "rcarriga/nvim-dap-ui"
(省略可) nvim-dapをGUI上で操作可能にします。あるととても便利です。 - "EthanJWright/vs-tasks.nvim"
VSCode用のタスクランナー(task.json)を実行します。- "nvim-lua/popup.nvim"
- "nvim-lua/plenary.nvim"
- "nvim-telescope/telescope.nvim"
vs-tasks.nvimの依存です。(ポップアップウィンドウ出したりなど)
- launchに使用するための各言語のLSP(C++ならcodelldbなど) LSPについてはこちらを参照してください。
インストール
筆者はプラグインマネージャにLazy.nvimを使用しているのでその例です。
またDapの設定はC++で使用するCodeLLDBの設定です。
{ { -- debug "mfussenegger/nvim-dap", dependencies = { -- 無くても可 "rcarriga/nvim-dap-ui", }, config = function() -- 設定1 end, }, { -- vscode tasks "EthanJWright/vs-tasks.nvim", dependencies = { "nvim-lua/popup.nvim", "nvim-lua/plenary.nvim", "nvim-telescope/telescope.nvim", }, }, -- 設定2 }
設定
nvim-dap.nvim(設定1)
config = function() -- 設定1 local dap = require("dap") dap.adapters = { -- LSPの設定 lldb = { type = "server", port = "${port}", executable = { command = vim.fn.stdpath("data") .. "/mason/packages/codelldb/extension/adapter/codelldb", args = { "--port", "${port}" }, }, }, } dap.configurations = { cpp = {}, } -- -- キーマップの登録 vim.keymap.set("n", "<F5>", dap.continue, { desc = "Debug: Start/Continue" }) vim.keymap.set("n", "<F1>", dap.step_into, { desc = "Debug: Step Into" }) vim.keymap.set("n", "<F2>", dap.step_over, { desc = "Debug: Step Over" }) vim.keymap.set("n", "<F3>", dap.step_out, { desc = "Debug: Step Out" }) vim.keymap.set("n", "<leader>b", dap.toggle_breakpoint, { desc = "Debug: Toggle Breakpoint" }) vim.keymap.set("n", "<leader>B", function() dap.set_breakpoint(vim.fn.input("Breakpoint condition: ")) end, { desc = "Debug: Set Breakpoint" }) -- .vscode/launch.jsonの読み込み require("dap.ext.vscode").load_launchjs(nil, { lldb = { "c", "cpp", "" } }) -- DAP-UIの設定(省略可) local dapui = require("dapui") -- 任意 vim.fn.sign_define("DapBreakpoint", { text = "", texthl = "DapBreakpointTextHl" }) vim.fn.sign_define("DapStopped", { text = "", texthl = "DapStoppedTextHl" }) dapui.setup({ controls = { element = "repl", enabled = true, icons = { pause = "⏸", play = "▶", step_into = "⏎", step_over = "⏭", step_out = "⏮", step_back = "b", run_last = "▶▶", terminate = "⏹", disconnect = "⏏", enabled = "●", }, }, element_mappings = {}, expand_lines = true, floating = { border = "single", mappings = { close = { "q", "<Esc>" }, }, }, force_buffers = true, icons = { collapsed = "", current_frame = "", expanded = "", }, layouts = { { elements = { { id = "scopes", size = 0.25, }, { id = "breakpoints", size = 0.25, }, { id = "stacks", size = 0.25, }, { id = "watches", size = 0.25, }, }, position = "left", size = 40, }, { elements = { { id = "repl", size = 0.5, }, { id = "console", size = 0.5, }, }, position = "bottom", size = 10, }, }, mappings = { edit = "e", expand = { "<CR>", "<2-LeftMouse>" }, open = "o", remove = "d", repl = "r", toggle = "t", }, render = { indent = 1, max_value_lines = 100, }, }) vim.keymap.set("n", "<F7>", dapui.toggle, { desc = "Debug: See last session result." }) dap.listeners.after.event_initialized["dapui_config"] = dapui.open dap.listeners.before.event_terminated["dapui_config"] = dapui.close dap.listeners.before.event_exited["dapui_config"] = dapui.close end
require("dap.ext.vscode").load_launchjs(nil, { lldb = { "c", "cpp", "" } })ですが、lldbの部分、VSCodeのCodelldbのtype名はlldbだったのでそちらに合わせます。
vs-tasks.nvim(設定2)
ショートカットからtaskを実行出来るようにします。
-- 設定2 vim.cmd([[ nnoremap <Leader>ta :lua require("telescope").extensions.vstask.tasks()<CR> nnoremap <Leader>ti :lua require("telescope").extensions.vstask.inputs()<CR> nnoremap <Leader>th :lua require("telescope").extensions.vstask.history()<CR> nnoremap <Leader>tl :lua require('telescope').extensions.vstask.launch()<cr> ]]),
実行
F5でデバッグ(launch.json)の選択・開始、(leader)taでタスク(tasks.json)を実行出来ます。デバッガが不要で起動だけするなら(leader)tlで可能です。