package snippets import ( "fmt" "os" "codeberg.org/turbo-editors/turbo-core/profile" "codeberg.org/turbo-editors/turbo-core/projectfile" ) // template returns the starter file: the profile's Templates.Snippets with the // name of the ungrouped group and the user's own snippets path filled into its // comments, in that order. func template(p profile.Profile) string { return fmt.Sprintf(p.Templates.Snippets, ungroupedName, userPathComment(p)) } // userPathComment returns the user's snippets path for the template's comment, // or a plain description when there is nowhere to put one. func userPathComment(p profile.Profile) string { if path := UserPath(p); path != "" { return path } return "(no configuration directory on this system)" } // Create writes a starter snippets file for a project and returns its path. // // A project that already has one gives ErrExists and is left untouched: a file // somebody has been editing is never overwritten by a menu item. // // path, err := snippets.Create(p, ".") // if errors.Is(err, snippets.ErrExists) { // // already there; open it instead // } func Create(p profile.Profile, projectDir string) (string, error) { path := ProjectPath(p, projectDir) if exists(path) { return path, fmt.Errorf("%w: %s", ErrExists, path) } if err := projectfile.Write(path, []byte(template(p))); err != nil { return path, err } return path, nil } // Exists reports whether a project has a snippets file that can be read. func Exists(p profile.Profile, projectDir string) bool { return exists(ProjectPath(p, projectDir)) } // exists reports whether a path is a regular file. A directory in its place // counts as absent: it is not something Load could have read. func exists(path string) bool { info, err := os.Stat(path) return err == nil && info.Mode().IsRegular() }