Skip to content
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

Add mode to strip type prefix for SourceCodePrinter #10172

Merged
merged 1 commit into from
Nov 27, 2020

Conversation

tgodzik
Copy link
Contributor

@tgodzik tgodzik commented Nov 4, 2020

This is a proposal to help with #9716

I see this is already an issue both in mdoc and in munit. It most likely also help with pprint's TypePrinter.

What do you think? I can add some tests if the solution is sensible.

Copy link
Member

@dottybot dottybot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, and thank you for opening this PR! 🎉

All contributors have signed the CLA, thank you! ❤️

Have an awesome day! ☀️

@@ -1157,12 +1162,14 @@ class SourceCodePrinter[R <: Reflection & Singleton](val reflect: R)(syntaxHighl
printFullClassName(tp)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
printFullClassName(tp)
if printTypePrefix then
printFullClassName(tp)
else ??? // print class name

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or maybe change the logic of printFullClassName.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a guard inside printFullClassName

@tgodzik tgodzik force-pushed the strip-type-prefix branch 2 times, most recently from f79097f to 894fe06 Compare November 25, 2020 15:42
@@ -94,9 +94,11 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
def showExtractors: String =
Extractors.showTree(using QuotesImpl.this)(self)
def show: String =
SourceCode.showTree(using QuotesImpl.this)(self)(SyntaxHighlight.plain)
SourceCode.showTree(using QuotesImpl.this)(self)(SyntaxHighlight.plain, fullNames = true)
def showShortTypes: String =
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about the additional showShortTypes @nicolasstucki ? Otherwise we will always need to bring in the compiler in with SyntaxHighlight

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would call it showShort.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed and added it to interfaces.

@tgodzik
Copy link
Contributor Author

tgodzik commented Nov 25, 2020

I finally managed to rebase and address the comments. Having an additional showShortTypes methods would be super useful for both mdoc and I think pprint (it would make it much easier to print types). Let me know what you think! I'll create tests if needed, let me know where best to add them if the current proposal is ok.

Copy link
Contributor

@nicolasstucki nicolasstucki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation looks good.

@@ -94,9 +94,11 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
def showExtractors: String =
Extractors.showTree(using QuotesImpl.this)(self)
def show: String =
SourceCode.showTree(using QuotesImpl.this)(self)(SyntaxHighlight.plain)
SourceCode.showTree(using QuotesImpl.this)(self)(SyntaxHighlight.plain, fullNames = true)
def showShortTypes: String =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would call it showShort.

…nd add an additional `showShortTypes` method
@tgodzik
Copy link
Contributor Author

tgodzik commented Nov 25, 2020

Managed to simplify the logic in mdoc to:

trait TPrint[T]{
  def render: String
}

object TPrint {
  inline given default[T] as TPrint[T] = ${ TypePrinter.typeString[T] }
}

object TypePrinter{

  def typeString[T](using ctx: Quotes, tpe: Type[T]): Expr[TPrint[T]] = {
    import quotes.reflect._
    val valueType = Type.showShort[T]

    '{  new TPrint[T]{ def render: String = ${ Expr(valueType) } }  }
  }
}

That should also work in pprint

Also we could use it here: https://github.com/scalameta/munit/blob/master/munit/shared/src/main/scala-3.0.0-M2/munit/internal/MacroCompat.scala#L28

@nicolasstucki nicolasstucki merged commit 9ce7791 into scala:master Nov 27, 2020
@nicolasstucki
Copy link
Contributor

Thank you @tgodzik

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants