From dda21033637fe1393f18149fbefe3c0d42185c41 Mon Sep 17 00:00:00 2001 From: Jan Larres Date: Mon, 17 Jan 2011 17:08:10 +1300 Subject: [PATCH] Allow sorting the tags by name or line (unsorted). --- plugin/tagbar.vim | 57 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/plugin/tagbar.vim b/plugin/tagbar.vim index 7a2a5f5..67549ac 100644 --- a/plugin/tagbar.vim +++ b/plugin/tagbar.vim @@ -53,6 +53,10 @@ if !exists('g:tagbar_autoclose') let g:tagbar_autoclose = 0 endif +if !exists('g:tagbar_sort') + let g:tagbar_sort = 1 +endif + function! s:InitTypes() let s:known_files = {} let s:known_types = {} @@ -100,6 +104,17 @@ function! s:InitTypes() let s:known_types.python = type_python call extend(s:known_types, g:tagbar_types) + + " Create a dictionary of the kind order for fast + " access in sorting functions + for type in values(s:known_types) + let i = 0 + let type.kinddict = {} + for kind in type.kinds + let type.kinddict[kind[0]] = i + let i += 1 + endfor + endfor endfunction call s:InitTypes() @@ -311,6 +326,16 @@ function! s:ProcessFile(fname, ftype) call extend(fileinfo.tags, scopedtags) endif + " Script-local variable needed since compare functions can't + " take extra arguments + let s:compare_typeinfo = typeinfo + + if g:tagbar_sort + call sort(fileinfo.tags, 's:CompareByKind') + else + call sort(fileinfo.tags, 's:CompareByLine') + endif + let s:known_files[a:fname] = fileinfo endfunction @@ -340,6 +365,28 @@ function! s:ParseTagline(line) return taginfo endfunction +function! s:CompareByKind(tag1, tag2) + let typeinfo = s:compare_typeinfo + + if typeinfo.kinddict[a:tag1.fields.kind] < + \ typeinfo.kinddict[a:tag2.fields.kind] + return -1 + elseif typeinfo.kinddict[a:tag1.fields.kind] > + \ typeinfo.kinddict[a:tag2.fields.kind] + return 1 + else + if a:tag1.name <= a:tag2.name + return -1 + else + return 1 + endif + endif +endfunction + +function! s:CompareByLine(tag1, tag2) + return a:tag1.fields.line - a:tag2.fields.line +endfunction + function! s:RenderContent(fname, ftype) let tagbarwinnr = bufwinnr('__Tagbar__') @@ -433,6 +480,16 @@ function! s:GetChildTags(tags, pscopetype, pscope, pname, typeinfo) let childtags = filter(copy(a:tags), is_child) call filter(a:tags, '!(' . is_child . ')') + " Script-local variable needed since compare functions can't + " take extra arguments + let s:compare_typeinfo = typeinfo + + if g:tagbar_sort + call sort(childtags, 's:CompareByKind') + else + call sort(childtags, 's:CompareByLine') + endif + " Recursively add children for tag in childtags if has_key(a:typeinfo.kind2scope, tag.fields.kind)