nvim-treesitter-cpp-tools icon indicating copy to clipboard operation
nvim-treesitter-cpp-tools copied to clipboard

C++ Helper Snippets Using Treesitter

trafficstars

nt-cpp-tools

Unit Tests

Experimental treesitter based neovim plugin to create intelligent implementations for C++

Features

  1. Out-of class member function implementation
  2. Concrete class implement from Abstract class or Interface
  3. Add missing functions to obey Rule of 3
  4. Add missing functions to obey Rule of 5

Install

Using paq

require "paq" {
    "nvim-treesitter/nvim-treesitter",
    "Badhi/nvim-treesitter-cpp-tools",
}

Using packer.nvim

use {
    requires = { "nvim-treesitter/nvim-treesitter" },
    "Badhi/nvim-treesitter-cpp-tools",
}

Setup

Append the following config to the nvim-treesitter config

require('nvim-treesitter.configs').setup({
    -- ...
    nt_cpp_tools = {
        enable = true,
        preview = {
            quit = 'q', -- optional keymapping for quit preview
            accept = '<tab>' -- optional keymapping for accept preview
        },
        header_extension = 'h', -- optional
        source_extension = 'cxx', -- optional
        custom_define_class_function_commands = { -- optional
            TSCppImplWrite = {
                output_handle = require'nvim-treesitter.nt-cpp-tools.output_handlers'.get_add_to_cpp()
            }
            --[[
            <your impl function custom command name> = {
                output_handle = function (str, context) 
                    -- string contains the class implementation
                    -- do whatever you want to do with it
                end
            }
            ]]
        }
    }
})

Usage

  • Select the range of the class using visual mode
  • Use below commands
Command Feature
TSCppDefineClassFunc Implement out of class member functions

subset of functions can be implemented by selecting required function declarations using visual mode or simply keeping the cursor on the function declaration before calling the command

Supported special features
1. Templates (with default args)
2. Function arguments with default values
3. Nested classes
(check test_cases for tested examples)
TSCppMakeConcreteClass Create a concrete class implementing all the pure virtual functions
TSCppRuleOf3 Adds the missing function declarations to the class to obey the Rule of 3 (if eligible)
TSCppRuleOf5 Adds the missing function declarations to the class to obey the Rule of 5 (if eligible)

Example

  1. TSCppDefineClassFunc

TSImplementFunc

  1. TSCppMakeConcreteClass

TSConcreteClass

  1. TSCppRuleOf3

TSRuleOf3