| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  | vim9script noclear | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Vim completion script | 
					
						
							|  |  |  | # Language:     C | 
					
						
							|  |  |  | # Maintainer:   Bram Moolenaar <Bram@vim.org> | 
					
						
							|  |  |  | #		Rewritten in Vim9 script by github user lacygoill | 
					
						
							| 
									
										
										
										
											2022-01-31 15:40:56 +00:00
										 |  |  | # Last Change:  2022 Jan 31 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | var prepended: string | 
					
						
							|  |  |  | var grepCache: dict<list<dict<any>>> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # This function is used for the 'omnifunc' option. | 
					
						
							| 
									
										
										
										
											2022-01-31 15:40:56 +00:00
										 |  |  | export def Complete(findstart: bool, abase: string): any # {{{1 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |   if findstart | 
					
						
							|  |  |  |     # Locate the start of the item, including ".", "->" and "[...]". | 
					
						
							|  |  |  |     var line: string = getline('.') | 
					
						
							|  |  |  |     var start: number = charcol('.') - 1 | 
					
						
							|  |  |  |     var lastword: number = -1 | 
					
						
							| 
									
										
										
										
											2005-09-01 20:46:49 +00:00
										 |  |  |     while start > 0 | 
					
						
							| 
									
										
										
										
											2005-10-10 20:59:28 +00:00
										 |  |  |       if line[start - 1] =~ '\w' | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |         --start | 
					
						
							| 
									
										
										
										
											2005-10-10 20:59:28 +00:00
										 |  |  |       elseif line[start - 1] =~ '\.' | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |         if lastword == -1 | 
					
						
							|  |  |  |           lastword = start | 
					
						
							|  |  |  |         endif | 
					
						
							|  |  |  |         --start | 
					
						
							|  |  |  |       elseif start > 1 && line[start - 2] == '-' | 
					
						
							|  |  |  |         && line[start - 1] == '>' | 
					
						
							|  |  |  |         if lastword == -1 | 
					
						
							|  |  |  |           lastword = start | 
					
						
							|  |  |  |         endif | 
					
						
							|  |  |  |         start -= 2 | 
					
						
							| 
									
										
										
										
											2006-02-04 00:59:56 +00:00
										 |  |  |       elseif line[start - 1] == ']' | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |         # Skip over [...]. | 
					
						
							|  |  |  |         var n: number = 0 | 
					
						
							|  |  |  |         --start | 
					
						
							|  |  |  |         while start > 0 | 
					
						
							|  |  |  |           --start | 
					
						
							|  |  |  |           if line[start] == '[' | 
					
						
							|  |  |  |             if n == 0 | 
					
						
							|  |  |  |               break | 
					
						
							|  |  |  |             endif | 
					
						
							|  |  |  |             --n | 
					
						
							|  |  |  |           elseif line[start] == ']'  # nested [] | 
					
						
							|  |  |  |             ++n | 
					
						
							|  |  |  |           endif | 
					
						
							|  |  |  |         endwhile | 
					
						
							| 
									
										
										
										
											2005-09-01 20:46:49 +00:00
										 |  |  |       else | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |         break | 
					
						
							| 
									
										
										
										
											2005-09-01 20:46:49 +00:00
										 |  |  |       endif | 
					
						
							|  |  |  |     endwhile | 
					
						
							| 
									
										
										
										
											2005-10-10 20:59:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |     # Return the column of the last word, which is going to be changed. | 
					
						
							|  |  |  |     # Remember the text that comes before it in prepended. | 
					
						
							| 
									
										
										
										
											2005-10-10 20:59:28 +00:00
										 |  |  |     if lastword == -1 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |       prepended = '' | 
					
						
							|  |  |  |       return byteidx(line, start) | 
					
						
							| 
									
										
										
										
											2005-10-10 20:59:28 +00:00
										 |  |  |     endif | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |     prepended = line[start : lastword - 1] | 
					
						
							|  |  |  |     return byteidx(line, lastword) | 
					
						
							| 
									
										
										
										
											2005-09-01 20:46:49 +00:00
										 |  |  |   endif | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |   # Return list of matches. | 
					
						
							| 
									
										
										
										
											2005-09-07 21:21:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |   var base: string = prepended .. abase | 
					
						
							| 
									
										
										
										
											2005-10-10 20:59:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |   # Don't do anything for an empty base, would result in all the tags in the | 
					
						
							|  |  |  |   # tags file. | 
					
						
							| 
									
										
										
										
											2006-02-04 00:59:56 +00:00
										 |  |  |   if base == '' | 
					
						
							|  |  |  |     return [] | 
					
						
							|  |  |  |   endif | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |   # init cache for vimgrep to empty | 
					
						
							|  |  |  |   grepCache = {} | 
					
						
							| 
									
										
										
										
											2006-03-09 22:37:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |   # Split item in words, keep empty word after "." or "->". | 
					
						
							|  |  |  |   # "aa" -> ['aa'], "aa." -> ['aa', ''], "aa.bb" -> ['aa', 'bb'], etc. | 
					
						
							|  |  |  |   # We can't use split, because we need to skip nested [...]. | 
					
						
							|  |  |  |   # "aa[...]" -> ['aa', '[...]'], "aa.bb[...]" -> ['aa', 'bb', '[...]'], etc. | 
					
						
							|  |  |  |   var items: list<string> | 
					
						
							|  |  |  |   var s: number = 0 | 
					
						
							|  |  |  |   var arrays: number = 0 | 
					
						
							| 
									
										
										
										
											2006-02-04 00:59:56 +00:00
										 |  |  |   while 1 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |     var e: number = base->charidx(match(base, '\.\|->\|\[', s)) | 
					
						
							| 
									
										
										
										
											2006-02-04 00:59:56 +00:00
										 |  |  |     if e < 0 | 
					
						
							|  |  |  |       if s == 0 || base[s - 1] != ']' | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |         items->add(base[s :]) | 
					
						
							| 
									
										
										
										
											2006-02-04 00:59:56 +00:00
										 |  |  |       endif | 
					
						
							|  |  |  |       break | 
					
						
							| 
									
										
										
										
											2005-10-10 20:59:28 +00:00
										 |  |  |     endif | 
					
						
							| 
									
										
										
										
											2006-02-04 00:59:56 +00:00
										 |  |  |     if s == 0 || base[s - 1] != ']' | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |       items->add(base[s : e - 1]) | 
					
						
							| 
									
										
										
										
											2006-02-04 00:59:56 +00:00
										 |  |  |     endif | 
					
						
							|  |  |  |     if base[e] == '.' | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |       # skip over '.' | 
					
						
							|  |  |  |       s = e + 1 | 
					
						
							| 
									
										
										
										
											2006-02-04 00:59:56 +00:00
										 |  |  |     elseif base[e] == '-' | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |       # skip over '->' | 
					
						
							|  |  |  |       s = e + 2 | 
					
						
							| 
									
										
										
										
											2006-02-04 00:59:56 +00:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |       # Skip over [...]. | 
					
						
							|  |  |  |       var n: number = 0 | 
					
						
							|  |  |  |       s = e | 
					
						
							|  |  |  |       ++e | 
					
						
							|  |  |  |       while e < strcharlen(base) | 
					
						
							|  |  |  |         if base[e] == ']' | 
					
						
							|  |  |  |           if n == 0 | 
					
						
							|  |  |  |             break | 
					
						
							|  |  |  |           endif | 
					
						
							|  |  |  |           --n | 
					
						
							|  |  |  |         elseif base[e] == '['  # nested [...] | 
					
						
							|  |  |  |           ++n | 
					
						
							|  |  |  |         endif | 
					
						
							|  |  |  |         ++e | 
					
						
							| 
									
										
										
										
											2006-02-04 00:59:56 +00:00
										 |  |  |       endwhile | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |       ++e | 
					
						
							|  |  |  |       items->add(base[s : e - 1]) | 
					
						
							|  |  |  |       ++arrays | 
					
						
							|  |  |  |       s = e | 
					
						
							| 
									
										
										
										
											2006-02-04 00:59:56 +00:00
										 |  |  |     endif | 
					
						
							|  |  |  |   endwhile | 
					
						
							| 
									
										
										
										
											2005-09-05 22:14:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |   # Find the variable items[0]. | 
					
						
							|  |  |  |   # 1. in current function (like with "gd") | 
					
						
							|  |  |  |   # 2. in tags file(s) (like with ":tag") | 
					
						
							|  |  |  |   # 3. in current file (like with "gD") | 
					
						
							|  |  |  |   var res: list<dict<any>> | 
					
						
							|  |  |  |   if items[0]->searchdecl(false, true) == 0 | 
					
						
							|  |  |  |     # Found, now figure out the type. | 
					
						
							|  |  |  |     # TODO: join previous line if it makes sense | 
					
						
							|  |  |  |     var line: string = getline('.') | 
					
						
							|  |  |  |     var col: number = charcol('.') | 
					
						
							|  |  |  |     if line[: col - 1]->stridx(';') >= 0 | 
					
						
							|  |  |  |       # Handle multiple declarations on the same line. | 
					
						
							|  |  |  |       var col2: number = col - 1 | 
					
						
							| 
									
										
										
										
											2008-06-24 22:58:06 +00:00
										 |  |  |       while line[col2] != ';' | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |         --col2 | 
					
						
							| 
									
										
										
										
											2008-06-24 22:58:06 +00:00
										 |  |  |       endwhile | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |       line = line[col2 + 1 :] | 
					
						
							|  |  |  |       col -= col2 | 
					
						
							| 
									
										
										
										
											2008-06-24 22:58:06 +00:00
										 |  |  |     endif | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |     if line[: col - 1]->stridx(',') >= 0 | 
					
						
							|  |  |  |       # Handle multiple declarations on the same line in a function | 
					
						
							|  |  |  |       # declaration. | 
					
						
							|  |  |  |       var col2: number = col - 1 | 
					
						
							| 
									
										
										
										
											2008-06-24 22:58:06 +00:00
										 |  |  |       while line[col2] != ',' | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |         --col2 | 
					
						
							| 
									
										
										
										
											2008-06-24 22:58:06 +00:00
										 |  |  |       endwhile | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |       if line[col2 + 1 : col - 1] =~ ' *[^ ][^ ]*  *[^ ]' | 
					
						
							|  |  |  |         line = line[col2 + 1 :] | 
					
						
							|  |  |  |         col -= col2 | 
					
						
							| 
									
										
										
										
											2008-06-24 22:58:06 +00:00
										 |  |  |       endif | 
					
						
							|  |  |  |     endif | 
					
						
							| 
									
										
										
										
											2006-02-04 00:59:56 +00:00
										 |  |  |     if len(items) == 1 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |       # Completing one word and it's a local variable: May add '[', '.' or | 
					
						
							|  |  |  |       # '->'. | 
					
						
							|  |  |  |       var match: string = items[0] | 
					
						
							|  |  |  |       var kind: string = 'v' | 
					
						
							|  |  |  |       if match(line, '\<' .. match .. '\s*\[') > 0 | 
					
						
							|  |  |  |         match ..= '[' | 
					
						
							| 
									
										
										
										
											2006-02-04 00:59:56 +00:00
										 |  |  |       else | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |         res = line[: col - 1]->Nextitem([''], 0, true) | 
					
						
							|  |  |  |         if len(res) > 0 | 
					
						
							|  |  |  |           # There are members, thus add "." or "->". | 
					
						
							|  |  |  |           if match(line, '\*[ \t(]*' .. match .. '\>') > 0 | 
					
						
							|  |  |  |             match ..= '->' | 
					
						
							|  |  |  |           else | 
					
						
							|  |  |  |             match ..= '.' | 
					
						
							|  |  |  |           endif | 
					
						
							|  |  |  |         endif | 
					
						
							| 
									
										
										
										
											2006-02-04 00:59:56 +00:00
										 |  |  |       endif | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |       res = [{match: match, tagline: '', kind: kind, info: line}] | 
					
						
							| 
									
										
										
										
											2018-09-02 21:07:30 +02:00
										 |  |  |     elseif len(items) == arrays + 1 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |       # Completing one word and it's a local array variable: build tagline | 
					
						
							|  |  |  |       # from declaration line | 
					
						
							|  |  |  |       var match: string = items[0] | 
					
						
							|  |  |  |       var kind: string = 'v' | 
					
						
							|  |  |  |       var tagline: string = "\t/^" .. line .. '$/' | 
					
						
							|  |  |  |       res = [{match: match, tagline: tagline, kind: kind, info: line}] | 
					
						
							| 
									
										
										
										
											2006-02-04 00:59:56 +00:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |       # Completing "var.", "var.something", etc. | 
					
						
							|  |  |  |       res = line[: col - 1]->Nextitem(items[1 :], 0, true) | 
					
						
							| 
									
										
										
										
											2006-02-04 00:59:56 +00:00
										 |  |  |     endif | 
					
						
							|  |  |  |   endif | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-02 21:07:30 +02:00
										 |  |  |   if len(items) == 1 || len(items) == arrays + 1 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |     # Only one part, no "." or "->": complete from tags file. | 
					
						
							|  |  |  |     var tags: list<dict<any>> | 
					
						
							| 
									
										
										
										
											2018-09-02 21:07:30 +02:00
										 |  |  |     if len(items) == 1 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |       tags = taglist('^' .. base) | 
					
						
							| 
									
										
										
										
											2018-09-02 21:07:30 +02:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |       tags = taglist('^' .. items[0] .. '$') | 
					
						
							| 
									
										
										
										
											2018-09-02 21:07:30 +02:00
										 |  |  |     endif | 
					
						
							| 
									
										
										
										
											2006-03-11 21:35:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |     tags | 
					
						
							|  |  |  |       # Remove members, these can't appear without something in front. | 
					
						
							|  |  |  |       ->filter((_, v: dict<any>): bool => | 
					
						
							|  |  |  |                 v->has_key('kind') ? v.kind != 'm' : true) | 
					
						
							|  |  |  |       # Remove static matches in other files. | 
					
						
							|  |  |  |       ->filter((_, v: dict<any>): bool => | 
					
						
							|  |  |  |                  !v->has_key('static') | 
					
						
							|  |  |  |               || !v['static'] | 
					
						
							|  |  |  |               || bufnr('%') == bufnr(v['filename'])) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-31 15:40:56 +00:00
										 |  |  |     res = res->extend(tags->map((_, v: dict<any>) => Tag2item(v))) | 
					
						
							| 
									
										
										
										
											2005-09-09 19:52:02 +00:00
										 |  |  |   endif | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if len(res) == 0 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |     # Find the variable in the tags file(s) | 
					
						
							|  |  |  |     var diclist: list<dict<any>> = taglist('^' .. items[0] .. '$') | 
					
						
							|  |  |  |       # Remove members, these can't appear without something in front. | 
					
						
							|  |  |  |       ->filter((_, v: dict<string>): bool => | 
					
						
							|  |  |  |                 v->has_key('kind') ? v.kind != 'm' : true) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     res = [] | 
					
						
							|  |  |  |     for i: number in len(diclist)->range() | 
					
						
							|  |  |  |       # New ctags has the "typeref" field.  Patched version has "typename". | 
					
						
							|  |  |  |       if diclist[i]->has_key('typename') | 
					
						
							| 
									
										
										
										
											2022-01-31 15:40:56 +00:00
										 |  |  |         res = res->extend(diclist[i]['typename']->StructMembers(items[1 :], true)) | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |       elseif diclist[i]->has_key('typeref') | 
					
						
							| 
									
										
										
										
											2022-01-31 15:40:56 +00:00
										 |  |  |         res = res->extend(diclist[i]['typeref']->StructMembers(items[1 :], true)) | 
					
						
							| 
									
										
										
										
											2005-09-09 19:52:02 +00:00
										 |  |  |       endif | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |       # For a variable use the command, which must be a search pattern that | 
					
						
							|  |  |  |       # shows the declaration of the variable. | 
					
						
							| 
									
										
										
										
											2005-09-05 22:14:46 +00:00
										 |  |  |       if diclist[i]['kind'] == 'v' | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |         var line: string = diclist[i]['cmd'] | 
					
						
							|  |  |  |         if line[: 1] == '/^' | 
					
						
							|  |  |  |           var col: number = line->charidx(match(line, '\<' .. items[0] .. '\>')) | 
					
						
							| 
									
										
										
										
											2022-01-31 15:40:56 +00:00
										 |  |  |           res = res->extend(line[2 : col - 1]->Nextitem(items[1 :], 0, true)) | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |         endif | 
					
						
							| 
									
										
										
										
											2005-09-05 22:14:46 +00:00
										 |  |  |       endif | 
					
						
							|  |  |  |     endfor | 
					
						
							|  |  |  |   endif | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |   if len(res) == 0 && items[0]->searchdecl(true) == 0 | 
					
						
							|  |  |  |     # Found, now figure out the type. | 
					
						
							|  |  |  |     # TODO: join previous line if it makes sense | 
					
						
							|  |  |  |     var line: string = getline('.') | 
					
						
							|  |  |  |     var col: number = charcol('.') | 
					
						
							|  |  |  |     res = line[: col - 1]->Nextitem(items[1 :], 0, true) | 
					
						
							| 
									
										
										
										
											2005-09-13 21:20:47 +00:00
										 |  |  |   endif | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |   # If the last item(s) are [...] they need to be added to the matches. | 
					
						
							|  |  |  |   var last: number = len(items) - 1 | 
					
						
							|  |  |  |   var brackets: string = '' | 
					
						
							| 
									
										
										
										
											2006-02-04 00:59:56 +00:00
										 |  |  |   while last >= 0 | 
					
						
							|  |  |  |     if items[last][0] != '[' | 
					
						
							|  |  |  |       break | 
					
						
							|  |  |  |     endif | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |     brackets = items[last] .. brackets | 
					
						
							|  |  |  |     --last | 
					
						
							| 
									
										
										
										
											2006-02-04 00:59:56 +00:00
										 |  |  |   endwhile | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |   return res->map((_, v: dict<any>): dict<string> => Tagline2item(v, brackets)) | 
					
						
							|  |  |  | enddef | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def GetAddition( # {{{1 | 
					
						
							| 
									
										
										
										
											2022-01-31 15:40:56 +00:00
										 |  |  |     line: string, | 
					
						
							|  |  |  |     match: string, | 
					
						
							|  |  |  |     memarg: list<dict<any>>, | 
					
						
							|  |  |  |     bracket: bool): string | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |   # Guess if the item is an array. | 
					
						
							|  |  |  |   if bracket && match(line, match .. '\s*\[') > 0 | 
					
						
							| 
									
										
										
										
											2006-02-04 00:59:56 +00:00
										 |  |  |     return '[' | 
					
						
							|  |  |  |   endif | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |   # Check if the item has members. | 
					
						
							|  |  |  |   if SearchMembers(memarg, [''], false)->len() > 0 | 
					
						
							|  |  |  |     # If there is a '*' before the name use "->". | 
					
						
							|  |  |  |     if match(line, '\*[ \t(]*' .. match .. '\>') > 0 | 
					
						
							| 
									
										
										
										
											2006-02-04 00:59:56 +00:00
										 |  |  |       return '->' | 
					
						
							| 
									
										
										
										
											2005-09-13 21:20:47 +00:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2006-02-04 00:59:56 +00:00
										 |  |  |       return '.' | 
					
						
							| 
									
										
										
										
											2005-09-13 21:20:47 +00:00
										 |  |  |     endif | 
					
						
							| 
									
										
										
										
											2005-09-09 19:52:02 +00:00
										 |  |  |   endif | 
					
						
							| 
									
										
										
										
											2006-02-04 00:59:56 +00:00
										 |  |  |   return '' | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  | enddef | 
					
						
							| 
									
										
										
										
											2005-09-09 19:52:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  | def Tag2item(val: dict<any>): dict<any> # {{{1 | 
					
						
							|  |  |  | # Turn the tag info "val" into an item for completion. | 
					
						
							|  |  |  | # "val" is is an item in the list returned by taglist(). | 
					
						
							|  |  |  | # If it is a variable we may add "." or "->".  Don't do it for other types, | 
					
						
							|  |  |  | # such as a typedef, by not including the info that GetAddition() uses. | 
					
						
							|  |  |  |   var res: dict<any> = {match: val['name']} | 
					
						
							| 
									
										
										
										
											2006-03-11 21:35:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |   res['extra'] = Tagcmd2extra(val['cmd'], val['name'], val['filename']) | 
					
						
							| 
									
										
										
										
											2006-03-11 21:35:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |   var s: string = Dict2info(val) | 
					
						
							| 
									
										
										
										
											2006-03-11 21:35:11 +00:00
										 |  |  |   if s != '' | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |     res['info'] = s | 
					
						
							| 
									
										
										
										
											2006-03-11 21:35:11 +00:00
										 |  |  |   endif | 
					
						
							| 
									
										
										
										
											2006-02-10 23:23:57 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |   res['tagline'] = '' | 
					
						
							|  |  |  |   if val->has_key('kind') | 
					
						
							|  |  |  |     var kind: string = val['kind'] | 
					
						
							|  |  |  |     res['kind'] = kind | 
					
						
							| 
									
										
										
										
											2006-03-11 21:35:11 +00:00
										 |  |  |     if kind == 'v' | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |       res['tagline'] = "\t" .. val['cmd'] | 
					
						
							|  |  |  |       res['dict'] = val | 
					
						
							| 
									
										
										
										
											2006-03-11 21:35:11 +00:00
										 |  |  |     elseif kind == 'f' | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |       res['match'] = val['name'] .. '(' | 
					
						
							| 
									
										
										
										
											2006-01-30 00:14:18 +00:00
										 |  |  |     endif | 
					
						
							|  |  |  |   endif | 
					
						
							| 
									
										
										
										
											2006-03-11 21:35:11 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return res | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  | enddef | 
					
						
							| 
									
										
										
										
											2006-02-04 00:59:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  | def Dict2info(dict: dict<any>): string # {{{1 | 
					
						
							|  |  |  | # Use all the items in dictionary for the "info" entry. | 
					
						
							|  |  |  |   var info: string = '' | 
					
						
							|  |  |  |   for k: string in dict->keys()->sort() | 
					
						
							|  |  |  |     info  ..= k .. repeat(' ', 10 - strlen(k)) | 
					
						
							| 
									
										
										
										
											2006-03-24 22:46:53 +00:00
										 |  |  |     if k == 'cmd' | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |       info ..= dict['cmd'] | 
					
						
							|  |  |  |         ->matchstr('/^\s*\zs.*\ze$/') | 
					
						
							|  |  |  |         ->substitute('\\\(.\)', '\1', 'g') | 
					
						
							| 
									
										
										
										
											2006-03-24 22:46:53 +00:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |       var dictk: any = dict[k] | 
					
						
							|  |  |  |       if typename(dictk) != 'string' | 
					
						
							|  |  |  |         info ..= dictk->string() | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |         info ..= dictk | 
					
						
							|  |  |  |       endif | 
					
						
							| 
									
										
										
										
											2006-03-24 22:46:53 +00:00
										 |  |  |     endif | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |     info ..= "\n" | 
					
						
							| 
									
										
										
										
											2006-03-24 22:46:53 +00:00
										 |  |  |   endfor | 
					
						
							|  |  |  |   return info | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  | enddef | 
					
						
							| 
									
										
										
										
											2006-03-24 22:46:53 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  | def ParseTagline(line: string): dict<any> # {{{1 | 
					
						
							|  |  |  | # Parse a tag line and return a dictionary with items like taglist() | 
					
						
							|  |  |  |   var l: list<string> = split(line, "\t") | 
					
						
							|  |  |  |   var d: dict<any> | 
					
						
							| 
									
										
										
										
											2006-03-24 22:46:53 +00:00
										 |  |  |   if len(l) >= 3 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |     d['name'] = l[0] | 
					
						
							|  |  |  |     d['filename'] = l[1] | 
					
						
							|  |  |  |     d['cmd'] = l[2] | 
					
						
							|  |  |  |     var n: number = 2 | 
					
						
							| 
									
										
										
										
											2006-03-24 22:46:53 +00:00
										 |  |  |     if l[2] =~ '^/' | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |       # Find end of cmd, it may contain Tabs. | 
					
						
							| 
									
										
										
										
											2006-03-24 22:46:53 +00:00
										 |  |  |       while n < len(l) && l[n] !~ '/;"$' | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |         ++n | 
					
						
							|  |  |  |         d['cmd'] ..= '  ' .. l[n] | 
					
						
							| 
									
										
										
										
											2006-03-24 22:46:53 +00:00
										 |  |  |       endwhile | 
					
						
							|  |  |  |     endif | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |     for i: number in range(n + 1, len(l) - 1) | 
					
						
							| 
									
										
										
										
											2006-03-24 22:46:53 +00:00
										 |  |  |       if l[i] == 'file:' | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |         d['static'] = 1 | 
					
						
							| 
									
										
										
										
											2006-03-24 22:46:53 +00:00
										 |  |  |       elseif l[i] !~ ':' | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |         d['kind'] = l[i] | 
					
						
							| 
									
										
										
										
											2006-03-24 22:46:53 +00:00
										 |  |  |       else | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |         d[l[i]->matchstr('[^:]*')] = l[i]->matchstr(':\zs.*') | 
					
						
							| 
									
										
										
										
											2006-03-24 22:46:53 +00:00
										 |  |  |       endif | 
					
						
							|  |  |  |     endfor | 
					
						
							|  |  |  |   endif | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return d | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  | enddef | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def Tagline2item(val: dict<any>, brackets: string): dict<string> # {{{1 | 
					
						
							|  |  |  | # Turn a match item "val" into an item for completion. | 
					
						
							|  |  |  | # "val['match']" is the matching item. | 
					
						
							|  |  |  | # "val['tagline']" is the tagline in which the last part was found. | 
					
						
							|  |  |  |   var line: string = val['tagline'] | 
					
						
							|  |  |  |   var add: string = GetAddition(line, val['match'], [val], brackets == '') | 
					
						
							|  |  |  |   var res: dict<string> = {word: val['match'] .. brackets .. add} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if val->has_key('info') | 
					
						
							|  |  |  |     # Use info from Tag2item(). | 
					
						
							|  |  |  |     res['info'] = val['info'] | 
					
						
							| 
									
										
										
										
											2006-03-11 21:35:11 +00:00
										 |  |  |   else | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |     # Parse the tag line and add each part to the "info" entry. | 
					
						
							|  |  |  |     var s: string = ParseTagline(line)->Dict2info() | 
					
						
							| 
									
										
										
										
											2006-03-11 21:35:11 +00:00
										 |  |  |     if s != '' | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |       res['info'] = s | 
					
						
							| 
									
										
										
										
											2006-03-11 21:35:11 +00:00
										 |  |  |     endif | 
					
						
							|  |  |  |   endif | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |   if val->has_key('kind') | 
					
						
							|  |  |  |     res['kind'] = val['kind'] | 
					
						
							| 
									
										
										
										
											2006-03-11 21:35:11 +00:00
										 |  |  |   elseif add == '(' | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |     res['kind'] = 'f' | 
					
						
							| 
									
										
										
										
											2006-03-11 21:35:11 +00:00
										 |  |  |   else | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |     var s: string = line->matchstr('\t\(kind:\)\=\zs\S\ze\(\t\|$\)') | 
					
						
							| 
									
										
										
										
											2006-03-11 21:35:11 +00:00
										 |  |  |     if s != '' | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |       res['kind'] = s | 
					
						
							| 
									
										
										
										
											2006-03-11 21:35:11 +00:00
										 |  |  |     endif | 
					
						
							|  |  |  |   endif | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |   if val->has_key('extra') | 
					
						
							|  |  |  |     res['menu'] = val['extra'] | 
					
						
							| 
									
										
										
										
											2006-03-11 21:35:11 +00:00
										 |  |  |     return res | 
					
						
							| 
									
										
										
										
											2006-02-08 09:20:24 +00:00
										 |  |  |   endif | 
					
						
							| 
									
										
										
										
											2006-02-10 23:23:57 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |   # Isolate the command after the tag and filename. | 
					
						
							|  |  |  |   var s: string = line->matchstr('[^\t]*\t[^\t]*\t\zs\(/^.*$/\|[^\t]*\)\ze\(;"\t\|\t\|$\)') | 
					
						
							| 
									
										
										
										
											2006-02-10 23:23:57 +00:00
										 |  |  |   if s != '' | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |     res['menu'] = s->Tagcmd2extra(val['match'], line->matchstr('[^\t]*\t\zs[^\t]*\ze\t')) | 
					
						
							| 
									
										
										
										
											2006-02-10 23:23:57 +00:00
										 |  |  |   endif | 
					
						
							| 
									
										
										
										
											2006-03-11 21:35:11 +00:00
										 |  |  |   return res | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  | enddef | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def Tagcmd2extra( # {{{1 | 
					
						
							| 
									
										
										
										
											2022-01-31 15:40:56 +00:00
										 |  |  |     cmd: string, | 
					
						
							|  |  |  |     name: string, | 
					
						
							|  |  |  |     fname: string): string | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  | # Turn a command from a tag line to something that is useful in the menu | 
					
						
							|  |  |  |   var x: string | 
					
						
							|  |  |  |   if cmd =~ '^/^' | 
					
						
							|  |  |  |     # The command is a search command, useful to see what it is. | 
					
						
							|  |  |  |     x = cmd | 
					
						
							|  |  |  |       ->matchstr('^/^\s*\zs.*\ze$/') | 
					
						
							|  |  |  |       ->substitute('\<' .. name .. '\>', '@@', '') | 
					
						
							|  |  |  |       ->substitute('\\\(.\)', '\1', 'g') | 
					
						
							|  |  |  |       .. ' - ' .. fname | 
					
						
							|  |  |  |   elseif cmd =~ '^\d*$' | 
					
						
							|  |  |  |     # The command is a line number, the file name is more useful. | 
					
						
							|  |  |  |     x = fname .. ' - ' .. cmd | 
					
						
							| 
									
										
										
										
											2006-02-10 23:23:57 +00:00
										 |  |  |   else | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |     # Not recognized, use command and file name. | 
					
						
							|  |  |  |     x = cmd .. ' - ' .. fname | 
					
						
							| 
									
										
										
										
											2006-02-10 23:23:57 +00:00
										 |  |  |   endif | 
					
						
							|  |  |  |   return x | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  | enddef | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def Nextitem( # {{{1 | 
					
						
							| 
									
										
										
										
											2022-01-31 15:40:56 +00:00
										 |  |  |     lead: string, | 
					
						
							|  |  |  |     items: list<string>, | 
					
						
							|  |  |  |     depth: number, | 
					
						
							|  |  |  |     all: bool): list<dict<string>> | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  | # Find composing type in "lead" and match items[0] with it. | 
					
						
							|  |  |  | # Repeat this recursively for items[1], if it's there. | 
					
						
							|  |  |  | # When resolving typedefs "depth" is used to avoid infinite recursion. | 
					
						
							|  |  |  | # Return the list of matches. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # Use the text up to the variable name and split it in tokens. | 
					
						
							|  |  |  |   var tokens: list<string> = split(lead, '\s\+\|\<') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # Try to recognize the type of the variable.  This is rough guessing... | 
					
						
							|  |  |  |   var res: list<dict<string>> | 
					
						
							|  |  |  |   for tidx: number in len(tokens)->range() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Skip tokens starting with a non-ID character. | 
					
						
							| 
									
										
										
										
											2006-03-09 22:37:52 +00:00
										 |  |  |     if tokens[tidx] !~ '^\h' | 
					
						
							|  |  |  |       continue | 
					
						
							|  |  |  |     endif | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |     # Recognize "struct foobar" and "union foobar". | 
					
						
							|  |  |  |     # Also do "class foobar" when it's C++ after all (doesn't work very well | 
					
						
							|  |  |  |     # though). | 
					
						
							|  |  |  |     if (tokens[tidx] == 'struct' | 
					
						
							|  |  |  |       || tokens[tidx] == 'union' | 
					
						
							|  |  |  |       || tokens[tidx] == 'class') | 
					
						
							|  |  |  |       && tidx + 1 < len(tokens) | 
					
						
							|  |  |  |       res = StructMembers(tokens[tidx] .. ':' .. tokens[tidx + 1], items, all) | 
					
						
							| 
									
										
										
										
											2005-09-05 22:14:46 +00:00
										 |  |  |       break | 
					
						
							|  |  |  |     endif | 
					
						
							| 
									
										
										
										
											2005-09-07 21:21:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |     # TODO: add more reserved words | 
					
						
							|  |  |  |     if ['int', 'short', 'char', 'float', | 
					
						
							|  |  |  |         'double', 'static', 'unsigned', 'extern']->index(tokens[tidx]) >= 0 | 
					
						
							| 
									
										
										
										
											2005-09-09 19:52:02 +00:00
										 |  |  |       continue | 
					
						
							|  |  |  |     endif | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |     # Use the tags file to find out if this is a typedef. | 
					
						
							|  |  |  |     var diclist: list<dict<any>> = taglist('^' .. tokens[tidx] .. '$') | 
					
						
							|  |  |  |     for tagidx: number in len(diclist)->range() | 
					
						
							|  |  |  |       var item: dict<any> = diclist[tagidx] | 
					
						
							| 
									
										
										
										
											2006-03-11 21:35:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |       # New ctags has the "typeref" field.  Patched version has "typename". | 
					
						
							|  |  |  |       if item->has_key('typeref') | 
					
						
							| 
									
										
										
										
											2022-01-31 15:40:56 +00:00
										 |  |  |         res = res->extend(item['typeref']->StructMembers(items, all)) | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |         continue | 
					
						
							| 
									
										
										
										
											2006-03-24 22:46:53 +00:00
										 |  |  |       endif | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |       if item->has_key('typename') | 
					
						
							| 
									
										
										
										
											2022-01-31 15:40:56 +00:00
										 |  |  |         res = res->extend(item['typename']->StructMembers(items, all)) | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |         continue | 
					
						
							| 
									
										
										
										
											2005-09-13 21:20:47 +00:00
										 |  |  |       endif | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |       # Only handle typedefs here. | 
					
						
							| 
									
										
										
										
											2006-03-11 21:35:11 +00:00
										 |  |  |       if item['kind'] != 't' | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |         continue | 
					
						
							| 
									
										
										
										
											2006-03-11 21:35:11 +00:00
										 |  |  |       endif | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |       # Skip matches local to another file. | 
					
						
							|  |  |  |       if item->has_key('static') && item['static'] | 
					
						
							|  |  |  |         && bufnr('%') != bufnr(item['filename']) | 
					
						
							|  |  |  |         continue | 
					
						
							| 
									
										
										
										
											2005-09-09 19:52:02 +00:00
										 |  |  |       endif | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |       # For old ctags we recognize "typedef struct aaa" and | 
					
						
							|  |  |  |       # "typedef union bbb" in the tags file command. | 
					
						
							|  |  |  |       var cmd: string = item['cmd'] | 
					
						
							|  |  |  |       var ei: number = cmd->charidx(matchend(cmd, 'typedef\s\+')) | 
					
						
							| 
									
										
										
										
											2005-09-13 21:20:47 +00:00
										 |  |  |       if ei > 1 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |         var cmdtokens: list<string> = cmd[ei :]->split('\s\+\|\<') | 
					
						
							|  |  |  |         if len(cmdtokens) > 1 | 
					
						
							|  |  |  |           if cmdtokens[0] == 'struct' | 
					
						
							|  |  |  |             || cmdtokens[0] == 'union' | 
					
						
							|  |  |  |             || cmdtokens[0] == 'class' | 
					
						
							|  |  |  |             var name: string = '' | 
					
						
							|  |  |  |             # Use the first identifier after the "struct" or "union" | 
					
						
							|  |  |  |             for ti: number in (len(cmdtokens) - 1)->range() | 
					
						
							|  |  |  |               if cmdtokens[ti] =~ '^\w' | 
					
						
							|  |  |  |                 name = cmdtokens[ti] | 
					
						
							|  |  |  |                 break | 
					
						
							|  |  |  |               endif | 
					
						
							|  |  |  |             endfor | 
					
						
							|  |  |  |             if name != '' | 
					
						
							| 
									
										
										
										
											2022-01-31 15:40:56 +00:00
										 |  |  |               res = res->extend(StructMembers(cmdtokens[0] .. ':' .. name, items, all)) | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |             endif | 
					
						
							|  |  |  |           elseif depth < 10 | 
					
						
							|  |  |  |             # Could be "typedef other_T some_T". | 
					
						
							| 
									
										
										
										
											2022-01-31 15:40:56 +00:00
										 |  |  |             res = res->extend(cmdtokens[0]->Nextitem(items, depth + 1, all)) | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |           endif | 
					
						
							|  |  |  |         endif | 
					
						
							| 
									
										
										
										
											2005-09-07 21:21:14 +00:00
										 |  |  |       endif | 
					
						
							|  |  |  |     endfor | 
					
						
							| 
									
										
										
										
											2005-09-09 19:52:02 +00:00
										 |  |  |     if len(res) > 0 | 
					
						
							| 
									
										
										
										
											2005-09-07 21:21:14 +00:00
										 |  |  |       break | 
					
						
							| 
									
										
										
										
											2005-09-05 22:14:46 +00:00
										 |  |  |     endif | 
					
						
							| 
									
										
										
										
											2005-09-09 19:52:02 +00:00
										 |  |  |   endfor | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return res | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  | enddef | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def StructMembers( # {{{1 | 
					
						
							| 
									
										
										
										
											2022-01-31 15:40:56 +00:00
										 |  |  |     atypename: string, | 
					
						
							|  |  |  |     items: list<string>, | 
					
						
							|  |  |  |     all: bool): list<dict<string>> | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | # Search for members of structure "typename" in tags files. | 
					
						
							|  |  |  | # Return a list with resulting matches. | 
					
						
							|  |  |  | # Each match is a dictionary with "match" and "tagline" entries. | 
					
						
							|  |  |  | # When "all" is true find all, otherwise just return 1 if there is any member. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # Todo: What about local structures? | 
					
						
							|  |  |  |   var fnames: string = tagfiles() | 
					
						
							|  |  |  |     ->map((_, v: string) => escape(v, ' \#%')) | 
					
						
							|  |  |  |     ->join() | 
					
						
							| 
									
										
										
										
											2005-09-09 19:52:02 +00:00
										 |  |  |   if fnames == '' | 
					
						
							| 
									
										
										
										
											2005-09-13 21:20:47 +00:00
										 |  |  |     return [] | 
					
						
							| 
									
										
										
										
											2005-09-09 19:52:02 +00:00
										 |  |  |   endif | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |   var typename: string = atypename | 
					
						
							|  |  |  |   var qflist: list<dict<any>> | 
					
						
							|  |  |  |   var cached: number = 0 | 
					
						
							|  |  |  |   var n: string | 
					
						
							|  |  |  |   if !all | 
					
						
							|  |  |  |     n = '1'  # stop at first found match | 
					
						
							|  |  |  |     if grepCache->has_key(typename) | 
					
						
							|  |  |  |       qflist = grepCache[typename] | 
					
						
							|  |  |  |       cached = 1 | 
					
						
							| 
									
										
										
										
											2006-03-09 22:37:52 +00:00
										 |  |  |     endif | 
					
						
							| 
									
										
										
										
											2006-03-07 22:38:47 +00:00
										 |  |  |   else | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |     n = '' | 
					
						
							| 
									
										
										
										
											2006-03-07 22:38:47 +00:00
										 |  |  |   endif | 
					
						
							| 
									
										
										
										
											2006-03-09 22:37:52 +00:00
										 |  |  |   if !cached | 
					
						
							|  |  |  |     while 1 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |       execute 'silent! keepjumps noautocmd ' | 
					
						
							|  |  |  |         .. n .. 'vimgrep ' .. '/\t' .. typename .. '\(\t\|$\)/j ' | 
					
						
							|  |  |  |         .. fnames | 
					
						
							| 
									
										
										
										
											2006-03-09 22:37:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |       qflist = getqflist() | 
					
						
							|  |  |  |       if len(qflist) > 0 || match(typename, '::') < 0 | 
					
						
							|  |  |  |         break | 
					
						
							| 
									
										
										
										
											2006-03-09 22:37:52 +00:00
										 |  |  |       endif | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |       # No match for "struct:context::name", remove "context::" and try again. | 
					
						
							|  |  |  |       typename = typename->substitute(':[^:]*::', ':', '') | 
					
						
							| 
									
										
										
										
											2006-03-09 22:37:52 +00:00
										 |  |  |     endwhile | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |     if !all | 
					
						
							|  |  |  |       # Store the result to be able to use it again later. | 
					
						
							|  |  |  |       grepCache[typename] = qflist | 
					
						
							| 
									
										
										
										
											2005-09-09 19:52:02 +00:00
										 |  |  |     endif | 
					
						
							| 
									
										
										
										
											2006-03-09 22:37:52 +00:00
										 |  |  |   endif | 
					
						
							| 
									
										
										
										
											2005-09-09 19:52:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |   # Skip over [...] items | 
					
						
							|  |  |  |   var idx: number = 0 | 
					
						
							|  |  |  |   var target: string | 
					
						
							| 
									
										
										
										
											2018-09-02 21:07:30 +02:00
										 |  |  |   while 1 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |     if idx >= len(items) | 
					
						
							|  |  |  |       target = ''  # No further items, matching all members | 
					
						
							| 
									
										
										
										
											2018-09-02 21:07:30 +02:00
										 |  |  |       break | 
					
						
							|  |  |  |     endif | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |     if items[idx][0] != '[' | 
					
						
							|  |  |  |       target = items[idx] | 
					
						
							| 
									
										
										
										
											2018-09-02 21:07:30 +02:00
										 |  |  |       break | 
					
						
							|  |  |  |     endif | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |     ++idx | 
					
						
							| 
									
										
										
										
											2018-09-02 21:07:30 +02:00
										 |  |  |   endwhile | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |   # Put matching members in matches[]. | 
					
						
							|  |  |  |   var matches: list<dict<string>> | 
					
						
							|  |  |  |   for l: dict<any> in qflist | 
					
						
							|  |  |  |     var memb: string = l['text']->matchstr('[^\t]*') | 
					
						
							|  |  |  |     if memb =~ '^' .. target | 
					
						
							|  |  |  |       # Skip matches local to another file. | 
					
						
							|  |  |  |       if match(l['text'], "\tfile:") < 0 | 
					
						
							|  |  |  |         || bufnr('%') == l['text']->matchstr('\t\zs[^\t]*')->bufnr() | 
					
						
							|  |  |  |         var item: dict<string> = {match: memb, tagline: l['text']} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Add the kind of item. | 
					
						
							|  |  |  |         var s: string = l['text']->matchstr('\t\(kind:\)\=\zs\S\ze\(\t\|$\)') | 
					
						
							|  |  |  |         if s != '' | 
					
						
							|  |  |  |           item['kind'] = s | 
					
						
							|  |  |  |           if s == 'f' | 
					
						
							|  |  |  |             item['match'] = memb .. '(' | 
					
						
							|  |  |  |           endif | 
					
						
							|  |  |  |         endif | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         matches->add(item) | 
					
						
							| 
									
										
										
										
											2006-03-11 21:35:11 +00:00
										 |  |  |       endif | 
					
						
							| 
									
										
										
										
											2005-09-09 19:52:02 +00:00
										 |  |  |     endif | 
					
						
							| 
									
										
										
										
											2005-09-05 22:14:46 +00:00
										 |  |  |   endfor | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-09-13 21:20:47 +00:00
										 |  |  |   if len(matches) > 0 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |     # Skip over next [...] items | 
					
						
							|  |  |  |     ++idx | 
					
						
							| 
									
										
										
										
											2006-02-04 00:59:56 +00:00
										 |  |  |     while 1 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |       if idx >= len(items) | 
					
						
							|  |  |  |         return matches  # No further items, return the result. | 
					
						
							| 
									
										
										
										
											2006-02-04 00:59:56 +00:00
										 |  |  |       endif | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |       if items[idx][0] != '[' | 
					
						
							|  |  |  |         break | 
					
						
							| 
									
										
										
										
											2006-02-04 00:59:56 +00:00
										 |  |  |       endif | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |       ++idx | 
					
						
							| 
									
										
										
										
											2006-02-04 00:59:56 +00:00
										 |  |  |     endwhile | 
					
						
							| 
									
										
										
										
											2005-09-07 21:21:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |     # More items following.  For each of the possible members find the | 
					
						
							|  |  |  |     # matching following members. | 
					
						
							|  |  |  |     return SearchMembers(matches, items[idx :], all) | 
					
						
							| 
									
										
										
										
											2005-09-07 21:21:14 +00:00
										 |  |  |   endif | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |   # Failed to find anything. | 
					
						
							| 
									
										
										
										
											2005-09-07 21:21:14 +00:00
										 |  |  |   return [] | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  | enddef | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def SearchMembers( # {{{1 | 
					
						
							| 
									
										
										
										
											2022-01-31 15:40:56 +00:00
										 |  |  |     matches: list<dict<any>>, | 
					
						
							|  |  |  |     items: list<string>, | 
					
						
							|  |  |  |     all: bool): list<dict<string>> | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | # For matching members, find matches for following items. | 
					
						
							|  |  |  | # When "all" is true find all, otherwise just return 1 if there is any member. | 
					
						
							|  |  |  |   var res: list<dict<string>> | 
					
						
							|  |  |  |   for i: number in len(matches)->range() | 
					
						
							|  |  |  |     var typename: string = '' | 
					
						
							|  |  |  |     var line: string | 
					
						
							|  |  |  |     if matches[i]->has_key('dict') | 
					
						
							|  |  |  |       if matches[i]['dict']->has_key('typename') | 
					
						
							|  |  |  |         typename = matches[i]['dict']['typename'] | 
					
						
							|  |  |  |       elseif matches[i]['dict']->has_key('typeref') | 
					
						
							|  |  |  |         typename = matches[i]['dict']['typeref'] | 
					
						
							| 
									
										
										
										
											2006-01-30 00:14:18 +00:00
										 |  |  |       endif | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |       line = "\t" .. matches[i]['dict']['cmd'] | 
					
						
							| 
									
										
										
										
											2006-01-30 00:14:18 +00:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |       line = matches[i]['tagline'] | 
					
						
							|  |  |  |       var eb: number = matchend(line, '\ttypename:') | 
					
						
							|  |  |  |       var e: number = charidx(line, eb) | 
					
						
							| 
									
										
										
										
											2006-03-24 22:46:53 +00:00
										 |  |  |       if e < 0 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |         eb = matchend(line, '\ttyperef:') | 
					
						
							|  |  |  |         e = charidx(line, eb) | 
					
						
							| 
									
										
										
										
											2006-03-24 22:46:53 +00:00
										 |  |  |       endif | 
					
						
							| 
									
										
										
										
											2006-01-30 00:14:18 +00:00
										 |  |  |       if e > 0 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |         # Use typename field | 
					
						
							|  |  |  |         typename = line->matchstr('[^\t]*', eb) | 
					
						
							| 
									
										
										
										
											2006-01-30 00:14:18 +00:00
										 |  |  |       endif | 
					
						
							|  |  |  |     endif | 
					
						
							| 
									
										
										
										
											2006-03-07 22:38:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-01-30 00:14:18 +00:00
										 |  |  |     if typename != '' | 
					
						
							| 
									
										
										
										
											2022-01-31 15:40:56 +00:00
										 |  |  |       res = res->extend(StructMembers(typename, items, all)) | 
					
						
							| 
									
										
										
										
											2005-09-13 21:20:47 +00:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |       # Use the search command (the declaration itself). | 
					
						
							|  |  |  |       var sb: number = line->match('\t\zs/^') | 
					
						
							|  |  |  |       var s: number = charidx(line, sb) | 
					
						
							| 
									
										
										
										
											2005-09-13 21:20:47 +00:00
										 |  |  |       if s > 0 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |         var e: number = line | 
					
						
							|  |  |  |           ->charidx(match(line, '\<' .. matches[i]['match'] .. '\>', sb)) | 
					
						
							|  |  |  |         if e > 0 | 
					
						
							| 
									
										
										
										
											2022-01-31 15:40:56 +00:00
										 |  |  |           res = res->extend(line[s : e - 1]->Nextitem(items, 0, all)) | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |         endif | 
					
						
							| 
									
										
										
										
											2005-09-13 21:20:47 +00:00
										 |  |  |       endif | 
					
						
							|  |  |  |     endif | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  |     if !all && len(res) > 0 | 
					
						
							| 
									
										
										
										
											2006-03-07 22:38:47 +00:00
										 |  |  |       break | 
					
						
							|  |  |  |     endif | 
					
						
							| 
									
										
										
										
											2005-09-13 21:20:47 +00:00
										 |  |  |   endfor | 
					
						
							|  |  |  |   return res | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  | enddef | 
					
						
							|  |  |  | #}}}1 | 
					
						
							| 
									
										
										
										
											2020-04-10 22:10:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 21:33:07 +00:00
										 |  |  | # vim: noet sw=2 sts=2 |