From ccb590459119828dd9a3bf618b410bba6ab3f68f Mon Sep 17 00:00:00 2001 From: Zachary Yedidia Date: Sat, 3 Aug 2019 21:01:57 -0700 Subject: [PATCH] Add mkparents option --- internal/buffer/save.go | 39 +++++++++++++++++----------------- internal/config/settings.go | 1 + internal/display/statusline.go | 2 -- 3 files changed, 20 insertions(+), 22 deletions(-) diff --git a/internal/buffer/save.go b/internal/buffer/save.go index d876b338..0ed64a7e 100644 --- a/internal/buffer/save.go +++ b/internal/buffer/save.go @@ -55,7 +55,8 @@ func (b *Buffer) Save() error { } // SaveAs saves the buffer to a specified path (filename), creating the file if it does not exist -func (b *Buffer) SaveAs(filename string) (err error) { +func (b *Buffer) SaveAs(filename string) error { + var err error if b.Type.Scratch { return errors.New("Cannot save scratch buffer") } @@ -88,24 +89,22 @@ func (b *Buffer) SaveAs(filename string) (err error) { // Removes any tilde and replaces with the absolute path to home absFilename, _ := ReplaceHome(filename) - // TODO: save creates parent dirs - // // Get the leading path to the file | "." is returned if there's no leading path provided - // if dirname := filepath.Dir(absFilename); dirname != "." { - // // Check if the parent dirs don't exist - // if _, statErr := os.Stat(dirname); os.IsNotExist(statErr) { - // // Prompt to make sure they want to create the dirs that are missing - // if yes, canceled := messenger.YesNoPrompt("Parent folders \"" + dirname + "\" do not exist. Create them? (y,n)"); yes && !canceled { - // // Create all leading dir(s) since they don't exist - // if mkdirallErr := os.MkdirAll(dirname, os.ModePerm); mkdirallErr != nil { - // // If there was an error creating the dirs - // return mkdirallErr - // } - // } else { - // // If they canceled the creation of leading dirs - // return errors.New("Save aborted") - // } - // } - // } + // Get the leading path to the file | "." is returned if there's no leading path provided + if dirname := filepath.Dir(absFilename); dirname != "." { + // Check if the parent dirs don't exist + if _, statErr := os.Stat(dirname); os.IsNotExist(statErr) { + // Prompt to make sure they want to create the dirs that are missing + if b.Settings["mkparents"].(bool) { + // Create all leading dir(s) since they don't exist + if mkdirallErr := os.MkdirAll(dirname, os.ModePerm); mkdirallErr != nil { + // If there was an error creating the dirs + return mkdirallErr + } + } else { + return errors.New("Parent dirs don't exist, enable 'mkparents' for auto creation") + } + } + } var fileSize int @@ -161,7 +160,7 @@ func (b *Buffer) SaveAs(filename string) (err error) { absPath, _ := filepath.Abs(filename) b.AbsPath = absPath b.isModified = false - return + return err } // SaveWithSudo saves the buffer to the default path with sudo diff --git a/internal/config/settings.go b/internal/config/settings.go index a6234032..8702bff1 100644 --- a/internal/config/settings.go +++ b/internal/config/settings.go @@ -164,6 +164,7 @@ var defaultCommonSettings = map[string]interface{}{ "keepautoindent": false, "matchbrace": false, "matchbraceleft": false, + "mkparents": false, "readonly": false, "rmtrailingws": false, "ruler": true, diff --git a/internal/display/statusline.go b/internal/display/statusline.go index 059c25d2..0fae348c 100644 --- a/internal/display/statusline.go +++ b/internal/display/statusline.go @@ -72,8 +72,6 @@ func SetStatusInfoFnLua(s string, fn string) { } } -// TODO: plugin modify status line formatter - // NewStatusLine returns a statusline bound to a window func NewStatusLine(win *BufWindow) *StatusLine { s := new(StatusLine)