Add a NERDTreeMinimalMenu feature (#938)

* Extract the menu action prompts to a function

* Add a NERDTreeMinimalMenu configuration option

If this option is set, Vim’s command line displays
a single-line version of the menu:

Which action? (a*/m/d/r/o/q/c/l):

Pressing ‘a’ will call the ‘add child node’ menu
item as that is its shortcut.

The * denotes the position of the currently
selected menu item which can be changed with
NERDTreeMenuDown and Up in the usual way.

The user may wish to set these to <left>, <right>
or h, l if they prefer.

* Minimise the text once a menu item is chosen

If NERDTreeMinimalMenu is set, then echo a single
line prompt after the menu item is chosen. This
reduces noise for users who are already familiar
with how the menu works and prevents Vim scrolling
down to accommodate the menu text.

If the directory is non-empty we display
‘Delete directory?’ instead of ‘Delete?’ and the
user must still type ‘yes’ to confirm, rather than
just pressing ‘y’.

* Document the new NERDTreeMinimalMenu feature

* Tweak the format for the minimal menu prompt

As per @PhilRunninger’s suggestions:

- The menu now shows the ‘j/k/enter’ help text
- The first word of the selected item is shown*
- Use commas between menu items so that it’s
  visually distinct from the ‘j/k/enter’ text

* I tried displaying the full menuItem text, but
this can be quite long, e.g.

> (o)pen the current node with system editor

This causes the menu to jump around a lot. We
could add another minimal versions of these, but
I think the first word seems ok for now.
This commit is contained in:
Chris Patuzzo
2019-02-24 23:01:12 +00:00
committed by Phil Runninger
parent b6cde142dd
commit 84737f2ebe
4 changed files with 111 additions and 35 deletions

View File

@@ -44,6 +44,47 @@ else
call NERDTreeAddMenuItem({'text': '(l)ist the current node', 'shortcut': 'l', 'callback': 'NERDTreeListNodeWin32'})
endif
"FUNCTION: s:inputPrompt(action){{{1
"returns the string that should be prompted to the user for the given action
"
"Args:
"action: the action that is being performed, e.g. 'delete'
function! s:inputPrompt(action)
if a:action == "add"
let title = "Add a childnode"
let info = "Enter the dir/file name to be created. Dirs end with a '/'"
let minimal = "Add node:"
elseif a:action == "copy"
let title = "Copy the current node"
let info = "Enter the new path to copy the node to:"
let minimal = "Copy to:"
elseif a:action == "delete"
let title = "Delete the current node"
let info = "Are you sure you wish to delete the node:"
let minimal = "Delete?"
elseif a:action == "deleteNonEmpty"
let title = "Delete the current node"
let info = "STOP! Directory is not empty! To delete, type 'yes'"
let minimal = "Delete directory?"
elseif a:action == "move"
let title = "Rename the current node"
let info = "Enter the new path for the node:"
let minimal = "Move to:"
endif
if g:NERDTreeMenuController.isMinimal()
redraw! " Clear the menu
return minimal . " "
else
let divider = "=========================================================="
return title . "\n" . divider . "\n" . info . "\n"
end
endfunction
"FUNCTION: s:promptToDelBuffer(bufnum, msg){{{1
"prints out the given msg and, if the user responds by pushing 'y' then the
"buffer with the given bufnum is deleted
@@ -114,14 +155,12 @@ function! s:renameBuffer(bufNum, newNodeName, isDirectory)
" This happens when answering Cancel if confirmation is needed. Do nothing.
endtry
endfunction
"FUNCTION: NERDTreeAddNode(){{{1
function! NERDTreeAddNode()
let curDirNode = g:NERDTreeDirNode.GetSelected()
let newNodeName = input("Add a childnode\n".
\ "==========================================================\n".
\ "Enter the dir/file name to be created. Dirs end with a '/'\n" .
\ "", curDirNode.path.str() . g:NERDTreePath.Slash(), "file")
let prompt = s:inputPrompt("add")
let newNodeName = input(prompt, curDirNode.path.str() . g:NERDTreePath.Slash(), "file")
if newNodeName ==# ''
call nerdtree#echo("Node Creation Aborted.")
@@ -144,6 +183,8 @@ function! NERDTreeAddNode()
call NERDTreeRender()
call newTreeNode.putCursorHere(1, 0)
endif
redraw!
catch /^NERDTree/
call nerdtree#echoWarning("Node Not Created.")
endtry
@@ -152,10 +193,8 @@ endfunction
"FUNCTION: NERDTreeMoveNode(){{{1
function! NERDTreeMoveNode()
let curNode = g:NERDTreeFileNode.GetSelected()
let newNodePath = input("Rename the current node\n" .
\ "==========================================================\n" .
\ "Enter the new path for the node: \n" .
\ "", curNode.path.str(), "file")
let prompt = s:inputPrompt("move")
let newNodePath = input(prompt, curNode.path.str(), "file")
if newNodePath ==# ''
call nerdtree#echo("Node Renaming Aborted.")
@@ -194,7 +233,7 @@ function! NERDTreeMoveNode()
call curNode.putCursorHere(1, 0)
redraw
redraw!
catch /^NERDTree/
call nerdtree#echoWarning("Node Not Renamed.")
endtry
@@ -209,21 +248,16 @@ function! NERDTreeDeleteNode()
if currentNode.path.isDirectory && ((currentNode.isOpen && currentNode.getChildCount() > 0) ||
\ (len(currentNode._glob('*', 1)) > 0))
let choice =input("Delete the current node\n" .
\ "==========================================================\n" .
\ "STOP! Directory is not empty! To delete, type 'yes'\n" .
\ "" . currentNode.path.str() . ": ")
let prompt = s:inputPrompt("deleteNonEmpty") . currentNode.path.str() . ": "
let choice = input(prompt)
let confirmed = choice ==# 'yes'
else
echo "Delete the current node\n" .
\ "==========================================================\n".
\ "Are you sure you wish to delete the node:\n" .
\ "" . currentNode.path.str() . " (yN):"
let prompt = s:inputPrompt("delete") . currentNode.path.str() . " (yN): "
echo prompt
let choice = nr2char(getchar())
let confirmed = choice ==# 'y'
endif
if confirmed
try
call currentNode.delete()
@@ -237,7 +271,7 @@ function! NERDTreeDeleteNode()
call s:promptToDelBuffer(bufnum, prompt)
endif
redraw
redraw!
catch /^NERDTree/
call nerdtree#echoWarning("Could not remove node")
endtry
@@ -292,10 +326,8 @@ function! NERDTreeCopyNode()
let l:shellslash = &shellslash
let &shellslash = 0
let currentNode = g:NERDTreeFileNode.GetSelected()
let newNodePath = input("Copy the current node\n" .
\ "==========================================================\n" .
\ "Enter the new path to copy the node to: \n" .
\ "", currentNode.path.str(), "file")
let prompt = s:inputPrompt("copy")
let newNodePath = input(prompt, currentNode.path.str(), "file")
if newNodePath != ""
"strip trailing slash
@@ -329,7 +361,7 @@ function! NERDTreeCopyNode()
call nerdtree#echo("Copy aborted.")
endif
let &shellslash = l:shellslash
redraw
redraw!
endfunction
" FUNCTION: NERDTreeQuickLook() {{{1