-
Notifications
You must be signed in to change notification settings - Fork 145
feat(command): Add Create Source/Header Pair command #840
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
anonchihaya0908
wants to merge
35
commits into
clangd:master
Choose a base branch
from
anonchihaya0908:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
898ba52
feat(command): Add Create Source/Header Pair command
anonchihaya0908 b8dfda3
Refactor and modularize source/header pair creation
anonchihaya0908 e82a3ad
This refactors the "Create Source/Header Pair" command into a unified,
anonchihaya0908 605aab9
Update create-source-header-pair.ts
anonchihaya0908 e84f24d
Refactor file pair creation logic and templates
anonchihaya0908 1c0cfd9
Update create-source-header-pair.ts
anonchihaya0908 9663d80
Update create-source-header-pair.ts
anonchihaya0908 76e3d19
Update create-source-header-pair.ts
anonchihaya0908 769cbba
Rename and enhance source/header pair commands
anonchihaya0908 438f11a
npm run format
anonchihaya0908 da3e288
fix
anonchihaya0908 85adf58
fix
anonchihaya0908 475839e
fix register.
anonchihaya0908 4345afc
npm run format
anonchihaya0908 c463be2
Refactor create-source-header-pair module structure
anonchihaya0908 2a60d7c
Refactor C++ pair creation workflow and improve UX
anonchihaya0908 e6b97fe
npm run format
anonchihaya0908 6b4a6c1
Refactor coordinator and UI for dependency injection
anonchihaya0908 aad7b7c
Update ui.ts
anonchihaya0908 6ac7352
Update ui.ts
anonchihaya0908 0d3e9dd
Update ui.ts
anonchihaya0908 9c6135a
fuck
anonchihaya0908 a820052
Add icon and explorer context menu for new source/header pair
anonchihaya0908 c31a851
Remove clangd.newSourcePair command from package.json
anonchihaya0908 21eeb6b
Update service.ts
anonchihaya0908 1ccc7d9
Update package.json
anonchihaya0908 2f7cb17
Update coordinator.ts
anonchihaya0908 173b431
fix
anonchihaya0908 7b9d7c0
Create vscode-clangd-fancy-0.2.1.vsix
anonchihaya0908 149d7c3
Update service.ts
anonchihaya0908 55cffa9
Update service.ts
anonchihaya0908 21eb998
format
anonchihaya0908 fc34485
Update package.json
anonchihaya0908 838165e
Update package.json
anonchihaya0908 aac5797
Merge branch 'clangd:master' into master
anonchihaya0908 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ import * as vscode from 'vscode'; | |
| import * as vscodelc from 'vscode-languageclient/node'; | ||
|
|
||
| import {ClangdContext} from './clangd-context'; | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) |
||
| import type {ASTParams, ASTNode} from '../api/vscode-clangd'; | ||
|
|
||
| const ASTRequestMethod = 'textDocument/ast'; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| // | ||
| // PAIR COORDINATOR | ||
| // ================ | ||
| // | ||
| // Lean coordinator that orchestrates the source/header pair creation workflow. | ||
| // Uses dependency injection and delegates all implementation details to | ||
| // service and UI layers. | ||
|
|
||
| import * as vscode from 'vscode'; | ||
|
|
||
| import {showConfigurationWizard} from '../pairing-rule-manager'; | ||
|
|
||
| import {PairCreatorService} from './service'; | ||
| import {PairCreatorUI} from './ui'; | ||
|
|
||
| // PairCoordinator orchestrates the workflow between UI and Service layers. | ||
| // It follows the single responsibility principle and uses dependency injection. | ||
| export class PairCoordinator implements vscode.Disposable { | ||
| private static readonly ERROR_MESSAGES = { | ||
| NO_TARGET_DIRECTORY: | ||
| 'Cannot determine target directory. Please open a folder or a file first.', | ||
| FILE_EXISTS: (filePath: string) => `File already exists: ${filePath}`, | ||
| UNEXPECTED_ERROR: 'An unexpected error occurred.' | ||
| } as const; | ||
|
|
||
| private newPairCommand: vscode.Disposable; | ||
| private configureRulesCommand: vscode.Disposable; | ||
|
|
||
| // Constructor with dependency injection - receives pre-configured instances | ||
| constructor(private readonly service: PairCreatorService, | ||
| private readonly ui: PairCreatorUI) { | ||
| // Register commands | ||
| this.newPairCommand = vscode.commands.registerCommand( | ||
| 'clangd.newSourcePair', this.create, this); | ||
| this.configureRulesCommand = vscode.commands.registerCommand( | ||
| 'clangd.newSourcePair.configureRules', this.configureRules, this); | ||
| } | ||
|
|
||
| // Dispose method for cleanup when extension is deactivated | ||
| dispose() { | ||
| this.newPairCommand.dispose(); | ||
| this.configureRulesCommand.dispose(); | ||
| } | ||
|
|
||
| // Main workflow orchestration | ||
| public async create(): Promise<void> { | ||
| try { | ||
| // 1. Determine where to create files | ||
| const targetDirectory = await this.getTargetDirectory(); | ||
| if (!targetDirectory) { | ||
| vscode.window.showErrorMessage( | ||
| PairCoordinator.ERROR_MESSAGES.NO_TARGET_DIRECTORY); | ||
| return; | ||
| } | ||
|
|
||
| // 2. Get user preferences for what to create | ||
| const {language, uncertain} = | ||
| await this.service.detectLanguageFromEditor(); | ||
| const rule = await this.ui.promptForPairingRule(language, uncertain); | ||
| if (!rule) | ||
| return; | ||
|
|
||
| const fileName = await this.ui.promptForFileName(rule); | ||
| if (!fileName) | ||
| return; | ||
|
|
||
| // 3. Prepare file paths and check for conflicts | ||
| const {headerPath, sourcePath} = | ||
| this.service.createFilePaths(targetDirectory, fileName, rule); | ||
| const existingFilePath = | ||
| await this.service.checkFileExistence(headerPath, sourcePath); | ||
| if (existingFilePath) { | ||
| vscode.window.showErrorMessage( | ||
| PairCoordinator.ERROR_MESSAGES.FILE_EXISTS(existingFilePath)); | ||
| return; | ||
| } | ||
|
|
||
| // 4. Create the files | ||
| await this.service.generateAndWriteFiles(fileName, rule, headerPath, | ||
| sourcePath); | ||
|
|
||
| // 5. Show success and handle post-creation tasks | ||
| await this.ui.showSuccessAndOpenFile(headerPath, sourcePath); | ||
| await this.service.handleOfferToSaveAsDefault(rule, language); | ||
|
|
||
| } catch (error: unknown) { | ||
| const errorMessage = | ||
| error instanceof Error | ||
| ? error.message | ||
| : PairCoordinator.ERROR_MESSAGES.UNEXPECTED_ERROR; | ||
| vscode.window.showErrorMessage(errorMessage); | ||
| } | ||
| } | ||
|
|
||
| // Determine target directory with fallback to workspace picker | ||
| private async getTargetDirectory(): Promise<vscode.Uri|undefined> { | ||
| return await this.service.getTargetDirectory( | ||
| vscode.window.activeTextEditor?.document?.uri.fsPath, | ||
| vscode.workspace.workspaceFolders) ?? | ||
| await this.ui.showWorkspaceFolderPicker(); | ||
| } | ||
|
|
||
| // Opens the configuration wizard for pairing rules | ||
| public async configureRules(): Promise<void> { | ||
| await showConfigurationWizard(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| // | ||
| // CREATE SOURCE HEADER PAIR - MODULE INDEX | ||
| // ======================================== | ||
| // | ||
| // This module provides functionality to create matching header/source file | ||
| // pairs for C/C++ development. It intelligently detects language context, | ||
| // offers appropriate templates, and handles custom file extensions. | ||
| // | ||
| // ARCHITECTURE: | ||
| // - templates.ts: Template rules and file content templates | ||
| // - service.ts: Business logic layer (language detection, file operations) | ||
| // - ui.ts: User interface layer (dialogs, input validation) | ||
| // - coordinator.ts: Main coordinator (orchestrates workflow, registers | ||
| // commands) | ||
| // | ||
| // WORKFLOW: | ||
| // Command triggered → Detect target directory → Analyze language context → | ||
| // Check for custom rules → Present template choices → Get file name → | ||
| // Validate uniqueness → Generate content → Write files → Open in editor | ||
| // | ||
| // FEATURES: | ||
| // - Smart language detection (C vs C++) | ||
| // - Multiple template types (class, struct, empty) | ||
| // - Custom file extension support | ||
| // - Header guard generation | ||
| // - Cross-language template options | ||
| // - Workspace-aware directory selection | ||
| // - Input validation for C/C++ identifiers | ||
| // | ||
| // INTEGRATION: | ||
| // Uses PairingRuleManager for custom extension configurations | ||
| // Integrates with VS Code file system and editor APIs | ||
| // | ||
|
|
||
| import {ClangdContext} from '../clangd-context'; | ||
|
|
||
| import {PairCoordinator} from './coordinator'; | ||
| import {PairCreatorService} from './service'; | ||
| import {PairCreatorUI} from './ui'; | ||
|
|
||
| // Registers the create source/header pair command with the VS Code extension | ||
| // context Uses dependency injection to create properly configured instances | ||
| export function registerCreateSourceHeaderPairCommand(context: ClangdContext) { | ||
| // Create instances with proper dependencies | ||
| const service = new PairCreatorService(); | ||
| const ui = new PairCreatorUI(service); | ||
| const coordinator = new PairCoordinator(service, ui); | ||
|
|
||
| context.subscriptions.push(coordinator); | ||
| } | ||
|
|
||
| // Re-export main types and classes for external usage | ||
| export {PairCoordinator} from './coordinator'; | ||
| export {PairCreatorService} from './service'; | ||
| export {PairCreatorUI} from './ui'; | ||
| export {Language, TemplateKey} from './templates'; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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