belav / csharpier

CSharpier is an opinionated code formatter for c#.
https://csharpier.com
MIT License
1.41k stars 98 forks source link

Configurable indentation/levels for fluent methods #1269

Open BenjaBobs opened 5 months ago

BenjaBobs commented 5 months ago

When using fluent methods with different levels of semantic meaning, it can be difficult to maintain good readability with CSharpier currently, since it doesn't know the semantic difference between two fluent method calls. Here's a tiny example of using a FluentMappingBuilder from Linq2Db to setup

Input:

// example from Linq2Db's FluentMappingBuilder
builder
  .Entity<Authors>().HasTableName("authors")
  .Property(x => x.Id).HasColumnName("Id").IsIdentity().IsPrimaryKey()
  .Property(x => x.Name).HasColumnName("Name").IsNotNull()
  .Entity<Book>().HasTableName("books")
  .Property(x => x.Id).HasColumnName("Id").IsIdentity().IsPrimaryKey()
  .Property(x => x.Name).HasColumnName("Name").IsNotNull()
  .Property(x => x.AuthorId).HasColumnName("AuthorId").IsNotNull()
  .Build();

Output:

builder
    .Entity<Authors>()
    .HasTableName("authors")
    .Property(x => x.Id)
    .HasColumnName("Id")
    .IsIdentity()
    .IsPrimaryKey()
    .Property(x => x.Name)
    .HasColumnName("Name")
    .IsNotNull()
    .Entity<Book>()
    .HasTableName("books")
    .Property(x => x.Id)
    .HasColumnName("Id")
    .IsIdentity()
    .IsPrimaryKey()
    .Property(x => x.Name)
    .HasColumnName("Name")
    .IsNotNull()
    .Property(x => x.AuthorId)
    .HasColumnName("AuthorId")
    .IsNotNull()
    .Build();

Expected behavior:

builder
  .Entity<Authors>().HasTableName("authors")
      .Property(x => x.Id).HasColumnName("Id").IsIdentity().IsPrimaryKey()
      .Property(x => x.Name).HasColumnName("Name").IsNotNull()
  .Entity<Book>().HasTableName("books")
      .Property(x => x.Id).HasColumnName("Id").IsIdentity().IsPrimaryKey()
      .Property(x => x.Name).HasColumnName("Name").IsNotNull()
      .Property(x => x.AuthorId).HasColumnName("AuthorId").IsNotNull()
  .Build();

But the issue is how we can teach CSharpier about the significance of calling Entity<>() vs for example calling Property() or HasColumnName(). I was thinking that maybe a comment-style approach similar to how // csharpier-ignore currently works might be a good solution. E.g. something like this to apply to level-rules to current scope:

// csharpier-level 1 Entity<>()
// csharpier-level 2 HasTableName(), Property()

This is kind of an advanced feature, and probably more thought would have to go into it for it to become really good, but I'm submitting now to start the discussion. Thoughts?