romainchapou / confiture.nvim

A neovim lua plugin to save and launch project specific commands.
MIT License
17 stars 0 forks source link

Add ToggleTerm support to execute commands via :TermExec #1

Open Cudochi opened 1 year ago

Cudochi commented 1 year ago

Could it be possible to add the option of executing commands via the plugin ToggleTerm ?

I modified the init.lua file of confiture.nvim so that the commands in my project.conf files would be executed by the command : :TermExec cmd="command_in_the_project.conf" go_back=0 (the go_back=0 option is to keep the cursor in the terminal that has been opened)

ToggleTerm is meant to be a more ergonomic replacement of the Neovim native terminal, I think it would be nice if confiture could use it.


Another idea (maybe better and easier to implement) is to leave the choice of the command that will execute the commands in the project.conf entirely to the user, via a string variable that would have a preset value by default.

romainchapou commented 1 year ago

Yes, this should be easy to support, I will look into this tomorrow.

I like your idea of simply having the command function given by the user, but it probably would have to be a function and not a string to be robust enough. I'm not sure how it would fit the confiture workflow though, I will think about it.

For now, the support for ToggleTerm will probably be with a command like :ConfitureExecToggleTerm my_confiture_cmd.

Also I'm not sure about the go_back=0 behaviour. It would be coherent with :ConfitureTerm but less with what ToggleTerm's workflow looks like.

romainchapou commented 1 year ago

Ok, I've pushed a possible solution to the toggleterm branch. I'm interested if you have any feedback @Cudochi as I'm not a fan of this implementation: I'm forced to add a cd command (visible for the user) for every confiture command as confiture's commands should always be launched from nvim's current working directory.

Cudochi commented 1 year ago

First of all, thank you for considering my suggestions. I tried the toggleterm branch and it works well. I don't understand lua and Neovim enough to get what is the problem with the cd command, but I haven't noticed anything during my tests.

As for the go_back=0 option, the idea is that when you execute some code, the cursor is left in ToggleTerm's control, and you just have to hit escape to close the frame and go back to edit code where you were. While testing the toggleterm branch, each time I executed a command via Confiture, I had to go back to ToggleTerm and exit it manually (with <Esc>). I tried modifying lua/confiture/init.lua to add the go_back=0 option to it, but I don't know how to add it to require("toggleterm").exec(state.commands[cmd], count)

I think it should be the default behavior for practical reasons, and I'm not sure what you mean when you say that it wouldn't be coherent with ToggleTerm's workflow. Mine is really basic : I have a keybind to open ToggleTerm, <Esc> closes it when the cursor is inside it.

Sorry I took time to respond, I'm mainly on GitLab, this is "shell" account.

romainchapou commented 1 year ago

Thanks for the feedback on your workflow. No worries for the response time, I also took some time to notice someone created an issue.

For the added cd command, the correct execution of Confiture's commands would not be guaranteed without it. Confiture's command are supposed to always be executed from nvim's current working directory. As any command can change what the current working directory of the toggle term is, I make sure it is correct by adding that cd command.

This solution works (in theory I would also like to check that the cd command succeeded) but it is a bit of a hack and the cd command is visible in the toggle term. If it didn't bother you, that's good to know, it's maybe not that big of a problem.

For the go_back=0 option, your workflow is coherent with ToggleTerm's workflow, it's just not what looks to be ToggleTerm's default workflow (as if it was, this option would be on by default).

I've just pushed to the toggleterm branch. If you add let g: confiture_toggle_term_go_back = false (or vim.g.confiture_toggle_term_go_back = false in lua) to your nvim config, it should work as you wanted.

I'm not thinking of merging this to master and making the support for ToggleTerm official though

Still, I'm not thinking yet of merging this into master and making the support for ToggleTerm official as a proper support would mean more options to support, and the two plugins don't seem that compatible anyway (with the cd command hack, but also there is no guarantee that a command is not already running in the toggle term, making a call to :ConfitureSendToToggleTerm silently fail)

The branch will still be up and I'll try to keep it updated with the master branch.