The exact syntax of Velocity Template in IDEA Copyright Profile

72 Views Asked by At

IntelliJ IDEA has a convenient feature which can maintain copyright comments in your code, via the Update Copyright action.

Yet, the documentation is scarce and barely covers one single use-case.

The question is, what is the exact signature (and behaviour) of the ${originalComment.match()} function? Looking at the Velocity template language documentation, there's no standard match() function.

Looking at the only example provided by JetBrains:

$originalComment.match("Copyright \(c\) (\d+)", 1, "-")
  • the 1st argument is the regular expression per se,
  • the 2nd one (1) is probably the number of the RE capturing group to extract, and
  • the last one ("-") is the suffix to be appended to the extracted RE group value.

Yet, after some testing, the behaviour of the function seems counter-intuitive.

My own use case:

  1. New files should have Copyright (c) 2024-2024 as their copyright statement.
  2. Copyright (c) 1970 should get updated to Copyright (c) 1970-2024.
  3. Copyright (c) 1970-1971Copyright (c) 1970-2024.
  4. Copyright (c) 2024Copyright (c) 2024-2024.
  5. Copyright (c) 2024-2024 should remain intact on update.

What my Velocity template should look like?

The closest I could get to the solution is

Copyright (c) My Company ${originalComment.match("Copyright \([Cc]\) .+ (\d{4})(?:\-\d{4})?", 1, "-")}${today.year}.

— but, obviously, it doesn't work for new code (Item 1 is not covered) and, strangely, Item 4 doesn't work, either (despite I'm not using any ignore values).

0

There are 0 best solutions below