-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathphpcomplete.vim
More file actions
5324 lines (5214 loc) · 292 KB
/
phpcomplete.vim
File metadata and controls
5324 lines (5214 loc) · 292 KB
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
" Vim completion script
" Language: PHP
" Maintainer: Mikolaj Machowski ( mikmach AT wp DOT pl )
" Maintainer: Shawn Biddle ( shawn AT shawnbiddle DOT com )
"
" TODO:
" - Switching to HTML (XML?) completion (SQL) inside of phpStrings
" - allow also for XML completion <- better do html_flavor for HTML
" completion
" - outside of <?php?> getting parent tag may cause problems. Heh, even in
" perfect conditions GetLastOpenTag doesn't cooperate... Inside of
" phpStrings this can be even a bonus but outside of <?php?> it is not the
" best situation
" compile regexes for phpdoc parsing - declared as globals
python << EOF
import vim
import re
regex_desc = re.compile(r"\*(?P<desc>.+?)(?:@param|@return|\*/)", re.MULTILINE|re.DOTALL)
regex_params = re.compile(r"@param\s+?(?P<type>.+?)\s+(?P<var>.+?)\s+(?P<desc>.+?)\n", re.MULTILINE|re.DOTALL)
regex_return = re.compile(r"@return\s+?(?P<type>.+?)\s+(?P<desc>.+?)\n", re.MULTILINE|re.DOTALL)
regex_throws = re.compile(r"@throws\s+?(?P<exc>.+?)\s*\n", re.MULTILINE|re.DOTALL)
EOF
function! phpcomplete#GetPhpDoc(sccontent,f_args) " {{{
python << EOF
content = vim.eval("a:sccontent")
func = vim.eval("a:f_args")
line = 0
phpdoc = ''
#vim.current.buffer.append('searching for: '+func)
for l in content:
if func in l:
#vim.current.buffer.append('found "'+func+'"at line: '+str(line))
commentParseStatus = 0
for i in range(line,line-15,-1):
#vim.current.buffer.append('line: '+str(i))
if commentParseStatus == 2:
break
if '*/' in content[i]:
commentParseStatus = 1
else:
if commentParseStatus == 1:
# read until you have close comment
phpdoc=content[i]+"\n"+phpdoc
if '/*' in content[i]:
commentParseStatus = 2
else:
continue
break
line+=1
# parse phpdoc for different items
readableDesc = ''
m = regex_desc.search(phpdoc)
if m:
desc = m.groupdict()['desc'].replace('*','').strip()
if desc != '':
readableDesc += '\nDescription:\n'+desc
params = tuple(regex_params.finditer(phpdoc))
if len(params) >0:
readableDesc += '\n\nArguments:\n'
for match in params:
readableDesc+= match.groupdict()['var']+' '+match.groupdict()['type']+': '+match.groupdict()['desc']+"\n"
m = regex_return.search(phpdoc)
if m:
readableDesc += '\n\nReturn:\n'+m.groupdict()['type']+' '+m.groupdict()['desc']
m = regex_throws.search(phpdoc)
if m:
readableDesc += '\nThrows:\n'+m.groupdict()['exc']
phpdoc = readableDesc.replace('"','\"').replace('\'','\'\'')
vim.command('let return2=\''+str(phpdoc)+'\'')
EOF
return return2
endfunction
" }}}
function! phpcomplete#CompletePHP(findstart, base)
if a:findstart
unlet! b:php_menu
" Check if we are inside of PHP markup
let pos = getpos('.')
let phpbegin = searchpairpos('<?', '', '?>', 'bWn',
\ 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\|comment"')
let phpend = searchpairpos('<?', '', '?>', 'Wn',
\ 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\|comment"')
if phpbegin == [0,0] && phpend == [0,0]
" We are outside of any PHP markup. Complete HTML
let htmlbegin = htmlcomplete#CompleteTags(1, '')
let cursor_col = pos[2]
let base = getline('.')[htmlbegin : cursor_col]
let b:php_menu = htmlcomplete#CompleteTags(0, base)
return htmlbegin
else
" locate the start of the word
let line = getline('.')
let start = col('.') - 1
let curline = line('.')
let compl_begin = col('.') - 2
while start >= 0 && line[start - 1] =~ '[a-zA-Z_0-9\x7f-\xff$]'
let start -= 1
endwhile
let b:compl_context = getline('.')[0:compl_begin]
return start
" We can be also inside of phpString with HTML tags. Deal with
" it later (time, not lines).
endif
endif
" If exists b:php_menu it means completion was already constructed we
" don't need to do anything more
if exists("b:php_menu")
return b:php_menu
endif
" Initialize base return lists
let res = []
let res2 = []
" a:base is very short - we need context
if exists("b:compl_context")
let context = b:compl_context
unlet! b:compl_context
endif
if !exists('g:php_builtin_functions')
call phpcomplete#LoadData()
endif
let scontext = substitute(context, '\$\?[a-zA-Z_\x7f-\xff][a-zA-Z_0-9\x7f-\xff]*$', '', '')
if scontext =~ '\(=\s*new\|extends\)\s\+$'
" Complete class name
" Internal solution for finding classes in current file.
let file = getline(1, '$')
call filter(file,
\ 'v:val =~ "class\\s\\+[a-zA-Z_\\x7f-\\xff][a-zA-Z_0-9\\x7f-\\xff]*\\s*("')
let fnames = join(map(tagfiles(), 'escape(v:val, " \\#%")'))
let jfile = join(file, ' ')
let int_values = split(jfile, 'class\s\+')
let int_classes = {}
for i in int_values
let c_name = matchstr(i, '^[a-zA-Z_\x7f-\xff][a-zA-Z_0-9\x7f-\xff]*')
if c_name != ''
let int_classes[c_name] = ''
endif
endfor
" Prepare list of classes from tags file
let ext_classes = {}
let fnames = join(map(tagfiles(), 'escape(v:val, " \\#%")'))
if fnames != ''
exe 'silent! vimgrep /^'.a:base.'.*\tc\(\t\|$\)/j '.fnames
let qflist = getqflist()
if len(qflist) > 0
for field in qflist
" [:space:] thing: we don't have to be so strict when
" dealing with tags files - entries there were already
" checked by ctags.
let item = matchstr(field['text'], '^[^[:space:]]\+')
let ext_classes[item] = ''
endfor
endif
endif
" Prepare list of built in classes from g:php_builtin_functions
if !exists("g:php_omni_bi_classes")
let g:php_omni_bi_classes = {}
for i in keys(g:php_builtin_object_functions)
let g:php_omni_bi_classes[substitute(i, '::.*$', '', '')] = ''
endfor
endif
let classes = sort(keys(int_classes))
let classes += sort(keys(ext_classes))
let classes += sort(keys(g:php_omni_bi_classes))
for m in classes
if m =~ '^'.a:base
call add(res, m)
endif
endfor
let final_menu = []
for i in res
let final_menu += [{'word':i, 'kind':'c'}]
endfor
return final_menu
elseif scontext =~ '\(->\|::\)$'
" Complete user functions and variables
" Internal solution for current file.
" That seems as unnecessary repeating of functions but there are
" few not so subtle differences as not appending of $ and addition
" of 'kind' tag (not necessary in regular completion)
if scontext =~ '->$' || scontext =~ '::'
" Get name of the class
let classname = phpcomplete#GetClassName(scontext)
" Get location of class definition, we have to iterate through all
" tags files separately because we need relative path from current
" file to the exact file (tags file can be in different dir)
if classname != ''
let classlocation = phpcomplete#GetClassLocation(classname)
else
let classlocation = ''
endif
if classlocation == 'VIMPHP_BUILTINOBJECT'
for object in keys(g:php_builtin_object_functions)
if object =~ '^'.classname
let res += [{'word':substitute(object, '.*::', '', ''),
\ 'info': g:php_builtin_object_functions[object]}]
endif
endfor
return res
endif
if filereadable(classlocation)
let classfile = readfile(classlocation)
let classcontent = ''
let classcontent .= "\n".phpcomplete#GetClassContents(classfile, classname)
let sccontent = split(classcontent, "\n")
let classAccess = expand('%:p') == fnamemodify(classlocation, ':p') ? '\\(public\\|private\\|protected\\)' : 'public'
" limit based on context to static or normal public methods
if scontext =~ '::'
let functions = filter(deepcopy(sccontent),
\ 'v:val =~ "^\\s*\\(\\(' . classAccess . '\\s\\+static\\|static\\)\\s\\+\\)*function"')
elseif scontext =~ '->$'
let functions = filter(deepcopy(sccontent),
\ 'v:val =~ "^\\s*\\(' . classAccess . '\\s\\+\\)*function"')
endif
let jfuncs = join(functions, ' ')
let sfuncs = split(jfuncs, 'function\s\+')
let c_functions = {}
let c_doc = {}
for i in sfuncs
let f_full = matchstr(i,
\ '^&\?\zs[a-zA-Z_\x7f-\xff][a-zA-Z_0-9\x7f-\xff]*\ze')
let f_name = matchstr(i,
\ '^&\?\zs[a-zA-Z_\x7f-\xff][a-zA-Z_0-9\x7f-\xff]*\ze')
let f_args = matchstr(i,
\ '^&\?[a-zA-Z_\x7f-\xff][a-zA-Z_0-9\x7f-\xff]*\s*(\zs.\{-}\ze)\_s*')
if f_full != ''
let phpdoc = phpcomplete#GetPhpDoc(sccontent,f_full)
else
let phpdoc = ''
endif
if f_name != ''
let c_functions[f_name.'('] = f_args
let c_doc[f_name.'('] = phpdoc
endif
endfor
" Variables declared with var or with public keyword are
" public
let variables = filter(deepcopy(sccontent),
\ 'v:val =~ "^\\s*\\(' . classAccess . '\\|var\\)\\s\\+\\$"')
let jvars = join(variables, ' ')
let svars = split(jvars, '\$')
let c_variables = {}
for i in svars
let c_var = matchstr(i,
\ '^\zs[a-zA-Z_\x7f-\xff][a-zA-Z_0-9\x7f-\xff]*\ze')
if c_var != ''
let c_variables[c_var] = ''
endif
endfor
let constants = filter(deepcopy(sccontent),
\ 'v:val =~ "^\\s*const\\s\\+"')
let jcons = join(constants, ' ')
let scons = split(jcons, 'const')
let c_constants = {}
for i in scons
let c_con = matchstr(i,
\ '^\s*\zs[a-zA-Z_\x7f-\xff][a-zA-Z_0-9\x7f-\xff]*\ze')
if c_con != ''
let c_constants[c_con] = ''
let c_doc[c_con] = ''
endif
endfor
let all_values = {}
call extend(all_values, c_functions)
call extend(all_values, c_variables)
call extend(all_values, c_constants)
for m in sort(keys(all_values))
if m =~ '^'.a:base && m !~ '::'
call add(res, m)
elseif m =~ '::'.a:base
call add(res2, m)
endif
endfor
let start_list = res + res2
let final_list = []
for i in start_list
if has_key(c_variables, i)
let class = ' '
if all_values[i] != ''
let class = i.' class '
endif
let final_list +=
\ [{'word':i,
\ 'info':class.all_values[i],
\ 'kind':'v'}]
else
let final_list +=
\ [{'word':substitute(i, '.*::', '', ''),
\ 'info':i.all_values[i].')'.c_doc[i],
\ 'kind':'f'}]
endif
endfor
return final_list
endif
endif
if a:base =~ '^\$'
let adddollar = '$'
else
let adddollar = ''
endif
let file = getline(1, '$')
let jfile = join(file, ' ')
let sfile = split(jfile, '\$')
let int_vars = {}
for i in sfile
if i =~ '^\$[a-zA-Z_\x7f-\xff][a-zA-Z_0-9\x7f-\xff]*\s*=\s*new'
let val = matchstr(i, '^[a-zA-Z_\x7f-\xff][a-zA-Z_0-9\x7f-\xff]*').'->'
else
let val = matchstr(i, '^[a-zA-Z_\x7f-\xff][a-zA-Z_0-9\x7f-\xff]*')
endif
if val !~ ''
let int_vars[adddollar.val] = ''
endif
endfor
" ctags has good support for PHP, use tags file for external
" variables
let fnames = join(map(tagfiles(), 'escape(v:val, " \\#%")'))
let ext_vars = {}
if fnames != ''
let sbase = substitute(a:base, '^\$', '', '')
exe 'silent! vimgrep /^'.sbase.'.*\tv\(\t\|$\)/j '.fnames
let qflist = getqflist()
if len(qflist) > 0
for field in qflist
let item = matchstr(field['text'], '^[^[:space:]]\+')
" Add -> if it is possible object declaration
let classname = ''
if field['text'] =~ item.'\s*=\s*new\s\+'
let item = item.'->'
let classname = matchstr(field['text'],
\ '=\s*new\s\+\zs[a-zA-Z_0-9\x7f-\xff]\+\ze')
endif
let ext_vars[adddollar.item] = classname
endfor
endif
endif
" Now we have all variables in int_vars dictionary
call extend(int_vars, ext_vars)
" Internal solution for finding functions in current file.
let file = getline(1, '$')
call filter(file,
\ 'v:val =~ "function\\s\\+&\\?[a-zA-Z_\\x7f-\\xff][a-zA-Z_0-9\\x7f-\\xff]*\\s*("')
let fnames = join(map(tagfiles(), 'escape(v:val, " \\#%")'))
let jfile = join(file, ' ')
let int_values = split(jfile, 'function\s\+')
let int_functions = {}
for i in int_values
let f_name = matchstr(i,
\ '^&\?\zs[a-zA-Z_\x7f-\xff][a-zA-Z_0-9\x7f-\xff]*\ze')
let f_args = matchstr(i,
\ '^&\?[a-zA-Z_\x7f-\xff][a-zA-Z_0-9\x7f-\xff]*\s*(\zs.\{-}\ze)\_s*{')
let int_functions[f_name.'('] = f_args.')'
endfor
" Prepare list of functions from tags file
let ext_functions = {}
if fnames != ''
exe 'silent! vimgrep /^'.a:base.'.*\tf\(\t\|$\)/j '.fnames
let qflist = getqflist()
if len(qflist) > 0
for field in qflist
" File name
let item = matchstr(field['text'], '^[^[:space:]]\+')
let fname = matchstr(field['text'], '\t\zs\f\+\ze')
let prototype = matchstr(field['text'],
\ 'function\s\+&\?[^[:space:]]\+\s*(\s*\zs.\{-}\ze\s*)\s*{\?')
let ext_functions[item.'('] = prototype.') - '.fname
endfor
endif
endif
let all_values = {}
call extend(all_values, int_functions)
call extend(all_values, ext_functions)
call extend(all_values, int_vars) " external variables are already in
call extend(all_values, g:php_builtin_object_functions)
for m in sort(keys(all_values))
if m =~ '\(^\|::\)'.a:base
call add(res, m)
endif
endfor
let start_list = res
let final_list = []
for i in start_list
if has_key(int_vars, i)
let class = ' '
if all_values[i] != ''
let class = i.' class '
endif
let final_list += [{'word':i, 'info':class.all_values[i], 'kind':'v'}]
else
let final_list +=
\ [{'word':substitute(i, '.*::', '', ''),
\ 'info':i.all_values[i],
\ 'kind':'f'}]
endif
endfor
return final_list
endif
if a:base =~ '^\$'
" Complete variables
" Built-in variables {{{
let g:php_builtin_vars = {'$GLOBALS':'',
\ '$_SERVER':'',
\ '$_GET':'',
\ '$_POST':'',
\ '$_COOKIE':'',
\ '$_FILES':'',
\ '$_ENV':'',
\ '$_REQUEST':'',
\ '$_SESSION':'',
\ '$HTTP_SERVER_VARS':'',
\ '$HTTP_ENV_VARS':'',
\ '$HTTP_COOKIE_VARS':'',
\ '$HTTP_GET_VARS':'',
\ '$HTTP_POST_VARS':'',
\ '$HTTP_POST_FILES':'',
\ '$HTTP_SESSION_VARS':'',
\ '$php_errormsg':'',
\ '$this':''
\ }
" }}}
" Internal solution for current file.
let file = getline(1, '$')
let jfile = join(file, ' ')
let int_vals = split(jfile, '\ze\$')
let int_vars = {}
for i in int_vals
if i =~ '^\$[a-zA-Z_\x7f-\xff][a-zA-Z_0-9\x7f-\xff]*\s*=\s*new'
let val = matchstr(i,
\ '^\$[a-zA-Z_\x7f-\xff][a-zA-Z_0-9\x7f-\xff]*').'->'
else
let val = matchstr(i,
\ '^\$[a-zA-Z_\x7f-\xff][a-zA-Z_0-9\x7f-\xff]*')
endif
if val != ''
let int_vars[val] = ''
endif
endfor
call extend(int_vars,g:php_builtin_vars)
" ctags has support for PHP, use tags file for external variables
let fnames = join(map(tagfiles(), 'escape(v:val, " \\#%")'))
let ext_vars = {}
if fnames != ''
let sbase = substitute(a:base, '^\$', '', '')
exe 'silent! vimgrep /^'.sbase.'.*\tv\(\t\|$\)/j '.fnames
let qflist = getqflist()
if len(qflist) > 0
for field in qflist
let item = '$'.matchstr(field['text'], '^[^[:space:]]\+')
let m_menu = ''
" Add -> if it is possible object declaration
if field['text'] =~ item.'\s*=\s*new\s\+'
let item = item.'->'
let m_menu = matchstr(field['text'],
\ '=\s*new\s\+\zs[a-zA-Z_0-9\x7f-\xff]\+\ze')
endif
let ext_vars[item] = m_menu
endfor
endif
endif
call extend(int_vars, ext_vars)
let g:a0 = keys(int_vars)
for m in sort(keys(int_vars))
if m =~ '^\'.a:base
call add(res, m)
endif
endfor
let int_list = res
let int_dict = []
for i in int_list
if int_vars[i] != ''
let class = ' '
if int_vars[i] != ''
let class = i.' class '
endif
let int_dict += [{'word':i, 'info':class.int_vars[i], 'kind':'v'}]
else
let int_dict += [{'word':i, 'kind':'v'}]
endif
endfor
return int_dict
else
" Complete everything else -
" + functions, DONE
" + keywords of language DONE
" + defines (constant definitions), DONE
" + extend keywords for predefined constants, DONE
" + classes (after new), DONE
" + limit choice after -> and :: to funcs and vars DONE
" Internal solution for finding functions in current file.
let file = getline(1, '$')
call filter(file,
\ 'v:val =~ "function\\s\\+&\\?[a-zA-Z_\\x7f-\\xff][a-zA-Z_0-9\\x7f-\\xff]*\\s*("')
let fnames = join(map(tagfiles(), 'escape(v:val, " \\#%")'))
let jfile = join(file, ' ')
let int_values = split(jfile, 'function\s\+')
let int_functions = {}
for i in int_values
let f_name = matchstr(i,
\ '^&\?\zs[a-zA-Z_\x7f-\xff][a-zA-Z_0-9\x7f-\xff]*\ze')
let f_args = matchstr(i,
\ '^&\?[a-zA-Z_\x7f-\xff][a-zA-Z_0-9\x7f-\xff]*\s*(\s*\zs.\{-}\ze\s*)\_s*{')
let int_functions[f_name.'('] = f_args.')'
endfor
" Prepare list of functions from tags file
let ext_functions = {}
if fnames != ''
exe 'silent! vimgrep /^'.a:base.'.*\tf\(\t\|$\)/j '.fnames
let qflist = getqflist()
if len(qflist) > 0
for field in qflist
" File name
let item = matchstr(field['text'], '^[^[:space:]]\+')
let fname = matchstr(field['text'], '\t\zs\f\+\ze')
let prototype = matchstr(field['text'],
\ 'function\s\+&\?[^[:space:]]\+\s*(\s*\zs.\{-}\ze\s*)\s*{\?')
let ext_functions[item.'('] = prototype.') - '.fname
endfor
endif
endif
" All functions
call extend(int_functions, ext_functions)
call extend(int_functions, g:php_builtin_functions)
" Internal solution for finding constants in current file
let file = getline(1, '$')
call filter(file, 'v:val =~ "define\\s*("')
let jfile = join(file, ' ')
let int_values = split(jfile, 'define\s*(\s*')
let int_constants = {}
for i in int_values
let c_name = matchstr(i, '\(["'']\)\zs[a-zA-Z_\x7f-\xff][a-zA-Z_0-9\x7f-\xff]*\ze\1')
" let c_value = matchstr(i,
" \ '\(["'']\)[a-zA-Z_\x7f-\xff][a-zA-Z_0-9\x7f-\xff]*\1\s*,\s*\zs.\{-}\ze\s*)')
if c_name != ''
let int_constants[c_name] = '' " c_value
endif
endfor
" Prepare list of constants from tags file
let fnames = join(map(tagfiles(), 'escape(v:val, " \\#%")'))
let ext_constants = {}
if fnames != ''
exe 'silent! vimgrep /^'.a:base.'.*\td\(\t\|$\)/j '.fnames
let qflist = getqflist()
if len(qflist) > 0
for field in qflist
let item = matchstr(field['text'], '^[^[:space:]]\+')
let ext_constants[item] = ''
endfor
endif
endif
" All constants
call extend(int_constants, ext_constants)
" Treat keywords as constants
let all_values = {}
" One big dictionary of functions
call extend(all_values, int_functions)
" Add constants
call extend(all_values, int_constants)
" Add keywords
call extend(all_values, g:php_keywords)
for m in sort(keys(all_values))
if m =~ '^'.a:base
call add(res, m)
endif
endfor
let int_list = res
let final_list = []
for i in int_list
if has_key(int_functions, i)
let final_list +=
\ [{'word':i,
\ 'info':i.int_functions[i],
\ 'kind':'f'}]
elseif has_key(int_constants, i)
let final_list += [{'word':i, 'kind':'d'}]
else
let final_list += [{'word':i}]
endif
endfor
return final_list
endif
endfunction
function! phpcomplete#GetClassName(scontext) " {{{
" Get class name
" Class name can be detected in few ways:
" @var $myVar class
" line above
" or line in tags file
if a:scontext =~ '\$this->' || a:scontext =~ '\(self\|static\)::'
let i = 1
while i < line('.')
let line = getline(line('.')-i)
" Don't complete self:: or $this if outside of a class
" (assumes correct indenting)
if line =~ '^}'
return ''
endif
if line =~ '^abstract\s*class'
let classname = matchstr(line, '^abstract\s*class \zs[a-zA-Z]\w\+\ze\(\s*\|$\)')
return classname
elseif line =~ '^class'
let classname = matchstr(line, '^class \zs[a-zA-Z]\w\+\ze\(\s*\|$\)')
return classname
else
let i += 1
continue
endif
endwhile
else
let object = matchstr(a:scontext, '\zs[a-zA-Z_0-9\x7f-\xff]\+\ze->')
let i = 1
while i < line('.')
let line = getline(line('.')-i)
if line =~ '^\s*\*\/\?\s*$'
let i += 1
continue
else
if line =~ '@var\s\+\$'.object.'\s\+[a-zA-Z_0-9\x7f-\xff]\+'
let classname = matchstr(line, '@var\s\+\$'.object.'\s\+\zs[a-zA-Z_0-9\x7f-\xff]\+')
return classname
else
break
endif
endif
endwhile
" do in-file lookup of $var = new Class
let i = 1
while i < line('.')
let line = getline(line('.')-i)
if line =~ '^\s*\$'.object.'\s*=\s*new\s\+[a-zA-Z_0-9\x7f-\xff]\+'
let classname = matchstr(line, '\$'.object.'\s*=\s*new \zs[a-zA-Z_0-9\x7f-\xff]\+')
return classname
else
let i += 1
continue
endif
endwhile
" do in-file lookup for Class::getInstance()
let i = 1
while i < line('.')
let line = getline(line('.')-i)
if line =~ '^\s*\$'.object.'\s*=&\?\s*\s\+[a-zA-Z_0-9\x7f-\xff]\+::getInstance\+'
let classname = matchstr(line, '\$'.object.'\s*=&\?\s*\zs[a-zA-Z_0-9\x7f-\xff]\+\ze::getInstance\+')
return classname
else
let i += 1
continue
endif
endwhile
" check Constant lookup
let constant_object = matchstr(a:scontext, '\zs[a-zA-Z_0-9\x7f-\xff]\+\ze::')
if constant_object != ''
return constant_object
endif
" OK, first way failed, now check tags file(s)
let fnames = join(map(tagfiles(), 'escape(v:val, " \\#%")'))
exe 'silent! vimgrep /^'.object.'.*\$'.object.'.*=\s*new\s\+.*\tv\(\t\|$\)/j '.fnames
let qflist = getqflist()
if len(qflist) == 0
return ''
else
" In all properly managed projects it should be one item list, even if it
" *is* longer we cannot solve conflicts, assume it is first element
let classname = matchstr(qflist[0]['text'], '=\s*new\s\+\zs[a-zA-Z_0-9\x7f-\xff]\+\ze')
return classname
endif
endif
endfunction
" }}}
function! phpcomplete#GetClassLocation(classname) " {{{
" Check classname may be name of built in object
if !exists("g:php_omni_bi_classes")
let g:php_omni_bi_classes = {}
for i in keys(g:php_builtin_object_functions)
let g:php_omni_bi_classes[substitute(i, '::.*$', '', '')] = ''
endfor
endif
if has_key(g:php_omni_bi_classes, a:classname)
return 'VIMPHP_BUILTINOBJECT'
endif
" do in-file lookup for class definition
let i = 1
while i < line('.')
let line = getline(line('.')-i)
if line =~ '^\s*class ' . a:classname . '\(\s\+\|$\)'
return expand('%:p')
else
let i += 1
continue
endif
endwhile
" Get class location
for fname in tagfiles()
let fhead = fnamemodify(fname, ":h")
if fhead != ''
let psep = '/' " Note: slash is potential problem!
let fhead .= psep
endif
let fname = escape(fname, " \\")
exe 'silent! vimgrep /^'.a:classname.'.*\tc\(\t\|$\)/j '.fname
let qflist = getqflist()
" As in GetClassName we can manage only one element if it exists
let classlocation = ''
if len(qflist) > 0
let classlocation = matchstr(qflist[0]['text'], '\t\zs\f\+\ze\t')
endif
" And only one class location
if classlocation != ''
if matchstr(classlocation,'^/') != '/'
let classlocation = fhead.classlocation
endif
return classlocation
endif
endfor
return ''
endfunction
" }}}
function! phpcomplete#GetClassContents(file, name) " {{{
let cfile = join(a:file, "\n")
" We use new buffer and (later) normal! because
" this is the most efficient way. The other way
" is to go through the looong string looking for
" matching {}
below 1new
0put =cfile
let endline = search('{')
call search('class\s\+'.a:name)
let cfline = line('.')
let content = join(getline(cfline, endline),"\n")
" Catch extends
if content =~ 'extends'
let extends_class = matchstr(content, 'class\_s\+'.a:name.'\_s\+extends\_s\+\zs[a-zA-Z_0-9\x7f-\xff]\+\ze')
else
let extends_class = ''
endif
call search('{')
normal! %
let classcontent = cfile
bw! %
if extends_class != ''
let classlocation = phpcomplete#GetClassLocation(extends_class)
if filereadable(classlocation)
let classfile = readfile(classlocation)
let classcontent .= "\n".phpcomplete#GetClassContents(classfile, extends_class)
endif
endif
return classcontent
endfunction
" }}}
function! phpcomplete#LoadData() " {{{
" Keywords/reserved words, all other special things {{{
" Later it is possible to add some help to values, or type of
" defined variable
let g:php_keywords = {
\ 'PHP_SELF':'',
\ 'argv':'',
\ 'argc':'',
\ 'GATEWAY_INTERFACE':'',
\ 'SERVER_ADDR':'',
\ 'SERVER_NAME':'',
\ 'SERVER_SOFTWARE':'',
\ 'SERVER_PROTOCOL':'',
\ 'REQUEST_METHOD':'',
\ 'REQUEST_TIME':'',
\ 'QUERY_STRING':'',
\ 'DOCUMENT_ROOT':'',
\ 'HTTP_ACCEPT':'',
\ 'HTTP_ACCEPT_CHARSET':'',
\ 'HTTP_ACCEPT_ENCODING':'',
\ 'HTTP_ACCEPT_LANGUAGE':'',
\ 'HTTP_CONNECTION':'',
\ 'HTTP_POST':'',
\ 'HTTP_REFERER':'',
\ 'HTTP_USER_AGENT':'',
\ 'HTTPS':'',
\ 'REMOTE_ADDR':'',
\ 'REMOTE_HOST':'',
\ 'REMOTE_PORT':'',
\ 'SCRIPT_FILENAME':'',
\ 'SERVER_ADMIN':'',
\ 'SERVER_PORT':'',
\ 'SERVER_SIGNATURE':'',
\ 'PATH_TRANSLATED':'',
\ 'SCRIPT_NAME':'',
\ 'REQUEST_URI':'',
\ 'PHP_AUTH_DIGEST':'',
\ 'PHP_AUTH_USER':'',
\ 'PHP_AUTH_PW':'',
\ 'AUTH_TYPE':'',
\ 'and':'',
\ 'or':'',
\ 'xor':'',
\ '__FILE__':'',
\ 'exception':'',
\ '__LINE__':'',
\ 'as':'',
\ 'break':'',
\ 'case':'',
\ 'class':'',
\ 'const':'',
\ 'continue':'',
\ 'declare':'',
\ 'default':'',
\ 'do':'',
\ 'echo':'',
\ 'else':'',
\ 'elseif':'',
\ 'enddeclare':'',
\ 'endfor':'',
\ 'endforeach':'',
\ 'endif':'',
\ 'endswitch':'',
\ 'endwhile':'',
\ 'extends':'',
\ 'for':'',
\ 'foreach':'',
\ 'function':'',
\ 'global':'',
\ 'if':'',
\ 'new':'',
\ 'static':'',
\ 'switch':'',
\ 'use':'',
\ 'var':'',
\ 'while':'',
\ '__FUNCTION__':'',
\ '__CLASS__':'',
\ '__METHOD__':'',
\ 'final':'',
\ 'php_user_filter':'',
\ 'interface':'',
\ 'implements':'',
\ 'public':'',
\ 'private':'',
\ 'protected':'',
\ 'abstract':'',
\ 'clone':'',
\ 'try':'',
\ 'catch':'',
\ 'throw':'',
\ 'cfunction':'',
\ 'old_function':'',
\ 'this':'',
\ 'PHP_VERSION': '',
\ 'PHP_OS': '',
\ 'PHP_SAPI': '',
\ 'PHP_EOL': '',
\ 'PHP_INT_MAX': '',
\ 'PHP_INT_SIZE': '',
\ 'DEFAULT_INCLUDE_PATH': '',
\ 'PEAR_INSTALL_DIR': '',
\ 'PEAR_EXTENSION_DIR': '',
\ 'PHP_EXTENSION_DIR': '',
\ 'PHP_PREFIX': '',
\ 'PHP_BINDIR': '',
\ 'PHP_LIBDIR': '',
\ 'PHP_DATADIR': '',
\ 'PHP_SYSCONFDIR': '',
\ 'PHP_LOCALSTATEDIR': '',
\ 'PHP_CONFIG_FILE_PATH': '',
\ 'PHP_CONFIG_FILE_SCAN_DIR': '',
\ 'PHP_SHLIB_SUFFIX': '',
\ 'PHP_OUTPUT_HANDLER_START': '',
\ 'PHP_OUTPUT_HANDLER_CONT': '',
\ 'PHP_OUTPUT_HANDLER_END': '',
\ 'E_ERROR': '',
\ 'E_WARNING': '',
\ 'E_PARSE': '',
\ 'E_NOTICE': '',
\ 'E_CORE_ERROR': '',
\ 'E_CORE_WARNING': '',
\ 'E_COMPILE_ERROR': '',
\ 'E_COMPILE_WARNING': '',
\ 'E_USER_ERROR': '',
\ 'E_USER_WARNING': '',
\ 'E_USER_NOTICE': '',
\ 'E_ALL': '',
\ 'E_STRICT': '',
\ '__COMPILER_HALT_OFFSET__': '',
\ 'EXTR_OVERWRITE': '',
\ 'EXTR_SKIP': '',
\ 'EXTR_PREFIX_SAME': '',
\ 'EXTR_PREFIX_ALL': '',
\ 'EXTR_PREFIX_INVALID': '',
\ 'EXTR_PREFIX_IF_EXISTS': '',
\ 'EXTR_IF_EXISTS': '',
\ 'SORT_ASC': '',
\ 'SORT_DESC': '',
\ 'SORT_REGULAR': '',
\ 'SORT_NUMERIC': '',
\ 'SORT_STRING': '',
\ 'CASE_LOWER': '',