Skip to content

[Feature Request] Add "Create Source/Header Pair" Command #839

@anonchihaya0908

Description

@anonchihaya0908

Hello,

Thank you for your excellent work on this extension.

Summary

To improve the C++ development workflow, I propose adding a new command: Create Source/Header Pair. This feature would allow users to create a .h and .cpp file simultaneously from a single class name, similar to the functionality found in IDEs like CLion.

The Problem (User Story)

Currently, when starting a new class, a developer has to perform several manual steps, which is repetitive and slows down the development flow. Separately, the existing Switch Between Source/Header command fails silently if the corresponding file doesn't exist, creating a dead end in the user's workflow.

Proposed Solution

I suggest a new command, clangd.createSourceHeaderPair, with the title "Create Source/Header Pair". When executed, it would:

  1. Prompt the user for a class name (e.g., MyClass).
  2. Intelligently determine the target directory (preferring the directory of the active file, otherwise falling back to the workspace root).
  3. Create two files in that directory:
    • MyClass.h (with a basic header template).
    • MyClass.cpp (with the corresponding include and implementation stubs).
  4. Automatically open the newly created header file in the editor for immediate use.

This command would be accessible from both the Command Palette and the editor's context menu.

##### Synergy with Existing Features

Crucially, this new command lays the groundwork for enhancing the existing Switch Between Source/Header command (Alt+O).

In a future enhancement, we could modify the Switch command's behavior:

  • Current: When switching from foo.cpp and foo.h does not exist, nothing happens.(Didn't find a corresponding file.)
  • Proposed: When foo.h does not exist, the extension could prompt the user: "Header 'foo.h' not found. Would you like to create it?"
  • If the user agrees, it would then leverage this new file-creation logic.

This would transform a simple file-switching utility into a seamless, IDE-like workflow for both navigating and creating files.

Demo

Here is a short video demonstrating the initial "Create Source/Header Pair" command:

Demo of Create Source/Header Pair

Future Enhancements

As mentioned, the primary enhancement is the synergy with the Switch command. Additionally, I have considered supporting more file extensions (e.g., .hpp, .cc), which could be made user-configurable in the settings. For now, this initial proposal focuses on establishing the core functionality.

Contribution

I have already implemented the standalone Create Source/Header Pair command, and the code aligns with the project's existing style. It is ready for review.

If this proposal is something you would consider for the extension, I am happy to open a Pull Request immediately for this foundational piece.

Thank you for your time and consideration. I look forward to your feedback.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions