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)
This commit is contained in:
Martin Grenfell 2014-07-05 20:51:21 +01:00
parent a7428eba38
commit 32cf3ee62d
4 changed files with 61 additions and 28 deletions

View File

@ -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

56
lib/nerdtree/flag_set.vim Normal file
View File

@ -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

View File

@ -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
"
"

View File

@ -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