This document describes the code formatting rules and automatic validation setup for the helix-toolkit-nex project.
The project uses:
- EditorConfig (
.editorconfig) - Defines coding style rules - Git pre-commit hooks - Automatically validates formatting before commits
- dotnet format - CLI tool to check and fix formatting issues
Private fields must follow the _camelCase naming convention:
// ✅ Correct
private int _myField;
private readonly string _myString;
private bool _isEnabled;
// ❌ Incorrect
private int myField; // Missing underscore prefix
private int myField_; // Underscore as suffix (old convention)
private int _MyField; // Should be camelCase, not PascalCase- Interfaces: Start with
I(e.g.,IContext,IRenderer) - Public/Internal fields: PascalCase (e.g.,
MaxValue,DefaultSize) - Constants: PascalCase (e.g.,
DefaultTimeout) - Static readonly fields: PascalCase (e.g.,
Empty,DefaultValue) - Classes, Structs, Enums: PascalCase
- Methods, Properties, Events: PascalCase
- Git installed
- .NET SDK installed (verify with
dotnet --version)
On Windows (PowerShell):
# Run from repository root
.\setup-git-hooks.ps1On Linux/macOS (Bash):
# Run from repository root
chmod +x setup-git-hooks.sh
./setup-git-hooks.shOn Windows:
- Open Command Prompt or PowerShell
- Navigate to repository root
- Run:
copy .git-hooks\pre-commit.bat .git\hooks\pre-commit
On Linux/macOS:
- Open Terminal
- Navigate to repository root
- Run:
cp .git-hooks/pre-commit .git/hooks/pre-commit chmod +x .git/hooks/pre-commit
Try making a commit with improperly formatted code:
git add .
git commit -m "Test commit"If the hook is working, you'll see:
Running code format validation...
Checking C# files for formatting issues...
Check all files in the solution:
dotnet format --verify-no-changesCheck specific files:
dotnet format --verify-no-changes --include File1.cs File2.csFix all files:
dotnet formatFix specific files:
dotnet format --include File1.cs File2.csdotnet format Source/HelixToolkit-Nex/HelixToolkit.Nex/HelixToolkit.Nex.csproj- The
.editorconfigfile is automatically detected - Format on save: Go to Tools > Options > Text Editor > C# > Code Style > Formatting
- Enable "Format document on save"
- Code analysis warnings will appear for naming violations
- Install the C# Dev Kit extension
- Install the EditorConfig for VS Code extension
- Add to
settings.json:{ "editor.formatOnSave": true, "omnisharp.enableEditorConfigSupport": true, "omnisharp.enableRoslynAnalyzers": true }
- EditorConfig is automatically supported
- Enable format on save: Settings > Tools > Actions on Save
- Check "Reformat code"
If you need to commit without formatting validation:
git commit --no-verify -m "Your commit message"Note: This should only be used in exceptional circumstances.
- Windows: Ensure the file doesn't have
.batextension in.git/hooks/ - Linux/macOS: Verify execute permissions:
chmod +x .git/hooks/pre-commit - Check Git hooks are enabled:
git config core.hooksPath(should be empty or.git/hooks)
Install the latest .NET SDK from: https://dotnet.microsoft.com/download
Verify installation:
dotnet --versionIf the hook reports issues but you believe the code is correct:
- Run
dotnet formatto see detailed issues - Check the
.editorconfigrules - Verify your IDE is using the correct settings
For large commits, you can temporarily disable the hook:
git commit --no-verify -m "Large refactoring"Then run formatting separately:
dotnet format
git add .
git commit --amend --no-editFor CI/CD pipelines, add this step to validate formatting:
# GitHub Actions example
- name: Check code formatting
run: dotnet format --verify-no-changes
# Azure Pipelines example
- script: dotnet format --verify-no-changes
displayName: 'Check code formatting'When contributing to this project:
- Ensure your IDE respects the
.editorconfigsettings - Run
dotnet formatbefore committing - Let the pre-commit hook validate your changes
- Follow the naming conventions outlined above
For questions or issues with formatting rules, please open an issue on GitHub.