Basil Bourque
Basil Bourque

Reputation: 340118

Does IntelliJ have feature for adding (or removing) 'this.' prefix in front of appropriate points in the code shown in code editor?

Some people like to omit the this. prefix to calls, such as:

addColor( Color.PURPLE ) ;

… while other folks prefer to include explicitly the this. prefix, such as:

this.addColor( Color.PURPLE ) ;

Does IntelliJ offer any feature for automatically adding and/or removing the this. prefix within the code shown in the editor?

Upvotes: 2

Views: 734

Answers (2)

H3AR7B3A7
H3AR7B3A7

Reputation: 5261

Save Actions plugin

The plugin, Save Actions, offers this functionality.

When installed (and activated) you can find these options under Other Settings -> Save Actions:

  • Add this to field access
  • Add this to method access
  • Remove unnecessary this to field and method

The plugin can be configured to execute these actions on save / idling / window deactivation.

Built into IntelliJ

InteliJ IDEA itself offers these checkstyle functionalities:

Ctrl + Alt + S -> Editor -> Inspections -> Code style issues:

  • Unnecessary 'this' qualifier
  • Instance field access not qualified with 'this'
  • Instance method call not qualified with 'this'

To inspect the code and apply the fix: Code -> Inspect

To auto apply fixes: Code -> Cleanup

Upvotes: 2

Bas Leijdekkers
Bas Leijdekkers

Reputation: 26577

IntelliJ has the following inspections to add a this qualifier to method calls and field references:

Java | Code style issues | Instance field access not qualified with 'this'
Java | Code style issues | Instance method call not qualified with 'this'

And the following inspection to remove unnecessary this qualifiers:

Java | Code style issues | Unnecessary 'this' qualifier

All three inspections have a quick fix, and code can be fixed in batch using Code | Inspect Code and applying the fix on the result or Code | Cleanup to immediately clean up the code.

The Save Actions plugin just calls these built-in inspections.

Upvotes: 2

Related Questions