How to undo / redo selective parts of code?

风流意气都作罢 提交于 2019-12-18 07:16:06

问题


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?


回答1:


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).




回答2:


I just found this paper:

Supporting Selective Undo in a Code Editor which will be presented in ICSE 2015 conference.

The authors show the history of changes graphically, so you can choose which changes to undo (and which one to keep).

Azurite (can be download and installed from here) is the name of their implemented Eclipse plugin that supports selective undo and lots of other simple features that are useful for developers.



来源:https://stackoverflow.com/questions/29949516/how-to-undo-redo-selective-parts-of-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!