浏览代码

Create an alias to open up sublime project (#5258)

* Adding an alias to open up the sublime project

* README update for stp command
Peter Han 8 年之前
父节点
当前提交
073ea01cce
共有 2 个文件被更改,包括 33 次插入2 次删除
  1. 4 2
      plugins/sublime/README.md
  2. 29 0
      plugins/sublime/sublime.plugin.zsh

+ 4 - 2
plugins/sublime/README.md

@@ -15,5 +15,7 @@ Plugin for Sublime Text, a cross platform text and code editor, available for Li
  * If `st` is passed a file, open it in Sublime Text
 
  * If `stt` command is called, it is equivalent to `st .`, opening the current folder in Sublime Text
- 
- * If `sst` command is called, it is like `sudo st`, opening the file or folder in Sublime Text. Useful for editing system protected files.
+
+ * If `sst` command is called, it is like `sudo st`, opening the file or folder in Sublime Text. Useful for editing system protected files.
+
+ * If `stp` command is called, it find a `.sublime-project` file by traversing up the directory structure. If there is no `.sublime-project` file, but if the current folder is a Git repo, opens up the root directory of the repo. If the current folder is not a Git repo, then opens up the current directory.

+ 29 - 0
plugins/sublime/sublime.plugin.zsh

@@ -56,3 +56,32 @@ elif [[ "$OSTYPE" = 'cygwin' ]]; then
 fi
 
 alias stt='st .'
+
+find_project()
+{
+    local PROJECT_ROOT="${PWD}"
+    local FINAL_DEST="."
+
+    while [[ $PROJECT_ROOT != "/" && ! -d "$PROJECT_ROOT/.git" ]]; do
+        PROJECT_ROOT=$(dirname $PROJECT_ROOT)
+    done
+
+    if [[ $PROJECT_ROOT != "/" ]]; then
+        local PROJECT_NAME="${PROJECT_ROOT##*/}"
+
+        local SUBL_DIR=$PROJECT_ROOT
+        while [[ $SUBL_DIR != "/" && ! -f "$SUBL_DIR/$PROJECT_NAME.sublime-project" ]]; do
+            SUBL_DIR=$(dirname $SUBL_DIR)
+        done
+
+        if [[ $SUBL_DIR != "/" ]]; then
+            FINAL_DEST="$SUBL_DIR/$PROJECT_NAME.sublime-project"
+        else
+            FINAL_DEST=$PROJECT_ROOT
+        fi
+    fi
+
+    st $FINAL_DEST
+}
+
+alias stp=find_project