Skip to content

A smarter print debugging statement generator for C/C++

License

Notifications You must be signed in to change notification settings

declancm/printf.nvim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

89 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

printf.nvim

A smarter print debugging statement generator for C/C++

printf.nvim

Features

  • Generates print statements for:
    • line numbers
    • function names
    • variable values
  • Syntax and context aware
  • Automatic format specifier detection
  • Customizable (see the Examples section for inspiration)

Requirements

  • Neovim >= 0.8.0
  • A tree-sitter parser for C/C++
  • The clangd language server

Installation

lazy.nvim

{
    'declancm/printf.nvim'
}

Usage

Lua API

require('printf').print_var()   -- Print the variable under the cursor
require('printf').print_line()  -- Print the current line number
require('printf').print_func()  -- Print the name of the enclosed function
require('printf').clean()       -- Cleanup generated statements

Vim Commands

The Printf command can be used to access the Lua API as Vim commands

:Printf print_var      " equivalent to `:lua require('printf').print_var()`
:Printf print_line     " equivalent to `:lua require('printf').print_line()`
:Printf print_func     " equivalent to `:lua require('printf').print_func()`
:Printf clean          " equivalent to `:lua require('printf').clean()`

Example Keymaps

Lua

vim.keymap.set('n', '<leader>dv', require('printf').print_var, {})
vim.keymap.set('n', '<leader>dl', require('printf').print_line, {})
vim.keymap.set('n', '<leader>df', require('printf').print_func, {})
vim.keymap.set('n', '<leader>dc', require('printf').clean, {})

VimL

nnoremap <leader>dv <cmd>Printf print_var<cr>
nnoremap <leader>dl <cmd>Printf print_line<cr>
nnoremap <leader>df <cmd>Printf print_func<cr>
nnoremap <leader>dc <cmd>Printf clean<cr>

Help

Help docs can be accessed with :help printf.nvim

Configuration

require('printf').setup({
    -- Generated function call options
    called_function = {
        -- Name of the called function
        name = 'printf',
        -- Add additional arguments before the format string
        additional_args = {},
    },
    -- 'print_var' specific options
    print_var = {
        -- Automatically dereference supported pointer types
        dereference_pointers = false,
        -- Format char * variables as strings
        char_ptr_strings = true,
    },
    -- 'print_line' specific options
    print_line = {
        -- The variable/identifier/macro with the line number integer value
        variable = '__LINE__',
    },
    -- 'print_func' specific options
    print_func = {
        -- The variable/identifier/macro with the function name string
        variable = '__func__',
    },
})

Examples

Minimal Neovim Config

-- Install plugins
require('lazy').setup({
    'neovim/nvim-lspconfig',
    'declancm/printf.nvim'
})

-- Setup the clangd language server
require('lspconfig').clangd.setup({})

-- Setup printf
require('printf').setup()

Standard Error Stream

Config:

require('printf').setup({
    called_function = {
        name = 'fprintf',
        additional_args = { 'stderr' }
    }
})

Output:

fprintf(stderr, "example: %d\n", example); // auto-generated printf

Pretty Function

Config:

require('printf').setup({
    print_func = { variable = '__PRETTY_FUNCTION__' }
})

Output:

printf("function: %s\n", __PRETTY_FUNCTION__); // auto-generated printf

About

A smarter print debugging statement generator for C/C++

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages