Write ctags support for diff/patch file in vim

130 views Asked by At

I often use this in my project, to see changes :

git diff 5b80e3be314143f  3b34d24a55ab -- include/* src/* | vim -

All the lines starting with the file being changes are written like this:

diff --git a/include/behaviors/mechanical/crystal_behavior.h b/include/behaviors/mechanical/crystal_behavior.h

I would like to be able to navigate easily the files with tagbar, which uses ctags, and obtain for instance the filename, here crystal_behavior.h. So, I know I can use something like this in a .ctags file:

--langdef=diff
--langmap=diff:.diff
--regex-diff=/^diff --git .*b\/(.*)(\/(.*))+$/\.\. \2/d,file/

Together with this in my .vimrc

let g:tagbar_type_diff = {
  \ 'ctagstype' : 'diff',
  \ 'kinds'     : [
    \ 'd:Files',
    \ ],
    \ 'sort'    : 0,
\ }

My problem here is what can I put in here --regex-diff=*, to obtain filename? (I got more or less) Is it possible to go further and also tag the hunks in each files? This is the most tricky part I cannot get.

Thank you

  • Bonus question: If I read the diff directly from buffer, tagbar doesn't generate tags. Whereas if I do git diff ...... > patch.diff, and then vim patch.diff, it works.
1

There are 1 answers

0
Masatake YAMATO On

Universal-ctags(https://ctags.io) has a built-in parser for diff imported from geany:

[jet@localhost tmp]$ cat foo.diff 
diff --git a/source.mak b/source.mak
index 2550028..eaa9154 100644
--- a/source.mak
+++ b/source.mak
@@ -44,6 +44,7 @@ PARSER_SOURCES =              \
    $(PARSER_DIR)/clojure.c         \
    $(PARSER_DIR)/css.c         \
    $(PARSER_DIR)/cobol.c           \
+   $(PARSER_DIR)/diff.c            \
    $(PARSER_DIR)/dosbatch.c        \
    $(PARSER_DIR)/eiffel.c          \
    $(PARSER_DIR)/erlang.c          \
[jet@localhost tmp]$ ~/var/ctags/ctags --fields=+K --sort=no -o - foo.diff 
a/source.mak    foo.diff    /^--- a\/source.mak$/;" modifiedFile
-44,6 +44,7 foo.diff    /^@@ -44,6 +44,7 @@ PARSER_SOURCES =                \\$/;"  hunk    modifiedFile:a/source.mak