package app import ( "os" "path/filepath" "strings" ) // openFromArgs 处理启动时的命令行参数。 // // 第一个参数可能是目录(当工作区)也可能是文件(当要打开的文件); // 文件则用它所在的目录当工作区。 func (a *App) openFromArgs(args []string) { if len(args) == 0 { if last := lastWorkspace(); last != "" { a.setRoot(last) } return } first := args[0] if st, err := os.Stat(first); err == nil && !st.IsDir() { a.setRoot(filepath.Dir(first)) a.OpenFile(first) return } a.setRoot(first) if len(args) > 1 { a.OpenFile(args[1]) } } // secondInstance 处理「又一个 MyGoland 被启动」:把窗口叫到前台, // 并按对方传来的参数打开文件或工作区。 func (a *App) secondInstance(args []string, wd string) { if a.win == nil { return } a.win.Show() a.win.Focus() for _, arg := range args { // 相对路径按对方的当前目录解析。 p := arg if !filepath.IsAbs(p) { p = filepath.Join(wd, p) } st, err := os.Stat(p) if err != nil { continue } if st.IsDir() { dir := p a.win.Update(func() { a.setRoot(dir) }) continue } file := p a.win.Update(func() { // 文件不在当前工作区里,连工作区一起换过去。 if a.root == "" || !within(a.root, file) { a.setRoot(filepath.Dir(file)) } go a.OpenFile(file) }) } } // within 判断 path 是否在 root 之下(忽略大小写,Windows 路径习惯)。 func within(root, path string) bool { r := strings.ToLower(filepath.Clean(root)) p := strings.ToLower(filepath.Clean(path)) if r == p { return true } return strings.HasPrefix(p, r+string(filepath.Separator)) }