I have written some code to jump to a location in the concrete syntax tree of a file (the tool itself does reusable queries using tree-sitter’s s-expression syntax. I am planning to make a polished release available soon!)
Thing is, I kind of wonder if the code I’m writing could be improved… I would love a code review!
Here’s the source (or on my git host):
declare-option str tree_grepper_path "tree-grepper"
declare-option str tree_grepper_fzf_path "fzf"
define-command -override -docstring "jump somewhere in an Elm file's definition outline" -params 0..1 outline-jump-elm %{
tmux-terminal-horizontal sh -c %{
set -euo pipefail
# what tools do we have available?
TREE_GREPPER=${1:-tree-grepper}
FZF=${2:-fzf}
# what do we care about?
FILE=$3
FZF_QUERY=$4
# where do we return results?
CLIENT=$5
SESSION=$6
# do the magic!
QUERY="(function_declaration_left (lower_case_identifier)@function) (type_declaration (type) (upper_case_identifier)@type) (type_alias_declaration (type) (alias) (upper_case_identifier)@alias) (union_variant (upper_case_identifier)@constructor) (field_type (lower_case_identifier)@field) (lower_pattern)@pattern (exposed_type)@exposed_type (exposed_value)@exposed_value (as_clause (as) (upper_case_identifier)@module_alias)"
EDIT_LOCATION="$("$TREE_GREPPER" --language elm "$QUERY" "$FILE" | fzf --with-nth 4,5 --nth 2,1 --delimiter=: --query "$FZF_QUERY" --select-1 | cut -d : -f 1-3 | tr : ' ')"
printf "evaluate-commands -client %s edit %s\n" "$CLIENT" "$EDIT_LOCATION" | indiekak -p "$SESSION"
} -- %opt{tree_grepper_path} %opt{tree_grepper_fzf_path} %val{bufname} %arg{1} %val{client} %val{session}
}
(indiekak
is a script in my PATH that calls kak
without my session management wrapper around it. You can pretend it’s just kak
!)