Skip to content

Commit 989cd0e

Browse files
committed
Review and cleanup autoload functions
1 parent 7344e5b commit 989cd0e

File tree

1 file changed

+70
-20
lines changed

1 file changed

+70
-20
lines changed

autoload/verilog_systemverilog.vim

Lines changed: 70 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,27 @@
44

55
"------------------------------------------------------------------------
66
" Omni completion functions
7+
" {{{
78
"
89
" Requires ctags from:
910
" https://github.com/fishman/ctags
1011
" https://github.com/exuberant-ctags/ctags
1112
" ctags must be run with --extra=+q
12-
" {{{
1313
function! verilog_systemverilog#Complete(findstart, base)
14+
" {{{
1415
"------------------------------------------------------------------------
1516
" Phase 1: Find and return prefix of completion
17+
" s:word contains the keyword to be completed
18+
" s:prefix contains the "name." prefix preceding s:word
19+
" s:instname name of the instance ("." completion only)
20+
" s:module name of the instance ("." completion only)
1621
if a:findstart
1722
let linenr = line('.')
1823
let line = getline('.')
1924
let start = col('.')
2025
let prefixpos = -1
2126
let s:instname = ''
22-
let s:insttype = ''
27+
let s:module = ''
2328

2429
" Define start position depending on relation with end of line
2530
if start == col('$')
@@ -56,11 +61,13 @@ function! verilog_systemverilog#Complete(findstart, base)
5661
let s:prefix = strpart(line, start, prefixpos - start)
5762
let s:word = strpart(line, prefixpos, wordpos - prefixpos)
5863

64+
" If the prefix is simply a "." assume that this is an instance
65+
" port connection and search for its declaration
5966
if s:prefix == '.'
6067
" Get instance info and break from while loop
6168
let values = s:GetInstanceInfo(linenr, start)
6269
let s:instname = values[0]
63-
let s:insttype = values[1]
70+
let s:module = values[1]
6471
endif
6572
endif
6673

@@ -72,36 +79,36 @@ function! verilog_systemverilog#Complete(findstart, base)
7279
if exists("s:prefix") && s:prefix != ''
7380
call verilog_systemverilog#Verbose("Prefix: " . s:prefix)
7481
call verilog_systemverilog#Verbose("Word : " . s:word)
75-
if s:insttype != ''
82+
if s:module != ''
7683
" Process an instance
7784
call verilog_systemverilog#Verbose("Process instance")
7885
if exists("s:word")
79-
let tags = taglist('^' . s:insttype . '\.' . s:word)
86+
let tags = taglist('^' . s:module . '\.' . s:word)
8087
else
81-
let tags = taglist('^' . s:insttype . '\.')
88+
let tags = taglist('^' . s:module . '\.')
8289
endif
8390
call verilog_systemverilog#Verbose("Number of tags found: " . len(tags))
8491
if s:instname != ''
8592
" In instances only return ports
86-
let tags = s:FilterPorts(tags)
93+
let tags = s:TagsFilterPorts(tags)
8794
" Filter out hierarchical ports
8895
call filter(tags, 'len(split(v:val["name"], "\\.")) > 2 ? 0 : 1')
8996
call verilog_systemverilog#Verbose("Number of tags after filtering: " . len(tags))
9097
" Remove the module name prefix
91-
call map(tags, 'strpart(v:val["name"], len(s:insttype . "."))')
98+
call map(tags, 'strpart(v:val["name"], len(s:module . "."))')
9299
if (v:version >= 704)
93100
return {'words' : tags}
94101
else
95102
return tags
96103
endif
97104
else
98105
" In parameter list only return constants
99-
let tags = s:FilterConstants(tags)
106+
let tags = s:TagsFilterConstants(tags)
100107
" Filter out hierarchical ports
101108
call filter(tags, 'len(split(v:val["name"], "\\.")) > 2 ? 0 : 1')
102109
call verilog_systemverilog#Verbose("Number of tags after filtering: " . len(tags))
103110
" Remove the module name prefix
104-
call map(tags, 'strpart(v:val["name"], len(s:insttype . "."))')
111+
call map(tags, 'strpart(v:val["name"], len(s:module . "."))')
105112
if (v:version >= 704)
106113
return {'words' : tags}
107114
else
@@ -120,7 +127,7 @@ function! verilog_systemverilog#Complete(findstart, base)
120127
let base = s:instname
121128
endif
122129
call verilog_systemverilog#Verbose("Searching tags starting with " . base)
123-
let tags = s:FilterPortsOrConstants(taglist('^' . base . '\.'))
130+
let tags = s:TagsFilterPortsOrConstants(taglist('^' . base . '\.'))
124131
call map(tags, 'strpart(v:val["name"], len(base . "."))')
125132
if (v:version >= 704)
126133
return {'words' : tags}
@@ -172,17 +179,19 @@ function! verilog_systemverilog#Complete(findstart, base)
172179
return -1
173180
endif
174181
endfunction
182+
" }}}
175183

176184
" Search file for instance information:
177185
" * name
178186
" * type (typically a module name, but can also be a function/task/etc)
179187
" * line number
180188
function! s:GetInstanceInfo(linenr, column)
189+
" {{{
181190
let linenr = a:linenr
182191
let line = getline(linenr)
183192
let start = a:column
184193
let instname = ""
185-
let insttype = ""
194+
let module = ""
186195
let ininstdecl = 0
187196
let ininsttype = 0
188197
let p = 0
@@ -253,11 +262,11 @@ function! s:GetInstanceInfo(linenr, column)
253262
let ininsttype = start
254263
elseif ininsttype > 0 && (line[start - 1] =~ '\s' || start == 1)
255264
if start == 1 && line[start - 1] !~ '\s'
256-
let insttype = strpart(line, 0, ininsttype)
265+
let module = strpart(line, 0, ininsttype)
257266
else
258-
let insttype = strpart(line, start, ininsttype - start)
267+
let module = strpart(line, start, ininsttype - start)
259268
endif
260-
call verilog_systemverilog#Verbose("Found instance type \"" . insttype . "\", on line " . linenr)
269+
call verilog_systemverilog#Verbose("Found instance type \"" . module . "\", on line " . linenr)
261270
break
262271
endif
263272

@@ -281,12 +290,14 @@ function! s:GetInstanceInfo(linenr, column)
281290
let start = len(line)
282291
endwhile
283292

284-
call verilog_systemverilog#Verbose("Found instance. Name: »" . instname . "« Type: »" . insttype . "«")
285-
return [instname, insttype, linenr]
293+
call verilog_systemverilog#Verbose("Found instance. Name: »" . instname . "« Type: »" . module . "«")
294+
return [instname, module, linenr]
286295
endfunction
296+
" }}}
287297

288298
" Append signature to functions and tasks
289299
function s:AppendSignature(tags)
300+
" {{{
290301
let newtags = []
291302
for t in a:tags
292303
if t["kind"] == "t" || t["kind"] == "f"
@@ -296,9 +307,11 @@ function s:AppendSignature(tags)
296307
endfor
297308
return newtags
298309
endfunction
310+
" }}}
299311

300312
" Get list of inheritance tags
301313
function s:GetInheritanceTags(class, object)
314+
" {{{
302315
call verilog_systemverilog#Verbose("Searching inheritance of " . a:object)
303316
let tags = []
304317
let inheritance = a:class
@@ -333,9 +346,11 @@ function s:GetInheritanceTags(class, object)
333346
endwhile
334347
return tags
335348
endfunction
349+
" }}}
336350

337351
" Searches for declaration of "word" and returns its type
338352
function s:GetVariableType(word)
353+
" {{{
339354
let position = getpos(".")
340355
if searchdecl(a:word, 0) == 0
341356
let line = getline('.')
@@ -348,9 +363,11 @@ function s:GetVariableType(word)
348363
endif
349364
return 0
350365
endfunction
366+
" }}}
351367

352368
" Searches for declaration of "object" and returns "parameter" initialization value
353369
function s:GetObjectParameterValue(object, parameter)
370+
" {{{
354371
let position = getpos(".")
355372
if searchdecl(a:object, 0) == 0
356373
let line = getline('.')
@@ -366,9 +383,11 @@ function s:GetObjectParameterValue(object, parameter)
366383
call setpos(".", position)
367384
return ""
368385
endfunction
386+
" }}}
369387

370388
" Searches for declaration of "class" and returns default "parameter" value
371389
function s:GetClassDefaultParameterValue(class, parameter)
390+
" {{{
372391
if a:class == ""
373392
call verilog_systemverilog#Verbose("Search for default value of parameter " . a:parameter . " in current class")
374393
let declaration = {'cmd': '/.*type\s\+' . a:parameter . '\s*='}
@@ -412,61 +431,75 @@ function s:GetClassDefaultParameterValue(class, parameter)
412431
return ""
413432
endif
414433
endfunction
434+
" }}}
415435

416436
" Filter tag list to only return ports
417-
function s:FilterPorts(tags)
437+
function s:TagsFilterPorts(tags)
438+
" {{{
418439
let tags = a:tags
419440
call filter(tags, 'has_key(v:val, "kind") ? v:val["kind"] == "p" : 1')
420441
return tags
421442
endfunction
443+
" }}}
422444

423445
" Filter tag list to only return constants
424-
function s:FilterConstants(tags)
446+
function s:TagsFilterConstants(tags)
447+
" {{{
425448
let tags = a:tags
426449
call filter(tags, 'has_key(v:val, "kind") ? v:val["kind"] == "c" : 1')
427450
return tags
428451
endfunction
452+
" }}}
429453

430454
" Filter tag list to only return ports or constants
431-
function s:FilterPortsOrConstants(tags)
455+
function s:TagsFilterPortsOrConstants(tags)
456+
" {{{
432457
let tags = a:tags
433458
call filter(tags, 'has_key(v:val, "kind") ? v:val["kind"] == "p" || v:val["kind"] == "c" : 1')
434459
return tags
435460
endfunction
436461
" }}}
462+
" }}}
437463

438464
"------------------------------------------------------------------------
439465
" Common functions
440466
" {{{
441467
" Verbose messaging
442468
" Only displays messages if b:verilog_verbose or g:verilog_verbose is defined
443469
function verilog_systemverilog#Verbose(message)
470+
" {{{
444471
if verilog_systemverilog#VariableExists("verilog_verbose")
445472
echom a:message
446473
endif
447474
endfunction
475+
" }}}
448476

449477
" Configuration control
450478
" Pushes value to list only if new
451479
" Based on: http://vi.stackexchange.com/questions/6619/append-to-global-variable-and-completion
452480
function verilog_systemverilog#PushToVariable(variable, value)
481+
" {{{
453482
let list = verilog_systemverilog#VariableGetValue(a:variable)
454483
if (count(list, a:value) == 0)
455484
call add(list, a:value)
456485
endif
457486
call verilog_systemverilog#VariableSetValue(a:variable, list)
458487
endfunction
488+
" }}}
459489

460490
function verilog_systemverilog#PopFromVariable(variable, value)
491+
" {{{
461492
let list = verilog_systemverilog#VariableGetValue(a:variable)
462493
call verilog_systemverilog#VariableSetValue(a:variable, filter(list, "v:val !=# a:value"))
463494
endfunction
495+
" }}}
464496

465497
" Get variable value
466498
" Searches for both b:variable and g:variable, with this priority.
467499
" If the variable name includes '_lst' it is automatically split into a
468500
" list.
469501
function verilog_systemverilog#VariableGetValue(variable)
502+
" {{{
470503
if exists('b:' . a:variable)
471504
let value = eval('b:' . a:variable)
472505
elseif exists('g:' . a:variable)
@@ -480,13 +513,15 @@ function verilog_systemverilog#VariableGetValue(variable)
480513
return value
481514
endif
482515
endfunction
516+
" }}}
483517

484518
" Set variable value
485519
" Searches for both b:variable and g:variable, with this priority.
486520
" If none exists, g: will be used
487521
" If the variable name includes '_lst' the value argument is assumed to
488522
" be a list.
489523
function verilog_systemverilog#VariableSetValue(variable, value)
524+
" {{{
490525
if a:variable =~ '_lst'
491526
let value = join(a:value, ',')
492527
else
@@ -498,17 +533,21 @@ function verilog_systemverilog#VariableSetValue(variable, value)
498533
exec 'let g:' . a:variable . ' = value'
499534
endif
500535
endfunction
536+
" }}}
501537

502538
" Checks for variable existence
503539
function verilog_systemverilog#VariableExists(variable)
540+
" {{{
504541
return exists('b:' . a:variable) || exists('g:' . a:variable)
505542
endfunction
506543
" }}}
544+
" }}}
507545

508546
"------------------------------------------------------------------------
509547
" Command completion functions
510548
" {{{
511549
function verilog_systemverilog#CompleteCommand(lead, command, cursor)
550+
" {{{
512551
" Get list with current values in variable
513552
if (a:command =~ 'Folding')
514553
let current_values = verilog_systemverilog#VariableGetValue("verilog_syntax_fold_lst")
@@ -614,18 +653,22 @@ function verilog_systemverilog#CompleteCommand(lead, command, cursor)
614653
endif
615654
endfunction
616655
" }}}
656+
" }}}
617657

618658
"------------------------------------------------------------------------
619659
" External functions
620660
" {{{
621661
function verilog_systemverilog#GotoInstanceStart(line, column)
662+
" {{{
622663
let values = s:GetInstanceInfo(a:line, col('$'))
623664
if values[2] != ""
624665
call cursor(values[2], a:column)
625666
endif
626667
endfunction
668+
" }}}
627669

628670
function verilog_systemverilog#FollowInstanceTag(line, column)
671+
" {{{
629672
let values = s:GetInstanceInfo(a:line, col('$'))
630673
if exists("g:verilog_navigate_split")
631674
exec "wincmd ".g:verilog_navigate_split
@@ -634,8 +677,10 @@ function verilog_systemverilog#FollowInstanceTag(line, column)
634677
execute "tag " . values[1]
635678
endif
636679
endfunction
680+
" }}}
637681

638682
function verilog_systemverilog#ReturnFromInstanceTag()
683+
" {{{
639684
if winnr('$') > 1 && exists("g:verilog_navigate_split")
640685
if exists("g:verilog_navigate_split_close")
641686
exec g:verilog_navigate_split_close
@@ -646,19 +691,23 @@ function verilog_systemverilog#ReturnFromInstanceTag()
646691
exec "pop"
647692
endif
648693
endfunction
694+
" }}}
649695

650696
function verilog_systemverilog#FollowInstanceSearchWord(line, column)
697+
" {{{
651698
let @/='\<'.expand("<cword>").'\>'
652699
call verilog_systemverilog#FollowInstanceTag(a:line, a:column)
653700
exec "normal!" . @/
654701
normal! n
655702
endfunction
656703
" }}}
704+
" }}}
657705

658706
"------------------------------------------------------------------------
659707
" Command to control errorformat and compiler
660708
" {{{
661709
function! verilog_systemverilog#VerilogErrorFormat(...)
710+
" {{{
662711
" Choose tool
663712
if (a:0 == 0)
664713
let l:tool = inputlist([
@@ -742,5 +791,6 @@ function! verilog_systemverilog#VerilogErrorFormat(...)
742791
endif
743792
endfunction
744793
" }}}
794+
" }}}
745795

746796
" vi: sw=2 sts=2 fdm=marker:

0 commit comments

Comments
 (0)