-
-
Save dobe/12c6a989f481d9e9769cefb0fea8271c to your computer and use it in GitHub Desktop.
intellij wrapper around the idea cli script which searches for the project in parent dirs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # Opens IntelliJ IDEA for a file or project directory. | |
| # Walks up from the target path to find the .idea project root, | |
| # ensures the project is loaded via IDEA's binary IPC, then navigates | |
| # to the file using JetBrains deep links. | |
| # | |
| # Usage: | |
| # i — open project for cwd | |
| # i file.kt — open project containing file.kt and navigate to it | |
| # i ../other/ — open that project | |
| set -e | |
| IDEA_BIN="/Applications/IntelliJ IDEA.app/Contents/MacOS/idea" | |
| if [ ! -x "$IDEA_BIN" ]; then | |
| echo "IntelliJ IDEA not found at: $IDEA_BIN" >&2 | |
| exit 1 | |
| fi | |
| target="${1:-.}" | |
| target=$(realpath "$target") | |
| if [ -d "$target" ]; then | |
| project_dir="$target" | |
| target_file="" | |
| elif [ -f "$target" ]; then | |
| project_dir=$(dirname "$target") | |
| target_file="$target" | |
| else | |
| echo "$target is not a file or directory" >&2 | |
| exit 1 | |
| fi | |
| # Walk up from project_dir to find the project root (.idea directory) | |
| search="$project_dir" | |
| project_root="" | |
| while [ "$search" != "/" ]; do | |
| if [ -d "$search/.idea" ]; then | |
| project_root="$search" | |
| break | |
| fi | |
| search=$(dirname "$search") | |
| done | |
| if [ -z "$project_root" ]; then | |
| echo "No .idea project found above: $project_dir" >&2 | |
| exit 1 | |
| fi | |
| # Open/activate the project via IDEA binary IPC. | |
| # This reuses the running instance (fast) or starts IDEA if not running. | |
| "$IDEA_BIN" "$project_root" | |
| # Navigate to the specific file via jetbrains:// deep link | |
| if [ -n "$target_file" ]; then | |
| # Project name: .idea/.name if set, otherwise the directory name | |
| name_file="$project_root/.idea/.name" | |
| if [ -f "$name_file" ]; then | |
| project_name=$(cat "$name_file") | |
| else | |
| project_name=$(basename "$project_root") | |
| fi | |
| rel_path="${target_file#"$project_root"/}" | |
| open "jetbrains://idea/navigate/reference?project=$project_name&path=$rel_path" | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment