diff --git a/doc/neovim-external-editor-setup.md b/doc/neovim-external-editor-setup.md index 07975d4..567b63c 100644 --- a/doc/neovim-external-editor-setup.md +++ b/doc/neovim-external-editor-setup.md @@ -64,50 +64,69 @@ This setup allows you to click on a script in Godot and open it directly in Neov Save this as `~/.local/bin/godot-nvr.sh` ```bash #!/usr/bin/env bash - # - # godot-nvr.sh - # + # Godot → Neovim launcher with GUI terminal focus # Usage: - # godot-nvr.sh [TERMINAL_NAME] [--tab|--vsplit] [:line[:col]] - # - # Example: - # godot-nvr.sh ghostty --tab ~/project/main.gd:10 + # godot-nvr.sh [terminal_name] +{line} {file} [--tab|--vsplit] - TERMINAL_APP="${1:-ghostty}" # default: ghostty - shift + # ----------------------------- + # Arguments + # ----------------------------- + DEFAULT_TERMINAL="ghostty" + ARG0="$1" - MODE="" + if [[ "$ARG0" == +* || "$ARG0" == --* || -f "$ARG0" ]]; then + # No terminal argument provided, use default + GODOT_TERMINAL="$DEFAULT_TERMINAL" + else + # First argument is terminal name + GODOT_TERMINAL="$ARG0" + shift + fi + + SOCKET="/private/tmp/godot.pipe" # Neovim socket path + NVR="/Library/Frameworks/Python.framework/Versions/3.8/bin/nvr" + + OPEN_MODE="window" + LINE="" FILE="" + + # ----------------------------- + # Parse remaining arguments + # ----------------------------- while [[ $# -gt 0 ]]; do - case "$1" in - --tab) - MODE="--remote-tab" - shift - ;; - --vsplit) - MODE="--remote-send 'v:edit '" - shift - ;; - *) - FILE="$1" - shift - ;; - esac + case "$1" in + --tab) OPEN_MODE="tab"; shift ;; + --vsplit) OPEN_MODE="vsplit"; shift ;; + +[0-9]*) LINE="${1#+}"; shift ;; + *) FILE="$1"; shift ;; + esac done - # Open file in Neovim - if [[ -n "$MODE" && "$MODE" == *remote-send* ]]; then - nvr --remote-send "v:edit ${FILE}" - elif [[ -n "$MODE" ]]; then - nvr $MODE "$FILE" + [ -z "$FILE" ] && exit 0 + + # ----------------------------- + # Open file in Neovim or jump to buffer + # ----------------------------- + if $NVR --servername "$SOCKET" --remote-expr \ + "bufexists(fnamemodify('$FILE', ':p'))" | grep -q 1; then + CMD=":buffer $(basename "$FILE")" else - nvr --remote "$FILE" + case "$OPEN_MODE" in + window) CMD=":e $FILE" ;; + tab) CMD=":tabedit $FILE" ;; + vsplit) CMD=":vsplit $FILE" ;; + esac fi - # Bring terminal GUI to front - if command -v osascript &>/dev/null; then - osascript -e "tell application \"$TERMINAL_APP\" to activate" - fi + [ -n "$LINE" ] && CMD="$CMD | call cursor($LINE,1)" + CMD="$CMD | normal! zz" + + $NVR --servername "$SOCKET" --remote-send "${CMD}" + + # ----------------------------- + # Focus GUI terminal (macOS) + # ----------------------------- + osascript -e "tell application \"$GODOT_TERMINAL\" to activate" ``` 1. And make it executable: