Code review: jumping to a source location

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?

        # what do we care about?

        # where do we return results?

        # 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!)

1 Like

Not much of a review but at a glance it looks fine (not a tmux user or Elm so…). On the other hand:
Am I correct thinking that your query is fixed (and basically a function block) ? what about using prompt to get an arbitrary TS query and a temp file (à la grep.kak) to make tree-sitter queries reads it ? (if as I suppose you are parsing the output of this command)

whoops, sorry it took so long to respond here!

the query is fixed, but the selection is dynamic—it returns multiple results, and I want to go through them with a fuzzy finder instead of a grep.kak interface. The basic outline is:

  1. get a filename
  2. get declarations inside that file
  3. select one with fzf
  4. tell kak to edit that location

I’m wondering if there is a better way to do step 4 than printf | kak?