Skip to content

LinkNexus/cpp_header_sync.nvim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 

Repository files navigation

cpp_header_sync.nvim

AST-powered C/C++ header <-> implementation sync for Neovim using clangd.

This plugin helps you:

  • Generate a function declaration in the paired header from a function definition in an implementation file.
  • Generate an implementation stub in the paired source file from a declaration in a header.
  • Inspect AST output for debugging.

Features

  • Uses clangd AST requests (textDocument/ast, with fallback to clangd/ast).
  • Supports C and C++ source/header pairs.
  • Handles storage class cleanup for header declarations:
    • Keeps static inline and inline.
    • Strips static and warns.
    • Strips extern and warns.
  • Includes K&R-style C function heuristic support when building declarations.
  • Adds C++ scope prefixes when creating impl stubs from class/namespace declarations (for example User::login).
  • Tries to avoid duplicate declarations/definitions by function name match.

Requirements

  • Neovim with Lua plugin support.
  • clangd attached to the current buffer via LSP.
  • A clangd build/config that supports AST request methods used by this plugin.

If clangd is not attached or AST request fails, the plugin will notify with an error.

Installation

Use your preferred plugin manager.

lazy.nvim

{
  "LinkNexus/cpp_header_sync.nvim",
  config = function()
    require("cpp_header_sync").setup()
  end,
}

packer.nvim

use {
  "LinkNexus/cpp_header_sync.nvim",
  config = function()
    require("cpp_header_sync").setup()
  end,
}

Replace LinkNexus/cpp_header_sync.nvim with the actual repository path.

Setup

require("cpp_header_sync").setup({
  mappings = {
    impl_to_header = "<leader>ch", -- default
    header_to_impl = "<leader>ci", -- default
  },
})

Default keymaps:

  • <leader>ch: sync implementation -> header
  • <leader>ci: sync header -> implementation

Set a mapping to nil to disable it:

require("cpp_header_sync").setup({
  mappings = {
    impl_to_header = nil,
    header_to_impl = "<leader>ci",
  },
})

Usage

Place your cursor on a function and run one of:

  • require("cpp_header_sync").impl_to_header()

    • Use from .c/.cpp/.cc/.cxx.
    • Inserts declaration into paired header (.h/.hpp/.hxx).
  • require("cpp_header_sync").header_to_impl()

    • Use from .h/.hpp/.hxx.
    • Inserts function stub into paired implementation (.c/.cpp/.cc/.cxx).
  • require("cpp_header_sync").dump_ast()

    • Opens a split with vim.inspect() output of AST response.

Pairing Rules

Given name.ext, pairing is based on file stem:

  • From headers:
    • .h prefers .c, then .cpp/.cc/.cxx
    • .hpp/.hxx prefer .cpp/.cc/.cxx, then .c
  • From impl files:
    • prefers existing .h, then .hpp, then .hxx

If no paired file exists, a default path is chosen (.h or .c/.cpp depending on source extension) and used for insertion.

Notes

  • Header insertion is made before the last #endif when present.
  • If a target file is empty, a header guard skeleton is auto-created.
  • The plugin warns when cursor is on a struct function-pointer field, because that is not treated as a normal function declaration target.

Development

Main module:

  • lua/cpp_header_sync/init.lua

The plugin currently exposes a Lua API rather than user commands; integrate via keymaps or direct Lua calls.

About

A Neovim plugin to easily create function implementations from their declarations or declarations from their implementations

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages