romainchapou / confiture.nvim

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

confiture.nvim

A simple way to save and launch your project specific commands.

Main features/motivations

Installation

Install with your package manager of choice, for example with lazy.nvim:

{
    "romainchapou/confiture.nvim",
    dependencies = "tpope/vim-dispatch", -- optional but highly recommended, for async support
}

Or with vim-plug:

Plug 'romainchapou/confiture.nvim'
Plug 'tpope/vim-dispatch' " optional but highly recommended, for async support

Usage

First, create a project.conf file in your project root directory. This is where you will define your project commands (which simply are the shell commands you would use in a terminal). The file follows a simple syntax:

# my project.conf file

# declaring variables
variable: "confiture <3"

# declaring commands
@build:      "make -j16"
@run:        "./run_test"
@my_command: "echo ${variable}"

Then start neovim in this same folder and use :Confiture to launch your commands.

project.conf content

Note: if you want to have another name for your Confiture configuration files, simply set g:confiture_file_name in your init.vim/init.lua to what you prefer.

Commands

Commands are declared with a @ and assigned a value with :. Any name is valid, but @build and @run have a special meaning. The command "cmd" can then be executed from Neovim with a call to :Confiture cmd (with nice completion).

If this default behaviour doesn't suit you, you can always launch any command with tpope/vim-dispatch or the integrated nvim terminal using :ConfitureDispatch and :ConfitureTerm.

Variables can be used in your command definition using the ${var} syntax, as well as other commands using the @{cmd} syntax (see the example section).

Variables

Variables can be assigned a value with : and then used in other variables or in commands using the ${var} syntax. Variables names can be anything you want, although some have a special meaning:

:Confiture build_and_run

:Confiture build_and_run is an additional command implicitly defined by @build and @run.

Async is also supported for this command if tpope/vim-dispatch is installed.

Examples

A super simple example for a script project

# my simple project.conf

# will be launched by either ':Confiture run' or ':Confiture build_and_run'
@run: "./main.py"

A simple example using make

# my project.conf for a simple cpp project using make
RUN_IN_TERM: false # run command will be executed with a simple call to ':!'

@build: "make -j16"
@run:   "./my_exec"
@clean: "make clean"

A more complex example for a cmake project

# my project.conf for a cmake project
build_type: "Release"
build_folder: "build-${build_type}"

@configure:     "mkdir -p ${build_folder} && cmake -B ${build_folder} -DCMAKE_BUILD_TYPE ${build_type}"
@build:         "make -j16 -C ${build_folder}"
@run:           "${build_folder}/test/my_test"
@clean:         "make -C ${build_folder} clean"
@clean_rebuild: "@{clean} && @{configure} && @{build}" # chaining commands!

Public API

Those functions can be useful if you want a more advanced used of confiture (do something special after a successful build, programmatically read/edit the project.conf file, ...). These can all be accessed using require("confiture").func_name.

API use example

local function my_build_and_debug()
  vim.api.nvim_command("silent! wa")

  -- set the local confiture variable "build_type" to "Debug" to use a debug build
  require("confiture").set_variable("build_type", "Debug")

  require("confiture").async_build_and_exec_on_success(function()
    -- run debugger on success
    require('dap').continue()
  end)
end

Advanced notes

# my project.conf
root: "$HOME/my path/with spaces/"
dir: "${root}/my_dir/"
@my_command: "ls ${dir}"
# my project.conf
@cmd1: "echo hi"
@cmd2: "echo hello"
@my_command: "@{cmd1} && @{cmd2}"

Some recommended mappings

" Save all buffers (ignore unnamed ones) and run a command
nnoremap <leader>c :silent! wa<cr>:ConfitureDispatch configure<cr>
nnoremap <leader>b :silent! wa<cr>:Confiture build<cr>
nnoremap <leader><cr> :silent! wa<cr>:Confiture build_and_run<cr>