How is 'Introduce Constant' supposed to work?

This is with 6.0.4.

I frequently have code that looks like:


where I want to turn 123 into a constant since I think that is better style.

But when I position the caret on either foo (the variable name) or 123 (the variable default value) and choose Refactor -> Introduce Constant, I am greeted with the following error:

"Cannot perform refactoring. The caret should be positioned at the name of the local variable to be refactored."

This is strange since 'Introduce Constant' should apply to the value assignment, not to the variable name.

What I expected IDEA to do is to create this following code:


where I can overwrite CHANGE_ME as with a live template.

Or am I understanding this refactoring completely wrong here?

S.

7 comments
Comment actions Permalink

But when I position the caret on either foo (the
variable name) or 123 (the variable default value)
and choose Refactor -> Introduce Constant, I am


Hmm, that's exactly how it works for me. Put the cursor inside 123, hit CtrlAltC (introduce constant) and it pops up a dialog for name/accessors for the constant.

I'm on 6.0.5 beta now (6161), but I use this all the time and I don't remember having any problems with it in previous builds.

You have to put the cursor on the value, not on the variable name (that's when you get the pop-up error message). You can also select the value before invoking the refactoring.

HTH,
Andrei

0
Comment actions Permalink

The error message is wrong.
You have to select (all three characters) the '123', not just put the caret in it.

There's also the SmartRefactor plugin that helps here.

0
Comment actions Permalink


That's how it works. Apart from the "live template" stuff, that's just a
textfield on a dialog.
In addition, having SmartIntroduce installed makes the whole Ctrl-Alt-C thing
very fluent.

-tt


0
Comment actions Permalink

Ah yes. Then it works.

Odd default. I think I'll report it.

S.

0
Comment actions Permalink

Good hint. I installed SmartIntroduce. But I still think this is a little IDEA annoyance that should be fixed instead of being patched by an plugin.

0
Comment actions Permalink


Sure, I fully agree: the 'flow' that SmartIntroduce provides should be IDEA
default.

That said, Sascha did a really good job with SmartIntroduce. I'm almost never
aware that I'm using that plugin, but can hardly use IDEA without it - really.
Btw, I've never changed any SmartIntroduce settings, or explored any of the
"rating" options...

-tt


0
Comment actions Permalink

Good point, I completely forgot about having the plugin installed. It's so "IDEA-like" that I just attributed to the default IDEA installation the functionality it offers. :)

Andrei

0

Please sign in to leave a comment.