Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IntelliJ: Dynamically updated file header

By default, IntelliJ Idea will insert (something like) the following as the header of a new source file:

/**
 * Created by JohnDoe on 2016-04-27.
 */

The corresponding template is:

/**
 * Created by ${USER} on ${DATE}.
 */

Is it possible to update this template so that it inserts the last date of modification when the file is changed? For example:

/**
 * Created by JohnDoe on 2016-03-27.
 * Last modified by JaneDoe on 2016-04-27
 */
like image 519
csvan Avatar asked Jul 07 '26 00:07

csvan


1 Answers

Here's a working solution similar to what I'm using. Tested on mac os.

  1. Create a bash script which will replace first occurrence of Last modified by JaneDoe on $DATE only if the exact value is not contained in the file:

    #!/bin/bash
    
    FILE=src/java/test/Test.java
    DATE=`date '+%Y-%m-%d'`
    PREFIX="Last modified by JaneDoe on "
    STRING="$PREFIX.*$"
    SUBSTITUTE="$PREFIX$DATE"
    if ! grep -q "$SUBSTITUTE" "$FILE"; then
      sed -i '' "1,/$(echo "$STRING")/ s/$(echo "$STRING")/$(echo "$SUBSTITUTE")/" $FILE
    fi
    
  2. Install File Watchers plugin.

  3. Create a file watcher with appropriate scope (it may be this single file or any other scope, so that any change in project's source code will update modified date or version etc.) and put a path to your bash script into Program field.

Now every time the file changes the date will update. If you want to update date for each file separately, an argument $FilePath$ should be passed to the script.

like image 150
Oleg Mikhailov Avatar answered Jul 11 '26 22:07

Oleg Mikhailov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!