Bass
Bass

Reputation: 5338

The exact syntax of Velocity Template in IDEA Copyright Profile

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, "-")

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).

Upvotes: 1

Views: 178

Answers (1)

martinBoy
martinBoy

Reputation: 127

For my personal use case I keep an original Copyright year in the code and do not update it to the current year.

#set( $oldYear = $originalComment.match("Copyright \(c\) (\d{4})", 1, ""))
Copyright (c) #if($oldYear)$oldYear#{else}$today.year#end

For your use cases you can define as many catching variables and use them in exact scenarios.

Upvotes: 0

Related Questions