Civitasv / cmake-tools.nvim

CMake integration in Neovim
GNU General Public License v3.0
354 stars 55 forks source link

How to choose target when every build/run #222

Open loaf4 opened 4 months ago

loaf4 commented 4 months ago

Hi, thanks for this project, I like it too much. I think it's more a question than a bug.

After the first build/run, when you have to select a target, the subsequent float window for selecting a target does not appear again. I want the target selection to appear every time after :CMakeBuild and :CMakeRun commands. Maybe I'm stupid, but I don't understand how to activate this option? Thanks!

khris190 commented 4 months ago

You could use cmake.select_launch_target with a cmake_tools.run callback? for example:

  vim.keymap.set("n", "<leader>dz", function()
    cmake.select_launch_target(function()
      vim.cmd("CMakeRun")
    end)
  end, { desc = "run with target select" })

is that good enough for You? of course the same could be done with selecting build target and debug, or even running the run function manually instead of the vim cmd, the code s Your oyster

loaf4 commented 4 months ago

I don't quite understand where it should be located and where the cmake variable comes from

khris190 commented 4 months ago

I don't quite understand where it should be located and where the cmake variable comes from

wherever in Your config, you can create your own keymap or own vim cmd, the cmake variable is imported like this: local cmake = require("cmake-tools")

loaf4 commented 4 months ago

Thanks a lot, everything worked out. But there is a new problem. I use toggleterm. For some reason, after the first command, if I run any other, I get the error: "A CMake task is already running". Although at the same time, the open terminal window closes.

cmake_executor = { -- executor to use
    name = "toggleterm", -- name of the executor
    toggleterm = {
        direction = "tab", -- 'vertical' | 'horizontal' | 'tab' | 'float'
        close_on_exit = true, -- whether close the terminal when exit
        auto_scroll = true, -- whether auto scroll to the bottom
    },
},
cmake_runner = { -- runner to use
    name = "toggleterm", -- name of the runner
    toggleterm = {
        direction = "tab", -- 'vertical' | 'horizontal' | 'tab' | 'float'
        close_on_exit = false, -- whether close the terminal when exit
        auto_scroll = true, -- whether auto scroll to the bottom
    },
},  
Civitasv commented 4 months ago

Use the latest commit, it should work now. @loaf4

loaf4 commented 4 months ago

Thanks a lot! It works.

loaf4 commented 4 months ago

The problem with the code:

  vim.keymap.set("n", "<leader>dz", function()
    cmake.select_launch_target(function()
      vim.cmd("CMakeRun")
    end)
  end, { desc = "run with target select" })

When I try to launch :CMakeRun or :CMakeBuild using select_launch_target, a message appears: "You can't close the last tab. This will close Neovim". A couple of seconds pass or by pressing Esc, only then the command starts to run.

And another question about :CMakeBuild. When the target selection window appears during the very first build, there is an item in it: all. When using select_launch_target, there is no such item, only specific goals.

And now it is impossible to launch :CMakeRun without using select_launch_target . I get a bunch of errors about the wrong window id many times:

...al/share/nvim/lazy/toggleterm.nvim/lua/toggleterm/ui.lua:344: Invalid window id: 1068
Press ENTER or type command to continue
stack traceback:
Press ENTER or type command to continue
^I[C]: in function 'nvim_win_close'
Press ENTER or type command to continue
^I...al/share/nvim/lazy/toggleterm.nvim/lua/toggleterm/ui.lua:344: in function 'close_tab'
Press ENTER or type command to continue
^I...al/share/nvim/lazy/toggleterm.nvim/lua/toggleterm/ui.lua:391: in function 'close'
Press ENTER or type command to continue
^I...re/nvim/lazy/toggleterm.nvim/lua/toggleterm/terminal.lua:288: in function 'close'
Press ENTER or type command to continue
^I...re/nvim/lazy/toggleterm.nvim/lua/toggleterm/terminal.lua:366: in function <...re/nvim/lazy/toggleterm.nvim/lua/toggleterm/terminal.lua:363> function: builtin#18 ...al/share/nvim
khris190 commented 4 months ago

select launch target does what it says selects LAUNCH target meaning when you debug or run using CMakeDebug and CMakeRun that target will be run and you dont usually run all the targets, VsCode doesn't allow that as well, for builds there is a :CMakeSelectBuildTarget vim command that You can run the same way by doing:

cmake.select_build_target(function()
      vim.cmd("CMakeBuild")
end)

and this will let You build all of your evecutables at once or just the one you need right now

about Your problem with "You can't close the last tab. This will close Neovim" could You elaborate a bit? because i dont even know how i would go about reporoducing it.

loaf4 commented 4 months ago

When I use this method:

  vim.keymap.set("n", "<leader>dz", function()
    cmake.select_launch_target(function()
      vim.cmd("CMakeRun")
    end)
  end, { desc = "run with target select" })

this message immediately appears and the command execution is paused for a couple of seconds. image

And only then, after a couple of seconds, the command starts executing. Also, if you additionally press Esc after the key combination of this bind after this message appears during this very delay before executing the command itself, then there will be no delay and the command will immediately start executing. And that's it.

Thank you, I understood your answer to my first question. What about the third one? This stacktrace appears more than once. That is, when I try to do this, this trace appears over and over again (only the window id changes) and does not stop appearing until I press `Esc'.

khris190 commented 4 months ago

Could You share your config? Also are you using toggleterm as the runner? Can you try to use terminal as your runner if that is the case and check if anything changes?

loaf4 commented 4 months ago

Basically, my config is almost completely default. But I use toggleterm (and I would like to continue using it, because it seems to me the most convenient for everyone)

opts = {
    cmake_build_directory = "out/${variant:buildType}", -- this is used to specify generate directory for cmake, allows macro expansion, relative to vim.loop.cwd()
    cmake_soft_link_compile_commands = true, -- this will automatically make a soft link from compile commands file to project root dir
    cmake_compile_commands_from_lsp = false, -- this will automatically set compile commands file location using lsp, to use it, please set `cmake_soft_link_compile_commands` to false
    cmake_kits_path = nil, -- this is used to specify global cmake kits path, see CMakeKits for detailed usage
    cmake_variants_message = {
        short = { show = true }, -- whether to show short message
        long = { show = true, max_length = 40 }, -- whether to show long message
    },
    cmake_dap_configuration = { -- debug settings for cmake
        name = "cpp",
        type = "codelldb",
        request = "launch",
        stopOnEntry = false,
        runInTerminal = true,
        console = "integratedTerminal",
    },
    cmake_executor = { -- executor to use
        name = "toggleterm", -- name of the executor
        opts = {}, -- the options the executor will get, possible values depend on the executor type. See `default_opts` for possible values.
        default_opts = { -- a list of default and possible values for executors
            toggleterm = {
                direction = "tab", -- 'vertical' | 'horizontal' | 'tab' | 'float'
                close_on_exit = true, -- whether close the terminal when exit
                auto_scroll = true, -- whether auto scroll to the bottom
            },
        },
    },
    cmake_runner = { -- runner to use
        name = "toggleterm", -- name of the runner
        opts = {}, -- the options the runner will get, possible values depend on the runner type. See `default_opts` for possible values.
        default_opts = { -- a list of default and possible values for runners
            toggleterm = {
                direction = "tab", -- 'vertical' | 'horizontal' | 'tab' | 'float'
                close_on_exit = false, -- whether close the terminal when exit
                auto_scroll = true, -- whether auto scroll to the bottom
            },
        },
    },
    cmake_notifications = {
        runner = { enabled = true },
        executor = { enabled = true },
        spinner = { "⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏" }, -- icons used for progress display
        refresh_rate_ms = 100, -- how often to iterate icons
    },
    cmake_virtual_text_support = true, -- Show the target related to current file using virtual text (at right corner)
},

And yes, you were right, problems occur only with the use of toggleterm, if I use terminal in the config, then everything works fine and not a single error appears. That is, there is no error "You can't close the last tab. This will close Neovim" and there is no error using :CMakeRun without select_launch_target.

khris190 commented 4 months ago

okay, so it seems that executor set to direction="tab" breaks things, i will try to deal with it, but You can just change executor direction for now, i'll post an update here later.

loaf4 commented 4 months ago

Okay, thanks very much!

khris190 commented 4 months ago

i think it's the way toggleterm handles checking if its the last tab by vim.api.nvim_list_tabpages , i'll try to check if we are doing something wrong when i have more free time

khris190 commented 4 months ago

ohhhhh, i think i understand, it goes something like this: command 1 start on_exit1 (our callback command 2) command 2 singleton deletes terminal no,1 end of command 2 terminal no.1 tries to delete itself but was deleted personally i don't know how to deal with it properly without some funky stuff @Civitasv

khris190 commented 4 months ago

But if you want tab to work in executor you can set close_on_exit in executor to false it should also work

Civitasv commented 3 months ago

Sorry for the late reply. But I cannot reproduce this error with the same configuration.

Edit: Maybe toggleterm fixed this? Can you test again?

Civitasv commented 3 months ago

ohhhhh, i think i understand, it goes something like this: command 1 start on_exit1 (our callback command 2) command 2 singleton deletes terminal no,1 end of command 2 terminal no.1 tries to delete itself but was deleted personally i don't know how to deal with it properly without some funky stuff @Civitasv

Yeah, I think this is the case. I guess they've already fix this problem.