Skip to content

Commit c427581

Browse files
committed
fix: prioritize exact namespace/class matches.
only prefer functions/methods over templates on the same context Signed-off-by: GuyBrush <miguel.barro@live.com>
1 parent 30b689b commit c427581

1 file changed

Lines changed: 50 additions & 18 deletions

File tree

start/cpptags/autoload/cpptags.vim

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,10 @@ function! cpptags#CppTagFunc(pattern, flags, info)
126126
let id = id->substitute("<.*>","","")
127127

128128
function! s:ClassFilter(idx, val) closure
129-
let keep = (a:val->has_key('namespace') && a:val['namespace'] =~ id)
130-
\ || (a:val->has_key('class') && a:val['class'] =~ id)
131-
\ || (a:val->has_key('struct') && a:val['struct'] =~ id)
129+
130+
let keep = a:val->has_key('namespace') && a:val['namespace'] =~ id
131+
\ || a:val->has_key('class') && a:val['class'] =~ id
132+
\ || a:val->has_key('struct') && a:val['struct'] =~ id
132133
if !keep
133134
call s:Log(string(a:val) .. " removed because doesn't match namespace/class " .. id)
134135
endif
@@ -139,19 +140,27 @@ function! cpptags#CppTagFunc(pattern, flags, info)
139140
" filtering matchlist by class or namespace
140141
let result = result->filter(funcref("s:ClassFilter"))
141142

142-
function! s:SortNamespace(item1, item2) closure
143-
let lastid = id .. "$"
143+
function! s:SortNamespace(item1, item2, exact = 0) closure
144+
145+
let lastid = id .. '$'
146+
if a:exact
147+
let lastid = "\\<" .. lastid
148+
endif
144149
let l1 = a:item1->has_key('namespace') && a:item1['namespace'] =~ lastid ||
145-
\ a:item1->has_key('class') && a:item1['class'] =~ lastid
150+
\ a:item1->has_key('class') && a:item1['class'] =~ lastid ||
151+
\ a:item1->has_key('struct') && a:item1['struct'] =~ lastid
146152
let l2 = a:item2->has_key('namespace') && a:item2['namespace'] =~ lastid ||
147-
\ a:item2->has_key('class') && a:item2['class'] =~ lastid
153+
\ a:item2->has_key('class') && a:item2['class'] =~ lastid ||
154+
\ a:item2->has_key('struct') && a:item2['struct'] =~ lastid
148155

149156
if l1 && l2
150-
return 0
157+
" favour exact match if any
158+
return a:exact ? 0 : s:SortNamespace(a:item1, a:item2, 1)
151159
elseif !l1 && l2
152160
call s:Log("reorder " .. string(a:item1) .. " after " .. string(a:item2))
153161
return 1
154162
elseif l1 && !l2
163+
call s:Log("reorder " .. string(a:item2) .. " after " .. string(a:item1))
155164
return -1
156165
else
157166
return 0
@@ -170,24 +179,47 @@ function! cpptags#CppTagFunc(pattern, flags, info)
170179
let result = result->filter({idx, val -> val->has_key('specialization') ?
171180
\ s:Log(string(val) .. " removed because is a template specialization") : 1})
172181

182+
function! s:ClassName(val)
183+
if a:val->has_key('class')
184+
return a:val['class']
185+
elseif a:val->has_key('struct')
186+
return a:val['struct']
187+
else
188+
return ''
189+
endif
190+
endfunction
191+
173192
function! s:SortTemplates(item1, item2)
174193
let t1 = a:item1->has_key('template')
175194
let t2 = a:item2->has_key('template')
176195

177-
if t1 && t2
178-
return 0
179-
elseif t1 && !t2
180-
call s:Log("reorder " .. string(a:item1) .. " after " .. string(a:item2))
181-
return 1
182-
elseif !t1 && t2
183-
return -1
184-
else
185-
return 0
196+
if t1 != t2
197+
let n1 = a:item1->has_key('namespace')
198+
let n2 = a:item2->has_key('namespace')
199+
200+
" check domain
201+
if n1 && n2 && a:item1['namespace'] == a:item2['namespace']
202+
\ || !n1 && !n2
203+
" Check class name
204+
if s:ClassName(a:item1) == s:ClassName(a:item2)
205+
206+
" Choose order
207+
if t1 && !t2
208+
call s:Log("reorder " .. string(a:item1) .. " after " .. string(a:item2))
209+
return 1
210+
else
211+
call s:Log("reorder " .. string(a:item2) .. " after " .. string(a:item1))
212+
return -1
213+
endif
214+
endif
215+
endif
186216
endif
187217

218+
return 0
219+
188220
endfunction
189221

190-
" deprioritize template
222+
" deprioritize template within same domain
191223
let result = result->sort(funcref("s:SortTemplates"))
192224

193225
elseif elems['template'] == '<>'

0 commit comments

Comments
 (0)