Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 329614

Summary: [linked mode] Incorrect selection and caret position after rename in file
Product: [Eclipse Project] JDT Reporter: Deepak Azad <deepakazad>
Component: TextAssignee: Markus Keller <markus.kell.r>
Status: RESOLVED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: daniel_megert
Version: 3.0   
Target Milestone: 3.7 M4   
Hardware: All   
OS: All   
Whiteboard:
Attachments:
Description Flags
fix and tests
none
fix and tests 2 none

Description Deepak Azad CLA 2010-11-06 11:32:47 EDT
I20101028-1441

Steps
- Select longName
- Ctrl+1 > Rename in file
- Rename the variable to 'name', press enter to exit linked mode
- 'n' in 'name' is selected and the caret is also placed after 'n'
Correct behavior: 'name' is selected and caret placed after 'name'
package test1;

public class E {
	private void foo() {
		int longName = 10;  //select on this line
		longName++;
		longName--;
		--longName;
		++longName;
	}
}

This works correctly with Ctrl+1 > Rename in Workspace. (Though if you notice carefully, for a split second it shows the same selection as Rename in File before it corrects itself.)
Comment 1 Dani Megert CLA 2010-11-08 03:36:18 EST
Like that since day one. Either a general Linked Mode bug or a bug in LinkedNamesAssistProposal.

Markus, can you take a look?
Comment 2 Deepak Azad CLA 2010-11-09 09:09:11 EST
I see the same problem with 'Extract to local variable', 'Extract to constant', 'Extract to method' quick assists.
Comment 3 Markus Keller CLA 2010-11-11 10:22:09 EST
Simpler example for debugging (note that the original position of the caret is important):

- have class:
class ClassToRename {}
- copy "bla" to clipboard
- put caret before "ClassToRename"
- Ctrl+2, R
- select "ClassTo"
- paste

Now what happens is this:
- DefaultPositionUpdater.update(DocumentEvent) gets an update event that replaces "ClassTo" with "bla" (good)
- The second position to update is a LinkedPosition; this is the exit position that has been set earlier in LinkedModeUI#setExitPosition(). Its length is 0.
- When DefaultPositionUpdater#adaptToRemove() is called for that position, the LinkedPosition's length is wrongly set to -1, but this get corrected at the end of the method.
- In DefaultPositionUpdater#adaptToInsert(), the bug happens on line 117, where
    fPosition.length += fReplaceLength;
wrongly increases the length of the exit position.
Comment 4 Markus Keller CLA 2010-11-11 13:30:20 EST
Created attachment 182928 [details]
fix and tests

This is a long standing bug in the DefaultPositionUpdater.

Will release after next I-build, since I'll not be around till Wednesday, and since this needs a full week of selfhost-testing (to find possible collateral damage in clients that relied on the wrong behavior).
Comment 5 Markus Keller CLA 2010-11-17 08:02:45 EST
Fixed in HEAD.
Comment 6 Markus Keller CLA 2010-11-18 15:23:17 EST
Created attachment 183420 [details]
fix and tests 2

The original fix was not fully consistent and produced test failures.
Fixed in HEAD with this patch.