-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathac-php-core.el
2823 lines (2399 loc) · 110 KB
/
ac-php-core.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
;;; ac-php-core.el --- The core library of the ac-php
;; Copyright (C) 2019 Serghei Iakovlev <[email protected]>
;; Copyright (C) 2014-2019 jim <[email protected]>
;; Copyright (C) 2011-2016 Jan Erik Hanssen and Anders Bakken
;; Copyright (C) 2011 Joseph <[email protected]>
;; Copyright (C) 2010 Brian Jiang
;; Author: jim <[email protected]>
;; Serghei Iakovlev <[email protected]>
;; Maintainer: jim
;; URL: https://github.com/xcwen/ac-php
;; Version: 2.0.8
;; Keywords: completion, convenience, intellisense
;; Package-Requires: ( (emacs "24.4") (dash "1") (php-mode "1") (s "1") (f "0.17.0") (popup "0.5.0") (xcscope "1.0"))
;; Compatibility: GNU Emacs: 24.4, 25.x, 26.x, 27.x
;; This file is NOT part of GNU Emacs.
;;; License
;; This program 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 3 of the License, or
;; (at your option) any later version.
;; This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; The core library of the `ac-php' package. Acts like a backend for the
;; following components:
;;
;; - `ac-php'
;; - `company-php'
;; - `helm-ac-php-apropros'
;;
;; Can be used as an API to build your own components. This engine currently
;; provides:
;;
;; - Support of PHP code completion
;; - Support of jumping to definition/declaration/inclusion-file
;;
;; When creating this package, the ideas of the following packages were used:
;;
;; - auto-java-complete
;;
;; - `ac-php-remove-unnecessary-items-4-complete-method'
;; - `ac-php-split-string-with-separator'
;;
;; - auto-complete-clang
;;
;; - rtags
;;
;; - `ac-php-location-stack-index'
;;
;; Many options available under Help:Customize
;; Options specific to ac-php-core are in
;; Convenience/Completion/Auto Complete
;;
;; Known to work with Linux and macOS. Windows support is in beta stage.
;; For more info and examples see URL `https://github.com/xcwen/ac-php' .
;;
;; Bugs: Bug tracking is currently handled using the GitHub issue tracker
;; (see URL `https://github.com/xcwen/ac-php/issues')
;;; Code:
(require 'json) ; `json-encode', `json-read-file'
(require 's) ; `s-equals', `s-upcase', `s-matches-p', `s-replace', ...
(require 'f) ; `f-write-text', `f-full', `f-join', `f-exists?', ...
(require 'xcscope) ; `cscope-find-egrep-pattern', `cscope-prompt-for-symbol'
(require 'popup) ; `popup-tip'
(require 'dash)
(require 'eldoc)
(require 'cl-lib) ; `cl-reduce', `cl-decf'
;;; Customization
;;;###autoload
(defgroup ac-php nil
"Auto Completion source for PHP."
:prefix "ac-php-"
:group 'auto-complete
:group 'completion
:group 'convenience
:link '(url-link :tag "Bug Tracker" "https://github.com/xcwen/ac-php/issues")
:link '(url-link :tag "GitHub Page" "https://github.com/xcwen/ac-php")
:link '(emacs-commentary-link :tag "Commentary" "ac-php"))
(defcustom ac-php-php-executable (executable-find "php")
"Set PHP command line interpreter executable path.
For more see URL `http://php.net/manual/en/features.commandline.php'."
:group 'ac-php
:type 'string)
(defcustom ac-php-cscope (executable-find "cscope")
"Set the Csope executable path.
For more see URL `http://cscope.sourceforge.net/'."
:group 'ac-php
:type 'string)
(defcustom ac-php-use-cscope-flag nil
"Non-nil means use Cscope if it is possible.
To use this feature you'll need to set cscope executable path in
`ac-php-cscope'. For more see URL `http://cscope.sourceforge.net'."
:group 'ac-php
:type 'boolean)
(defcustom ac-php-auto-update-intval 3600
"The interval between automatic re-indexing project's files (in seconds)."
:group 'ac-php
:type 'integer)
(defcustom ac-php-project-root-dir-use-truename t
"Non-nil means always expand filenames using function `file-truename'."
:group 'ac-php
:type 'boolean)
(defcustom ac-php-mode-line
'(:eval (format "AP%s" (ac-php-mode-line-project-status)))
"Mode line lighter for ac-php.
Set this variable to nil to disable the lighter."
:group 'ac-php
:type 'sexp
:risky t)
(defcustom ac-php-tags-path (concat (getenv "HOME") "/.cache/ac-php")
"Use this directory as a base path for the per-projects tags directories..
The idea is to have a common local directory for the all projects. This path
get extended with the directory tree of the project that you are indexing the
tags for."
:group 'ac-php
:type 'string)
;;; Internal configuration
(defconst ac-php-config-file ".ac-php-conf.json"
"Per-project configuration file.")
(defvar ac-php-root-directory (file-name-directory (or load-file-name buffer-file-name))
"The ac-php package location.")
(defvar ac-php-ctags-executable (concat ac-php-root-directory "phpctags")
"Set the Phpctags executable path. Don't change the value of this variable.")
(defvar ac-php-debug-flag nil
"Non-nil means enable verbose mode when processing autocomplete.
Please notice, enabling this option entails detailed output of debugging
information to the ‘*Messages*’ buffer. This feature is designed for
ac-php developer only.")
(defvar ac-php-gen-tags-flag nil
"Non-nil means that remaking tags currently is under process.")
(defvar ac-php-phptags-index-progress 0
"The re-index progress indicator.
Meant for `ac-php-mode-line-project-status'")
;; The database of the all tags.
;;
;; A schematic designation of every element of this database:
;;
;; (tags-file-name tags-file-mtime
;; (class-list
;; functions-array
;; inherit-list
;; project-files-array
;; project-root))
;;
;; Below is a small example to understand the structure
;; of this database:
;;
;; (("/path/to/the/tags.el" 1553935654
;; (("class1" "class2" "...")
;; ["fn1" "fn2" "fn3" "fn4" "fn5" "..."]
;; ("inherit1" "inherit2" "...")
;; ["/file3.php" "/file4.php" "..."]
;; "/path/to/the/project-1/root"))
;; ("/path/to/the/tags.el" 1553935654
;; (("class1" "class2")
;; ["fn1" "fn2" "fn3" "fn4" "fn5" "..."]
;; ("inherit1" "inherit2" "...")
;; ["/file1.php" "/file2.php" "..."]
;; "/path/to/the/project-2/root"))
;; (...)
;; (...))
;;
;; The number '1553935654' in this example means the modification
;; time of the '/path/to/the/tags.el' file.
(defvar ac-php-tag-last-data-list nil
"Holds in-memory database for per-project tags.")
(defconst ac-php-re-classlike-pattern
(concat
;; Class declaration may begin at the 1st line.
;; The file may start with <?php, <? or <%.
;; Example:
;; <?php class Foo {}
"^\\(?:<\\(?:\\?\\(?:php\\)?\\|%\\)\\)?"
;; Then see if 'abstract' or 'final' appear
"\\s-*\\(?:\\(?:abstract\\|final\\)\\s-+\\)?"
;; The classlike type
"\\(?:class\\|trait\\|enum\\)"
;; Its name, which is the first captured group in the regexp.
;; See URL `https://www.php.net/manual/en/language.oop5.basic.php'
"\\s-+\\([a-zA-Z_-ÿ][a-zA-Z0-9_-ÿ]*\\)")
"The regular expression for classlike.")
(defconst ac-php-re-namespace-unit-pattern
(concat
;; First see if '\' appear, although really it is not valid for all use cases
"\\(?:\\\\\\)?"
;; We allow backslashes in the name to handle namespaces, parts of namespaces
;; and fully qualified class names, but again this is not necessarily correct
;; for all use cases.
;; See URL `https://www.php.net/manual/en/language.oop5.basic.php'
"\\(?:[a-zA-Z_-ÿ][a-zA-Z0-9_-ÿ\\]*\\)")
"The regular expression for a part of a namespace.")
(defconst ac-php-re-namespace-pattern
(concat
;; Namespace declaration may begin at the 1st line.
;; The file may start with <?php, <? or <%.
;; Example:
;; <?php namespace Acme;
"^\\(?:<\\(?:\\?\\(?:php\\)?\\|%\\)\\)?"
;; Namespace keyword
"\\s-*namespace"
;; Namespace value, which is the first captured group in the regexp
"\\s-+\\(" ac-php-re-namespace-unit-pattern "\\)\\s-*;")
"The regular expression for a namespace.")
(defconst ac-php-re-beginning-of-defun-pattern
(concat
"^\\s-*"
"\\(?:\\(?:abstract\\|final\\|private\\|protected"
"\\|public\\|static\\)\\s-+\\)"
"*function\\s-+&?\\(\\(?:\\sw\\|\\s_\\)+\\)\\s-*(")
"Regular expression for a PHP function.")
(defconst ac-php-re-annotated-var-pattern
(concat
"@var"
"\\s-+\\(" ac-php-re-namespace-unit-pattern "\\)\\>\\s-+")
"The regular expression for a class inside an annotated variable.")
(defvar ac-php-prefix-str "")
(defvar ac-php-location-stack-index 0)
(defvar ac-php-location-stack nil)
(defvar ac-php--php-key-list '("public"
"class" "namespace" "protected"
"private" "function" "while"
"extends" "return" "static" "global" "continue" "abstract" "finally" "instanceof"))
(defvar ac-php-rebuild-tmp-error-msg nil)
(defvar ac-php-max-bookmark-count 500)
;;; Utils
(defmacro ac-php--debug (format-string &rest args)
"Display a debug message at the bottom of the screen.
The message also goes into the ‘*Messages*’ buffer, if ‘message-log-max’
is non-nil. Return the debug message. For FORMAT-STRING and ARGS explanation
refer to `message' function."
`(when ac-php-debug-flag
(message (concat "[DEBUG]: " ,format-string) ,@args)))
(defun ac-php--get-timestamp (time-spec)
"Get UNIX timestamp from the TIME-SPEC."
(+ (* (nth 0 time-spec) 65536)
(nth 1 time-spec)))
(defun ac-php--reduce-path (path max-len)
"Return a modified version of PATH no longer than MAX-LEN.
This function replaces some components with single characters starting from the
left to try and get the path down to MAX-LEN"
(let* ((components (split-string (abbreviate-file-name path) "/"))
(len (+ (1- (length components))
(cl-reduce '+ components :key 'length)))
(str ""))
(while (and (> len max-len)
(cdr components))
(setq str (concat str (if (= 0 (length (car components)))
"/"
(string (elt (car components) 0) ?/)))
len (- len (1- (length (car components))))
components (cdr components)))
(concat str (cl-reduce (lambda (a b) (concat a "/" b)) components))))
(defun ac-php-g--project-root-dir (tags-data)
"Return a project path using the TAGS-DATA list."
(nth 4 tags-data))
(defsubst ac-php--in-comment-p (&optional pos)
"Determine whether POS is inside a comment."
(let ((state (save-excursion (syntax-ppss pos))))
(nth 4 state)))
(defsubst ac-php--in-string-or-comment-p (&optional pos)
"Determine whether POS is inside a string or comment."
(let ((state (save-excursion (syntax-ppss pos))))
(nth 8 state)))
;; See: https://github.com/emacs-php/php-mode/issues/503
(defun ac-php--beginning-of-defun (&optional arg)
"Move to the beginning of the ARGth PHP function from point.
A replacemant for PHP's version `php-beginning-of-defun'."
(let (found-p (arg (or arg 1)))
(while (> arg 0)
(setq found-p (re-search-backward
ac-php-re-beginning-of-defun-pattern
nil 'noerror))
(setq arg (1- arg)))
(while (< arg 0)
(end-of-line 1)
(let ((opoint (point)))
(ac-php--beginning-of-defun 1)
(forward-list 2)
(forward-line 1)
(if (eq opoint (point))
(setq found-p (re-search-forward
ac-php-re-beginning-of-defun-pattern
nil 'noerror)))
(setq arg (1+ arg))))
(not (null found-p))))
;; See: https://github.com/emacs-php/php-mode/issues/503
(defun ac-php--end-of-defun (&optional arg)
"Move the end of the ARGth PHP function from point.
A replacemant for PHP's version `php-en-of-defun'.
See `ac-php--beginning-of-defun'."
(ac-php--beginning-of-defun (- (or arg 1))))
(defsubst ac-php--in-function-p (&optional pos)
"Determine whether POS is inside a function."
(let (bof (pos (or pos (point))))
(save-excursion
(goto-char pos)
(when (ac-php--beginning-of-defun)
(setq bof (point))
(ac-php--end-of-defun)
(and (> pos bof)
(< pos (point)))))))
(defun ac-php-toggle-debug ()
"Toggle debug mode.
Please notice, enabling debug mode entails detailed output of debugging
information to the ‘*Messages*’ buffer. This feature is designed for
ac-php developer only."
(interactive)
(let ((debug-p (not ac-php-debug-flag)))
(progn
(setq ac-php-debug-flag debug-p
debug-on-error debug-p)
(message "Debug mode was %s in ac-php"
(if debug-p "enabled" "disabled")))))
(defun ac-php-mode-line-project-status ()
"Report status of current project index."
(format ":%02d%%%%" ac-php-phptags-index-progress))
(defun ac-php-location-stack-push ()
"Doc."
(let ((bm (ac-php-current-location)))
(if (functionp 'xref-push-marker-stack)
(xref-push-marker-stack)
(ring-insert (with-no-warnings find-tag-marker-ring) (point-marker)))
(while (> ac-php-location-stack-index 0)
(cl-decf ac-php-location-stack-index)
(pop ac-php-location-stack))
(unless (string= bm (nth 0 ac-php-location-stack))
(push bm ac-php-location-stack)
(when (> (length ac-php-location-stack) ac-php-max-bookmark-count)
(nbutlast ac-php-location-stack
(- (length ac-php-location-stack)
ac-php-max-bookmark-count))))))
;; function
(defun ac-php-goto-line-col (line column)
"Doc LINE COLUMN."
(goto-char (point-min))
(forward-line (1- line))
(beginning-of-line)
(forward-char (1- column)))
(defun ac-php--get-common-json-file ()
"Doc LINE COLUMN."
(concat ac-php-tags-path "/common.el" )
)
(defun ac-php-current-location (&optional offset)
"Doc OFFSET."
(format "%s:%d:%d" (or (buffer-file-name) (buffer-name))
(line-number-at-pos offset) (1+ (- (or offset (point)) (line-beginning-position)))))
(defun ac-php--string=-ignore-care(str1 str2)
"Doc STR2 STR1."
(s-equals?(s-upcase str1) (s-upcase str2))
;; (not (integer-or-marker-p (compare-strings str1 0 nil str2 0 nil t)))
)
(defun ac-php-find-file-or-buffer (file-or-buffer &optional other-window)
"Doc FILE-OR-BUFFER OTHER-WINDOW."
(if (file-exists-p file-or-buffer)
(if other-window
(find-file-other-window file-or-buffer)
(find-file file-or-buffer))
(let ((buf (get-buffer file-or-buffer)))
(cond ((not buf) (message "No buffer named %s; you can M-x: ac-php-remake-tags-all fix it" file-or-buffer))
(other-window (switch-to-buffer-other-window file-or-buffer))
(t (switch-to-buffer file-or-buffer))))))
(defun ac-php-goto-location (location &optional other-window)
"Go to a location passed in.
It can be either: file,12 or file:13:14 or plain file LOCATION OTHER-WINDOW."
;; (message (format "ac-php-goto-location \"%s\"" location))
(when (> (length location) 0)
(cond ((string-match "\\(.*\\):\\([0-9]+\\):\\([0-9]+\\)" location)
(let ((line (string-to-number (match-string-no-properties 2 location)))
(column (string-to-number (match-string-no-properties 3 location))))
(ac-php-find-file-or-buffer (match-string-no-properties 1 location) other-window)
;; (run-hooks ac-php-after-find-file-hook)
(ac-php-goto-line-col line column)
t))
((string-match "\\(.*\\):\\([0-9]+\\)" location)
(let ((line (string-to-number (match-string-no-properties 2 location))))
(ac-php-find-file-or-buffer (match-string-no-properties 1 location) other-window)
;; (run-hooks ac-php-after-find-file-hook)
(goto-char (point-min))
(forward-line (1- line))
t))
((string-match "\\(.*\\),\\([0-9]+\\)" location)
(let ((offset (string-to-number (match-string-no-properties 2 location))))
(ac-php-find-file-or-buffer (match-string-no-properties 1 location) other-window)
;; (run-hooks ac-php-after-find-file-hook)
(goto-char (1+ offset))
t))
(t
(if (string-match "^ +\\(.*\\)$" location)
(setq location (match-string-no-properties 1 location)))
(ac-php-find-file-or-buffer location other-window)))
;; (ac-php-location-stack-push)
))
(defsubst ac-php-clean-document (s)
"Doc S."
(when s
(setq s (replace-regexp-in-string "<#\\|#>\\|\\[#" "" s))
(setq s (replace-regexp-in-string "#\\]" " " s)))
s)
(defun ac-php--tag-name-is-function (tag-name)
"Doc TAG-NAME."
(s-matches-p "(" tag-name))
;; "Split STR into substrings bounded by REGEXP.
;; This function is a tool like `split-string', but it treat separator as an
;; element of returned list for example:
;; \(ac-php-split-string-with-separator 'abc.def.g' '\\.' '.')
;; will return:
;; '('abc' '.' 'def' '.' 'g')
;; The REPLACEMENT may used to return instead of REGEXP. For OMIT-NULLS
;; refer to original `split-string' function.
;; Note: To conveniently describe in the documentation, double quotes (\") have
;; been replaced by '."
(defun ac-php-split-string-with-separator (str regexp &optional replacement omit-nulls)
"Split STR into substrings bounded by REGEXP.
The REPLACEMENT may used to return instead of REGEXP. For OMIT-NULLS
refer to original `split-string' function.
Note: To conveniently describe in the documentation, double quotes (\") have
been replaced by '."
(when str
(let (split-list substr match-end)
(if (string-match regexp str)
(progn
(while (string-match regexp str)
(setq match-end (match-end 0))
(setq substr (substring-no-properties str 0 (- match-end 1)))
(when (or (not omit-nulls) (> (length substr) 0))
(setq split-list (append split-list (list substr))))
(setq split-list (append split-list (list (or replacement regexp))))
(setq str (substring-no-properties str match-end)))
(when (or (not omit-nulls) (> (length str) 0))
(setq split-list (append split-list (list str)))))
(setq split-list (list str)))
split-list)))
;; "Clean PARSER-DATA from unnecessary elements.
;; This function is used to drop all elements before ';'. For example:
;; \(ac-php--get-clean-node '('A' ';' 'B'))
;; will return:
;; \('B')
;; The CHECK-LEN may be passed to indicate the limit to analyze items:
;; \(ac-php--get-clean-node '('A' 'B' 'C' 'D') 2)
;; will return:
;; \('A' 'B')
;; Note: To conveniently describe in the documentation, double quotes (\") have
;; been replaced by '."
(defun ac-php--get-clean-node (parser-data &optional check-len)
"Clean PARSER-DATA from unnecessary elements.
The CHECK-LEN may be passed to indicate the limit to analyze items."
(ac-php--debug "Going to clean parser data: %S" parser-data)
(let ((i 0) ret-data item)
(unless check-len
(setq check-len (length parser-data)))
(while (< i check-len)
(setq item (nth i parser-data))
(if (and (stringp item)
(string= item ";"))
(setq ret-data nil)
(push item ret-data))
(setq i (1+ i)))
(setq ret-data (reverse ret-data))
(ac-php--debug "Parser data after cleaning up is: %S" ret-data)
ret-data))
(defun ac-php--get-node-parser-data (parser-data)
"Get keywords node from a PARSER-DATA."
(let* ((check-len (1- (length parser-data)))
(last-item (nth check-len parser-data))
ret-data)
(if (and (stringp last-item)
(string= last-item "__POINT__"))
(setq ret-data (ac-php--get-clean-node parser-data check-len))
;; TODO: Until version 2.0.7 the code below worked incorrectly.
;; Previous implementation just did the following test:
;;
;; (when last-item
;; (setq ret-data (ac-php--get-node-parser-data last-item)))
;;
;; So I fixed this. However I'll need to verify that
;; all still works as expected. Consider this as an experimental branch.
(when (and last-item (listp last-item))
(progn
(setq ret-data (ac-php--get-node-parser-data last-item))
(ac-php--debug "The node after deep scan is: %S" ret-data))))
ret-data))
(defun ac-php--get-key-list-from-parser-data (parser-data)
"Get keywords list from the PARSER-DATA list."
(ac-php--debug "Building a key list from the parser data: %S" parser-data)
(let ((first-key (nth 0 parser-data))
item
(i 1)
ret
(parser-data-len (length parser-data)))
(if (and (listp first-key) first-key)
(setq ret (ac-php--get-clean-node
(ac-php--get-key-list-from-parser-data first-key)))
(if (and (> parser-data-len 1) (listp (nth 1 parser-data)))
(setq ret (list (concat first-key "(")))
(setq ret (list first-key))))
(while (< i parser-data-len)
(setq item (nth i parser-data))
(cond
((and (stringp item)
(and (< (1+ i) parser-data-len)
(listp (nth (1+ i) parser-data))))
;; function
(setq ret (append ret (list (concat item "("))))
(setq i (+ i 2)))
((stringp item)
;; variable
(setq ret (append ret (list item)))
(setq i (1+ i)))
(t (setq i (1+ i)))))
ret))
;; "Remove unnecessary items in the SPLITED-LINE-ITEMS.
;; Used to sanitize auto completion data. Below are some examples for possible
;; return values:
;; :-------------------------------:------------------------:
;; | SPLITED-LINE-ITEMS | Will return |
;; :-------------------------------------------:------------:
;; | ('foo' '.' 'bar' '(' ')' '.') | ('foo' '.' 'bar(' '.') |
;; | ('foo' '.' 'bar' '(' 'a') | ('a') |
;; | ('foo' '.' 'bar') | ('foo' '.' 'bar') |
;; | ('foo' '.') | ('foo' '.') |
;; | ('foo') | ('foo') |
;; :-------------------------------:------------------------:
;; Meant for `ac-php-get-class-at-point' .
;; Note: To conveniently describe in the documentation, double quotes (\") have
;; been replaced by '."
(defun ac-php-remove-unnecessary-items-4-complete-method (splited-line-items)
"Remove unnecessary items in the SPLITED-LINE-ITEMS.
Used to sanitize auto completion data. Below are some examples for possible
return values:
Meant for `ac-php-get-class-at-point' .
Note: To conveniently describe in the documentation, double quotes (\") have
been replaced by '."
(ac-php--debug "Start removing unnecessary items for complete method...")
(ac-php--debug "Intial items are: %S" splited-line-items)
(let ((need-add-right-count 1)
(item-count (length splited-line-items))
(i 0)
item
(elisp-str "(")
parser-data
ret)
(while (< i item-count)
(setq item (nth i splited-line-items))
(cond
((string= "(" item)
(setq elisp-str (concat elisp-str "("))
(setq need-add-right-count (1+ need-add-right-count)))
((string= ")" item)
(when (> need-add-right-count 0)
(setq elisp-str (concat elisp-str ")"))
(setq need-add-right-count (1- need-add-right-count)))
)
(t
(setq elisp-str (concat
elisp-str "\""
(s-replace "\\" "\\\\" item) "\" "))))
(setq i (1+ i)))
(if (> need-add-right-count 0)
(progn
(setq elisp-str (concat elisp-str "\"__POINT__\""))
(setq i 0)
(while (< i need-add-right-count)
(setq elisp-str (concat elisp-str ")"))
(setq i (1+ i))))
(setq elisp-str "()"))
(ac-php--debug "Prepared Elisp string to read: %s" elisp-str)
(setq parser-data (read elisp-str))
(unless parser-data
(setq parser-data (read (concat "(" elisp-str ")") ))
)
(setq parser-data (ac-php--get-node-parser-data parser-data))
(setq ret (ac-php--get-key-list-from-parser-data parser-data))
(ac-php--debug "The list after removing unnecessary items is: %S" ret)
ret))
(defun ac-php--get-class-full-name-in-cur-buffer (first-key function-map get-return-type-flag)
"DOCSTRING FIRST-KEY FUNCTION-MAP GET-RETURN-TYPE-FLAG."
(let (cur-namespace tmp-name ret-name tmp-ret)
(let (split-arr cur-class-name)
(ac-php--debug "ac-php--get-class-full-name-in-cur-buffer first-key:%s" first-key)
(when (string= "this" first-key)
(setq first-key (ac-php-get-cur-full-class-name)))
(if (ac-php--check-global-name first-key)
(setq tmp-name first-key)
(progn
(setq split-arr (s-split-up-to "\\\\" first-key 1))
(ac-php--debug " split-arr 22 len:%d " (length split-arr))
;; check for use
(cond
((= 2 (length split-arr))
(setq cur-namespace (nth 0 split-arr))
(setq cur-class-name (nth 1 split-arr))
(setq tmp-name (ac-php-get-use-as-name cur-namespace))
(ac-php--debug "tmp-name 22 %s" tmp-name)
(if tmp-name
(setq tmp-name (concat tmp-name "\\" cur-class-name))
(setq tmp-name first-key)))
((= 1 (length split-arr))
;; check use as
(setq cur-class-name (nth 0 split-arr))
(setq tmp-name (ac-php-get-use-as-name cur-class-name))
(unless tmp-name (setq tmp-name first-key))
(ac-php--debug "XXXX %s " tmp-name)))
(unless (ac-php--check-global-name tmp-name)
(let ((tmp-name-as-global (concat "\\" tmp-name))
(cur-namepace-tmp-name (concat (ac-php-get-cur-namespace-name) tmp-name)))
(ac-php--debug " check as cur namespace %s " cur-namepace-tmp-name )
(if (ac-php--get-item-from-funtion-map cur-namepace-tmp-name function-map)
(setq tmp-name cur-namepace-tmp-name)
(setq tmp-name tmp-name-as-global))))
(ac-php--debug "22222 %s " tmp-name))))
(when tmp-name
(setq tmp-name (ac-php--as-global-name tmp-name))
(setq tmp-ret (ac-php--get-item-from-funtion-map tmp-name function-map))
(ac-php--debug "11 tmp-re %s=> %S" tmp-name tmp-ret)
(if tmp-ret
(if get-return-type-flag
(setq ret-name (aref tmp-ret 4))
(setq ret-name (aref tmp-ret 1)))))
(unless ret-name
(setq tmp-name (if (ac-php--check-global-name first-key) first-key (concat "\\" first-key)))
(setq tmp-ret (ac-php--get-item-from-funtion-map tmp-name function-map))
(ac-php--debug "22 tmp-ret %S" tmp-ret)
(if tmp-ret
(if get-return-type-flag
(setq ret-name (aref tmp-ret 4))
(setq ret-name (aref tmp-ret 1)))))
(ac-php--debug " ac-php--get-class-full-name-in-cur-buffer ret-name %s" ret-name)
ret-name))
;; "This function is used to tokinize PHP string.
;; First this function will split LINE-STRING to small items.
;; For example, suppose LINE-STRING is:
;; '$class->method($parameter)'
;; then this function split it to:
;; 'class' '.' 'method' '(' 'parameter' ')'
;; Note: To conveniently describe in the documentation, double quotes (\") have
;; been replaced by '."
(defun ac-php-split-line-4-complete-method (line-string)
"This function is used to tokinize PHP string.
First this function will split LINE-STRING to small items.
For example, suppose LINE-STRING is:
Note: To conveniently describe in the documentation, double quotes (\") have
been replaced by '."
(ac-php--debug "Start splitting the string to items")
(save-excursion
(let ((stack-list nil)
(old-string line-string))
;; "a sequence of characters" => string
(setq line-string (replace-regexp-in-string
"\".*?\"" "string"
line-string))
;; dot => ;
(setq line-string (replace-regexp-in-string
"[.]" ";"
line-string))
;; foo:bar => foo;bar
(setq line-string (replace-regexp-in-string
"\\([^:]\\):\\([^:]\\)" "\\1;\\2"
line-string))
;; class->method => class.method
(setq line-string (replace-regexp-in-string
"[ \t]*\\??->[ \t]*" "."
line-string) )
;; :: => ::.
(setq line-string (replace-regexp-in-string
"[ \t]*::[ \t]*" "::."
line-string))
;; new | return | echo => ;
(setq line-string (replace-regexp-in-string
"\\bnew\\b\\|\\breturn\\b\\|\\becho\\b" ";"
line-string))
;; case | yield => ;
(setq line-string (replace-regexp-in-string
"\\bcase\\b\\|\\byield\\b" ";"
line-string))
;; $ => (empty string)
(setq line-string (replace-regexp-in-string
"\\$" ""
line-string))
;; @ | equal operators => ;
(setq line-string (replace-regexp-in-string
"@\\|!?=>?\\|<=?\\|>=?\\|=" ";"
line-string))
;; Some operators => ;
(setq line-string (replace-regexp-in-string
"[&|!,?^+/*\-]" ";"
line-string))
(unless (string= old-string line-string)
(ac-php--debug "Input string was changed during to splitting: \"%s\""
line-string))
;; Split ‘line-string’ with ".", but add "." as an element at
;; its position in list
(setq stack-list (ac-php-split-string-with-separator
line-string "[ \t]*\\.[ \t]*" "." t))
(let ((ele)(tmp-list))
(cl-dolist (ele stack-list)
(setq tmp-list
(append tmp-list
(ac-php-split-string-with-separator ele "[{}]" ";" t))))
(setq stack-list tmp-list))
(let ((ele)(tmp-list))
(dolist (ele stack-list)
(setq tmp-list
(append tmp-list
(ac-php-split-string-with-separator ele "[>)]\\|]" ")" t))))
(setq stack-list tmp-list))
(let ((ele)(tmp-list))
(dolist (ele stack-list)
(setq tmp-list
(append tmp-list
(ac-php-split-string-with-separator ele "[<([]" "(" t))))
(setq stack-list tmp-list))
(let ((ele)(tmp-list))
(dolist (ele stack-list)
(setq tmp-list
(append tmp-list
(ac-php-split-string-with-separator ele ";" ";" t))))
(setq stack-list tmp-list))
(let ((ele)(tmp-list))
(dolist (ele stack-list)
(setq tmp-list
(append tmp-list (split-string ele "[ \t]+" t))))
(setq stack-list tmp-list))
stack-list)))
(defun ac-php-get-syntax-backward (regexp &rest args)
"Search backward from current point for regular expression REGEXP.
Possible additional ARGS:
:sexp Specifies which parenthesized expression in the REGEXP
should be returned.
:comment Indicates should we search inside a comment or not.
:defun Indicates should we search inside a defun or not.
:bound A buffer position that bounds the search. The match found must
not end after that position. A value of nil means search to the
end of the accessible portion of the buffer.
Return a propertized string in a format:
#(\"some string\" 0 11 (pos POINT))
where POINT is a point position that bounds the search. Return nil in case of
unsuccessful search."
(let ((old-cfs case-fold-search)
(found-p nil)
line-txt
ret-str
search-pos
in-comment-ctx
in-defun-ctx
(sexp (plist-get args :sexp))
(in-comment-p (plist-get args :comment))
(in-defun-p (plist-get args :defun))
(bound (plist-get args :bound)))
(save-excursion
(ac-php--debug "Search backward from current point up to %s"
(if bound (format "point: %d" bound)
"accessible portion of the buffer"))
(ac-php--debug "Used regular expression: \"%s\"" regexp)
(while (not found-p)
(setq search-pos (re-search-backward regexp bound t 1))
(if search-pos
(progn
(setq
;; Determine actual comment context
in-comment-ctx (if in-comment-p
(ac-php--in-comment-p (point))
(not (ac-php--in-string-or-comment-p (point))))
;; Determine actual defun context
in-defun-ctx (if in-defun-p
(ac-php--in-function-p (point))
(not (ac-php--in-function-p (point)))))
(when (and in-comment-ctx in-defun-ctx)
(setq line-txt (buffer-substring-no-properties
(line-beginning-position)
(line-end-position)))
(when (string-match regexp line-txt)
(setq ret-str (match-string sexp line-txt))
(setq ret-str (propertize ret-str 'pos search-pos))
(setq found-p t))))
(setq found-p t))))
(setq case-fold-search old-cfs)
(ac-php--debug "Search result: %s" ret-str)
ret-str))
(defun ac-php-get-cur-class-name ()
"Get current class name.
Tries to retrieve current class name if it is possible.
Returns the name of the current class as a string or nil if the search failed."
(ac-php-get-syntax-backward
ac-php-re-classlike-pattern
:sexp 1))
(defun ac-php-get-cur-namespace-name (&optional trim-trailing-backslash-p)
"Get fully qualified namespace.
Tries to retrieve current fully qualified namespace if it is possible.
TRIM-TRAILING-BACKSLASH-P is used to indicate whether we should trim trailng
backslash or not. Always returns a string, even if the namespace was not found."
(let (namespace (not-found ""))
(setq namespace (ac-php-get-syntax-backward
ac-php-re-namespace-pattern
:sexp 1))
(if namespace
(progn
;; Concatenate leading backslash
(unless (string= (substring namespace 0 1) "\\")
(setq namespace (concat "\\" namespace)))
;; Trim trailng backslash
(setq namespace (replace-regexp-in-string "\\\\$" "" namespace))
;; Add trailing backslash only if needed
(if (not trim-trailing-backslash-p)
(setq namespace (concat namespace "\\"))
namespace))
not-found)))
(defun ac-php-clean-namespace-name (namespace-name)
"Doc NAMESPACE-NAME."
(if (and (stringp namespace-name)
(> (length namespace-name) 1)
(string= (substring-no-properties namespace-name 0 1) "\\"))
(substring-no-properties namespace-name 1)
namespace-name))
(defun ac-php-get-cur-full-class-name ()
"Get current class name in a fully qualified form.
Returns nil if could not find class name in current buffer."
(let (class-name namespace)
(setq class-name (ac-php-get-cur-class-name)
namespace (ac-php-get-cur-namespace-name))
(if class-name
(progn
(when (string= "" namespace)
(setq namespace "\\"))
(concat namespace class-name))
nil)))
(defun ac-php-get-use-as-name (item-name)
"DOCSTRING ITEM-NAME."
(let ((item-name (nth 0 (s-split "(" item-name))))
(or
(ac-php-get-syntax-backward
(concat
"^[ \t]*use[ \t]+\\("
ac-php-re-namespace-unit-pattern
"\\\\"