This commit is contained in:
Liang Ding 2014-10-11 14:08:29 +08:00
parent 7ef40d03c9
commit dba18c5458
2 changed files with 64 additions and 18 deletions

View File

@ -12,6 +12,7 @@ import (
"strings" "strings"
"github.com/b3log/wide/conf" "github.com/b3log/wide/conf"
"github.com/b3log/wide/file"
"github.com/b3log/wide/session" "github.com/b3log/wide/session"
"github.com/b3log/wide/util" "github.com/b3log/wide/util"
"github.com/golang/glog" "github.com/golang/glog"
@ -20,14 +21,6 @@ import (
var editorWS = map[string]*websocket.Conn{} var editorWS = map[string]*websocket.Conn{}
// 代码片段. 这个结构可用于“查找使用”、“文件搜索”的返回值.
type snippet struct {
Path string `json:"path"` // 文件路径
Line int `json:"line"` // 行号
Ch int `json:"ch"` // 列号
Contents []string `json:"contents"` // 附近几行
}
// 建立编辑器通道. // 建立编辑器通道.
func WSHandler(w http.ResponseWriter, r *http.Request) { func WSHandler(w http.ResponseWriter, r *http.Request) {
session, _ := session.HTTPSession.Get(r, "wide-session") session, _ := session.HTTPSession.Get(r, "wide-session")
@ -388,7 +381,7 @@ func FindUsagesHandler(w http.ResponseWriter, r *http.Request) {
} }
founds := strings.Split(result, "\n") founds := strings.Split(result, "\n")
usages := []snippet{} usages := []*file.Snippet{}
for _, found := range founds { for _, found := range founds {
found = strings.TrimSpace(found) found = strings.TrimSpace(found)
@ -398,7 +391,7 @@ func FindUsagesHandler(w http.ResponseWriter, r *http.Request) {
cursorLine, _ := strconv.Atoi(found[cursorSep+1 : strings.LastIndex(found, ":")]) cursorLine, _ := strconv.Atoi(found[cursorSep+1 : strings.LastIndex(found, ":")])
cursorCh, _ := strconv.Atoi(found[strings.LastIndex(found, ":")+1:]) cursorCh, _ := strconv.Atoi(found[strings.LastIndex(found, ":")+1:])
usage := snippet{Path: path, Line: cursorLine, Ch: cursorCh /* TODO: 获取附近的代码片段 */} usage := &file.Snippet{Path: path, Line: cursorLine, Ch: cursorCh /* TODO: 获取附近的代码片段 */}
usages = append(usages, usage) usages = append(usages, usage)
} }

View File

@ -17,6 +17,24 @@ import (
"github.com/golang/glog" "github.com/golang/glog"
) )
// 文件节点,用于构造文件树.
type FileNode struct {
Name string `json:"name"`
Path string `json:"path"`
IconSkin string `json:"iconSkin"` // 值的末尾应该有一个空格
Type string `json:"type"` // "f":文件,"d":文件夹
Mode string `json:"mode"`
FileNodes []*FileNode `json:"children"`
}
// 代码片段. 这个结构可用于“查找使用”、“文件搜索”等的返回值.
type Snippet struct {
Path string `json:"path"` // 文件路径
Line int `json:"line"` // 行号
Ch int `json:"ch"` // 列号
Contents []string `json:"contents"` // 附近几行
}
// 构造用户工作空间文件树. // 构造用户工作空间文件树.
// //
// 将 Go API 源码包($GOROOT/src/pkg也作为子节点这样能方便用户查看 Go API 源码. // 将 Go API 源码包($GOROOT/src/pkg也作为子节点这样能方便用户查看 Go API 源码.
@ -203,14 +221,32 @@ func RemoveFile(w http.ResponseWriter, r *http.Request) {
} }
} }
// 文件节点,用于构造文件树. // 在目录中搜索包含指定字符串的文件.
type FileNode struct { func Search(w http.ResponseWriter, r *http.Request) {
Name string `json:"name"` data := map[string]interface{}{"succ": true}
Path string `json:"path"` defer util.RetJSON(w, r, data)
IconSkin string `json:"iconSkin"` // 值的末尾应该有一个空格
Type string `json:"type"` // "f":文件,"d":文件夹 var args map[string]interface{}
Mode string `json:"mode"`
FileNodes []*FileNode `json:"children"` if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
glog.Error(err)
data["succ"] = false
return
}
// TODO: 选定目录
dir := args["dir"].(string)
dir = ""
_ = dir
founds := []*Snippet{}
usage := &Snippet{Path: "", Line: 1, Ch: 2 /* TODO: 获取附近的代码片段 */}
founds = append(founds, usage)
data["founds"] = founds
} }
// 遍历指定的路径,构造文件树. // 遍历指定的路径,构造文件树.
@ -390,6 +426,23 @@ func removeFile(path string) bool {
return true return true
} }
// 在 dir 指定的目录(包含子目录)中的 extension 指定后缀的文件中搜索包含 text 文本的文件,类似 grep/findstr 命令.
func search(dir, extension, text string, snippets []*Snippet) []*Snippet {
f, _ := os.Open(dir)
fi, err := f.Readdir(-1)
f.Close()
if nil != err {
glog.Errorf("Read dir [%s] failed: [%s]", dir, err.Error())
return snippets
}
_ = fi
return snippets
}
// 根据文件名后缀判断是否是图片文件. // 根据文件名后缀判断是否是图片文件.
func isImg(extension string) bool { func isImg(extension string) bool {
ext := strings.ToLower(extension) ext := strings.ToLower(extension)