javavscode icon indicating copy to clipboard operation
javavscode copied to clipboard

Renaming should not blindly replace occurrences in comments

Open anthonyvdotbe opened this issue 2 years ago • 1 comments

Consider the following code:

import java.io.ByteArrayInputStream;
import java.io.IOException;

/**
 * In is common in English text.
 */
class Test {

    void run() throws IOException {
        try(var in = new ByteArrayInputStream(new byte[0])) {
            in.read();
        }
    }

}

Now if I rename the in variable in the try statement, the in in the class' Javadoc is also renamed. I can see how updating comments as well can be useful, but then the extension should figure out which occurrences refer to the replaced variable and which don't (which, in the general case, would be extremely hard to determine). For example in this case, I'm renaming a local variable, so I'd never refer to it in the class' Javadoc.

anthonyvdotbe avatar Oct 29 '23 07:10 anthonyvdotbe

I ran into this as well, but only occurrences in the comment on the current method were affected. My workaround: temporarily detach the Javadoc comment from the method by inserting a field declaration, rename the variable, remove the dummy field.

/**
 * Method comment, containing the word "name".
 */
int dummy;

void func() {
  int name = 0;  // now I can rename `name` without affecting the word in the comment.
}

jcsahnwaldt avatar Nov 09 '23 13:11 jcsahnwaldt

Merged

Achal1607 avatar Sep 11 '24 17:09 Achal1607