1

I am attempting to improve a Scala Companion Object plus Case Class code generating template in IntelliJ 2021.3 (Build #IU-213.5744.223, built on November 27, 2021).

I would like to allow the user to be able to leave some of the input parameters unspecified (empty) and then have the template generate sane defaults. This is because the generated code can then be refactored by the client to something more pertinent later.

My question is this: Is there another way to approach achieving my goal than the way I show below using the Velocity #macro (which I got from here, and then doesn't appear to work anyway)?

With the current approach using the #marco, when I invoke it...

With:

  1. NAME = "Longitude"
  2. PROPERTY_1_VARIABLE = "value"
  3. PROPERTY_1_TYPE - "Double"

The Scala code target:

final case class Longitude(value: Double)

With:

  1. NAME = "Longitude"
  2. PROPERTY_1_VARIABLE = ""
  3. PROPERTY_1_TYPE - ""

The Scala code target using defaults for 2 and 3:

final case class Longitude(property1: Property1Type)

And here's my attempt to make it work with the IntelliJ (Velocity) Code Template:

#macro(condOp $check, $default)
  #if ($check == "")
    $default
  #else
    $check
  #end
#end

#set (${PROPERTY_1_VARIABLE} = "#condOp(${PROPERTY_1_VARIABLE}, 'property1')")
#set (${PROPERTY_1_TYPE} = "#condOp(${PROPERTY_1_TYPE}, 'Property1Type')")

#if ((${PACKAGE_NAME} && ${PACKAGE_NAME} != ""))package ${PACKAGE_NAME} #end

final case class ${NAME} (${PROPERTY_1_VARIABLE}: ${PROPERTY_1_TYPE})

First, the IntelliJ popup box for entering the parameters correctly shows "Name:". And then incorrectly shows "default:" and "check". I have submitted a ticket with Jetbrains for this IntelliJ issue. Please vote for it, if you get a chance so it increases in priority.

And then when the macro finishes executing (I provide the Name of "LongLat", but leave the default and check parameters empty), I don't understand why I get the following gibberish.



package org.public_domain

${PROPERTY_1_TYPE})

Any guidance on fixing my current Velocity Code Template script or suggest another way to effectively approach solving the problem would be deeply appreciated.

chaotic3quilibrium
  • 5,661
  • 8
  • 53
  • 86
  • Just to be clear, this is about the (Velocity) macro functionality provided within IntelliJ. While Scala is the language used in the examples, I also need this problem solved for Java and Koitlin, too. IOW, this is NOT about Scala, nor is it about the macros feature within Scala itself. – chaotic3quilibrium Feb 04 '22 at 19:46
  • There is now a Scala article where this Jetbrains IntelliJ defect is especially pronounced and highlighted: https://gist.github.com/chaotic3quilibrium/58e78a2e21ce43bfe0042bbfbb93e7dc – chaotic3quilibrium Feb 25 '22 at 22:53

0 Answers0