Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
646 views
in Technique[技术] by (71.8m points)

eclipse - How to undo / redo selective parts of code?

I am using Eclipse.

It happens a lot when we develop code like this (assume it is developed sequentially, from top to bottom):

Part 1 (*)
Part 2
Part 3
Part 4 (*)
Part 5

But we just figured out that parts 1 and 4 (marked with (*)) are wrong and the others are just fine. The question is that how we can undo just those two parts (1 and 4) without undoing the rest?

If we could undo selectively, it was great. Note that simply reverting the code to the version 1 loses parts 2, 3 and 5 which are correct parts and should remain in the code. Also note that usually these parts are mixed in one or two code blocks (not in separate blocks).

Example:

Part 1: Add method f1(x, y) and move some code from main() to f1() --> incorrect (should be reverted)    
Part 2: Add method f2(a, b, c, d) --> correct (should remain)    
Part 3: Change another part of main() implementation --> correct (should remain)       
Part 4: Change f2 signature to f2(s, n) --> incorrect (should be reverted)       
Part 5: Change body of f2 --> correct (should remain)

The current approach I use is:

  1. Keeping a copy of the latest version somewhere (e.g., in a temporary text file), then undo to before part 1, and add those correct parts from the temporary text file to the source code.
  2. Manual comparison of different versions, and resolving conflicts.

Does anybody think of easier, yet more automatic way of selecting which change to undo and which one to keep?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Eclipse keeps a history of your changes for a few days (configured in the Preferences in 'General > Workspace > Local History'). You can right click on a file and select 'Compare With > Local History' to see the differences between two version of your file. You can copy changes from the old version to the current version.

For the longer term you should use a version control system such as SVN or GIT. There are Eclipse plugins for these which let you do similar 'Compare With' operations - but covering the entire history of the file (provided you commit your changes regularily).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...