From 33f080f658b36c7569dbd4fe51f7fbde763ecc5f Mon Sep 17 00:00:00 2001 From: Nutchanon Ninyawee Date: Wed, 5 Mar 2025 19:56:44 +0700 Subject: [PATCH] editors: add Cursor support and improve VSCode-like detection - Added new `is-cursor` command to detect Cursor editor - Created `is-vscode-like` command to generalize VSCode-like editor detection - Updated various commands to recognize Cursor alongside VSCode - Modified editor selection logic to include Cursor as a preferred editor --- commands/config-edit | 2 +- commands/dorothy | 3 +- commands/edit | 6 ++- commands/get-terminal-title-support | 2 +- commands/is-cursor | 64 +++++++++++++++++++++++++++++ commands/is-vscode | 2 +- commands/is-vscode-like | 53 ++++++++++++++++++++++++ 7 files changed, 127 insertions(+), 5 deletions(-) create mode 100755 commands/is-cursor create mode 100755 commands/is-vscode-like diff --git a/commands/config-edit b/commands/config-edit index 940f2b494..b938ec09a 100755 --- a/commands/config-edit +++ b/commands/config-edit @@ -233,7 +233,7 @@ function config_edit() ( esac done - if is-vscode; then + if is-vscode-like ; then # https://github.com/Microsoft/vscode/issues/29523 if [[ $option_line == *' '* ]]; then option_line="${option_line/ / }" diff --git a/commands/dorothy b/commands/dorothy index a9a12f912..73a16885a 100755 --- a/commands/dorothy +++ b/commands/dorothy @@ -1639,7 +1639,8 @@ function dorothy_() ( echo-style --h1='Edit Dorothy' # ensure_minimal_dependencies <-- if they are editing, then we assume they are already setup ensure_permissions_configured - if [[ "$(edit --dry --only-editor)" == 'code' ]]; then + local editor="$(edit --dry --only-editor)" + if [[ "$editor" == 'code' || "$editor" == 'cursor' ]]; then edit -- "$DOROTHY/.vscode/workspace.code-workspace" else edit -- "$DOROTHY" diff --git a/commands/edit b/commands/edit index ecc675488..b0aefdfd9 100755 --- a/commands/edit +++ b/commands/edit @@ -114,7 +114,11 @@ function edit_() ( local editors=() if [[ -z $terminal && -z $gui ]]; then # no terminal or gui preference, determine sensible defaults - if is-vscode; then + if is-cursor; then + editors+=(cursor) + gui='yes' + terminal='yes' + elif is-vscode; then # if running within vscode, add vscode as first preference editors+=(code) gui='yes' diff --git a/commands/get-terminal-title-support b/commands/get-terminal-title-support index 33c43f234..b8297977d 100755 --- a/commands/get-terminal-title-support +++ b/commands/get-terminal-title-support @@ -46,7 +46,7 @@ function get_terminal_title_support() ( # Action function __check { - is-ci || is-vscode + is-ci || is-vscode-like } if [[ $option_quiet == 'yes' ]]; then diff --git a/commands/is-cursor b/commands/is-cursor new file mode 100755 index 000000000..888f552cc --- /dev/null +++ b/commands/is-cursor @@ -0,0 +1,64 @@ +#!/usr/bin/env bash + +# It has been checked and developed on window platform wsl + +function is_cursor() ( + source "$DOROTHY/sources/bash.bash" + + # ===================================== + # Arguments + + function help { + cat <<-EOF >/dev/stderr + ABOUT: + Checks if the environment is running within Cursor editor. + + USAGE: + is-cursor + + RETURNS: + [0] if the environment is within Cursor editor + [1] if the environment is not within Cursor editor + EOF + if [[ $# -ne 0 ]]; then + echo-error "$@" + fi + return 22 # EINVAL 22 Invalid argument + } + + # process + local item + while [[ $# -ne 0 ]]; do + item="$1" + shift + case "$item" in + '--help' | '-h') help ;; + '--'*) help "An unrecognised flag was provided: $item" ;; + *) help "An unrecognised argument was provided: $item" ;; + esac + done + + # ===================================== + # Action + + # Check for Cursor-specific environment variables + if [[ ${NAME-} == "Cursor" ]]; then + return 0 + fi + + # Also check if we're in VSCode with Cursor-specific environment variables + if [[ ${TERM_PROGRAM-} == 'vscode' ]]; then + # Check for Cursor in environment variables + if env | grep -q "NAME=Cursor"; then + return 0 + fi + fi + + # If none of the above conditions are met, we're not in Cursor + return 1 +) + +# fire if invoked standalone +if [[ $0 == "${BASH_SOURCE[0]}" ]]; then + is_cursor "$@" +fi diff --git a/commands/is-vscode b/commands/is-vscode index f72ab5f3d..d122ca6f9 100755 --- a/commands/is-vscode +++ b/commands/is-vscode @@ -39,7 +39,7 @@ function is_vscode() ( # ===================================== # Action - if [[ ${TERM_PROGRAM-} == 'vscode' ]]; then + if [[ ${TERM_PROGRAM-} == 'vscode' && ! is-cursor ]]; then return 0 else return 1 diff --git a/commands/is-vscode-like b/commands/is-vscode-like new file mode 100755 index 000000000..71f0ecbb5 --- /dev/null +++ b/commands/is-vscode-like @@ -0,0 +1,53 @@ +#!/usr/bin/env bash + +function is_vscode_like() ( + source "$DOROTHY/sources/bash.bash" + + # ===================================== + # Arguments + + function help { + cat <<-EOF >/dev/stderr + ABOUT: + Checks if the environment is running within a VSCode-like editor. + Currently supports VSCode, Cursor, and VSCode Insiders. + + USAGE: + is-vscode-like + + RETURNS: + [0] if the environment is within a VSCode-like editor + [1] if the environment is not within a VSCode-like editor + EOF + if [[ $# -ne 0 ]]; then + echo-error "$@" + fi + return 22 # EINVAL 22 Invalid argument + } + + # process + local item + while [[ $# -ne 0 ]]; do + item="$1" + shift + case "$item" in + '--help' | '-h') help ;; + '--'*) help "An unrecognised flag was provided: $item" ;; + *) help "An unrecognised argument was provided: $item" ;; + esac + done + + # ===================================== + # Action + + if [[ ${TERM_PROGRAM-} == 'vscode']]; then + return 0 + else + return 1 + fi +) + +# fire if invoked standalone +if [[ $0 == "${BASH_SOURCE[0]}" ]]; then + is_vscode_like "$@" +fi