ui.vim 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. "CLASS: UI
  2. "============================================================
  3. let s:UI = {}
  4. let g:NERDTreeUI = s:UI
  5. function! s:UI.lolcats()
  6. echomsg "lolcats"
  7. endfunction
  8. "FUNCTION: s:UI.centerView() {{{2
  9. "centers the nerd tree window around the cursor (provided the nerd tree
  10. "options permit)
  11. function! s:UI.centerView()
  12. if g:NERDTreeAutoCenter
  13. let current_line = winline()
  14. let lines_to_top = current_line
  15. let lines_to_bottom = winheight(nerdtree#getTreeWinNum()) - current_line
  16. if lines_to_top < g:NERDTreeAutoCenterThreshold || lines_to_bottom < g:NERDTreeAutoCenterThreshold
  17. normal! zz
  18. endif
  19. endif
  20. endfunction
  21. "FUNCTION: s:UI.new(nerdtree) {{{1
  22. function! s:UI.New(nerdtree)
  23. let newObj = copy(self)
  24. let newObj.nerdtree = a:nerdtree
  25. return newObj
  26. endfunction
  27. "FUNCTION: s:UI.getPath(ln) {{{1
  28. "Gets the full path to the node that is rendered on the given line number
  29. "
  30. "Args:
  31. "ln: the line number to get the path for
  32. "
  33. "Return:
  34. "A path if a node was selected, {} if nothing is selected.
  35. "If the 'up a dir' line was selected then the path to the parent of the
  36. "current root is returned
  37. function! s:UI.getPath(ln)
  38. let line = getline(a:ln)
  39. let rootLine = self.getRootLineNum()
  40. "check to see if we have the root node
  41. if a:ln == rootLine
  42. return b:NERDTreeRoot.path
  43. endif
  44. if !g:NERDTreeDirArrows
  45. " in case called from outside the tree
  46. if line !~# '^ *[|`▸▾ ]' || line =~# '^$'
  47. return {}
  48. endif
  49. endif
  50. if line ==# nerdtree#treeUpDirLine()
  51. return b:NERDTreeRoot.path.getParent()
  52. endif
  53. let indent = self._indentLevelFor(line)
  54. "remove the tree parts and the leading space
  55. let curFile = nerdtree#stripMarkupFromLine(line, 0)
  56. let wasdir = 0
  57. if curFile =~# '/$'
  58. let wasdir = 1
  59. let curFile = substitute(curFile, '/\?$', '/', "")
  60. endif
  61. let dir = ""
  62. let lnum = a:ln
  63. while lnum > 0
  64. let lnum = lnum - 1
  65. let curLine = getline(lnum)
  66. let curLineStripped = nerdtree#stripMarkupFromLine(curLine, 1)
  67. "have we reached the top of the tree?
  68. if lnum == rootLine
  69. let dir = b:NERDTreeRoot.path.str({'format': 'UI'}) . dir
  70. break
  71. endif
  72. if curLineStripped =~# '/$'
  73. let lpindent = self._indentLevelFor(curLine)
  74. if lpindent < indent
  75. let indent = indent - 1
  76. let dir = substitute (curLineStripped,'^\\', "", "") . dir
  77. continue
  78. endif
  79. endif
  80. endwhile
  81. let curFile = b:NERDTreeRoot.path.drive . dir . curFile
  82. let toReturn = g:NERDTreePath.New(curFile)
  83. return toReturn
  84. endfunction
  85. "FUNCTION: s:UI.getLineNum(file_node){{{1
  86. "returns the line number this node is rendered on, or -1 if it isnt rendered
  87. function! s:UI.getLineNum(file_node)
  88. "if the node is the root then return the root line no.
  89. if a:file_node.isRoot()
  90. return b:NERDTree.ui.getRootLineNum()
  91. endif
  92. let totalLines = line("$")
  93. "the path components we have matched so far
  94. let pathcomponents = [substitute(b:NERDTreeRoot.path.str({'format': 'UI'}), '/ *$', '', '')]
  95. "the index of the component we are searching for
  96. let curPathComponent = 1
  97. let fullpath = a:file_node.path.str({'format': 'UI'})
  98. let lnum = b:NERDTree.ui.getRootLineNum()
  99. while lnum > 0
  100. let lnum = lnum + 1
  101. "have we reached the bottom of the tree?
  102. if lnum ==# totalLines+1
  103. return -1
  104. endif
  105. let curLine = getline(lnum)
  106. let indent = self._indentLevelFor(curLine)
  107. if indent ==# curPathComponent
  108. let curLine = nerdtree#stripMarkupFromLine(curLine, 1)
  109. let curPath = join(pathcomponents, '/') . '/' . curLine
  110. if stridx(fullpath, curPath, 0) ==# 0
  111. if fullpath ==# curPath || strpart(fullpath, len(curPath)-1,1) ==# '/'
  112. let curLine = substitute(curLine, '/ *$', '', '')
  113. call add(pathcomponents, curLine)
  114. let curPathComponent = curPathComponent + 1
  115. if fullpath ==# curPath
  116. return lnum
  117. endif
  118. endif
  119. endif
  120. endif
  121. endwhile
  122. return -1
  123. endfunction
  124. "FUNCTION: s:UI.getRootLineNum(){{{1
  125. "gets the line number of the root node
  126. function! s:UI.getRootLineNum()
  127. let rootLine = 1
  128. while getline(rootLine) !~# '^\(/\|<\)'
  129. let rootLine = rootLine + 1
  130. endwhile
  131. return rootLine
  132. endfunction
  133. "FUNCTION: s:UI._indentLevelFor(line) {{{2
  134. function! s:UI._indentLevelFor(line)
  135. let level = match(a:line, '[^ \-+~▸▾`|]') / nerdtree#treeWid()
  136. " check if line includes arrows
  137. if match(a:line, '[▸▾]') > -1
  138. " decrement level as arrow uses 3 ascii chars
  139. let level = level - 1
  140. endif
  141. return level
  142. endfunction
  143. "FUNCTION: s:UI.restoreScreenState() {{{2
  144. "
  145. "Sets the screen state back to what it was when nerdtree#saveScreenState was last
  146. "called.
  147. "
  148. "Assumes the cursor is in the NERDTree window
  149. function! s:UI.restoreScreenState()
  150. if !has_key(self, '_screenState')
  151. return
  152. endif
  153. exec("silent vertical resize " . self._screenState['oldWindowSize'])
  154. let old_scrolloff=&scrolloff
  155. let &scrolloff=0
  156. call cursor(self._screenState['oldTopLine'], 0)
  157. normal! zt
  158. call setpos(".", self._screenState['oldPos'])
  159. let &scrolloff=old_scrolloff
  160. endfunction
  161. "FUNCTION: s:UI.saveScreenState() {{{2
  162. "Saves the current cursor position in the current buffer and the window
  163. "scroll position
  164. function! s:UI.saveScreenState()
  165. let win = winnr()
  166. try
  167. call nerdtree#putCursorInTreeWin()
  168. let self._screenState = {}
  169. let self._screenState['oldPos'] = getpos(".")
  170. let self._screenState['oldTopLine'] = line("w0")
  171. let self._screenState['oldWindowSize']= winwidth("")
  172. call nerdtree#exec(win . "wincmd w")
  173. catch /^NERDTree.InvalidOperationError/
  174. endtry
  175. endfunction
  176. "FUNCTION: s:UI.render() {{{2
  177. function! s:UI.render()
  178. setlocal modifiable
  179. "remember the top line of the buffer and the current line so we can
  180. "restore the view exactly how it was
  181. let curLine = line(".")
  182. let curCol = col(".")
  183. let topLine = line("w0")
  184. "delete all lines in the buffer (being careful not to clobber a register)
  185. silent 1,$delete _
  186. call nerdtree#dumpHelp()
  187. "delete the blank line before the help and add one after it
  188. if g:NERDTreeMinimalUI == 0
  189. call setline(line(".")+1, "")
  190. call cursor(line(".")+1, col("."))
  191. endif
  192. if b:NERDTreeShowBookmarks
  193. call nerdtree#renderBookmarks()
  194. endif
  195. "add the 'up a dir' line
  196. if !g:NERDTreeMinimalUI
  197. call setline(line(".")+1, nerdtree#treeUpDirLine())
  198. call cursor(line(".")+1, col("."))
  199. endif
  200. "draw the header line
  201. let header = b:NERDTreeRoot.path.str({'format': 'UI', 'truncateTo': winwidth(0)})
  202. call setline(line(".")+1, header)
  203. call cursor(line(".")+1, col("."))
  204. "draw the tree
  205. let old_o = @o
  206. let @o = b:NERDTreeRoot.renderToString()
  207. silent put o
  208. let @o = old_o
  209. "delete the blank line at the top of the buffer
  210. silent 1,1delete _
  211. "restore the view
  212. let old_scrolloff=&scrolloff
  213. let &scrolloff=0
  214. call cursor(topLine, 1)
  215. normal! zt
  216. call cursor(curLine, curCol)
  217. let &scrolloff = old_scrolloff
  218. setlocal nomodifiable
  219. endfunction
  220. "FUNCTION: UI.renderViewSavingPosition {{{1
  221. "Renders the tree and ensures the cursor stays on the current node or the
  222. "current nodes parent if it is no longer available upon re-rendering
  223. function! s:UI.renderViewSavingPosition()
  224. let currentNode = g:NERDTreeFileNode.GetSelected()
  225. "go up the tree till we find a node that will be visible or till we run
  226. "out of nodes
  227. while currentNode != {} && !currentNode.isVisible() && !currentNode.isRoot()
  228. let currentNode = currentNode.parent
  229. endwhile
  230. call b:NERDTree.render()
  231. if currentNode != {}
  232. call currentNode.putCursorHere(0, 0)
  233. endif
  234. endfunction
  235. " FUNCTION: s:UI.toggleIgnoreFilter() {{{1
  236. " toggles the use of the NERDTreeIgnore option
  237. function! s:UI.toggleIgnoreFilter()
  238. let b:NERDTreeIgnoreEnabled = !b:NERDTreeIgnoreEnabled
  239. call b:NERDTree.ui.renderViewSavingPosition()
  240. call b:NERDTree.ui.centerView()
  241. endfunction
  242. " FUNCTION: s:UI.toggleShowBookmarks() {{{1
  243. " toggles the display of bookmarks
  244. function! s:UI.toggleShowBookmarks()
  245. let b:NERDTreeShowBookmarks = !b:NERDTreeShowBookmarks
  246. if b:NERDTreeShowBookmarks
  247. call b:NERDTree.render()
  248. call nerdtree#putCursorOnBookmarkTable()
  249. else
  250. call b:NERDTree.ui.renderViewSavingPosition()
  251. endif
  252. call b:NERDTree.ui.centerView()
  253. endfunction
  254. " FUNCTION: s:UI.toggleShowFiles() {{{1
  255. " toggles the display of hidden files
  256. function! s:UI.toggleShowFiles()
  257. let b:NERDTreeShowFiles = !b:NERDTreeShowFiles
  258. call b:NERDTree.ui.renderViewSavingPosition()
  259. call b:NERDTree.ui.centerView()
  260. endfunction
  261. " FUNCTION: s:UI.toggleShowHidden() {{{1
  262. " toggles the display of hidden files
  263. function! s:UI.toggleShowHidden()
  264. let b:NERDTreeShowHidden = !b:NERDTreeShowHidden
  265. call b:NERDTree.ui.renderViewSavingPosition()
  266. call self.centerView()
  267. endfunction
  268. " FUNCTION: s:UI.toggleZoom() {{{1
  269. " zoom (maximize/minimize) the NERDTree window
  270. function! s:UI.toggleZoom()
  271. if exists("b:NERDTreeZoomed") && b:NERDTreeZoomed
  272. let size = exists("b:NERDTreeOldWindowSize") ? b:NERDTreeOldWindowSize : g:NERDTreeWinSize
  273. exec "silent vertical resize ". size
  274. let b:NERDTreeZoomed = 0
  275. else
  276. exec "vertical resize"
  277. let b:NERDTreeZoomed = 1
  278. endif
  279. endfunction