I'm trying to refactor a python module in wh开发者_开发问答ich the main problem is duplicate code.
Is there any tool that I can use to view diff of two selected sections, so that I can see the difference between those almost-the-same codes?
I've done this occasionally before using Emacs:
- Clone an indirect buffer (
M-x clone-indirect-buffer
). - Mark the one section and narrow (
C-x n n
) to it. - Switch to the original buffer and narrow to the other section.
- Run Ediff (
M-x ediff-two-buffers
) on the buffer and its clone.
A nice perk is that you can merge chunks between them and it won't get confused.
While I don't think the interface is great, check Clone Digger. See the example.
The Thinking Craftsman's Tool Kit (TC Toolkit) might make it easier to find the duplicated code, but doesn't seem to offer good visualization.
Edit: If you're on Willing to pay, Atomiq looks interesting.
For good visualization, I had some success with Diffuse, as it allows arbitrarily matching lines from one file to the other:
However, I had to actually copy the file with a different name for things to work well.
I use ExamDiff Pro for all of my file diffs. It's a graphical analyzer.
http://www.prestosoft.com/edp_examdiffpro.asp
If you only have clones in one file and you know where they are, a clone detection tool might be overkill. But thier ability to detect the clones and show them to you directly seems to be exactly what you want.
Our CloneDR has a Python-specific version. If it finds clones (even in the "same" file), it shows them to you in isolation, and it shows you exactly how they differ according to that language (Python) rules, as opposed to just layout, including places where the clones vary and precisely how they vary. (Atomiq as I understand it will only detect clones that have exactly the same text lines).
CloneDR runs under Windows just fine.
Beyond Compare's text compare has an "Open Clipboard" command that will put whatever is on the clipboard on one side of the comparison. If you then copy the second function and use the command again it will line them up with differences highlighted.
精彩评论