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

@ -719,6 +719,9 @@ the NERD tree. These settings should be set in your vimrc, using `:let`.
|NERDTreeMinimalUI| Disables display of the 'Bookmarks' label and |NERDTreeMinimalUI| Disables display of the 'Bookmarks' label and
'Press ? for help' text. 'Press ? for help' text.
|NERDTreeMinimalMenu| Use a compact menu that fits on a single line
for adding, copying, deleting, etc
|NERDTreeCascadeSingleChildDir| |NERDTreeCascadeSingleChildDir|
Collapses on the same line directories that have Collapses on the same line directories that have
only one child directory. only one child directory.
@ -1116,6 +1119,26 @@ of the following lines for this setting: >
let NERDTreeMinimalUI=1 let NERDTreeMinimalUI=1
< <
------------------------------------------------------------------------------
*NERDTreeMinimalMenu*
Values: 0 or 1
Default: 0
This setting makes NERD tree use a smaller, more compact menu for adding,
copying, deleting nodes. This menu fits on a single line so Vim doesn't need to
scroll down to present it. This setting is recommended for users already
familiar with the menu items. It will look similar to this:
Menu: [ (a)dd ,m,d,r,o,q,c,l] (Use j/k/enter or shortcut):
An action can be selected with its shortcut key or with the NERDTreeMenuUp and
NERDTreeMenuDown keys, then pressing enter.
Use one of the following lines for this setting: >
let NERDTreeMinimalMenu=0
let NERDTreeMinimalMenu=1
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
*NERDTreeCascadeSingleChildDir* *NERDTreeCascadeSingleChildDir*
Values: 0 or 1 Values: 0 or 1

View File

@ -15,6 +15,11 @@ function! s:MenuController.New(menuItems)
return newMenuController return newMenuController
endfunction endfunction
" FUNCTION: s:MenuController.isMinimal() {{{1
function! s:MenuController.isMinimal()
return g:NERDTreeMinimalMenu
endfunction
" FUNCTION: MenuController.showMenu() {{{1 " FUNCTION: MenuController.showMenu() {{{1
" Enter the main loop of the NERDTree menu, prompting the user to select " Enter the main loop of the NERDTree menu, prompting the user to select
" a menu item. " a menu item.
@ -49,16 +54,27 @@ endfunction
"FUNCTION: MenuController._echoPrompt() {{{1 "FUNCTION: MenuController._echoPrompt() {{{1
function! s:MenuController._echoPrompt() function! s:MenuController._echoPrompt()
echo "NERDTree Menu. Use " . g:NERDTreeMenuDown . "/" . g:NERDTreeMenuUp . "/enter and the shortcuts indicated" let navHelp = "Use " . g:NERDTreeMenuDown . "/" . g:NERDTreeMenuUp . "/enter"
echo "=========================================================="
for i in range(0, len(self.menuItems)-1) if self.isMinimal()
if self.selection == i let selection = self.menuItems[self.selection].text
echo "> " . self.menuItems[i].text
else let shortcuts = map(copy(self.menuItems), "v:val['shortcut']")
echo " " . self.menuItems[i].text let shortcuts[self.selection] = " " . split(selection)[0] . " "
endif
endfor echo "Menu: [" . join(shortcuts, ",") . "] (" . navHelp . " or shortcut): "
else
echo "NERDTree Menu. " . navHelp . " . or the shortcuts indicated"
echo "========================================================="
for i in range(0, len(self.menuItems)-1)
if self.selection == i
echo "> " . self.menuItems[i].text
else
echo " " . self.menuItems[i].text
endif
endfor
endif
endfunction endfunction
"FUNCTION: MenuController._current(key) {{{1 "FUNCTION: MenuController._current(key) {{{1
@ -129,7 +145,11 @@ endfunction
"FUNCTION: MenuController._setCmdheight() {{{1 "FUNCTION: MenuController._setCmdheight() {{{1
"sets &cmdheight to whatever is needed to display the menu "sets &cmdheight to whatever is needed to display the menu
function! s:MenuController._setCmdheight() function! s:MenuController._setCmdheight()
let &cmdheight = len(self.menuItems) + 3 if self.isMinimal()
let &cmdheight = 1
else
let &cmdheight = len(self.menuItems) + 3
endif
endfunction endfunction
"FUNCTION: MenuController._saveOptions() {{{1 "FUNCTION: MenuController._saveOptions() {{{1

View File

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

View File

@ -51,6 +51,7 @@ call s:initVariable("g:NERDTreeSortHiddenFirst", 1)
call s:initVariable("g:NERDTreeChDirMode", 0) call s:initVariable("g:NERDTreeChDirMode", 0)
call s:initVariable("g:NERDTreeCreatePrefix", "silent") call s:initVariable("g:NERDTreeCreatePrefix", "silent")
call s:initVariable("g:NERDTreeMinimalUI", 0) call s:initVariable("g:NERDTreeMinimalUI", 0)
call s:initVariable("g:NERDTreeMinimalMenu", 0)
if !exists("g:NERDTreeIgnore") if !exists("g:NERDTreeIgnore")
let g:NERDTreeIgnore = ['\~$'] let g:NERDTreeIgnore = ['\~$']
endif endif