tek / splain

better implicit errors for scala
MIT License
370 stars 29 forks source link

Configuration for shortening types / better support for newtypes #24

Closed mrdziuban closed 5 years ago

mrdziuban commented 5 years ago

Would it be possible to add configuration for how much to shorten types in errors? Maybe just an int to denote the minimum number of package/path parts to keep, defaulting to 1? e.g. for a type foo.bar.baz.Thing, setting the value to 2 would print baz.Thing instead of just Thing.

This would make it a lot easier to use splain along with newtypes, which generate code like this:

@newtype case class WidgetId(toInt: Int)
// becomes
type WidgetId = WidgetId.Type
object WidgetId {
  type Base = Any { type WidgetId$newtype }
  trait Tag extends Any
  type Type <: Base with Tag
}

so any errors for the WidgetId type printed by splain always end up just saying Type.

tek commented 5 years ago

https://github.com/tek/splain#truncating-module-paths

please see if this is satisfying to you!

mrdziuban commented 5 years ago

Aha, I should have scrolled down further 🤦‍♂️ thank you!

tek commented 5 years ago

oh no, I just built this!

should be on maven is a few minutes/hours.

mrdziuban commented 5 years ago

Oh, I see! Well then thank you again -- I'll give it a shot!

mrdziuban commented 5 years ago

@tek just following up, this works great. I ended up using this flag:

-P:splain:rewrite:^(([^\\.]+\\.)*)([^\\.]+)\\.Type$/$3

to rewrite anything like foo.bar.Baz.Type into just Baz. Thanks for the quick fix and all your work on splain!

tek commented 5 years ago

it's my pleasure. thanks for the feedback! glad to here it's working out for you