"Life is all about sharing. If we are good at something, pass it on." - Mary Berry

Turning Helix into an IDE with the help of WezTerm and CLI tools

2023-08-19

Categories: Development Environment

Helix as IDE

This post recaps previous series of posts about Helix.

  1. Running code
  2. Jumping to build errors
  3. Quickly select a command and open it in a new pane
  4. Testing a single function
  5. Git integration
  6. File tree
  7. Creating snippets
  8. How to debug?
  9. Interactive global search
  10. Opening file in GitHub

1. Running code

In my previous post, I shared a method for running code from within Helix by using this PR.

I later discovered another useful trick here. We can use wezterm cli get-text command to extract the filename and line number from the status line:

1status_line=$(wezterm cli get-text | rg -e "(?:NORMAL|INSERT|SELECT)\s+[\x{2800}-\x{28FF}]*\s+(\S*)\s[^│]* (\d+):*.*" -o --replace '$1 $2')
2filename=$(echo $status_line | awk '{ print $1}')
3line_number=$(echo $status_line | awk '{ print $2}')

2. Jumping to build errors

3. Quickly select a command and open it in a new pane

4. Testing a single function

As we can retrieve the filename and line number from the status line, we can easily extract the test name and pass it to cargo test:

 1  "test_single")
 2    test_name=$(head -$line_number $filename | tail -1 | sed -n 's/^.*fn \([^ ]*\)().*$/\1/p')
 3    split_pane_down
 4    case "$extension" in
 5      "rs")
 6        run_command="cd $PWD/$(dirname "$basedir"); cargo test $test_name; if [ \$status = 0 ]; wezterm cli activate-pane-direction up; end;"
 7        ;;
 8    esac
 9    echo "$run_command" | $send_to_bottom_pane
10    ;;

5. Git integration

While Helix currently does not support Git, we can open lazygit in the bottom pane using the following script:

1  "lazygit")
2    split_pane_down
3    program=$(wezterm cli list | awk -v pane_id="$pane_id" '$3==pane_id { print $6 }')
4    if [ "$program" = "lazygit" ]; then
5        wezterm cli activate-pane-direction down
6    else
7        echo "lazygit" | $send_to_bottom_pane
8    fi
9    ;;

and reload automatically after switching back.

git blame

Since we can obtain the filename and line number, we can easily send it to tig:

1  "blame")
2    split_pane_down
3    echo "tig blame $filename +$line_number" | $send_to_bottom_pane
4    ;;

6. File tree

Discover how to open broot from within Helix:

 1  "explorer")
 2    left_pane_id=$(wezterm cli get-pane-direction left)
 3    if [ -z "${left_pane_id}" ]; then
 4      left_pane_id=$(wezterm cli split-pane --left --percent 20)
 5    fi
 6
 7    left_program=$(wezterm cli list | awk -v pane_id="$left_pane_id" '$3==pane_id { print $6 }')
 8    if [ "$left_program" != "br" ]; then
 9      echo "br" | wezterm cli send-text --pane-id $left_pane_id --no-paste
10    fi
11
12    wezterm cli activate-pane-direction left
13    ;;

And when opening a file from broot, ensure it opens in the right pane:

 1fpath="$1"
 2
 3pane_id=$(wezterm cli get-pane-direction right)
 4if [ -z "${pane_id}" ]; then
 5  pane_id=$(wezterm cli split-pane --right --percent 80)
 6fi
 7
 8program=$(wezterm cli list | awk -v pane_id="$pane_id" '$3==pane_id { print $6 }')
 9if [ "$program" = "hx" ]; then
10  echo ":open ${fpath}\r" | wezterm cli send-text --pane-id $pane_id --no-paste
11else
12  echo "hx ${fpath}" | wezterm cli send-text --pane-id $pane_id --no-paste
13fi
14
15wezterm cli activate-pane-direction --pane-id $pane_id right

7. Creating snippets

8. How to debug?

I submitted this PR to allow the target directory to appear in the completion list in the debugger prompt, and it got merged so quickly.

I’m implementing a mechanism that involves sending a command using fzf (combine with ripgrep) to the bottom pane when a key is pressed:

1  "fzf")
2    split_pane_down
3    echo "hx-fzf.sh \$(rg --line-number --column --no-heading --smart-case . | fzf --delimiter : --preview 'bat --style=full --color=always --highlight-line {2} {1}' --preview-window '~3,+{2}+3/2' | awk '{ print \$1 }' | cut -d: -f1,2,3)" | $send_to_bottom_pane
4    ;;

Once a selection is made, the file will be opened by Helix in the top pane:

 1selected_file=$1
 2top_pane_id=$(wezterm cli get-pane-direction Up)
 3if [ -z "$selected_file" ]; then
 4    if [ -n "${top_pane_id}" ]; then
 5        wezterm cli activate-pane-direction --pane-id $top_pane_id Up
 6        wezterm cli toggle-pane-zoom-state
 7    fi
 8    exit 0
 9fi
10
11if [ -z "${top_pane_id}" ]; then
12    top_pane_id=$(wezterm cli split-pane --top)
13fi
14
15wezterm cli activate-pane-direction --pane-id $top_pane_id Up
16
17send_to_top_pane="wezterm cli send-text --pane-id $top_pane_id --no-paste"
18
19program=$(wezterm cli list | awk -v pane_id="$top_pane_id" '$3==pane_id { print $6 }')
20if [ "$program" = "hx" ]; then
21    echo ":open $selected_file\r" | $send_to_top_pane
22else
23    echo "hx $selected_file" | $send_to_top_pane
24fi
25
26wezterm cli toggle-pane-zoom-state

I have submitted this PR to hide the bottom pane after that.

10. Opening file in GitHub

As simple it seems:

1  "open")
2    gh browse $filename:$line_number  
3    ;;

You can see my full script here and the Helix configuration below:

1[keys.normal.";"]
2b = ":sh hx-wezterm.sh blame"
3e = ":sh hx-wezterm.sh explorer"
4f = ":sh hx-wezterm.sh fzf"
5g = ":sh hx-wezterm.sh lazygit"
6o = ":sh hx-wezterm.sh open"
7r = ":sh hx-wezterm.sh run"
8s = ":sh hx-wezterm.sh test_single"
9t = ":sh hx-wezterm.sh test_all"

Tags: broot helix lazygit tig wezterm

Edit on GitHub

Related Posts: