forked from imakado/php-completion
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathphp-completion.el
2180 lines (1998 loc) · 79.4 KB
/
php-completion.el
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
;;; php-completion.el -- complete everything PHP with Anything.el or helm.el
;; Copyright (c) 2009 by KAYAC Inc.
;; Author: IMAKADO <[email protected]>
;; blog: http://d.hatena.ne.jp/IMAKADO (japanese)
;; Author: Norio Suzuki <[email protected]>
;; GitHub: https://github.com/suzuki
;;
;; Prefix: phpcmp-
;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; log:
;; ver 0.04
;; * Norio Suzuki:
;; * support helm
;; * Emacs 24.3+, use cl-labels instead of labels
;; ver 0.03
;; * IMAKADO:
;; * candidates from TAGS file.
;; * added some commands.
;; * refactoring
;; ver 0.02
;; * IMAKADO:
;; * candidates from GLOBAL tag files.
;;; Commentary:
;; complete php functions, symbols, ini directives, keywords with Anything.el's Interface.
;;; Commands:
;;
;; Below are complete command list:
;;
;;
;;; Customizable Options:
;;
;; Below are customizable option list:
;;
;; `phpcmp-browse-function'
;; called with one argment URL
;; default is (quote browse-url)
;; `phpcmp-manual-url-format'
;; `%s' is replaced to query.
;; default = (cond ((ignore-errors ...) "http://jp2.php.net/manual-lookup.php?lang=ja&pattern=%s") (t "http://jp2.php.net/manual-lookup.php?lang=en&pattern=%s"))
;; `phpcmp-lighter'
;; value is displayed in the modeline when the php-completion-mode is on.
;; default = " Completion"
;; `phpcmp-global-enable-auto-update-tag-files'
;; If value is non-nil, automatically update GNU global's tag files.
;; default = nil
;; `phpcmp-showtip-timeout'
;; Seconds to wait before displaying a tooltip the first time.
;; default = 10
;; `phpcmp-showtip-top-adjust'
;; Basic adjust.
;; default = 40
;; Installation:
;;
;; put `php-completion.el' somewhere in your emacs load path.
;; add these lines to your .emacs file:
;; -------------- .emacs -----------------------------
;; (add-hook 'php-mode-hook
;; (lambda ()
;; (require 'php-completion)
;; (php-completion-mode t)
;; (define-key php-mode-map (kbd "C-o") 'phpcmp-complete)))
;; ---------------------------------------------------
;; Cooperation with auto-complete.el:
;;
;; add these lines to your .emacs file:
;; (add-hook 'php-mode-hook
;; (lambda ()
;; (when (require 'auto-complete nil t)
;; (make-variable-buffer-local 'ac-sources)
;; (add-to-list 'ac-sources 'ac-source-php-completion)
;; ;; if you like patial match,
;; ;; use `ac-source-php-completion-patial' instead of `ac-source-php-completion'.
;; ;; (add-to-list 'ac-sources 'ac-source-php-completion-patial)
;; (auto-complete-mode t))))
;; Features that might be required by this library:
;;
;; `cl' `rx' `browse-url' `url-util' `etags' `thingatpt'
;; `anything' `anything-match-plugin' `auto-complete' `pp'
;;
(require 'cl)
(require 'rx)
(require 'browse-url)
(require 'url-util)
(require 'etags)
(require 'thingatpt)
(require 'tool-bar)
(require 'auto-complete)
;; require helm or anything
(condition-case nil
(require 'helm)
(error (require 'anything)))
(defun phpcmp-setup-helm ()
"Setting up function alias for helm"
(defalias 'phpcmp-anything (symbol-function 'helm))
(defalias 'phpcmp-anything-candidate-buffer (symbol-function 'helm-candidate-buffer))
)
(defun phpcmp-setup-anything ()
"Setting up function alias for anything"
(defalias 'phpcmp-anything (symbol-function 'anything))
(defalias 'phpcmp-anything-candidate-buffer (symbol-function 'anything-candidate-buffer))
)
(if (featurep 'helm)
(progn
(require 'helm-match-plugin)
(phpcmp-setup-helm))
(progn
(require 'anything-match-plugin)
(phpcmp-setup-anything)))
;; Emacs 24.3+, use "cl-labels" instead of "labels"
(if (version<= emacs-version "24.3")
(defalias 'phpcmp-labels (symbol-function 'labels))
(defalias 'phpcmp-labels (symbol-function 'cl-labels)))
(defvar phpcmp-version 0.04)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Customize Variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defgroup php-completion nil
"php-completion"
:group 'php-completion)
(defcustom phpcmp-browse-function 'browse-url
"called with one argment URL"
:group 'php-completion
:type 'function)
(defcustom phpcmp-manual-url-format
(cond
((ignore-errors
(string= current-language-environment "Japanese"))
"http://jp2.php.net/manual-lookup.php?lang=ja&pattern=%s")
(t
"http://jp2.php.net/manual-lookup.php?lang=en&pattern=%s"
))
"`%s' is replaced to query.
see `phpcmp-search-url'"
:group 'php-completion
:type 'string)
(defcustom phpcmp-lighter " Completion"
"value is displayed in the modeline when the php-completion-mode is on."
:group 'php-completion
:type 'string)
(defcustom phpcmp-global-enable-auto-update-tag-files nil
"If value is non-nil, automatically update tag files."
:group 'php-completion
:type 'boolean)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defvar phpcmp-identifier-regexp "[a-zA-Z_][a-zA-Z_0-9]*")
;; etags
(defvar phpcmp-etags-tag-file-name "TAGS")
(defvar phpcmp-etags-tag-file-search-limit 10)
(defvar phpcmp-etags-completions-table nil
"alist, ((name . (list of candidates))
(name . (list of candidates))
...)")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Hooks ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defvar phpcmp--initialize-hook nil
"run when invoke completion command")
(defvar phpcmp-obj-instance-of-module-maybe-alist nil)
(add-hook 'phpcmp--initialize-hook
(lambda ()
(setq phpcmp-obj-instance-of-module-maybe-alist
(phpcmp-get-obj-instance-of-module-maybe-alist))))
(defun phpcmp-get-obj-instance-of-module-maybe-alist ()
(let* ((re (rx-to-string `(and (group
"$" ;1 var name
(regexp ,phpcmp-identifier-regexp))
(* space)
"="
(* space)
"new"
(+ space)
(group
(regexp ,phpcmp-identifier-regexp))))))
(save-excursion
(loop initially (goto-char (point-min))
while (re-search-forward re nil t)
collect `(,(match-string-no-properties 1) . ,(match-string-no-properties 2))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Structure ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Struct `phpcmp-tag'
;; tag-file: TAGS file path
;; path: fileのfullpath
;; classes:
;; list of Struct `phpcmp-class'
;; functions: list of functions
;; variables: list of variables
(defstruct (phpcmp-tag
(:constructor phpcmp-make-tag
(&key tag-file path relative-path classes functions variables)))
tag-file path relative-path classes functions variables)
(defstruct (phpcmp-class
(:constructor phpcmp-make-class
(&key name parent methods variables)))
name parent methods variables)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Log ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defvar phpcmp-debug nil)
(defsubst phpcmp-log-buf ()
(get-buffer-create "*php-completion debug*"))
(defsubst phpcmp-log-insert (type-str &rest messages)
(ignore-errors
(let* ((str (or (ignore-errors (apply 'format messages))
(pp-to-string (car messages))))
(str (concat type-str str "\n")))
(with-current-buffer (phpcmp-log-buf)
(goto-char (point-max))
(insert str)))))
(defsubst phpcmp-log (type &rest messages)
(ignore-errors
(when phpcmp-debug
(require 'pp)
(case type
(info (apply 'phpcmp-log-insert "info: " messages))
(debug (unless (eq phpcmp-debug 'info)
(apply 'phpcmp-log-insert "debug: " messages)))))))
(defsubst phpcmp-log-info (&rest messages)
(apply 'phpcmp-log 'info messages))
(defsubst phpcmp-log-debug (&rest messages)
(apply 'phpcmp-log 'debug messages))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Utilities ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defsubst phpcmp-get-current-directory ()
(file-name-directory
(expand-file-name
(or (buffer-file-name)
default-directory))))
(defsubst phpcmp-tramp-p ()
(when (and (featurep 'tramp)
(fboundp 'tramp-tramp-file-p))
(tramp-tramp-file-p (phpcmp-get-current-directory))))
(defun* phpcmp-collect-matches
(re &optional (count 0) (match-string-fn 'match-string)
(point-min (point-min)) (point-max (point-max)))
(save-excursion
(loop initially (goto-char point-min)
while (re-search-forward re point-max t)
collect (funcall match-string-fn count))))
(defun* phpcmp-async-do
(&key command args buffer-name
(callback 'identity)
(errorback (lambda() (message (buffer-string)))))
(lexical-let ((buf (get-buffer-create buffer-name))
(callback callback)
(errorback errorback))
(lexical-let
((sentinel (lambda (proc event)
(cond ((and (string= event "finished\n")
(= (process-exit-status proc) 0))
(with-current-buffer buf
(funcall callback))
(ignore-errors
(kill-buffer buf)))
((and (string= event "finished\n")
(/= (process-exit-status proc) 0))
(with-current-buffer buf
(funcall errorback)))))))
(set-process-sentinel (apply 'start-process command buf command args) sentinel))))
(defsubst phpcmp-preceding-line-string ()
(or (ignore-errors (buffer-substring-no-properties (point-at-bol) (point)))
""))
(defsubst* phpcmp-preceding-string (&optional (n 1))
(buffer-substring-no-properties
(point)
(or (ignore-errors
(save-excursion
(backward-char n)
(point)))
(point))))
(defun phpcmp-remove-dups (los)
"fast remove-duplicates"
(let ((hash (make-hash-table :test 'equal))
(ret nil))
(loop for s in los
do (puthash s nil hash))
(maphash (lambda (k v) (push k ret)) hash)
ret))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Candidates from php command ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; silence compiler
;; (defun phpcmp-get-functions () ())
(defun phpcmp-async-set-functions (&rest dummy))
(defun phpcmp-get-functions (&rest dummy))
(defvar phpcmp-get-functions-async-buffer-name "*php-completion functions*")
(lexical-let (set-functions-done)
(defun phpcmp-async-set-functions ()
(unless set-functions-done
(let* ((buf-name phpcmp-get-functions-async-buffer-name)
(process-running? (eq 'run
(let ((proc (get-buffer-process buf-name)))
(when (processp proc)
(process-status proc)))))
(php-command (executable-find "php")))
(when (and (not (phpcmp-tramp-p))
(not process-running?)
php-command)
(phpcmp-async-do
:buffer-name buf-name
:command php-command
:args '("-r"
"foreach (get_defined_functions() as $vars) { foreach ($vars as $var) {echo \"$var\n\";}}")
:callback (lambda ()
(let ((los (phpcmp-collect-matches "[[:print:]]+")))
(phpcmp-db-update 'functions los)
(setq set-functions-done t))))))))
(defun phpcmp-get-functions ()
(prog1 (phpcmp-db-get 'functions)
(unless set-functions-done
(phpcmp-async-set-functions)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Completion ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defvar phpcmp-initial-input nil)
(defun phpcmp-get-initial-input ()
(setq phpcmp-initial-input
(buffer-substring-no-properties (point)
(progn (save-excursion
(skip-syntax-backward "w_")
(point))))))
(defvar phpcmp--smart-sort-rules nil)
(defun phpcmp-add-smart-sort-rule (detector regexps-or-fn)
""
(add-to-list 'phpcmp--smart-sort-rules (list detector regexps-or-fn)))
(defun phpcmp-smart-sort (table)
;; silence compiler
(declare (special detector))
(let ((do-sort (lambda (regexps-or-fn ret-detector)
(cond
((functionp regexps-or-fn)
(if ret-detector
(funcall regexps-or-fn table ret-detector)
(error "phpcmp-smart-sort: regexps-or-fn is function. but detector returns nil.\n: %S"
detector)))
((or (stringp regexps-or-fn)
(consp regexps-or-fn))
(phpcmp-re-sort-table regexps-or-fn table))
(t
(error "phpcmp-smart-sort: must not error!!"))))))
(loop for (detector regexps-or-fn) in phpcmp--smart-sort-rules
with ret-detector
when (and (functionp detector)
(save-excursion
(setq ret-detector (funcall detector))))
do (setq table
(condition-case e
(funcall do-sort regexps-or-fn ret-detector)
(error (phpcmp-log-debug (error-message-string e))
table)))
finally return table)))
(defvar phpcmp-get-words-in-buffer-regexp
(rx (>= 3 (or (syntax word)
(syntax symbol)))))
(defun phpcmp-get-words-in-buffer ()
(let ((cur-point (point))
(ret nil))
(save-excursion
;; search backward
(goto-char (point-min))
(loop always (re-search-forward phpcmp-get-words-in-buffer-regexp cur-point t)
do (push (match-string-no-properties 0) ret))
;; search forward
(goto-char cur-point)
(loop always (re-search-forward phpcmp-get-words-in-buffer-regexp nil t)
do (push (match-string-no-properties 0) ret)))
;; return
(nreverse ret)))
(defun phpcmp-completions-table ()
"list of list(name get-candidates-funcion)"
(let* ((table '(("words in buffer" (lambda ()
(phpcmp-get-words-in-buffer)))
("functions" (lambda ()
(phpcmp-get-functions)))
("ini directives" (lambda ()
(phpcmp-db-get 'ini-directives)))
("constants" (lambda ()
(phpcmp-db-get 'constants)))
("keywords" (lambda ()
(phpcmp-db-get 'keywords)))
("superglobals" (lambda ()
(phpcmp-db-get 'superglobals)))
("types" (lambda ()
(phpcmp-db-get 'types)))
("global tags" (lambda ()
(phpcmp-global-get-tags)))
("global symbols" (lambda ()
(phpcmp-global-get-symbols)))
("phpunit assertions" (lambda ()
(phpcmp-db-get 'phpunit-assertions)))
("phpunit methods" (lambda ()
(phpcmp-db-get 'phpunit-methods)))))
(table (append table phpcmp-etags-completions-table)))
(run-hooks 'phpcmp--initialize-hook)
(phpcmp-smart-sort table)))
(defun phpcmp-re-sort-table (regexps table &optional reverse)
(condition-case e
(lexical-let*
((make-list (lambda (a) (if (consp a) a (list a))))
(regexps (funcall make-list regexps))
(any-match? (lambda (list)
(let ((name (first list)))
(some (lambda (re)
(string-match re name))
regexps)))))
(phpcmp-log-info "called phpcmp-re-sort-table regexps:%s" regexps)
(loop for list in table
if (funcall any-match? list)
collect list into matches
else
collect list into unmaches
finally return (if reverse
(nconc unmaches matches)
(nconc matches unmaches))))
(error table)))
(defun phpcmp-make-completion-sources ()
(phpcmp-labels ((make-source (&key name candidates)
`((name . ,name)
(init . (lambda ()
(with-current-buffer (phpcmp-anything-candidate-buffer 'global)
(insert (mapconcat 'identity
(if (functionp ',candidates)
(funcall ',candidates)
',candidates)
"\n")))))
(candidates-in-buffer)
(action . (("Insert" . (lambda (candidate)
(delete-backward-char (length phpcmp-initial-input))
(insert candidate)))
("Search". (lambda (candidate)
(phpcmp-search-manual candidate))))))))
(loop for (name candidates) in (phpcmp-completions-table)
collect (make-source
:name name
:candidates candidates))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Define Smart Completion Rule ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Class::function`!!'
(defun phpcmp-smart-sort-rule-full-name ()
(backward-sexp)
(let ((s (phpcmp-preceding-line-string)))
(when (string-match (rx-to-string `(and (group (regexp ,phpcmp-identifier-regexp)) "::" eol))
s)
(let ((s (match-string 1 s)))
(prog1 s
(phpcmp-log-info "sort-rule: full name call. returns %s" s))))))
(phpcmp-add-smart-sort-rule
'phpcmp-smart-sort-rule-full-name
(lambda (completion-table class-name) ; `phpcmp-smart-sort-rule-full-name' returns class-name
(phpcmp-re-sort-table class-name completion-table)))
(phpcmp-add-smart-sort-rule
(lambda ()
(backward-sexp)
(eq (preceding-char) ?$))
"variable")
;;; $this->`!!'
(phpcmp-add-smart-sort-rule
(lambda ()
(backward-sexp)
(let ((s (phpcmp-preceding-string 2)))
(string= s "->")))
"method")
;;; $var`!!'
(defun phpcmp-smart-sort-variable ()
(interactive)
(backward-sexp)
(let ((s (phpcmp-preceding-string 1)))
(when (and (stringp s)
(string= s "$"))
s)))
(phpcmp-add-smart-sort-rule
'phpcmp-smart-sort-variable
"variable")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Document ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun phpcmp-search-manual (query)
(let ((url (phpcmp-search-url query)))
(funcall phpcmp-browse-function url)))
(defun phpcmp-search-url (query)
(format phpcmp-manual-url-format
(url-hexify-string query)))
(defun phpcmp-get-document-string (s)
(cond
((executable-find "w3m")
(let ((url (phpcmp-search-url s)))
(let ((coding-system-for-write 'utf-8)
(coding-system-for-read 'utf-8))
(let ((docstring (shell-command-to-string (format "w3m -dump %s" (shell-quote-argument (phpcmp-search-url s))))))
(phpcmp-take-document docstring)))))
(t
"can't find \"w3m\" command")))
(defun phpcmp-take-document (docstring)
(with-temp-buffer
(cond
((stringp docstring)
(insert docstring)
(goto-char (point-min))
(re-search-forward (rx bol "説明") nil t)
(buffer-substring-no-properties (point-at-bol) (point-max)))
(t
""))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; DB ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defvar phpcmp--db (make-hash-table)
"hash table.
'functions -> list of function
'variables -> list of variable
'class -> `phpcmp--db-class'")
(defvar phpcmp--db-class (make-hash-table :test 'equal)
"String class-name -> Struct `phpcmp-class'")
;; (phpcmp-db-get 'functions)
;; (phpcmp-db-get 'variables)
;; (phpcmp-db-get 'ini-directives) ini
;; (phpcmp-db-get 'constants) constants
;; (phpcmp-db-get 'keywords) keywords
;; (phpcmp-db-get 'superglobals) superglobals
;; (phpcmp-db-get 'types) types
;; (phpcmp-db-get 'class "class-name" 'methods)
;; (phpcmp-db-get 'class "class-name" 'variables)
(defun phpcmp-db-get (type &rest args)
(case type
(class (apply 'phpcmp-db-get-class args))
(otherwise (apply 'phpcmp--db-get-otherwise type args))))
(defun phpcmp--db-get-class (class-name type)
(let ((phpcmp-class (gethash (downcase class-name) phpcmp--db-class)))
(when (phpcmp-class-p phpcmp-class)
(ecase type
(methods (append (phpcmp-class-methods phpcmp-class)
;; parent class
(let ((parent-class (phpcmp-class-parent phpcmp-class)))
(phpcmp--db-get-class parent-class 'methods))))
(variables (append (phpcmp-class-variables phpcmp-class)
(let ((parent-class (phpcmp-class-parent phpcmp-class)))
(phpcmp--db-get-class parent-class 'variables))))))))
(defun phpcmp--db-get-otherwise (key)
(gethash key phpcmp--db))
(defun phpcmp-db-update (type &rest args)
(case type
(class (apply 'phpcmp--db-update-class args))
(otherwise (apply 'phpcmp--db-update-otherwise type args))))
(defun phpcmp--db-update-class (phpcmp-class)
"`phpcmp-class' -> do update DB"
(assert (phpcmp-class-p phpcmp-class))
(let ((name (phpcmp-class-name phpcmp-class)))
(and name
(stringp name)
;; downcase name. case-insensitive.
(puthash (downcase name) phpcmp-class phpcmp--db-class))))
(defun phpcmp--db-update-otherwise (key values)
(let ((cur-val (gethash key phpcmp--db)))
(puthash key (phpcmp-remove-dups (append cur-val values)) phpcmp--db)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; GLOBAL ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun phpcmp-global-execute-command (option)
(let ((global-cmd (executable-find "global"))
(tags-not-found? (lambda (los)
(and (stringp (car los))
(string= "global: GTAGS not found." (car los))))))
(when phpcmp-global-enable-auto-update-tag-files
(phpcmp-global-auto-update-tag-files))
(when global-cmd
(with-temp-buffer
(call-process "global" nil t nil option)
(let ((los (phpcmp-collect-matches "[[:print:]]+")))
(unless (funcall tags-not-found? los)
los))))))
(defun phpcmp-global-get-symbols ()
(phpcmp-global-execute-command "-cs"))
(defun phpcmp-global-get-tags ()
(phpcmp-global-execute-command "-c"))
(defun phpcmp-global-auto-update-tag-files ()
(lexical-let* ((global-cmd (executable-find "global"))
(buf-name " *php-completion GLOBAL update*")
(process-running? (eq 'run
(let ((proc (get-buffer-process buf-name)))
(when (processp proc)
(process-status proc))))))
(when (and global-cmd
(not process-running?))
(phpcmp-async-do
:command global-cmd
:args '("-u")
:buffer-name buf-name
:callback (lambda ()
(kill-buffer buf-name))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Etags ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; this function is copied from anything-etags.el
(defun phpcmp-etags-find-tag-file ()
"Find TAGS file, if can't find TAGS file in same directory, `phpcmp-etags-find-tag-file' try to find upper directory.
`phpcmp-etags-tag-file-search-limit' is limit number to find. default 10
Return tags file(full path).
If file is not found, return nil"
(let ((file-exists? (lambda (dir)
(let ((tag-path (concat dir phpcmp-etags-tag-file-name)))
(and (stringp tag-path)
(file-exists-p tag-path)
(file-readable-p tag-path)))))
(current-dir (phpcmp-get-current-directory)))
(ignore-errors
(loop with count = 0
until (funcall file-exists? current-dir)
;; Return nil if outside the value of
;; `phpcmp-etags-tag-file-search-limit'.
if (= count phpcmp-etags-tag-file-search-limit)
do (return nil)
;; Or search upper directories.
else
do (progn (incf count)
(setq current-dir (expand-file-name (concat current-dir "../"))))
finally return (concat current-dir phpcmp-etags-tag-file-name)))))
(defun* phpcmp-etags-get-tags (&optional (tag-file (phpcmp-etags-find-tag-file)))
"Return list of struct `phpcmp-tag'"
(when (and (stringp tag-file)
(file-exists-p tag-file)
(file-readable-p tag-file))
(with-temp-buffer
(insert-file-contents tag-file)
(phpcmp-etags-parse-tags-buffer tag-file))))
(defun* phpcmp-etags-update-db (&optional (tags (phpcmp-etags-get-tags)))
(let ((update-class (lambda (phpcmp-tag)
(loop for phpcmp-class in (phpcmp-tag-classes phpcmp-tag)
when (phpcmp-class-p phpcmp-class)
do (phpcmp-db-update 'class phpcmp-class))))
(update-functions (lambda (phpcmp-tag)
(phpcmp-db-update 'functions
(phpcmp-tag-functions phpcmp-tag))))
(update-variables (lambda (phpcmp-tag)
(phpcmp-db-update 'variables (phpcmp-tag-variables phpcmp-tag)))))
(mapc (lambda (tag)
(funcall update-class tag)
(funcall update-functions tag)
(funcall update-variables tag))
tags)))
(defun phpcmp-build-class-alist (class)
(when (phpcmp-class-p class)
(let ((name (phpcmp-class-name class))
(methods (phpcmp-class-methods class))
(variables (phpcmp-class-variables class)))
`((,(concat name " methods") ,methods)
(,(concat name " variables") ,variables)))))
(defun phpcmp-build-class-alist-from-tag (tag)
(when (phpcmp-tag-p tag)
(let ((classes (phpcmp-tag-classes tag)))
(loop for class in classes
append (phpcmp-build-class-alist class)))))
(defun phpcmp-tags-to-alist (tags)
"list of `phpcmp-tag' to alist"
(loop for tag in tags
append (phpcmp-build-class-alist-from-tag tag)))
(defun phpcmp-set-etags-alist (tags)
(setq phpcmp-etags-completions-table
(nconc phpcmp-etags-completions-table (phpcmp-tags-to-alist tags))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Etags: TAGS Parser ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; This monster regexp matches an etags tag line.
;; \1 is the string to match;
;; \2 is not interesting;
;; \3 is the guessed tag name; XXX guess should be better eg DEFUN
;; \4 is not interesting;
;; \5 is the explicitly-specified tag name.
;; \6 is the line to start searching at;
;; \7 is the char to start searching at.
(defvar phpcmp-etags-parse-tags-file-regexp
(rx bol
(group ;1
(regexp "\\([^\177]+[^-a-zA-Z0-9_+*$:\177]+\\)?") ;2
(regexp "\\([-a-zA-Z0-9_+*$?:]+\\)") ;3
(regexp "[^-a-zA-Z0-9_+*$?:\177]*"))
"\177"
(regexp "\\(\\([^\n\001]+\\)\001\\)?") ;4, 5
(regexp "\\([0-9]+\\)?") ;6
","
(regexp "\\([0-9]+\\)?") ;7
"\n"
))
(defsubst phpcmp-deftag (tag-string tag-file)
(with-temp-buffer
(insert tag-string)
(goto-char (point-min))
;; Return struct `phpcmp-tag'
(phpcmp-deftag-parse tag-file)))
(defun phpcmp-etags-parse-tags-buffer (tag-file)
(let ((each-file-tag-strings
(delete "" (split-string (buffer-string) "\14[ \n]+"))))
(loop for s in each-file-tag-strings
collect (phpcmp-deftag s tag-file))))
(defsubst phpcmp-deftag-parse-file-info ()
(when (looking-at (rx bol (group (+ (not (any ",")))) "," (? (* digit)) "\n"))
(let* ((relative-file-path (match-string 1)))
(prog1 relative-file-path
(forward-line)))))
(defsubst phpcmp-take-same-indent-string ()
"move point"
(let ((cur-indent (current-indentation))
(cur-point (point)))
(forward-line)
(buffer-substring-no-properties
cur-point
(loop while (and
(not
(>= cur-indent
(current-indentation)))
(not (eobp)))
do (forward-line)
finally return (point)))))
(defsubst phpcmp-deftag-parse-class ()
(let ((class-str (phpcmp-take-same-indent-string))
name parent methods variables)
(with-temp-buffer
(insert class-str)
(goto-char (point-min))
(while (not (eobp))
(cond
((looking-at (rx bol (* space) "class" (? (*? not-newline) "extends" (+ space) (group (+ (any alpha "_"))))))
(let ((parent-class (match-string 1)))
(when parent-class
(setq parent parent-class)))
(when (looking-at phpcmp-etags-parse-tags-file-regexp)
(let ((class-name (match-string 5)))
(setq name class-name)))
(forward-line))
((looking-at (rx bol (* space) (? (or "public" "private")) (* space) "function"))
(when (looking-at phpcmp-etags-parse-tags-file-regexp)
(let ((function (match-string 5)))
(push function methods)))
(forward-line))
((looking-at (rx bol (* space) "$" (+ (any alnum "_")) (* space) "="))
(when (looking-at phpcmp-etags-parse-tags-file-regexp)
(let ((variable (match-string 5)))
(push variable variables)))
(forward-line))
(t
(forward-line)))))
(phpcmp-make-class
:name name
:parent parent
:methods methods
:variables variables)))
(defun phpcmp-deftag-parse (tag-file)
(let ((relative-file-path (phpcmp-deftag-parse-file-info)))
(let (path relative-path classes functions variables)
(while (not (eobp))
(cond
((looking-at (rx bol (* space) "class"))
(push (phpcmp-deftag-parse-class) classes))
((looking-at (rx bol (* space) (? (or "public" "private")) (* space) "function"))
(when (looking-at phpcmp-etags-parse-tags-file-regexp)
(let ((function (match-string 5)))
(push function functions)))
(forward-line))
((looking-at (rx bol (* space) "$" (+ (any alnum "_")) (* space) "="))
(when (looking-at phpcmp-etags-parse-tags-file-regexp)
(let ((variable (match-string 5)))
(push variable variables)))
(forward-line))
(t
(forward-line))))
(phpcmp-make-tag
:tag-file tag-file
:path (concat (file-name-directory tag-file)
relative-file-path)
:relative-path relative-file-path
:classes classes
:functions functions
:variables variables))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Commands ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun phpcmp-complete ()
(interactive)
(phpcmp-anything (phpcmp-make-completion-sources)
(phpcmp-get-initial-input)))
(defun phpcmp-showdoc-at-point ()
(interactive)
(let* ((s (or (thing-at-point 'symbol) ""))
(docstring (phpcmp-get-document-string s)))
(phpcmp-showtip docstring)))
(defvar phpcmp-popup-document-buffer "*php-completion document*")
(defun phpcmp-popup-document-at-point ()
(interactive)
(let* ((s (or (thing-at-point 'symbol) ""))
(docstring (phpcmp-get-document-string s)))
(let ((b (get-buffer-create phpcmp-popup-document-buffer)))
(with-current-buffer b
(erase-buffer)
(insert docstring)
(goto-char (point-min)))
(pop-to-buffer b))))
(defun phpcmp-reset-all-db ()
(interactive)
(setq phpcmp--db (make-hash-table)
phpcmp--db-class (make-hash-table :test 'equal))
(load (locate-library "php-completion")))
(defun phpcmp-read-tags-file (tags-file)
(interactive "fTAGS file: ")
(and (stringp tags-file)
(file-readable-p tags-file)
(phpcmp-set-etags-alist (phpcmp-etags-get-tags tags-file))))
(defun phpcmp-search ()
(interactive)
(let* ((initial (substring-no-properties (or (thing-at-point 'symbol) "")))
(query (completing-read "Query: " (phpcmp-ac-get-cands) nil nil initial)))
(and (stringp query)
(phpcmp-search-manual query))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; auto-complete.el ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defvar ac-source-php-completion
'((candidates . phpcmp-ac-build-cands)))
(defvar ac-source-php-completion-patial
'((candidates . phpcmp-ac-build-cands-patial)))
(defun phpcmp-ac-build-cands ()
(all-completions ac-target (phpcmp-ac-get-cands)))
(defun phpcmp-ac-build-cands-patial ()
(let ((los (phpcmp-ac-get-cands)))
(append (all-completions ac-target los)
(phpcmp-ac-build-cands1 los))))
(defun phpcmp-ac-build-cands1 (los)
(let ((match (lambda (s)
(and (stringp s)
(string-match ac-target s)
(not (string= ac-target s))))))
(remove-if-not match los)))
(defun phpcmp-ac-get-cands ()
(loop for (name get-cands-fn) in (phpcmp-completions-table)
append (if (functionp get-cands-fn)
(funcall get-cands-fn)
get-cands-fn)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; DATA ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; (let ((s (shell-command-to-string "perl scrape-funcs.pl"))
;; (insert (lambda (s) (insert (format "%S" s) "\n"))))
;; (mapcar insert (split-string s "\n")))
(defconst phpcmp-php-functions-file "phpcmp-index-of-php-functions"
"Filename of PHP functions index")
(defconst phpcmp-phpunit-assertion-file "phpcmp-index-of-phpunit-assertions"
"Filename of PHPUnit assertion index")
(defun phpcmp-get-index-list-from-file(file)
(with-temp-buffer
(insert-file-contents
(expand-file-name
file
(file-name-directory (or load-file-name buffer-file-name))))
(split-string (buffer-substring (point-min) (point-max)))))
(phpcmp-db-update
'functions
(phpcmp-get-index-list-from-file phpcmp-php-functions-file))
(phpcmp-db-update
'ini-directives
'(
"allow_call_time_pass_reference"
"allow_url_fopen"
"allow_url_include"
"always_populate_raw_post_data"
"apc.cache_by_default"
"apc.enabled"
"apc.enable_cli"
"apc.file_update_protection"
"apc.filters"
"apc.gc_ttl"
"apc.include_once_override"
"apc.localcache"
"apc.localcache.size"
"apc.max_file_size"
"apc.mmap_file_mask"
"apc.num_files_hint"
"apc.optimization"
"apc.report_autofilter"
"apc.rfc1867"