fc9e427ef947d945cfbd436e2320562d194a0dd8

Author
Marc Cornellà <marc.cornella@live.com>
Committer
GitHub <noreply@github.com>
Date

Message

sublime: refactor plugin and fix documentation (#7715)

* convert to 2-space indentation
* standardize sublime function name (subl)
* simplify subl function definition into a single loop
* convert functions into aliases
* simplify alias creation
* search for Sublime Text command only if not found
* reorganize and clean up plugin
* fix README
* simplify cygwin path lookup
* support path lookup for msys (Windows)
* support path lookup for WSL (Windows)
* fix for spaces in sublime path alias

Diff

This diff is truncated to protect this page.

  1diff --git a/plugins/sublime/README.md b/plugins/sublime/README.md
  2index 2ad629fad98458d67b504ee9abf6857e4210b6d9..22999241f56b8b777fa62db53ff089ba8887bda7 100644
  3--- a/plugins/sublime/README.md
  4+++ b/plugins/sublime/README.md
  5@@ -1,25 +1,37 @@
  6-## sublime
  7+# sublime
  8 
  9-Plugin for Sublime Text, a cross platform text and code editor, available for Linux, Mac OS X, and Windows.
 10+Plugin for [Sublime Text](https://www.sublimetext.com/), a cross platform text and code editor,
 11+available for Linux, macOS, and Windows.
 12 
 13-### Requirements
 14+To use the plugin, add `sublime` to the plugins array of your zshrc file:
 15 
 16- * [Sublime Text](https://www.sublimetext.com/)
 17+```zsh
 18+plugins=(... sublime)
 19+```
 20 
 21-### Usage
 22+Sublime Text has to be installed to use the plugin.
 23 
 24- * If `st` command is called without an argument, launch Sublime Text
 25+## Usage
 26 
 27- * If `st` is passed a directory, `cd` to it and open it in Sublime Text
 28+The plugin defines several aliases, such as:
 29 
 30- * If `st` is passed a file, open it in Sublime Text
 31+- `st`: opens Sublime Text. If passed a file or directory, Sublime Text will open it.
 32 
 33- * If `stt` command is called, it is equivalent to `st .`, opening the current folder in Sublime Text
 34+- `stt`: open Sublime Text on the current directory.
 35 
 36- * If `sst` command is called, it is like `sudo st`, opening the file or folder in Sublime Text. Useful for editing system protected files.
 37+- `sst`: if `sudo` is available, `sst` will open Sublime Text with root permissions, so that
 38+  you can modify any file or directory that you pass it. Useful to edit system files.
 39 
 40diff --git a/plugins/sublime/sublime.plugin.zsh b/plugins/sublime/sublime.plugin.zsh
 41index 485028d9fbb4091d8b37ad5bea071a6516268c51..3a82d6c7fa8018a399aeae207b38a0fcdd11a991 100644
 42--- a/plugins/sublime/sublime.plugin.zsh
 43+++ b/plugins/sublime/sublime.plugin.zsh
 44@@ -1,121 +1,118 @@
 45-# Sublime Text Aliases
 46+# Sublime Text aliases
 47 
 48-() {
 49+alias st=subl
 50+alias stt='subl .'
 51 
 52-if [[ "$OSTYPE" == linux* ]]; then
 53-    local _sublime_linux_paths
 54-    _sublime_linux_paths=(
 55+# Define sst only if sudo exists
 56+(( $+commands[sudo] )) && alias sst='sudo subl'
 57+
 58+alias stp=find_project
 59+alias stn=create_project
 60+
 61+
 62+# Search for the Sublime Text command if not found
 63+(( $+commands[subl] )) || {
 64+  declare -a _sublime_paths
 65+
 66+  if [[ "$OSTYPE" == linux* ]]; then
 67+    if [[ "$(uname -r)" = *Microsoft* ]]; then
 68+      _sublime_paths=(
 69+        "$(wslpath -u 'C:\Program Files\Sublime Text 3\subl.exe')"
 70+        "$(wslpath -u 'C:\Program Files\Sublime Text 2\subl.exe')"
 71+      )
 72+    else
 73+      _sublime_paths=(
 74         "$HOME/bin/sublime_text"
 75         "/opt/sublime_text/sublime_text"
 76         "/opt/sublime_text_3/sublime_text"
 77         "/usr/bin/sublime_text"
 78         "/usr/local/bin/sublime_text"
 79         "/usr/bin/subl"
 80-        "/opt/sublime_text_3/sublime_text"
 81         "/usr/bin/subl3"
 82+      )
 83+    fi
 84+  elif [[ "$OSTYPE" = darwin* ]]; then
 85+    _sublime_paths=(
 86+      "/usr/local/bin/subl"
 87+      "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl"
 88+      "/Applications/Sublime Text 3.app/Contents/SharedSupport/bin/subl"
 89+      "/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl"
 90+      "$HOME/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl"
 91+      "$HOME/Applications/Sublime Text 3.app/Contents/SharedSupport/bin/subl"
 92+      "$HOME/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl"
 93     )
 94-    for _sublime_path in $_sublime_linux_paths; do
 95-        if [[ -a $_sublime_path ]]; then
 96-            st_run() { $_sublime_path $@ >/dev/null 2>&1 &| }
 97-            st_run_sudo() {sudo $_sublime_path $@ >/dev/null 2>&1}
 98-            alias sst=st_run_sudo
 99-            alias st=st_run
100-            break
101-        fi
102-    done
103-elif  [[ "$OSTYPE" = darwin* ]]; then
104-    local _sublime_darwin_paths
105-    _sublime_darwin_paths=(
106-        "/usr/local/bin/subl"
107-        "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl"
108-        "/Applications/Sublime Text 3.app/Contents/SharedSupport/bin/subl"
109-        "/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl"
110-        "$HOME/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl"
111-        "$HOME/Applications/Sublime Text 3.app/Contents/SharedSupport/bin/subl"
112-        "$HOME/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl"
113+  elif [[ "$OSTYPE" = cygwin ]]; then
114+    _sublime_paths=(
115+      "$(cygpath "$ProgramW6432/Sublime Text 2")/subl.exe"
116+      "$(cygpath "$ProgramW6432/Sublime Text 3")/subl.exe"
117     )
118-    for _sublime_path in $_sublime_darwin_paths; do
119-        if [[ -a $_sublime_path ]]; then
120-            subl () { "$_sublime_path" $* }
121-            alias st=subl
122-            break
123-        fi
124-    done
125-elif [[ "$OSTYPE" = 'cygwin' ]]; then
126-    local _sublime_cygwin_paths
127-    _sublime_cygwin_paths=(
128-        "$(cygpath $ProgramW6432/Sublime\ Text\ 2)/sublime_text.exe"
129-        "$(cygpath $ProgramW6432/Sublime\ Text\ 3)/sublime_text.exe"
130+  elif [[ "$OSTYPE" = msys ]]; then
131+    _sublime_paths=(
132+      "/c/Program Files/Sublime Text 2/subl.exe"
133+      "/c/Program Files/Sublime Text 3/subl.exe"
134     )
135-    for _sublime_path in $_sublime_cygwin_paths; do
136-        if [[ -a $_sublime_path ]]; then
137-            subl () { "$_sublime_path" $* }
138-            alias st=subl
139-            break
140-        fi
141-    done
142-fi
143+  fi