Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Tom Snee
    @tsnee
    I've had some success writing a library of mostly generated code. I read XML and use scalameta to write Scala sources that I compile and package. I throw away a lot of documentation that my users would like to have because I don't know how to put it in the generated files. I'm told I can use scalafix APIs to take my scalameta objects and basically decorate them with comments before I write them out, which would allow my users to hover over my generated code in their IDEs and see the documentation. Reading the scalafix developer's guide did not tell me how to do that. Would this approach even work? If so, is there some code I could read to figure out how to get started?
    Ólafur Páll Geirsson
    @olafurpg
    @tsnee there’s no clean way to generate comments on scalameta trees. We had a WIP pretty-printer that supported this usecase but it never completed
    I personally prefer to use something like Paiges to print scala code instead of Scalameta
    It’s nice to use scala.meta.Lit to escape string literals, but otherwise there’s not much you gain from scalameta for this usecase. Sure, scalameta helps you generate syntactically valid code but you still need tests to validate the code typechecks
    Tomasz Godzik
    @tgodzik
    @nafg so is this a change stemming from -Xsource:3 ? I thought it was connected to optional braces
    we can move that to the source3 dialect
    just shoot out an issue, we will take a look
    Tom Snee
    @tsnee
    @olafurpg Thank you for the advice, I appreciate it.
    nafg
    @nafg
    @tgodzik without -Xsource:3 in scala 2 you have to use with not &
    It's true that allowing operators on the beginning of the next line was added during the course of supporting whitespace syntax. But is & an operator?
    In any case operators were always allowed on the next line if there was no chance for the expression to be over. For instance this has been valid since maybe forever:
    val x = (1
     + 2)
    Even if the same rules apply to infix types, and & is an infix type, since a self type has to be followed by => it shouldn't require special support to put the & on the next line
    Previously I had with on the next line so I don't see why replacing with with & in place should break
    Tomasz Godzik
    @tgodzik
    @nafg then it's a bug, not sure what's it caused by yet
    MartinWelgemoed
    @MartinWelgemoed

    Hello, I have a fairly specific question that I can't seem to figure out how to do.

    What I am doing is loading in a Tree from a file, modifying the Tree, and writing it back out to a file. What I want to know is, on which lines in the output file can I find my changed code?

    The only way I can see how to do this is to load the output back in again and search inside it for the change I made and then read the origin, but is that the best/only way?

    Tomasz Godzik
    @tgodzik
    @MartinWelgemoed you would need to do a diff of the file, there are cmd file such as diff that should help out here
    alternatively there is a diff library included in the testkit that should also help
    MartinWelgemoed
    @MartinWelgemoed
    Ah, thank you.
    blast_hardcheese
    @blast_hardcheese:matrix.org
    [m]

    on the subject of not being able to decorate trees with comments, is the reason this is "supported" in scalafix because it just renders strings of the trees it encounters as-is, or something else?

    I did some research into how to make this happen on a custom fork of the pretty printer, but I don't remember where I got stuck.

    Ólafur Páll Geirsson
    @olafurpg
    @blast_hardcheese:matrix.org scalafix patches work on the level of tokens, not treea
    trees*
    Scalameta trees are effectively immutable, which combined with parent links make them pretty difficult to work with
    blast_hardcheese
    @blast_hardcheese:matrix.org
    [m]
    aah, right, that jogs my memory. I found the comment tokens, but I couldn't easily round-trip them through unapply, so they were easily stripped.
    I utilize scalameta heavily in guardrail, not just for source generation but to add a bit of type safety to functions.
    I really value the work you've done, for the most part using it has been fantastic.
    being able to write in quasiquotes but still able to inspect and unpack the trees is brilliant.
    we added Java support a while back through javaparser (also has ASTs and source generation), the resulting code is so less clean for not having quasiquotes.
    Ólafur Páll Geirsson
    @olafurpg
    My general advice is to use something else than Scalameta for code generation. You need to validate the generated code compiles either way so you don't get much benefit from the compile-time safety of scala.meta.Tree (which only guarantees you generate syntactically valid code, not semantically valid)
    I personally like typelevel/paiges since it has a beautiful api and includes line wrapping
    including comments becomes trivial once you build on top of something like paiges
    blast_hardcheese
    @blast_hardcheese:matrix.org
    [m]
    I've already got quite a bit powered by scalameta, I've been doing that for four years at this point. For my use case Paiges wouldn't have offered a serious benefit to me over scalameta, fwiw. It's more than formatting, it's also escaping terms, as well as having useful toString in debug messages
    but your advice is appreciated anyway, just trying to raise awareness that there is a space that works quite well using scalameta exclusively
    Ólafur Páll Geirsson
    @olafurpg
    I’m glad to hear that. Was not my intention to discount your experience, just wanted to share my perspective given that code generation is a common topic in this channel
    Márcio Conceịç̣̣ão Goulart
    @marciocg
    image.png
    Hello! Are someone experiencing some issues in debug test within VSCode? Since last update I can't debug code anymore, the debugger halts saying that reached the breakpoint (red square on image) but doesn't make F10/F11 available (blue square on image). Already cleaned the project e reimported the builds, but it didn't worked. Any ideas?
    1 reply
    Tomasz Godzik
    @tgodzik
    @marciocg there was an issue for windows in the last release, you can update to a latest snapshot, where it's fixed
    4 replies
    rcano
    @rcano
    Hello! is it possible to keep comments when generating AST nodes with quasiquotes? when using parse[Stat] or similar, comments are retained, but when producing trees with the several interpolators, they get discarded. I also can't find any mean to modify the generated node (typically by using copy or some such) to add comments to it
    Ólafur Páll Geirsson
    @olafurpg
    Kai Matsuda
    @vangogh500
    Hi how can I accomplish below in scalameta?
    typeOf[MyType[_]].typeSymbol.asType
    Basically i have higher order type MyType which I want to convert to a scala.meta.Type.Apply
    Ólafur Páll Geirsson
    @olafurpg
    @vangogh500 have you tried using quasiquotes?
    alternatively, you can parse the FQN of that type
    Max Konstantinov
    @MaxNevermind
    Hi all,
    I've just started looking into scalameta and yet aware of its capabilities. I want to apply a custom formatting for Scala multi-line stings inside of unit tests in my pet project. I'm not asking about how to do that, just wanted to ask if scalameta has the capabilities of achieving that?
    Ólafur Páll Geirsson
    @olafurpg
    @MaxNevermind have you looked at whether you can achieve what you need with scalafix?
    scalafix uses scalameta to implement refactoring rules. This sounds like a feature you can implement with a syntactic rule
    Tom Snee
    @axtsnee
    Doing another code generation project. All my functions return the same type: String. My coworkers feel that better types would make the code more maintainable. Paiges just uses Doc and so would not be a big improvement by itself. How realistic would it be to think about adding support for comments to scalameta? Is there a better solution?
    Parth Sareen
    @ParthSareen
    Hi! I'm using scalafix to do simple replacements in my codebase. However I'm having trouble being able to match with Either.right.get this is what I have so far val getMatch = SymbolMatcher.normalized("scala/util/Either#right#get") I get successful matches for Either.right using SymbolMatcher.normalized("scala/util#Either#right"). Thank you!