summaryrefslogtreecommitdiff
path: root/.vim/ftplugin/java/CTree.vim
blob: a67fdbc0c109f14de1b4d98caf3433fbc7e329d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
" -------------------------------------------------------------------
"  CTree.vim -- Display Class/Interface Hierarchy "{{{
"
"  Author:   Yanbiao Zhao (yanbiao_zhao at yahoo.com)
"  Requires: Vim 7
"  Version:  1.1.2
"
"  Command:
"      CTree -- Display a tree of Class/Interface hierarchy
"      CTag  -- Jump to the class/interface definition of the tag
"  }}}

if v:version < 700
	echomsg "Vim 7 or higher is required for CTree.vim"
	finish
endif

command! -nargs=1 -complete=tag CTree      call s:CTree_GetTypeTree(<f-args>)
command! -nargs=1 -complete=tag CTag       call s:CT_Jump_To_ClassName(<f-args>)

"Short cut to use the commands
"nmap <silent> <M-F9>  :exec "CTree ".expand("<cword>")<CR>
"nmap <silent> <M-]>   :exec "CTag  ".expand("<cword>")<CR>

function! s:CT_Jump_To_ClassName(className)
    let tagEntry = {}
    let tagEntry["name"] = a:className 
    if s:CT_Jump_To_Class(tagEntry)== 0
        echohl WarningMsg | echo 'tag not found: '.a:className | echohl None
    endif
endfunction

let s:CTree_AllTypeEntries = []
let s:CTree_TagEnvCache = ''
let s:CTree_tagFilesCache = {}

function! s:CTree_GetTypeTree(typeName)
    call s:CTree_LoadAllTypeEntries()

    let rootEntry = s:CTree_GetRootType(a:typeName, '')

    if empty(rootEntry)
        let rootEntry["name"] = a:typeName
        let rootEntry["namespace"] = ""
        let rootEntry["kind"] = 'c' 
        let rootEntry["inherits"] = ""
    endif

    echohl Title | echo '   #  tag' | echohl None

    let allEntries = []
    call s:CTree_GetChildren(allEntries, rootEntry, 0)

    let i = input('Choice number (<Enter> cancels):')
    let i = str2nr(i)
    if i > 0 && i <= len(allEntries)
        call s:CT_Jump_To_Class(allEntries[i-1])
    endif
endfunction

function! s:CTree_GetChildren(allEntries, rootEntry, depth)
    call add(a:allEntries, a:rootEntry)
    call s:CTree_DisplayTagEntry(len(a:allEntries), a:rootEntry, a:depth)

    let children = []
    let rootTypeName = a:rootEntry["name"]
    for tagEntry in s:CTree_AllTypeEntries
        if index(split(tagEntry["inherits"], ","), rootTypeName) >= 0 
            call add(children, tagEntry)
        endif
    endfor

    let rootKind = a:rootEntry["kind"]
    for child in children
        "We only want to display class that implement an interface directly
        if child["kind"] == 'c' && rootKind == 'i'
            call add(a:allEntries, child) 
            call s:CTree_DisplayTagEntry(len(a:allEntries), child, a:depth+1)
        else
            call s:CTree_GetChildren(a:allEntries, child, a:depth+1)
        endif
    endfor
    
endfunction

" Return if a tag file has changed in tagfiles()
function! s:HasTagFileChanged()
    let result = 0
    let tagFiles = map(tagfiles(), 'escape(v:val, " ")')
    let newTagFilesCache = {}

    if len(tagFiles) != len(s:CTree_tagFilesCache)
        let result = 1
    endif

    for tagFile in tagFiles
        let currentFiletime = getftime(tagFile)
        let newTagFilesCache[tagFile] = currentFiletime

        if !has_key(s:CTree_tagFilesCache, tagFile)
            let result = 1
        elseif currentFiletime != s:CTree_tagFilesCache[tagFile]
            let result = 1
        endif
    endfor

    let s:CTree_tagFilesCache = newTagFilesCache
    return result
endfunc

function! s:CTree_LoadAllTypeEntries()
    if s:HasTagFileChanged()
        let s:CTree_AllTypeEntries = []
    else
        return
    endif

    echo 'Loading tag information. It may take a while...'
    let ch = 'A'
    while ch <= 'Z'
        call s:CTree_GetTypeEntryWithCh(ch)
        let ch = nr2char(char2nr(ch)+1)
    endwhile 

    call s:CTree_GetTypeEntryWithCh('_')

    let ch = 'a'
    while ch <= 'z'
        call s:CTree_GetTypeEntryWithCh(ch)
        let ch = nr2char(char2nr(ch)+1)
    endwhile 

     echo "Count of type tag entries loaded: ".len(s:CTree_AllTypeEntries)
endfunction

function! s:CTree_GetTypeEntryWithCh(ch)
    for tagEntry in taglist('^'.a:ch)
        let kind = tagEntry["kind"]
        if (kind == 'i' || kind == 'c') && has_key(tagEntry, "inherits")
            call add(s:CTree_AllTypeEntries, tagEntry)
        endif
    endfor
endfunction

function! s:CTree_GetRootType(typeName, originalKind)
    for tagEntry in taglist("^".a:typeName."$")  

        let kind = tagEntry["kind"] 
        if kind != 'c' && kind != 'i'
            continue
        endif

        let originalKind = a:originalKind
        if originalKind == '' 
            let originalKind = kind
        elseif originalKind != tagEntry["kind"]
            "We will not accept interface as a parent of class
            return {}
        endif

        if !has_key(tagEntry, "inherits")
            return tagEntry
        endif

        "interface support multiple inheritance, so we will not try to get its
        "parent if it has more than one parent 
        let parents = split(tagEntry["inherits"], ",")
        if originalKind == 'i' && len(parents) > 1
            return tagEntry
        endif

        for parent in parents 
            let rootEntry = s:CTree_GetRootType(parent, originalKind)

            if !empty(rootEntry)
                return rootEntry
            endif
        endfor

        return tagEntry
    endfor

    return {}
endfunction 

function! s:CTree_DisplayTagEntry(index, typeEntry, depth)
    let s = string(a:index)
    while strlen(s) < 4
        let s = ' '.s
    endwhile

    let s = s." "
    let i = 0
    while i < a:depth
        let s = s."    "
        let i = i + 1
    endwhile

    let s = s.a:typeEntry["name"]

    if has_key(a:typeEntry, "namespace")
        let s = s.' ['.a:typeEntry["namespace"].']'
    elseif has_key(a:typeEntry, "class")
        let s = s.' <'.a:typeEntry["class"].'>'
    endif

    echo s
endfunction

function! s:CT_Jump_To_Class(tagEntry)
    let className = a:tagEntry["name"]

    if has_key(a:tagEntry, "namespace")
        let keyName = "namespace"
    elseif has_key(a:tagEntry, "class")
        let keyName = "class"
    else
        let keyName = ""
    endif

    if keyName == "" 
        let namespace = ""
    else
        let namespace = a:tagEntry[keyName] 
    endif

    let i = 1
    let entries = taglist('^'.className.'$')
    for entry in entries 
        let kind = entry["kind"]
        if kind == 'c' || kind == 'i' || kind == 'g'
            if namespace == "" || namespace == entry[keyName]
                exec "silent ".i."tag ".className
                return 1
            endif
        endif
        let i += 1
    endfor
    return 0
endfunction