CSharpier

repository·main·Indexed 25 days ago

https://github.com/belav/csharpier

An opinionated code formatter for C# and XML that automates code style consistency by parsing and re-printing code. It is available as a .NET tool, a programmatic library via CSharpier.Core, and as extensions for Visual Studio, VS Code, and JetBrains Rider. The tool follows an "Option Philosophy" to minimize configuration fatigue and includes a printing engine evolved from Prettier.

Tokens
16.9K
Snippets
57
Records
110
Agent score
78%

What's inside CSharpier

  1. Overview of CSharpier

    main
    CSharpier is an opinionated code formatter for C# and XML. It works by parsing your code and re-printing it according to its own internal rules. The printing engine is a port of Prettier that has been evolved for C# and XML. It follows an "Option Philosophy" similar to Prettier, providing only a few basic options to minimize configuration fatigue.
  2. What is SyntaxFinder used for?

    main

    SyntaxFinder is a tool used to scan C# code files using SyntaxWalkers to analyze developer preferences and code patterns. It is primarily used for research and data collection, such as:

    • Determining if developers prefer spaces or tabs.
    • Determining if developers prefer always using braces or no braces.
    • Identifying if developers add empty lines into object initializers.
    • Finding files that contain specific types of SyntaxNodes.
    • Printing examples of how specific types of Syntax were formatted.
  3. What is CSharpier?

    main

    CSharpier is an opinionated code formatter for C# and XML. It works by parsing your code and re-printing it according to its own internal rules. The printing process is based on a port of Prettier but has evolved specifically for C# and XML.

    CSharpier follows the Prettier 'Option Philosophy', meaning it provides a minimal set of configuration options to ensure consistent formatting across projects. Requests for new formatting options are generally considered out of scope.

  4. Integration options for CSharpier

    main

    Beyond manual CLI usage, CSharpier can be integrated into your development workflow in several ways:

    • Editor Integration: Configure your editor to format code automatically on save.
    • Pre-commit Hooks: Use CSharpier to ensure code is formatted before commits are finalized.
    • Build Process: Integrate formatting as part of your MSBuild process.
    • Programmatic Usage: Call CSharpier via its API within your own applications.
    • CI/CD: Use CSharpier in your continuous integration pipelines to enforce formatting standards across the codebase.
  5. How CSharpier resolves the formatter version

    main

    When performing a formatting action, CSharpier follows a specific resolution order to determine which version of the tool to use. It attempts to find the most relevant version in this priority:

    1. MSBuild version: It first looks for a version defined within the .csproj file.
    2. Local dotnet tool: It checks for a local version installed via dotnet tools (e.g., via a tool manifest).
    3. Global version: It falls back to a globally installed version of CSharpier.

    Ensuring the correct version is used is critical for consistent formatting (for example, ensuring specific rules like adding blank lines between methods are applied correctly).

  6. CSharpier extension formatting behavior

    main

    The CSharpier extension supports several formatting triggers and contexts:

    • Automatic Warming: Existing open documents are warmed up for formatting.
    • Edit-based Formatting: Formatting can be triggered by edits made to the document.
    • Run on Save: The extension supports 'run on save' functionality (specifically in VS Code).
    • Unsaved Documents: Unsaved documents can be formatted (primarily applicable to VS Code).
    • Language Filtering: Formatting actions are only shown/available for C# files.
  7. How CSharpier locates the dotnet CLI

    main

    The extension requires the dotnet CLI to function. It uses the following resolution logic to find the dotnet executable:

    1. Uses the path specified in dotnet.dotnetPath if set.
    2. Uses the paths specified in omnisharp.dotNetCliPaths if set.
    3. Attempts to run dotnet --info to check if it is on the system PATH.
    4. On non-Windows systems, it attempts to run sh -c "dotnet --info" to check the PATH.
  8. How CSharpier versioning is determined in Rider

    main

    The Rider plugin does not include the formatter itself; it uses the dotnet tool CSharpier. The plugin determines which version of CSharpier to use by following this priority:

    1. It looks for a dotnet manifest file in your project to use a local, project-specific version.
    2. If no manifest is found, it looks for a globally installed version of CSharpier on your system.
  9. Enable debug logs in editor extensions

    main

    If the CSharpier extension is unable to format files, you can enable debug logging to capture more information about the failure. The process varies by editor:

    Visual Studio

    1. Navigate to Tools - Options - CSharpier.
    2. Set Log Debug Messages to true.

    VSCode

    1. Navigate to File - Preferences - Settings - Extensions - CSharpier.
    2. Check Enable debug logs.
    3. Restart VSCode.

    Rider

    1. Execute the action Debug Log Settings.
    2. Add an entry for #com.intellij.csharpier.CSharpierLogger.
    3. Restart Rider.
  10. Ignore XML code blocks using csharpier-ignore comments

    main

    Ignoring code in XML follows a similar pattern to C#. Use XML comments to wrap the elements you wish to exclude from formatting.

    • Single element: Use <!-- csharpier-ignore --> before the element.
    • Ranged elements: Use <!-- csharpier-ignore-start --> and <!-- csharpier-ignore-end --> to wrap a block of XML.
    <Root>
      <Child>
        <!-- csharpier-ignore -->
        <Element   />
        
        <!-- csharpier-ignore-start -->
        <Element   />
        <Element   SomeAttribute = "yeah"   />
        <!-- csharpier-ignore-end -->
      </Child>
    </Root>
  11. Configure CSharpier using configuration files

    main

    CSharpier can be configured using several file types. The configuration file is resolved based on the location of the file being formatted: it looks for a .csharpierrc file (JSON or YAML) or a .csharpierrc.json/.csharpierrc.yaml file at or above the target file. If none are found, it falls back to an .editorconfig file, respecting standard editorconfig inheritance.

    Supported configuration files:

    • .csharpierrc (JSON or YAML)
    • .csharpierrc.json (JSON)
    • .csharpierrc.yaml (YAML)
    • .editorconfig (INI)