-
Notifications
You must be signed in to change notification settings - Fork 23
/
phpcomplete_extended.vim
1805 lines (1535 loc) · 58.9 KB
/
phpcomplete_extended.vim
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
"=============================================================================
" AUTHOR: Mun Mun Das <m2mdas at gmail.com>
" FILE: phpcomplete_extended.vim
" Last Modified: September 11, 2013
" License: MIT license {{{
" Permission is hereby granted, free of charge, to any person obtaining
" a copy of this software and associated documentation files (the
" "Software"), to deal in the Software without restriction, including
" without limitation the rights to use, copy, modify, merge, publish,
" distribute, sublicense, and/or sell copies of the Software, and to
" permit persons to whom the Software is furnished to do so, subject to
" the following conditions:
"
" The above copyright notice and this permission notice shall be included
" in all copies or substantial portions of the Software.
"
" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
" OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
" IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
" CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
" TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
" SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
" }}}
"=============================================================================
let s:save_cpo = &cpo
set cpo&vim
if !exists("s:current_project_dir")
let s:current_project_dir = ""
endif
if !exists("s:update_info")
let s:update_info = {}
endif
if !exists("s:plugins")
let s:plugins = {}
endif
if !exists("s:plugin_ftime")
let s:plugin_ftime = -1
endif
if !exists("s:plugin_index")
let s:plugin_index = {}
endif
if !exists("s:plugin_php_files")
let s:plugin_php_files = []
endif
if !exists("s:psr_class_complete")
let s:psr_class_complete = 0
endif
if !exists("s:phpcomplete_enabled")
let s:phpcomplete_enabled = 1
endif
let s:disabled_projects = []
let s:T = {
\ 'number': type(0),
\ 'string': type(''),
\ 'function': type(function('function')),
\ 'list': type([]),
\ 'dictionary': type({}),
\ 'float': type(0.0),
\ }
function! phpcomplete_extended#CompletePHP(findstart, base) " {{{
if a:findstart
let start = s:get_complete_start_pos()
if !empty(b:completeContext)
let b:completeContext.start = start
let b:completeContext.base = getline('.')[start : col('.')-2]
endif
return start
endif
if !s:phpcomplete_enabled
return []
endif
if !phpcomplete_extended#is_phpcomplete_extended_project()
return []
endif
if empty(b:completeContext)
return []
endif
let s:psr_class_complete = 0
if b:completeContext.complete_type == "use"
return s:getUseMenuEntries(a:base)
elseif b:completeContext.complete_type == "nonclass"
let s:psr_class_complete = 1
return s:getNonClassMenuEntries(a:base)
elseif b:completeContext.complete_type == "insideQuote"
return s:get_plugin_inside_qoute_menu_entries(b:completeContext.fqcn, b:completeContext.lastToken)
elseif b:completeContext.complete_type == "new"
let s:psr_class_complete = 1
return s:getClassMenuEntries(a:base)
elseif b:completeContext.complete_type == "class"
let is_static = 0
let is_this = 0
let lastResolutor = b:completeContext.last_resolutor
let fqcn = b:completeContext.fqcn
let sourceFile = phpcomplete_extended#util#substitute_path_separator(expand("%:."))
let sourceFQCN = s:getFQCNFromFileLocation(sourceFile)
if sourceFQCN == fqcn
let is_this = 1
endif
if lastResolutor == '::'
let is_static = 1
endif
if b:completeContext.complete_type == 'class'
\ && lastResolutor == ""
return []
endif
let menu_entries = []
let baseMenuEntries = phpcomplete_extended#getMenuEntries(fqcn, a:base, is_this, is_static)
"plugin callback
let plugin_menu_entries = s:get_plugin_menu_entries(fqcn, a:base, is_this, is_static)
let menu_entries += plugin_menu_entries
let menu_entries += baseMenuEntries
return menu_entries
endif
endfunction "}}}
function! s:get_complete_start_pos() "{{{
let line = getline('.')
let start = col('.') - 1
while start >= 0 && line[start - 1] =~ '[\\a-zA-Z_0-9\x7f-\xff$]'
let start -= 1
endwhile
let b:completeContext = {}
let completeContext = s:get_complete_context()
if empty(completeContext) || !s:phpcomplete_enabled
return start
endif
let b:completeContext = completeContext
if b:completeContext.complete_type == "class" && getline('.')[start-2 : start-1] !~ '->\|::'
let b:completeContext.last_resolutor = ""
endif
if !empty(completeContext)
\ && has_key(completeContext, 'complete_type')
\ && completeContext['complete_type'] == 'insideQuote'
\ && match(completeContext.lastToken['insideBraceText'], '\.') != -1
"having some problem with dot charater
let splits = split(completeContext.lastToken['insideBraceText'], '\\\.')
let displace = len(splits)
if match(completeContext.lastToken['insideBraceText'], '\\\.$') < 0
let displace += len(splits[-1]) -1
endif
let start = start - len(completeContext.lastToken['insideBraceText']) + displace
endif
return start
endfunction "}}}
function! s:get_complete_context() "{{{
let cursorLine = getline('.')[0:col('.')-2]
if !exists('phpcomplete_extended_context')
let completeContext = {}
endif
let completeContext.complete_type = "class"
if cursorLine =~? '^\s*use\s\+'
" namespace completeion
let completeContext.complete_type = "use"
elseif cursorLine =~? '\(\s*new\|extends\)\s\+'
\ && len(phpcomplete_extended#parsereverse(cursorLine, line('.'))) == 1
"new class completion
let completeContext.complete_type = "new"
else
if !phpcomplete_extended#is_phpcomplete_extended_project()
return {}
endif
let parsedTokens = phpcomplete_extended#parsereverse(cursorLine, line('.'))
if empty(parsedTokens)
let completeContext = {}
return {}
endif
if has_key(parsedTokens[0], "nonClass") && parsedTokens[0]["nonClass"]
let completeContext.complete_type = "nonclass"
elseif has_key(parsedTokens[-1], "insideQuote")
let lastToken = remove(parsedTokens, -1)
let fqcn = s:guessTypeOfParsedTokens(deepcopy(parsedTokens))
let completeContext.lastToken = lastToken
let completeContext.lastToken['insideBraceText'] = matchstr(lastToken['insideBraceText'], '[''"]\?\zs.*\ze[''"]\?')
let completeContext.fqcn = fqcn
let completeContext.complete_type = "insideQuote"
else
let lastToken = remove(parsedTokens, -1)
let fqcn = s:guessTypeOfParsedTokens(deepcopy(parsedTokens))
let completeContext.complete_type = "class"
let completeContext.last_resolutor = matchstr(cursorLine, '.*\zs\(->\|::\)\ze.*')
let completeContext.lastToken = lastToken
let completeContext.fqcn = fqcn
endif
endif
return completeContext
endfunction "}}}
function! s:guessTypeOfParsedTokens(parsedTokens) "{{{
if !exists('g:phpcomplete_index')
return ""
endif
let parsedTokens = a:parsedTokens
let objectGraph = []
let sourceFile = phpcomplete_extended#util#substitute_path_separator(expand("%:."))
let sourceFQCN = s:getFQCNFromFileLocation(sourceFile)
"if empty(sourceFQCN)
"return ""
"endif
if empty(parsedTokens)
return ""
endif
let sourceNamespace = matchstr(sourceFQCN, '\zs.*\ze\\')
let firstToken = remove(parsedTokens, 0)
let currentFQCN = ""
let parentFQCN = ""
let nonClass = has_key(firstToken, "nonClass")? firstToken['nonClass']: 0
if len(parsedTokens) == 1 && nonClass
"normal class/function completion
endif
let isThis = 0
let pluginFQCN = s:get_plugin_fqcn(currentFQCN, firstToken)
if firstToken['methodPropertyText'] == "$this"
\ || firstToken['methodPropertyText'] == "self"
\ || firstToken['methodPropertyText'] == "static"
let currentFQCN = sourceFQCN
let isThis = 1
elseif firstToken['methodPropertyText'] == "parent"
"parent
let currentClassData = phpcomplete_extended#getClassData(sourceFQCN)
if !has_key(currentClassData, 'parentclass')
return ""
endif
let currentFQCN = currentClassData['parentclass']
elseif pluginFQCN != ""
let currentFQCN = pluginFQCN
elseif has_key(firstToken, "isNew")
let currentClassData = phpcomplete_extended#getClassData(sourceFQCN)
let namespaces = {}
if has_key(currentClassData, 'namespaces')
let namespaces = currentClassData['namespaces']
endif
let currentFQCN = s:getFQCNForLocalVar(
\firstToken['methodPropertyText'], namespaces)
elseif firstToken['methodPropertyText'][0] == "$"
"local variable
let linesTilFunc = s:getLinesTilFunc(line('.'))
let currentFQCN = s:geussLocalVarType(firstToken['methodPropertyText'],
\ sourceFQCN, linesTilFunc)
elseif firstToken['methodPropertyText'][0] == "\\"
let methodPropertyText = firstToken['methodPropertyText']
let currentFQCN = s:getFQCNForNsKeyword(methodPropertyText, sourceFQCN)
return currentFQCN
else
let methodPropertyText = firstToken['methodPropertyText']
let sourceData = phpcomplete_extended#getClassData(sourceFQCN)
let aliases = {}
if !empty(sourceData) && has_key(sourceData['namespaces'], 'alias') && !empty(sourceData['namespaces']['alias'])
let aliases = sourceData['namespaces']['alias']
endif
if empty(sourceData)
let currentFQCN = ""
elseif has_key(g:phpcomplete_index['classes'], methodPropertyText)
\ || has_key(g:phpcomplete_extended_core_index['classes'], methodPropertyText)
let currentFQCN = methodPropertyText
elseif has_key(sourceData, 'namespaces')
\ && has_key(sourceData['namespaces'], 'uses')
\ && !empty(sourceData['namespaces']['uses'])
\ && has_key(sourceData['namespaces']['uses'], methodPropertyText)
let currentFQCN = sourceData['namespaces']['uses'][methodPropertyText]. "\\" . methodPropertyText
elseif !empty(aliases) && has_key(aliases, methodPropertyText)
let uses_key = aliases[methodPropertyText]
let uses_value = sourceData['namespaces']['uses'][uses_key]
let currentFQCN = uses_value == uses_key? uses_key : uses_value. "\\" . uses_key
endif
endif
if empty(currentFQCN)
return ""
endif
return s:getFQCNFromTokens(parsedTokens, currentFQCN, isThis)
endfunction "}}}
function! s:getFQCNFromTokens(parsedTokens, currentFQCN, isThis) "{{{
let parsedTokens = a:parsedTokens
let currentFQCN = a:currentFQCN
let isThis = a:isThis
let isPrevTokenArray = 0
if currentFQCN =~ '\[\]$' && len(parsedTokens)
\ && has_key(parsedTokens[0], 'isArrayElement')
let currentFQCN = matchstr(currentFQCN, '\zs.*\ze\[\]$')
let isPrevTokenArray = 1
endif
for token in parsedTokens
let insideBraceText = token['insideBraceText']
let methodPropertyText = token['methodPropertyText']
let insideBraceText = token['insideBraceText']
let isArrayElement = has_key(token, 'isArrayElement')? 1 :0
let currentClassData = phpcomplete_extended#getClassData(currentFQCN)
let pluginFQCN = s:get_plugin_fqcn(currentFQCN,token)
if insideBraceText[0] == "("
let currentFQCN = ""
elseif pluginFQCN != ""
let currentFQCN = pluginFQCN
elseif isArrayElement
let isPrevTokenArray = 0
if phpcomplete_extended#isClassOfType(currentFQCN, 'ArrayAccess')
\ && has_key(currentClassData['methods']['all'], 'offsetGet')
let offsetType = currentClassData['methods']['all']['offsetGet']['return']
if empty(offsetType)
return ""
endif
let currentFQCN = offsetType
endif
continue
else
let classPropertyType = token.isMethod == 1 ? 'method' : 'property'
let [currentFQCN, isPrevTokenArray] = phpcomplete_extended#getFQCNForClassProperty(
\ classPropertyType, methodPropertyText, currentFQCN, isThis)
endif
if isThis == 1
let isThis = 0
endif
if empty(currentFQCN)
return ""
endif
endfor
if isPrevTokenArray
return currentFQCN . '[]'
endif
return currentFQCN
endfunction "}}}
function! s:isScalar(type) "{{{
let scalars =['boolean', 'integer',"float", "string", 'array', 'object',
\ 'resource', 'mixed', 'number', 'callback', 'null', 'void',
\ 'bool', 'self', 'int', 'callable']
return index(scalars, a:type)
endfunction "}}}
function! s:getLinesTilFunc(currentLineNum) "{{{
let lineNum = a:currentLineNum
let lines = []
"get the lines till function declaration
while 1
let line = phpcomplete_extended#util#trim(getline(lineNum))
if !empty(line)
call add(lines, line)
endif
if lineNum == 1 || match(line, 'function\s*\w\+\s*(.*)') > 1
break
endif
let lineNum = lineNum - 1
endwhile
return lines
endfunction "}}}
function! s:geussLocalVarType(var, sourceFQCN, lines) "{{{
let lines = a:lines
let sourceFQCN = a:sourceFQCN
let var = a:var
let fqcn = ""
for line in lines
"echoerr match(line, a:var.'\s*=.*') >= 0
if match(line, a:var.'\s*=.*') >= 0
"var = .*
"echoerr line
let compiedLines = deepcopy(lines)
let parsedTokens = s:parseForward(
\ reverse(compiedLines),
\ line)
if empty(parsedTokens)
return ""
endif
let fqcn = phpcomplete_extended#util#trim(s:guessTypeOfParsedTokens(parsedTokens))
"echoerr fqcn
elseif match(line, '@var\s*'.var.'\s*.*') >=0
"@var $var fqcn"
let fqcn = phpcomplete_extended#util#trim(matchstr(line, '@var\s*'.var.'\s*\zs.*\ze\s*\*'))
let fqcn = phpcomplete_extended#getFQCNFromWord(fqcn)
endif
if fqcn != ""
break
endif
endfor
"try to guess from method params
if empty(fqcn)
let localMethodName = matchstr(lines[-1], 'function\s*\zs.*\ze(.*)')
let sourceClassData = phpcomplete_extended#getClassData(sourceFQCN)
if empty(sourceClassData)
return ""
endif
if !has_key(sourceClassData['methods']['all'], localMethodName)
return ""
endif
let methodParams = sourceClassData['methods']['all'][localMethodName]['params']
if empty(methodParams)
let fqcn = ""
elseif has_key(methodParams, var) && s:isScalar(methodParams[var]) == -1
let fqcn = methodParams[var]
endif
endif
return fqcn
endfunction "}}}
function! s:parseLocalVar(lines, varLine) "{{{
let parsedTokens = s:parseForward(a:lines, a:varLine)
endfunction "}}}
function! phpcomplete_extended#trackMenuChanges() "{{{
let g:complete_word = ""
let current_char = getline('.')[col('.') -2]
if !pumvisible() && s:psr_class_complete
\ && (current_char == '(' || current_char == ':' || current_char == ' ')
let s:psr_class_complete = 0
let cur_pos = getpos(".")
let prev_pos = copy(cur_pos)
let new_pos = copy(cur_pos)
let new_pos[2] = new_pos[2] -2
call setpos(".", new_pos)
let cur_word = expand("<cword>")
if cur_word[0] =~ '\d'
let keyword = matchstr(getline('.')[0:new_pos[2]-1], '\s*\zs.\{-}-\(\d\+\)\?\ze')
if match(keyword, '-') > 0
let cur_word = keyword
endif
call feedkeys("\<C-o>dF-")
call feedkeys("\<C-o>x")
call feedkeys("\<C-o>l")
else
call setpos(".", prev_pos)
endif
let fqcn = ""
if match(cur_word, '-') > 0
let word = split(cur_word, '-')[0]
let idx = split(cur_word, '-')[1] - 1
"echoerr word
if has_key(g:phpcomplete_index['class_fqcn'], word)
\ && string(g:phpcomplete_index['class_fqcn'][word])[0] == '['
let fqcn = g:phpcomplete_index['class_fqcn'][word][idx]
endif
elseif has_key(g:phpcomplete_index['class_fqcn'], cur_word)
\ && string(g:phpcomplete_index['class_fqcn'][cur_word])[0] == "'"
let fqcn = g:phpcomplete_index['class_fqcn'][cur_word]
endif
if fqcn != "" && g:phpcomplete_extended_auto_add_use
call phpcomplete_extended#addUse(cur_word, fqcn)
endif
endif
endfunction "}}}
function! phpcomplete_extended#addUse(word, fqcn) "{{{
let word = a:word
let fqcn = a:fqcn
let cur_pos = getpos('.')
if empty(fqcn)
if !has_key(g:phpcomplete_index['class_fqcn'], word)
return
endif
let fqcn_data = g:phpcomplete_index['class_fqcn'][word]
if empty(fqcn) && string(fqcn_data)[0] == '['
let fqcn_data = deepcopy(g:phpcomplete_index['class_fqcn'][word])
let prompt_data = map(copy(fqcn_data), 'v:key+1.". " . v:val')
call insert(prompt_data, "Select FQCN:")
let selected = inputlist(prompt_data)
let fqcn = fqcn_data[selected-1]
elseif empty(fqcn) && string(fqcn_data)[0] == "'"
let fqcn = fqcn_data
endif
if empty(fqcn)
return
endif
endif
let lines_to_class = getline(0, search('^\s*\(class\|interface\)'))
call setpos('.', cur_pos)
if empty(lines_to_class) || phpcomplete_extended#util#trim(lines_to_class[0]) != "<?php"
return
endif
let last_use_pos = -1
let namespace_pos = -1
for line in lines_to_class
if match(line, '^\s*use\s*'.escape(fqcn, '\').'\s*;') >= 0
return
endif
if match(line, '^\s*use\s*.*;') >=0
let last_use_pos = index(lines_to_class, line)+1
endif
if match(line, '^\s*namespace\s*.*;') >=0
let namespace_pos = index(lines_to_class, line)+1
endif
endfor
if last_use_pos == -1
let last_use_pos = namespace_pos == -1? 1 : namespace_pos + 1
endif
call append(last_use_pos, ['use '. fqcn. ';'])
let cur_pos[1] = cur_pos[1] +1
call setpos('.', cur_pos)
endfunction "}}}
function! phpcomplete_extended#gotoSymbolORDoc(type) "{{{
let data = {}
let type = a:type
if match(expand('%'), 'phpcomplete_extended-Doc') == 0
let type = 'doc'
endif
let cur_word = expand("<cword>")
let word_fqcn = s:get_plugin_resolve_fqcn(cur_word)
if word_fqcn == cur_word
let word_fqcn = phpcomplete_extended#getFQCNFromWord(cur_word)
endif
"messed up
if word_fqcn != ""
let classData = phpcomplete_extended#getClassData(word_fqcn)
if !has_key(g:phpcomplete_index['fqcn_file'], word_fqcn)
if type == "doc"
let data = {
\'doc': classData['docComment'],
\ 'title': word_fqcn
\}
elseif type == "goto"
let data = {}
endif
else
let filename = g:phpcomplete_index['fqcn_file'][word_fqcn]
let line = classData['startLine']
let data = {}
let data.file = filename
let data.command = '+' . line
let data.title = word_fqcn
let data.doc = classData['docComment']
endif
else
let data = s:getJumpDataOfCurrentWord(type)
endif
if empty(data)
if type == 'doc'
call feedkeys('K', 'n')
elseif type == 'goto'
call feedkeys("\<C-]>", 'n')
endif
return
endif
if type == 'doc'
call s:openDoc(data)
elseif type == 'goto'
call s:gotoLine(data)
endif
endfunction "}}}
function! phpcomplete_extended#getFQCNFromWord(word) "{{{
let word = a:word
let current_file_location = phpcomplete_extended#util#substitute_path_separator(fnamemodify(bufname('%'), ':p:.')) "current file
let current_fqcn = s:getFQCNFromFileLocation(current_file_location)
if current_fqcn == ""
return ""
endif
let current_class_data = phpcomplete_extended#getClassData(current_fqcn)
if empty(current_class_data)
return ""
endif
let fqcn = s:getFQCNForLocalVar(word, current_class_data['namespaces'])
if fqcn == ""
return ""
endif
return fqcn
endfunction "}}}
function! s:gotoLine(data) "{{{
silent! execute "e ". a:data.command . ' ' . a:data.file
silent! execute "normal! zt"
normal! zv
normal! zz
endfunction "}}}
function! s:openDoc(data) "{{{
let doc_buf_exists = 0
for i in range(1, winnr('$'))
let bufname = bufname(winbufnr(i))
if match(bufname, 'phpcomplete_extended-Doc') != -1
let doc_buf_exists = 1
let winnr = i
endif
endfor
"TODO: go to previous doc
if doc_buf_exists
silent! execute winnr."wincmd w"
setlocal modifiable noreadonly
normal ggdG
else
silent! execute "new"
setlocal modifiable noreadonly
setlocal nobuflisted
setlocal buftype=nofile noswapfile
setlocal bufhidden=delete
setlocal nonumber
let bufname = printf("%s: %s", 'phpcomplete_extended-Doc', a:data['title'])
silent! file `=bufname`
endif
let doc = map(split(a:data['doc'], "\n"), "substitute(v:val, ' ', '', 'g')")
call append(0, doc)
"TODO: set ft=php "slow, have to do some thing about it
normal! gg
setlocal nomodifiable readonly
endfunction "}}}
function! s:getJumpDataOfCurrentWord(type) "{{{
let cur_word = expand("<cword>")
let match_till_cur_word = matchstr(getline('.'), '\s*\zs\C.\{-}\<'.cur_word . '\>' .'(\?')
if match_till_cur_word[len(match_till_cur_word)-1] == "("
let match_till_cur_word .= "'')"
endif
let parsedTokens = phpcomplete_extended#parsereverse(match_till_cur_word, line('.'))
if empty(parsedTokens)
return {}
endif
"very messed up, have to break it
let static_or_ref = 0
if len(parsedTokens) == 1 &&
\ (
\ has_key(parsedTokens[0], 'isNew')
\ || has_key(parsedTokens[0], 'nonClass')
\ || parsedTokens[0]['methodPropertyText'][0] == "$"
\)
let lastToken = parsedTokens[0]
"elseif len(parsedTokens) == 2
"\ && match(match_till_cur_word, "::") > 0
"let lastToken = remove(parsedTokens, 0)
"let static_or_ref = 1
else
let lastToken = remove(parsedTokens, -1)
endif
if has_key(lastToken, 'nonClass')
let fqcn = lastToken['methodPropertyText']
let fqcn = s:get_plugin_resolve_fqcn(fqcn)
else
let fqcn = s:guessTypeOfParsedTokens(parsedTokens)
let fqcn = s:get_plugin_resolve_fqcn(fqcn)
if fqcn == ""
return {}
endif
endif
if static_or_ref
let methodPropertyText = parsedTokens[0]['methodPropertyText']
else
let methodPropertyText = lastToken['methodPropertyText']
endif
let fqcn_data = {}
let isInternal = 0
let isMethod = 0
let isClass = 0
let isProperty = 0
if has_key(lastToken, 'nonClass') && lastToken['nonClass']
let isInternal = 1
if static_or_ref
let isMethod = 1
let cur_word = fqcn
endif
if has_key(g:phpcomplete_extended_core_index['functions'], cur_word)
let fqcn_data = g:phpcomplete_extended_core_index['functions'][cur_word]
elseif has_key(g:phpcomplete_extended_core_index['classes'], cur_word)
let fqcn_data = g:phpcomplete_extended_core_index['classes'][cur_word]
endif
if static_or_ref
\ && has_key(fqcn_data['methods']['all'], methodPropertyText)
let fqcn_data = fqcn_data['methods']['all'][methodPropertyText]
endif
elseif has_key(lastToken, 'isMethod')
let isProperty = !lastToken['isMethod']
let isMethod = lastToken['isMethod']
let fqcn_data = {}
let method_property_key = lastToken['isMethod']? 'methods' : 'properties'
if has_key(g:phpcomplete_index['classes'], fqcn)
\ && has_key(g:phpcomplete_index['classes'][fqcn][method_property_key]['all'], methodPropertyText)
let fqcn_data = g:phpcomplete_index['classes'][fqcn][method_property_key]['all'][methodPropertyText]
elseif has_key(g:phpcomplete_extended_core_index['classes'], fqcn)
\ && has_key(g:phpcomplete_extended_core_index['classes'][fqcn][method_property_key]['all'], methodPropertyText)
let fqcn_data = g:phpcomplete_extended_core_index['classes'][fqcn][method_property_key]['all'][methodPropertyText]
endif
elseif has_key(lastToken, 'isNew')
let isClass = 1
let fqcn_data = phpcomplete_extended#getClassData(fqcn)
else
let isClass = 1
let fqcn_data = phpcomplete_extended#getClassData(fqcn)
endif
if empty(fqcn_data)
return {}
endif
if a:type == 'doc'
let title = fqcn
if isMethod
let title = fqcn. "--". methodPropertyText . "()"
endif
if isProperty
let title = fqcn. "--". methodPropertyText
endif
if isInternal
let title = fqcn
endif
return {
\'doc': fqcn_data['docComment'],
\ 'title': title
\}
elseif a:type == 'goto'
if isInternal
return {}
endif
let classData = phpcomplete_extended#getClassData(fqcn)
let gotoData = {}
if isClass
let gotoData.file = fqcn_data['file']
let gotoData.command = '+'.fqcn_data['startLine']
elseif isMethod
let gotoData.file = fqcn_data['origin']
let gotoData.command = '+'.fqcn_data['startLine']
elseif isProperty
let gotoData.file = fqcn_data['origin']
let gotoData.command ='+/^\\s*\\(private\\|public\\|protected\\|static\\)\\s*.*'.'$'.methodPropertyText
endif
return gotoData
endif
return {}
endfunction "}}}
function! s:parseForward(lines, varLine) "{{{
call remove(a:lines, 0, index(a:lines, a:varLine))
let lines = a:lines
let varDecLine = phpcomplete_extended#util#trim(matchstr(a:varLine, '=\zs.*'))
let joinedLine = varDecLine.join(lines, "")
let parsedTokens = []
let parsedTokens = phpcomplete_extended#parser#forwardParse(joinedLine, parsedTokens)
if len(parsedTokens) && !has_key(parsedTokens[-1], 'pEnd')
return []
endif
return parsedTokens
endfunction "}}}
function! phpcomplete_extended#parsereverse(cursorLine, cursorLineNumber) "{{{
if !exists('g:phpcomplete_index')
return []
endif
let cursorLine = phpcomplete_extended#util#trim(a:cursorLine)
let parsedTokens = []
let parsedTokens = phpcomplete_extended#parser#reverseParse(cursorLine, [])
if empty(parsedTokens)
\ || (len(parsedTokens) && has_key(parsedTokens[0], 'start') && parsedTokens[0].start == 0)
let linesTillFunc = s:getLinesTilFunc(a:cursorLineNumber)
let joinedLines = join(reverse(linesTillFunc),"")
let parsedTokens = phpcomplete_extended#parser#reverseParse(joinedLines, [])
return parsedTokens
endif
return parsedTokens
endfunction "}}}
function! s:getFQCNForNsKeyword(keyword, sourceFQCN) "{{{
let sourceData = phpcomplete_extended#getClassData(a:sourceFQCN)
let keyword = matchstr(a:keyword, '\\\zs.*')
let fqcn = ""
if empty(sourceData)
return ""
endif
let sourceUses = sourceData['namespaces']['uses']
let splitedKeyword = split(keyword, "\\")
if has_key(sourceUses, splitedKeyword[0])
let key = remove(splitedKeyword, 0)
let fqcn = sourceUses[key] . "\\" .keyword
else
let keywordClassData = phpcomplete_extended#getClassData(keyword)
if !empty(keywordClassData)
let fqcn = keyword
endif
endif
return fqcn
endfunction "}}}
function! s:getFQCNForLocalVar(classname, namespaces) " {{{
let classname = a:classname
let namespaces = a:namespaces
let aliases = {}
if has_key(a:namespaces, 'alias') && !empty(a:namespaces['alias'])
let aliases = a:namespaces['alias']
endif
let fqcn = ""
if has_key(g:phpcomplete_index['fqcn_file'], classname)
return classname
endif
if has_key(namespaces, 'uses') && len(namespaces['uses']) != 0 && has_key(namespaces['uses'], classname) "if no data found json_encode make it to dictionary
let fqcn = namespaces['uses'][classname]. "\\". classname
return fqcn
endif
if !empty(aliases) && has_key(aliases, classname)
let uses_key = aliases[classname]
let uses_value = namespaces['uses'][uses_key]
let fqcn = uses_value == uses_key? uses_key : uses_value. "\\" . uses_key
return fqcn
endif
if has_key(g:phpcomplete_extended_core_index['classes'], classname)
return classname
endif
let namespace_section = ""
if has_key(namespaces, 'file')
let namespace_section = namespaces['file'] . "\\"
endif
let full_fqcn = namespace_section . classname
if has_key(g:phpcomplete_index['fqcn_file'], full_fqcn)
return full_fqcn
endif
return ""
endfunction
" }}}
function! phpcomplete_extended#getFQCNForClassProperty(type, property, parent_fqcn, is_this) " {{{
let type = a:type
let is_this = a:is_this
let property = a:property
let classname = ''
let isArray = 0
let this_fqcn = a:parent_fqcn
let this_fqcn = s:get_plugin_resolve_fqcn(this_fqcn)
let this_class_data = phpcomplete_extended#getClassData(this_fqcn)
if empty(this_class_data)
return ["", 0]
endif
"in same namespace folder. so not declared in use section
if type == 'property'
let this_properties = this_class_data['properties']['all']
if !has_key(this_properties, property)
return ['', 0]
endif
let classname = this_properties[property]['type']
if has_key(this_properties[property], 'array_type')
let isArray = this_properties[property]['array_type']
endif
elseif type == 'method'
let this_methods = this_class_data['methods']['all']
if !has_key(this_methods, property)
return ['', 0]
endif
if has_key(this_methods[property], 'return')
let classname = this_methods[property]['return']
if has_key(this_methods[property], 'array_return')
let isArray = this_methods[property]['array_return']
endif
endif
endif
return [classname, isArray]
endfunction " }}}
function! s:getClassMenuEntries(base) "{{{
let class_menu_entries = deepcopy(g:phpcomplete_index['class_func_menu_entries'])
let class_menu_entries = filter(class_menu_entries, 'v:val.word =~ "^' . a:base .'" && v:val.kind == "c"')
return class_menu_entries
endfunction "}}}
function! s:getUseMenuEntries(base) "{{{
let menu_list = []
let fqcns = deepcopy(keys(g:phpcomplete_index['fqcn_file']))
if a:base != ''
"echoerr escape(a:base, ' \')
"let fqcns = filter(fqcns, 'v:val =~ "^' . escape(a:base, ' \') .'"')
let fqcns = filter(fqcns, 'v:val =~ "^' . escape(a:base, ' \\') .'"')
endif
for fqcn in fqcns
let menu_list += [{'word': fqcn,'kind': 'v', 'menu': fqcn, 'info': fqcn}]
endfor
return menu_list
endfunction "}}}
function! s:getNonClassMenuEntries(base) "{{{
let menu_entries = []
let class_func_menu_entries = deepcopy(g:phpcomplete_index['class_func_menu_entries'])
let class_func_menu_entries = filter(class_func_menu_entries, 'v:val.word =~# "^' . a:base .'"')
let plugin_menu_entries = s:get_plugin_menu_entries("", a:base, 1, 0)
let menu_entries += plugin_menu_entries
let menu_entries += class_func_menu_entries
return menu_entries