ctags.kak (8920B)
1 # Kakoune CTags support script 2 # 3 # This script requires the readtags command available in universal-ctags 4 5 declare-option -docstring "minimum characters before triggering autocomplete" \ 6 int ctags_min_chars 3 7 8 declare-option -docstring "list of paths to tag files to parse when looking up a symbol" \ 9 str-list ctagsfiles 'tags' 10 11 declare-option -hidden completions ctags_completions 12 13 declare-option -docstring "shell command used to read tag files" str readtagscmd "readtags" 14 15 define-command -params ..1 \ 16 -shell-script-candidates %{ 17 realpath() { ( cd "$(dirname "$1")"; printf "%s/%s\n" "$(pwd -P)" "$(basename "$1")" ) } 18 eval "set -- $kak_quoted_opt_ctagsfiles" 19 for candidate in "$@"; do 20 [ -f "$candidate" ] && realpath "$candidate" 21 done | awk '!x[$0]++;' | # remove duplicates 22 while read -r tags; do 23 namecache="${tags%/*}/.kak.${tags##*/}.namecache" 24 if [ -z "$(find "$namecache" -prune -newer "$tags")" ]; then 25 cut -f 1 "$tags" | grep -v '^!' | uniq > "$namecache" 26 fi 27 cat "$namecache" 28 done 29 } \ 30 -docstring %{ 31 ctags-search [<symbol>]: jump to a symbol's definition 32 If no symbol is passed then the current selection is used as symbol name 33 } \ 34 ctags-search %[ require-module menu; evaluate-commands %sh[ 35 realpath() { ( cd "$(dirname "$1")"; printf "%s/%s\n" "$(pwd -P)" "$(basename "$1")" ) } 36 export tagname="${1:-${kak_selection}}" 37 eval "set -- $kak_quoted_opt_ctagsfiles" 38 for candidate in "$@"; do 39 [ -f "$candidate" ] && realpath "$candidate" 40 done | awk '!x[$0]++' | # remove duplicates 41 while read -r tags; do 42 printf '!TAGROOT\t%s\n' "$(realpath "${tags%/*}")/" 43 ${kak_opt_readtagscmd} -t "$tags" "$tagname" 44 done | awk -F '\t|\n' ' 45 /^!TAGROOT\t/ { tagroot=$2 } 46 /[^\t]+\t[^\t]+\t\/\^.*\$?\// { 47 line = $0; sub(".*\t/\\^", "", line); sub("\\$?/$", "", line); 48 menu_info = line; gsub("!", "!!", menu_info); gsub(/^[\t ]+/, "", menu_info); gsub(/\t/, " ", menu_info); 49 keys = line; gsub(/</, "<lt>", keys); gsub(/\t/, "<c-v><c-i>", keys); gsub("!", "!!", keys); gsub("&", "&&", keys); gsub("#", "##", keys); gsub("\\|", "||", keys); gsub("\\\\/", "/", keys); 50 menu_item = $2; gsub("!", "!!", menu_item); 51 edit_path = path($2); gsub("&", "&&", edit_path); gsub("#", "##", edit_path); gsub("\\|", "||", edit_path); 52 select = $1; gsub(/</, "<lt>", select); gsub(/\t/, "<c-v><c-i>", select); gsub("!", "!!", select); gsub("&", "&&", select); gsub("#", "##", select); gsub("\\|", "||", select); 53 out = out "%!" menu_item ": " menu_info "! %!evaluate-commands %# try %& edit -existing %|" edit_path "|; execute-keys %|/\\Q" keys "<ret>vc| & catch %& fail unable to find tag &; try %& execute-keys %|s\\Q" select "<ret>| & # !" 54 } 55 /[^\t]+\t[^\t]+\t[0-9]+/ { 56 menu_item = $2; gsub("!", "!!", menu_item); 57 select = $1; gsub(/</, "<lt>", select); gsub(/\t/, "<c-v><c-i>", select); gsub("!", "!!", select); gsub("&", "&&", select); gsub("#", "##", select); gsub("\\|", "||", select); 58 menu_info = $3; gsub("!", "!!", menu_info); 59 edit_path = path($2); gsub("!", "!!", edit_path); gsub("#", "##", edit_path); gsub("&", "&&", edit_path); gsub("\\|", "||", edit_path); 60 line_number = $3; 61 out = out "%!" menu_item ": " menu_info "! %!evaluate-commands %# try %& edit -existing %|" edit_path "|; execute-keys %|" line_number "gx| & catch %& fail unable to find tag &; try %& execute-keys %|s\\Q" select "<ret>| & # !" 62 } 63 END { print ( length(out) == 0 ? "fail no such tag " ENVIRON["tagname"] : "menu -auto-single " out ) } 64 # Ensure x is an absolute file path, by prepending with tagroot 65 function path(x) { return x ~/^\// ? x : tagroot x }' 66 ]] 67 68 define-command ctags-complete -docstring "Generate completions for the current selection from the tag file" %{ 69 nop %sh{ 70 ( 71 header="${kak_cursor_line}.${kak_cursor_column}@${kak_timestamp}" 72 compl=$( 73 eval "set -- $kak_quoted_opt_ctagsfiles" 74 for ctagsfile in "$@"; do 75 ${kak_opt_readtagscmd} -p -t "$ctagsfile" ${kak_selection} 76 done | awk '{ uniq[$1]++ } END { for (elem in uniq) printf " %s||%s", elem, elem }' 77 ) 78 printf %s\\n "evaluate-commands -client ${kak_client} set-option buffer=${kak_bufname} ctags_completions ${header}${compl}" | \ 79 kak -p ${kak_session} 80 ) > /dev/null 2>&1 < /dev/null & 81 } 82 } 83 84 define-command \ 85 -docstring %{ 86 Display ctags information about the current function 87 88 Expects the cursor to be after the opening parenthesis of a function invocation. 89 } \ 90 ctags-funcinfo %{ 91 evaluate-commands -draft %{ 92 try %{ 93 execute-keys '[(;B<a-k>[a-zA-Z_]+\(<ret><a-;>' 94 evaluate-commands %sh{ 95 f=${kak_selection%?} 96 sig='\tsignature:(.*)' 97 csn='\t(class|struct|namespace):(\S+)' 98 sigs=$(${kak_opt_readtagscmd} -e -Q '(eq? $kind "f")' "${f}" | sed -Ee "s/^.*${csn}.*${sig}$/\3 [\2::${f}]/ ;t ;s/^.*${sig}$/\1 [${f}]/") 99 if [ -n "$sigs" ]; then 100 printf %s\\n "evaluate-commands -client ${kak_client} %{info -anchor $kak_cursor_line.$kak_cursor_column -style above '$sigs'}" 101 fi 102 } 103 } 104 } 105 } 106 107 define-command \ 108 -docstring %{ 109 Automatically show matching symbols from the tag file in this window. 110 111 Expects the cursor to be after the opening parenthesis of a function invocation. 112 } \ 113 ctags-enable-autoinfo %{ 114 hook window -group ctags-autoinfo NormalIdle .* ctags-funcinfo 115 hook window -group ctags-autoinfo InsertIdle .* ctags-funcinfo 116 } 117 118 define-command ctags-disable-autoinfo -docstring "Disable automatically showing matching symbols from the tag file in this window" %{ remove-hooks window ctags-autoinfo } 119 120 declare-option -docstring "shell command used to generate tag files" \ 121 str ctagscmd "ctags -R --fields=+S" 122 declare-option -docstring "path to the directories in which the tags file will be generated" str-list ctagspaths "." 123 124 define-command ctags-generate -docstring 'Generate tag file asynchronously' %{ 125 echo -markup "{Information}launching tag generation in the background" 126 nop %sh{ ( 127 trap - INT QUIT 128 while ! mkdir .tags.kaklock 2>/dev/null; do sleep 1; done 129 trap 'rmdir .tags.kaklock' EXIT 130 131 if eval "${kak_opt_ctagscmd}" -f .tags.kaktmp ${kak_opt_ctagspaths}; then 132 mv .tags.kaktmp tags 133 msg="tags generation complete" 134 else 135 msg="tags generation failed" 136 fi 137 138 printf %s\\n "evaluate-commands -client $kak_client echo -markup '{Information}${msg}'" | kak -p ${kak_session} 139 ) > /dev/null 2>&1 < /dev/null & } 140 } 141 142 define-command ctags-update-tags -docstring 'Update tags for the current file' %{ 143 nop %sh{ ( 144 trap - INT QUIT 145 while ! mkdir .tags.kaklock 2>/dev/null; do sleep 1; done 146 trap 'rmdir .tags.kaklock' EXIT 147 148 if ${kak_opt_ctagscmd} -f .file_tags.kaktmp $kak_bufname; then 149 export LC_COLLATE=C LC_ALL=C # ensure ASCII sorting order 150 # merge the updated tags tags with the general tags (filtering out out of date tags from it) into the target file 151 grep -Fv "$(printf '\t%s\t' "$kak_bufname")" tags | grep -v '^!' | sort --merge - .file_tags.kaktmp >> .tags.kaktmp 152 rm .file_tags.kaktmp 153 mv .tags.kaktmp tags 154 msg="tags updated for $kak_bufname" 155 else 156 msg="tags update failed for $kak_bufname" 157 fi 158 159 printf %s\\n "evaluate-commands -client $kak_client echo -markup '{Information}${msg}'" | kak -p ${kak_session} 160 ) > /dev/null 2>&1 < /dev/null & } 161 } 162 163 define-command ctags-enable-autocomplete -docstring "Enable tags as autocomplete suggestions in this window" %{ 164 set-option window completers "option=ctags_completions" %opt{completers} 165 hook window -group ctags-autocomplete InsertIdle .* %{ 166 try %{ 167 evaluate-commands -draft %{ # select previous word >= ctags_min_chars 168 execute-keys ",b_<a-k>.{%opt{ctags_min_chars},}<ret>" 169 ctags-complete # run in draft context to preserve selection 170 } 171 } 172 } 173 } 174 175 define-command ctags-disable-autocomplete -docstring "Disable tags as autocomplete suggestions in this window" %{ 176 set-option -remove window completers option=ctags_completions 177 remove-hooks window ctags-autocomplete 178 }