nerdtree

A tree explorer plugin for vim.
Index Commits Files Refs
commit 32cf3ee62d6ce722238525e3a0ea98a93f9297e2
parent a7428eba38fcd23a50f1cf5c26938c68ffd62673
Author: Martin Grenfell <martin.grenfell@gmail.com>
Date:   Sat,  5 Jul 2014 20:51:21 +0100

allow flags to be scoped to a plugin

Add new FlagSet class and init each Path with one.

Call Path.flagSet.addFlag(scope, flag) instead of Path.addFlag(flag)

Diffstat:
Mautoload/nerdtree.vim | 1+
Alib/nerdtree/flag_set.vim | 56++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mlib/nerdtree/path.vim | 28++--------------------------
Mnerdtree_plugin/git.vim | 4++--
4 files changed, 61 insertions(+), 28 deletions(-)
diff --git a/autoload/nerdtree.vim b/autoload/nerdtree.vim
@@ -239,6 +239,7 @@ function! nerdtree#loadClassFiles()
     runtime lib/nerdtree/opener.vim
     runtime lib/nerdtree/creator.vim
     runtime lib/nerdtree/refresh_notifier.vim
+    runtime lib/nerdtree/flag_set.vim
 endfunction
 
 " FUNCTION: nerdtree#postSourceActions() {{{2
diff --git a/lib/nerdtree/flag_set.vim b/lib/nerdtree/flag_set.vim
@@ -0,0 +1,56 @@
+"CLASS: FlagSet
+"============================================================
+let s:FlagSet = {}
+let g:NERDTreeFlagSet = s:FlagSet
+
+"FUNCTION: FlagSet.addFlag(scope, flag) {{{1
+function! s:FlagSet.addFlag(scope, flag)
+    let flags = self._flagsForScope(a:scope)
+    if index(flags, a:flag) == -1
+        call add(flags, a:flag)
+    end
+endfunction
+
+"FUNCTION: FlagSet.clearFlags(scope) {{{1
+function! s:FlagSet.clearFlags(scope, flag)
+    let self._flags[a:scope] = []
+endfunction
+
+"FUNCTION: FlagSet._flagsForScope(scope) {{{1
+function! s:FlagSet._flagsForScope(scope)
+    if !has_key(self._flags, a:scope)
+        let self._flags[a:scope] = []
+    endif
+    return self._flags[a:scope]
+endfunction
+
+"FUNCTION: FlagSet.New() {{{1
+function! s:FlagSet.New()
+    let newObj = copy(self)
+    let newObj._flags = {}
+    return newObj
+endfunction
+
+"FUNCTION: FlagSet.removeFlag(scope, flag) {{{1
+function! s:FlagSet.removeFlag(scope, flag)
+    let flags = self._flagsForScope(a:scope)
+
+    let i = index(flags, a:flag)
+    if i >= 0
+        call remove(flags, i)
+    endif
+endfunction
+
+"FUNCTION: FlagSet.renderToString() {{{1
+function! s:FlagSet.renderToString()
+    let flagstring = ""
+    for i in values(self._flags)
+        let flagstring .= join(i)
+    endfor
+
+    if len(flagstring) == 0
+        return ""
+    endif
+
+    return '[' . flagstring . ']'
+endfunction
diff --git a/lib/nerdtree/path.vim b/lib/nerdtree/path.vim
@@ -24,13 +24,6 @@ function! s:Path.AbsolutePathFor(str)
     return toReturn
 endfunction
 
-"FUNCTION: Path.addFlag(flag) {{{1
-function! s:Path.addFlag(flag)
-    if index(self._flags, a:flag) == -1
-        call add(self._flags, a:flag)
-    endif
-endfunction
-
 "FUNCTION: Path.bookmarkNames() {{{1
 function! s:Path.bookmarkNames()
     if !exists("self._bookmarkNames")
@@ -41,7 +34,7 @@ endfunction
 
 "FUNCTION: Path.cacheDisplayString() {{{1
 function! s:Path.cacheDisplayString() abort
-    let self.cachedDisplayString = self._flagString()
+    let self.cachedDisplayString = self.flagSet.renderToString()
 
     let self.cachedDisplayString .= self.getLastPathComponent(1)
 
@@ -359,15 +352,6 @@ function! s:Path.getSortOrderIndex()
     return s:NERDTreeSortStarIndex
 endfunction
 
-"FUNCTION: Path._flagString() {{{1
-function! s:Path._flagString()
-    if empty(self._flags)
-        return ""
-    endif
-
-    return '[' . join(self._flags, ',') . ']'
-endfunction
-
 "FUNCTION: Path.isUnixHiddenFile() {{{1
 "check for unix hidden files
 function! s:Path.isUnixHiddenFile()
@@ -478,7 +462,7 @@ function! s:Path.New(path)
     call newPath.readInfoFromDisk(s:Path.AbsolutePathFor(a:path))
 
     let newPath.cachedDisplayString = ""
-    let newPath._flags = []
+    let newPath.flagSet = g:NERDTreeFlagSet.New()
 
     return newPath
 endfunction
@@ -499,14 +483,6 @@ function! s:Path.Resolve(path)
     return tmp =~# '.\+/$' ? substitute(tmp, '/$', '', '') : tmp
 endfunction
 
-"FUNCTION: Path.removeFlag(flag) {{{1
-function! s:Path.removeFlag(flag)
-    let i = index(self._flags, a:flag)
-    if i >= 0
-        call remove(self._flags, i)
-    endif
-endfunction
-
 "FUNCTION: Path.readInfoFromDisk(fullpath) {{{1
 "
 "
diff --git a/nerdtree_plugin/git.vim b/nerdtree_plugin/git.vim
@@ -23,9 +23,9 @@ function! g:NERDTreeGitRefreshListener(path)
 
     let modifiedFiles = s:GetModifiedFiles()
     if index(modifiedFiles, a:path.str()) >= 0
-        call a:path.addFlag("+")
+        call a:path.flagSet.addFlag("git", "+")
     else
-        call a:path.removeFlag("+")
+        call a:path.flagSet.removeFlag("git", "+")
     endif
 endfunction