Skip to content

Conversation

@anonchihaya0908
Copy link

@anonchihaya0908 anonchihaya0908 commented Jul 16, 2025

Improves the C++ development workflow by providing a new command to create corresponding source and header files simultaneously, mirroring common IDE functionality.

This new command, clangd.createSourceHeaderPair, provides the following features:

  • Prompts the user for a valid C++ class name with robust validation.
  • Intelligently detects the target directory, preferring the active file's location.
  • Generates professional-grade file templates for both the header and source file.
  • Handles cross-platform line endings (EOL) correctly by respecting user settings.
  • Prevents accidental file overwrites by checking for existing files before creation.
  • Is accessible from the Command Palette and the editor context menu.

Resolves #839

Improves the C++ development workflow by providing a new command to create corresponding source and header files simultaneously, mirroring common IDE functionality.

This new command, `clangd.createSourceHeaderPair`, provides the following features:
- Prompts the user for a valid C++ class name with robust validation.
- Intelligently detects the target directory, preferring the active file's location.
- Generates professional-grade file templates for both the header and source file.
- Handles cross-platform line endings (EOL) correctly by respecting user settings.
- Prevents accidental file overwrites by checking for existing files before creation.
- Is accessible from the Command Palette and the editor context menu.

Resolves #[Your-Issue-Number-Here]
Refactored the PairCreator class to modularize logic for file name validation, language detection, template generation, file existence checks, and file writing. Added interfaces and constants for better type safety and maintainability. Improved user prompts and error handling, and made the code more extensible for both C and C++ file generation.
context-aware wizard, significantly improving user experience and flexibility.

Key improvements include:
- A single, multi-step input flow for all creation logic.
- Dynamically sorts template choices based on the active language context (C vs. C++), prioritizing the most likely user intention.
- Disambiguates between C and C++ structs, providing correct templates and file extensions for each.
- Introduces intelligent, dynamic placeholders in the file name input box based on the current file and selected template.
- The underlying code has been significantly refactored for clarity, maintainability, and type safety, breaking down complex logic into single-responsibility helper methods.
@anonchihaya0908
Copy link
Author

anonchihaya0908 commented Jul 16, 2025

Hi
屏幕截图 2025-07-16 192943
屏幕截图 2025-07-16 193000

Following up on my previous comment, I took some time to explore the idea of context-aware support for both C and C++, and I'm excited to share the results.
I've just pushed a major update that fully implements this concept, creating what I believe is a much more robust and intuitive final version. Instead of a simple C++-only feature, it is now a unified creation wizard that intelligently adapts to the user's context.
Here are the key features of this new implementation:
Unified Template Selection: There is now a single, powerful wizard for all users.
Context-Aware UI: The list of choices (C Files, C++ Class, etc.) is dynamically sorted based on the active file's language, gently guiding the user towards the most logical option without restricting their freedom.
Disambiguated struct Support: The menu now clearly distinguishes between a C Struct (with typedef) and a C++ Struct, providing the correct template for each language's conventions.
Intelligent Placeholders: The file name prompt now suggests a smart default based on the current file's name and the chosen template type.
This addresses the C language support issue I raised earlier, and I believe it does so in a very flexible and user-friendly way. The underlying code has also been significantly refactored to support this new UI while maintaining high standards of quality.
This PR is now in its final state and is ready for a full review.
Thank you for your patience and for creating the space for this iteration. I'm very much looking forward to your feedback

Replaces the old template and file extension logic with a unified PairingRule structure, simplifying template selection and file generation for C and C++ header/source pairs. Improves language detection, user prompts, and file content generation, making the code more maintainable and user-friendly.
@anonchihaya0908
Copy link
Author

Added intelligent sorting logic for options and more precise expressions.
image
Temporarily locked.cpp/.h.

@anonchihaya0908
Copy link
Author

Just wanted to post a final summary of what this PR currently accomplishes and what I see as potential next steps, based on all the great feedback and discussion in issue #839.

What this PR delivers:

  • A new command, "Create Source/Header Pair," accessible from the command palette and context menu.
  • A multi-step wizard UI that allows users to choose the type of content they want (Class, Struct, Empty, etc.).
  • The wizard is context-aware: it intelligently reorders the template choices based on whether the user is in a C or C++ file.
  • It includes robust features like input validation, file existence checks, platform-aware EOL handling, and smart placeholder generation.
  • Crucially, it correctly infers the language context for .h files by checking for companion .c source files, solving a key usability issue.

Current Limitations & Future Work:

  • Hardcoded Extensions: As a focused first step, this implementation currently creates only .h/.cpp or .h/.c pairs. The logic for user-configurable extensions (like .hpp or .cc) is something I've planned as a clear follow-up enhancement in a separate PR.
  • Complex Project Structures: The context detection relies on finding companion files in the same directory. A future improvement could be to support more complex structures (e.g., src/ and include/ folders), potentially by leveraging compile_commands.json or other project-level information.

I believe this PR, in its current state, provides a solid and valuable foundation. Thank you again for all the guidance. I'm looking forward to your review!

Renamed 'createSourceHeaderPair' to 'newSourcePair' throughout the codebase and updated command titles for clarity. Added a new command for configuring source/header pairing rules. Improved template rule labels with icon syntax for better UI presentation. Created a placeholder for pairing rule service.
@anonchihaya0908
Copy link
Author

It's almost done, but the template issue still isn't resolved, and it can only generate .h type templates. Besides that, the code is pretty long due to a lot of templates and comments. If possible, can we break it down into separate files?

Split the monolithic create-source-header-pair.ts into separate files: coordinator.ts, service.ts, ui.ts, and templates.ts, and updated the module index and registration logic. Updated package.json command and configuration descriptions for clarity. Adjusted extension and command registration in extension.ts and updated pairing-rule-manager.ts as needed.
Refactored the coordinator to move directory and language detection to the service layer, streamlined the file creation flow, and added logic to offer saving custom C++ extension pairs as defaults. The UI now prompts for template type and file extensions in a more intuitive order, and success messages are shown non-blockingly. Service layer now provides file path creation and improved file existence caching.
Refactors the PairCoordinator to use dependency injection for service and UI layers, simplifying the workflow and improving testability. Moves file creation and rule-saving logic into the service layer, and moves workspace folder picking into the UI layer. Updates the registration logic to construct dependencies explicitly. Also, centralizes file extension selection UI in PairingRuleUI and cleans up related code.
Added an icon to the 'New Source/Header Pair' command and registered it in the explorer context menu for folders. This improves discoverability and usability of the command within the file explorer.
Deleted the clangd.newSourcePair command configuration from the package.json contributes.commands section. This cleans up unused or deprecated command definitions.
@anonchihaya0908
Copy link
Author

finished.

Copy link
Contributor

@JVApen JVApen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comments

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vscode-nls seems to be deprecated: https://github.com/microsoft/vscode-nls

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment in bug, though I'd be more inclined to ask for a snippet ID rather than having fields to indicate a class/struct.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like your addition of relevant icons, though can you do this in a separate PR, such that it's easier to track the relevant changes and/or land these changes separately?

import * as vscodelc from 'vscode-languageclient/node';

import {ClangdContext} from './clangd-context';

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

File is touched without any significant changes. Can you undo this newline? (Unless required by formatting rules)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This contains a lot of code trying to find out which rule the user intended. Can't we delegate that to the user? If they add a 'label' as part of the configuration, they can have a rule for classes, structs, free functions or anything custom with the exact same extensions. They can than pick the intended entry from a list.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Binaries should not be included

{
"name": "vscode-clangd",
"version": "0.2.0",
"name": "vscode-clangd-fancy",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't rename the project

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants