Community
Participate
Working Groups
Build ID: I20111207-2118 If a programmer selects the left-hand side "i" from a statement of form "i = 0;" and invokes the Extract Method refactoring, the refactoring tool will report the following problem and prevent the programmer from continuing the refactoring: >Cannot extract the left-hand side of an assignment. However, we noticed that the refactoring tool will report the above error message even if the programmer selects an expression of form "i = 0" from a statement of form "i = 0;". Two of the participants of the CodingSpectator study <http://codingspectator.cs.illinois.edu/> forgot to include the trailing semicolon in their selections and ran into this problem. This problem occurred three times. Because our participants sometimes repeated the refactoring multiple times, they received the above error message five times. In all three instances, our participants eventually overcome the problem by including the trailing semicolon in their selections. It would be nice if the refactoring automatically extends the selection to cover the trailing semicolon the same way that it shrinks the selection to exclude the trailing semicolon in some cases (See Bug 324237).
The if condition in ExtractMethodAnalyzer.visit(Assignment) does not look perfect.
What about using this: if ((getSelection().covers(node.getLeftHandSide()) && !getSelection().covers(node.getRightHandSide())) || getSelection().coveredBy(node.getLeftHandSide())) {
(In reply to comment #2) > if ((getSelection().covers(node.getLeftHandSide()) && > !getSelection().covers(node.getRightHandSide())) || > getSelection().coveredBy(node.getLeftHandSide())) { Thanks! That works. Fixed in master - 371ad683cf46d904a9c00c3494d31522d33f5b08 Note that the result is slightly different when you select 'i = 0;' or 'i = 0', but I think that is OK.
(In reply to comment #3) > Note that the result is slightly different when you select 'i = 0;' or 'i = 0' How are the results different?
(In reply to comment #4) > How are the results different? New method with ; selected private void extracted() { field = 1; } New method with ; NOT selected private int extracted() { return field = 1; } Full code snippets are available here - http://git.eclipse.org/c/jdt/eclipse.jdt.ui.git/commit/?id=371ad683cf46d904a9c00c3494d31522d33f5b08
(In reply to comment #5) > (In reply to comment #4) > > How are the results different? > > New method with ; selected > private void extracted() { > field = 1; > } > > New method with ; NOT selected > private int extracted() { > return field = 1; > } > > Full code snippets are available here - > http://git.eclipse.org/c/jdt/eclipse.jdt.ui.git/commit/?id=371ad683cf46d904a9c00c3494d31522d33f5b08 Deepak, Thanks for pointing me to the examples. The different behaviors in the above two cases seem consistent with how Eclipse handles other similar selections. I will try a few more examples when a build of Eclipse that contains your patch appears at http://download.eclipse.org/eclipse/downloads/