gpt4 book ai didi

python - 使用 emacs/python/winmerge 跨两个 diff 文件映射行号

转载 作者:太空狗 更新时间:2023-10-30 02:49:25 26 4
gpt4 key购买 nike

考虑以下两个略有不同的文件:

foo(旧版本):

<Line 1> a
<Line 2> b
<Line 3> c
<Line 4> d

foo(新版本):

<Line 1> a
<Line 2> e
<Line 3> b
<Line 4> c
<Line 5> f
<Line 6> d

如您所见,新文件中引入了字符ef

我有一组与旧文件相对应的行号……例如,134(对应于字母 acd)。

有没有办法在这两个文件之间进行映射,以便我可以获得较新文件中相应字符的行号?

例如,结果将是:

Old file line numbers (1,3,4) ===> New File line numbers (1,4,6)

不幸的是,我只有 emacs(带有可用的 ediff)、Python 和 winmerge 可供我使用。

最佳答案

您可以在 Emacs 中完成这一切:

(defun get-joint-index (file-a index file-b)
(let ((table (make-hash-table :test #'equal)))
(flet ((line () (buffer-substring-no-properties
(point-at-bol) (point-at-eol))))
(with-temp-buffer (insert-file file-b)
(loop for i from 1 do (puthash (line) i table)
while (zerop (forward-line))))
(with-temp-buffer (insert-file file-a)
(loop for i in index do (goto-line i)
collect (gethash (line) table))))))

运行,

M-:(get-joint-index "/tmp/old"'(1 3 4) "/tmp/new")

-> (1 4 6)

关于python - 使用 emacs/python/winmerge 跨两个 diff 文件映射行号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8337098/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com