-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathphp-funcs.el
1415 lines (1349 loc) · 65.8 KB
/
php-funcs.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-funcs.el --- Utility functions for php and zf modes.
;; Version: 2.0
;; Created: 10-1-2009
;; Copyright © 2009 Brian Zwahr
;; Author(s):
;; Brian Zwahr <[email protected]>
;;; *****
;;; About
;;; *****
;; php-funcs.el is a part of the php+-mode suite and contains
;; convenience functions for php programming, such as moving between
;; methods and quick insertion of common code.
;; The functions below work in the standard that places the opening
;; brace for classes, functions, etc. on the next line and for ifs,
;; fors, etc. on the same line.
;; ********************************************************************
;; ************
;; REQUIREMENTS
;; ************
(require 'cl)
(require 'php-parse)
(require 'thingatpt)
;; *********
;; VARIABLES
;; *********
(defcustom php-hide-show-ignore-extensions '()
"File extensions to ignore when running hide-show
automatically."
:type '(repeat :tag "File extension" (string))
:group 'php)
(defcustom php-hide-show-hide-doc-blocks nil
"Whether `php-hide-innards' should hide the initial
docblocks. This will currently only work on
constant/property/method docblocks. The ability to choose
file/class/interface docblocks may be added in the future."
:type 'boolean
:group 'php)
(defcustom php-jump-show-hidden t
"Whether jump commands should show things hidden by hideshow."
:type 'boolean
:group 'php)
(defcustom php-doc-tag "EOT"
"The default tag to use for PHP {here,now}docs."
:type 'string
:group 'php)
; declared for compiler
(defvar php+-mode-protected-underscore)
;; *********
;; FUNCTIONS
;; *********
; declarations for compiler
(declare-function hs-show-all "hideshow")
(declare-function hs-show-block "hideshow")
(declare-function hs-hide-block "hideshow")
(declare-function hs-overlay-at "hideshow")
(declare-function php-single-quote-string "php-string")
(declare-function php-format-region "php-format")
(declare-function php-format-spacing "php-format")
(declare-function php-doc-class-insert "php-doc")
(declare-function php-doc-method-insert "php-doc")
(defun php-jump-to-first-struct (type)
"Jumps to the first TYPE structure in the file. TYPE may be a
list."
(let ((types (if (listp type) type `(,type)))
(file (php-parse-current 'file))
(structs '()))
(when (php-parse-p file)
(let* ((scripts (rest (assq 'scripts file)))
(structs
(catch 'found
(dolist (type types)
(let ((structs
(apply 'append
(mapcar
(lambda (s)
(rest (assq (php-parse-pluralize-type type)
s))) scripts))))
(when structs
(throw 'found structs)))))))
(when structs
(let ((first-struct (catch 'found
(dolist (s structs)
(when (php-parse-p s)
(throw 'found s))))))
(goto-char (rest (assq 'begin first-struct)))))))))
(defun php-jump-to-first-class/interface ()
"Jumps to the first class/interface in the file."
(php-jump-to-first-struct '(class interface)))
(defun php-hide-innards (&optional show just-doc-blocks force-doc-blocks)
"This function goes through the buffer and hides (closes) all
methods in a class. If `php-hide-show-hide-doc-blocks' is set, it
will also hide any accompanying doc blocks. Optionally, SHOW
instead of hide. You may also hide JUST-DOC-BLOCKS and/or
FORCE-DOC-BLOCKS. This requires hideshow."
(interactive)
(when (and hs-minor-mode
(buffer-file-name)
(not (member (file-name-extension (buffer-file-name) t)
php-hide-show-ignore-extensions)))
(save-excursion
(goto-char (point-min))
(let ((in-class (php-jump-to-first-class/interface)))
(when in-class
(php-jump-to-first-statement))
(when (not just-doc-blocks)
(if show
(hs-show-all)
(hs-hide-level (if in-class 1 0))))
(when (or force-doc-blocks php-hide-show-hide-doc-blocks)
(let* ((top-struct (php-parse-current (if in-class
'(class interface)
'script)))
(structs (if in-class
(append (rest (assq 'constants top-struct))
(rest (assq 'properties top-struct))
(rest (assq 'methods top-struct)))
(rest (assq 'methods top-struct)))))
(dolist (struct structs)
(goto-char (rest (assq 'begin struct)))
(when (looking-at-p "/")
(if show
(hs-show-block)
(hs-hide-block))))))))))
(defun php-show-all ()
"This is just a wrapper around hideshow's hs-show-all.
We have this wrapper to make sure that hide-show is loaded first."
(when hs-minor-mode
(hs-show-all)))
(defun php-get-named-things (&optional types)
"Returns a list of parses of all named things in the current
buffer. Optionally limit to TYPES"
(let* ((types (or types '(interfaces classes methods properties constants)))
(types (if (listp types) types `(,types)))
(file (php-parse-current 'file)))
(when (php-parse-p file)
(apply 'append (mapcar (lambda (script)
(let (things)
(dolist (type types things)
(setf things
(append (rest (assq type script))
things)))))
(rest (assq 'scripts file)))))))
(defun php-get-thing-names (&optional types)
"Returns a list of all the names of PHP structures in the
current buffer. Optionally limit to TYPES."
(mapcar (lambda (thing)
(rest (assq 'name thing)))
(php-get-named-things types)))
(defun php-get-method-arg-names (&optional with-dollar method-name)
"Return a list of argument names for the method at point or
METHOD-NAME."
(when (or (not method-name)
(php-jump-to-thing method-name))
(let ((parse (php-parse-current 'method)))
(when (php-parse-p parse)
(mapcar (lambda (x)
(if with-dollar
(rest (assoc 'name x))
(replace-regexp-in-string "[$&]" ""
(rest (assoc 'name x)))))
(rest (assoc 'arguments parse)))))))
(defun php-find-thing (name)
"Finds the PHP structure named NAME in the
current buffer."
(let* ((file (php-parse-current 'file)))
(catch 'found
(dolist (script (rest (assq 'scripts file)))
(dolist (thing (append (rest (assq 'interfaces script))
(rest (assq 'classes script))
(rest (assq 'methods script))
(rest (assq 'properties script))
(rest (assq 'constants script))))
(when (string= name (rest (assq 'name thing)))
(throw 'found thing)))))))
(defvar php-jump-showed-hidden nil
"Whether php-jump-first-statement ran hs-show-block.")
(defun php-jump-to-declaration (&optional type)
"Jumps to the first non-documentation line in the current
struct. Optionally restrict TYPE according to the semantics of
`php-parse-current'. Returns (point) at the location."
(let* ((type (or type '(constant property method class interface script)))
(parse (php-parse-current type)))
(when (php-parse-p parse)
(goto-char (rest (assq 'begin parse)))
(goto-char (+ 1 (point) (length (rest (assq 'documentation-text parse)))))
(beginning-of-line-non-whitespace)
(point))))
(defun php-jump-to-first-statement (&optional struct no-show)
"Auxilliary function. Will start at beginning of struct or
point. Finds the beginning of the first statment in struct.
Will show any hide-show hidden blocks unless NO-SHOW.
Returns (point)."
(unless (php-parse-p struct)
(setq struct (php-parse-current '(constant property method class
interface))))
(when (php-parse-p struct)
(goto-char (rest (assq 'begin struct))))
(when (looking-at-p "/\\*")
(forward-char 3)
(when (and hs-minor-mode (hs-overlay-at (point)) php-jump-show-hidden
(not no-show))
(hs-show-block)
(setq php-jump-showed-hidden t))
(re-search-forward (concat "\\*/" ws-re "*") nil t))
(let ((begin (point))
(type (rest (assq 'type struct))))
(when (or (member type '(constant property))
(and (eq type 'method)
(not (rest (assq 'abstractp struct)))))
(save-excursion
(let ((end (save-excursion
(cond ((eq type 'method)
(re-search-forward "{" nil t)
(re-search-backward ")" nil t))
(t (re-search-forward ";" nil t)))
(point))))
(when (re-search-forward "(" end t)
(backward-char)
(catch 'done
(while (< (point) end)
(forward-char)
(when (and hs-minor-mode (hs-overlay-at (point))
php-jump-show-hidden (not no-show))
(hs-show-block)
(setq php-jump-showed-hidden t)
(throw 'done (point)))))))))
(re-search-forward "[;{]" nil t)
(when (and hs-minor-mode (hs-overlay-at (point)) php-jump-show-hidden
(not no-show))
(hs-show-block)
(setq php-jump-showed-hidden t))
(if (= (char-before) ?\;)
(goto-char begin)
(let ((begin (point)))
(re-search-forward non-ws-re nil t)
(if (= (char-before) ?\})
(goto-char begin)
(backward-char)))))
(point))
(defun php-jump-hide-if-last-command-was-jump ()
(when (and (member last-command '(php-jump-to-next-thing
php-jump-to-previous-thing))
php-jump-showed-hidden)
(let ((parse (php-parse-current 'method)))
(when (and hs-minor-mode
(eq 'method (rest (assq 'type parse)))
(not (rest (assq 'abstractp parse))))
(hs-hide-block)
(re-search-backward ")" nil t)
(hs-hide-block)
(setq php-jump-showed-hidden nil))
(when (and hs-minor-mode
php-hide-show-hide-doc-blocks
(rest (assq 'documentation parse)))
(save-excursion
(goto-char (rest (assq 'begin parse)))
(hs-hide-block)
(setq php-jump-showed-hidden nil))))))
(defun php-get-first/last-struct-in-class/interface (first-or-last)
"Returns the parse of the FIRST-OR-LAST
constant/property/method in the first class/interface in the
file."
(let* ((things (php-get-named-things))
(parse
(first
(sort (remove-if-not (lambda (p)
(let ((type
(rest (assq 'type p))))
(member type
'(class interface)))) things)
(lambda (a b)
(cond ((eq first-or-last 'last)
'php-parse-pos>)
((eq first-or-last 'first)
'php-parse-pos<))))))
(parse
(first
(sort (append (rest (assq 'constants parse))
(rest (assq 'properties parse))
(rest (assq 'methods parse)))
(cond ((eq first-or-last 'last) 'php-parse-pos>)
((eq first-or-last 'first) 'php-parse-pos<))))))
parse))
(defun php-jump-to-next/previous-thing (direction)
"This function moves the cursor to the first statement in the
next/previous PHP structure."
(unless (member direction '(previous next))
(error (format "Invalid direction %s!" direction)))
(let ((parse (php-parse-current '(constant property method))))
(unless (catch 'moved
(when (php-parse-p parse)
(let ((begin (rest (assq 'begin parse)))
(end (rest (assq 'end parse)))
(first-statement (save-excursion
(let ((php-jump-show-hidden nil))
(php-jump-to-first-statement)))))
(if (or (and (eq direction 'previous)
(and (> (point) first-statement)
(<= (point) end)))
(and (eq direction 'next)
(and (>= (point) begin)
(< (point) first-statement))))
(progn
(php-jump-to-first-statement)
(throw 'moved t))
(let ((first-or-last (cond ((eq direction 'previous) 'first)
((eq direction 'next) 'last))))
(when (equal
parse
(php-get-first/last-struct-in-class/interface
first-or-last))
(throw 'moved t)))
(goto-char (cond ((eq direction 'previous) begin)
((eq direction 'next) end)))
(when (and (eq direction 'next) (looking-at-p "//"))
(end-of-line))
(throw 'moved nil)))))
(let* ((search-func (cond ((eq direction 'previous) 're-search-backward)
((eq direction 'next) 're-search-forward)))
(thing (save-excursion
(funcall search-func non-ws-re nil t)
(when (eq direction 'previous)
(let ((bound (point)))
(beginning-of-line)
(if (re-search-forward "//" bound t)
(backward-char 2)
(goto-char bound))))
(let* ((parse (php-parse-current '(constant property
method
class
interface
script
file))))
(when (php-parse-p parse)
(let ((parse-type (rest (assq 'type parse))))
(cond
((member parse-type
'(constant property method)) parse)
(t (php-get-first/last-struct-in-class/interface
(cond ((eq direction 'previous) 'last)
((eq direction 'next) 'first)))))))))))
(if thing
(progn
(when (php-parse-p parse)
(save-excursion
(cond ((eq direction 'previous)
(forward-char))
((eq direction 'next)
(backward-char)))
(let ((php-jump-show-hidden nil))
(php-jump-to-first-statement)
(php-jump-hide-if-last-command-was-jump))))
(goto-char (rest (assq (cond ((eq direction 'previous) 'end)
((eq direction 'next) 'begin))
thing)))
(php-jump-to-first-statement))
(cond ((eq direction 'previous)
(backward-char))
((eq direction 'next)
(forward-char)))
(php-jump-to-first-statement))))))
(defun php-jump-to-next-thing ()
"This function moves the cursor to the first statement in the
next PHP structure."
(interactive)
(php-jump-to-next/previous-thing 'next))
(defun php-jump-to-previous-thing ()
"This function moves the cursor to the first statement in the
previous PHP structure."
(interactive)
(php-jump-to-next/previous-thing 'previous))
(defun php-jump-to-thing (name)
"Jumps to the beginning of the PHP structure named NAME in the
current buffer."
(interactive `(,(completing-read "Jump to: " (php-get-thing-names) nil t)))
(let ((thing (php-find-thing name)))
(when (php-parse-p thing)
(goto-char (rest (assq 'begin thing))))))
(defun* php-insert-get-interfaces (&key (verb "implements")
completion-filter default)
"This function gathers interfaces for a PHP class definition."
(let ((interface-list default))
(catch 'done
(while t
(let ((answer
(completing-read
(format
"Modify interfaces %s ([a]dd/[d]elete/[c]lear/e[X]it)? "
(or interface-list "(*none*)"))
'("a" "A" "d" "D" "c" "C" "x" "X") nil t nil nil "x")))
(cond ((member answer '("x" "X"))
(throw 'done t))
((member answer '("a" "A"))
(let ((interface
(completing-read (concat (upcase-initials verb) ": ")
(append (php-completion-get-etags)
(php-completion-candidates
"" '("class")))
completion-filter nil default)))
(unless (nil-or-blank interface)
(add-to-list 'interface-list interface t))))
((member answer '("c" "C"))
(setf interface-list nil))
((member answer '("d" "A"))
(let ((interface (completing-read "Delete: " interface-list)))
(unless (nil-or-blank interface)
(setf interface-list
(remove interface interface-list)))))))))
interface-list))
(defun* php-get-insert-interface-arguments (&optional name &key
(extends nil extends-p)
extends-completion-filter
extends-default
(finalp nil finalp-p)
(abstractp nil
abstractp-p))
"Read arguments necessary for an interface definition."
(let ((class-completion-list (append (php-completion-get-etags)
(php-completion-candidates
"" '("class")))))
`(,(or name (completing-read "Interface name: " class-completion-list))
,(if extends-p
extends
(php-insert-get-interfaces :verb "extends"
:completion-filter
extends-completion-filter
:default extends-default)))))
(defun* php-get-insert-class-arguments (&optional name &key
(extends nil extends-p)
extends-completion-filter
extends-default
(implements nil implements-p)
implements-completion-filter
implements-default
(finalp nil finalp-p)
(abstractp nil abstractp-p))
"Read arguments necessary for a class definition."
(let ((class-completion-list (append (php-completion-get-etags)
(php-completion-candidates
"" '("class")))))
`(,(or name (completing-read "Class name: " class-completion-list))
,(if extends-p
extends
(let ((input (completing-read "Extends: " class-completion-list
extends-completion-filter nil
extends-default nil extends-default)))
(unless (nil-or-blank input) input)))
,(if implements-p
implements
(php-insert-get-interfaces :completion-filter
implements-completion-filter
:default implements-default))
,(if finalp-p
finalp
(y-or-n-p "Make final? "))
,(if abstractp-p
abstractp
(y-or-n-p "Make abstract? ")))))
(defun php-insert-interface (name &optional extends)
"Inserts an interface element named NAME in the buffer, with
the passed-in properties. EXTENDS may be lists."
(interactive (php-get-insert-interface-arguments))
(if (nil-or-blank name)
(message "Must provide a name!")
(call-interactively 'php-doc-interface-insert)
(let* ((extends (if (listp extends) extends (list extends)))
(extend-string (mapconcat 'identity extends ", "))
(interface-def
(concat "interface " (upcase-initials name)
(if (and (stringp extend-string)
(not (nil-or-blank extend-string)))
(concat " extends " (upcase-initials extend-string))
""))))
(insert interface-def)
(newline)
(insert "{")
(php-format-break-statement)
(newline-and-indent)
(save-excursion
(newline)
(insert "}")))))
(defun* php-insert-class (name &optional extends implements finalp abstractp
&key (doc-args nil doc-args-p))
"Inserts a class named NAME in the buffer, with the
passed-in properties. Both EXTENDS and IMPLEMENTS may be lists."
(interactive (php-get-insert-class-arguments))
(if (nil-or-blank name)
(message "Must provide a name!")
(if doc-args-p
(apply 'php-doc-class-insert doc-args)
(call-interactively 'php-doc-class-insert))
(php-insert-class-definition name extends implements finalp abstractp)
(save-excursion
(newline)
(insert "}"))))
(defun php-insert-class-definition (name extends implements finalp abstractp)
"Inserts a properly formatted class definition for NAME into
the buffer with the supplied parameters."
(let* ((implements (if (listp implements) implements (list implements)))
(implement-string (mapconcat 'identity implements ", "))
(class-def
(concat (if finalp "final " "")
(if abstractp "abstract " "")
"class " (upcase-initials name)
(if (and (stringp extends)
(not (nil-or-blank extends)))
(concat " extends " (upcase-initials extends))
"")
(if (and (stringp implement-string)
(not (nil-or-blank implement-string)))
(concat " implements "
(upcase-initials implement-string)) ""))))
(insert class-def)
(newline)
(insert "{")
(unless (looking-at-p "[[:space:]]*$")
(save-excursion
(newline-and-indent)))
(php-format-break-statement)))
(defun* php-get-insert-constant-arguments (&optional name value)
"Read arguments necessary for a constant definition."
`(,(or name
(read-string "Constant name: "))
,(or value (read-string "Value: "))))
(defun* php-insert-constant (name &optional value
&key (doc-args nil doc-args-p))
"Inserts a constant named NAME with value VALUE into the
buffer."
(interactive (php-get-insert-constant-arguments))
(if (nil-or-blank name)
(message "Must provide a name!")
(if (nil-or-blank value)
(message "Must provide a value!")
(let* ((pos (php-find-struct-position 'constant name)))
(goto-char pos)
(let ((doc-args (if doc-args-p
(apply 'php-doc-constant-insert doc-args)
(call-interactively 'php-doc-constant-insert))))
(php-insert-constant-definition name value doc-args))))))
(defun php-insert-constant-definition (name value doc-args)
"Inserts a properly formatted constant definition for NAME into
the buffer with the supplied parameters."
(let ((pos (point))
(str ""))
(setf str (concat str "const " (upcase name)))
(let* ((value-str (if (and (string= "string" (second doc-args))
(not (php-is-a-constant-p value)))
(php-single-quote-string value)
value))
(value-str (if (stringp value-str)
value-str
(number-to-string value-str))))
(setf str (concat str " = " value-str ";"))
(insert str)
(php-format-region pos (point) (looking-at-p "}"))
(php-format-spacing))))
(defun* php-get-insert-property-arguments (&optional name scope value &key
(staticp nil staticp-p))
"Read arguments necessary for a property definition."
`(,(or name
(read-string
"Property name (without leading characters such as $ or _): "))
,(or scope (completing-read "Public, protected, or private (U/o/i)? "
'("u" "o" "i") nil t nil nil "u"))
,(or value (read-string "Value: "))
,(if staticp-p
staticp
(y-or-n-p "Make static? "))))
(defun* php-insert-property (name &optional scope value staticp
&key (doc-args nil doc-args-p))
"Inserts a property named NAME into the buffer. SCOPE is
whether the method should be public (u, default), protected (o),
or private (i). The symbols 'public, 'private and 'protected are
also accepted. The default VALUE may be supplied. The property
may also be STATICP."
(interactive (php-get-insert-property-arguments))
(if (nil-or-blank name)
(message "Must provide a name!")
(let* ((scope (if (stringp scope) (php-initial->scope scope) scope))
(name (concat (if (or (eq scope 'private)
(and php+-mode-protected-underscore
(eq scope 'protected))) "_" "") name))
(pos (php-find-struct-position 'property name scope)))
(goto-char pos)
(when (looking-at-p "[[:space:]]*}")
(newline-and-indent)
(save-excursion
(newline-and-indent)))
(indent-according-to-mode)
(let ((doc-args (if doc-args-p
(apply 'php-doc-property-insert doc-args)
(call-interactively 'php-doc-property-insert))))
(php-insert-property-definition name scope value staticp doc-args)))))
(defun php-insert-property-definition (name scope value staticp doc-args)
"Insert a properly formatted property definition for NAME into
the buffer with the supplied parameters."
(let ((str ""))
(when staticp
(setf str (concat str "static ")))
(setf str (concat str (symbol-name scope) " $" name))
(unless (nil-or-blank value)
(let* ((value-str (if (and (string= "string" (second doc-args))
(not (php-is-a-constant-p value)))
(php-single-quote-string value)
value))
(value-str (if (stringp value-str)
value-str
(number-to-string value-str))))
(setf str (concat str " = " value-str))))
(setf str (concat str ";"))
(let ((pos (point)))
(insert str)
(php-format-region pos (point) (looking-at-p "}"))
(php-format-spacing t))))
(defun php-initial->scope (i)
(let ((scope-assoc '(("u" . public)
("o" . protected)
("i" . private))))
(rest (assoc i scope-assoc))))
(defun* php-get-insert-method-arguments (&optional name scope &key
(staticp nil staticp-p)
(abstractp nil abstractp-p)
(finalp nil finalp-p)
(non-class nil non-class-p))
"Read arguments necessary for a method definition."
(let ((non-class (if non-class-p
non-class
(not (php-parse-current '(class interface))))))
`(,(or name (read-string "Method name: "))
,(unless non-class
(or scope (completing-read "Public, protected, or private (U/o/i)? "
'("u" "o" "i") nil t nil nil "u")))
,(unless non-class (if staticp-p staticp (y-or-n-p "Make static? ")))
,(unless non-class (if abstractp-p
abstractp (y-or-n-p "Make abstract? ")))
,(unless non-class (if finalp-p finalp (y-or-n-p "Make final? ")))
,non-class)))
(defun* php-insert-method (name &optional scope staticp abstractp finalp
non-class &key (var-list nil var-list-p)
(doc-args nil doc-args-p))
"Inserts a method element named NAME in the buffer. SCOPE is
whether the method should be public (u, default), protected (o),
or private (i). The symbols 'public, 'private and 'protected are
also accepted. The method may also be STATICP, ABSTRACTP or
FINALP unless it is NON-CLASS. You may pass in VAR-LIST and/or
DOC-ARGS if you do not wish to be prompted for them."
(interactive (php-get-insert-method-arguments))
(if (nil-or-blank name)
(message "Must provide a name!")
(if (save-match-data
(or (not (string-match (substring (php-type-regexp 'identifier) 2)
name))
(not (= (length name) (- (match-end 0) (match-beginning 0))))))
(message "Must provide a valid name!")
(let* ((scope (if (symbolp scope) scope (php-initial->scope scope)))
(name (concat (if (or (eq scope 'private)
(and php+-mode-protected-underscore
(eq scope 'protected))) "_" "")
name))
(pos (if (not non-class)
(php-find-struct-position 'method name scope staticp
abstractp finalp)
(unless (looking-at-p "[[:space:]]*$")
(save-excursion
(newline-and-indent)))
(indent-according-to-mode)
(point))))
(goto-char pos)
(unless (looking-back-p "\\(\n[[:space:]]*\\|{\\)\n[[:space:]]*")
(newline-and-indent))
(unless (looking-at-p "[[:space:]]*$")
(save-excursion
(newline-and-indent)))
(indent-according-to-mode)
(let* ((confirm-constants (not var-list-p))
(var-list (if var-list-p
(when doc-args-p
(apply 'php-doc-method-insert
(append doc-args `(:var-list ,var-list))))
var-list
(if doc-args-p
(apply 'php-doc-method-insert doc-args)
(call-interactively 'php-doc-method-insert)))))
(php-insert-method-definition name var-list scope staticp abstractp
finalp confirm-constants non-class)
(unless abstractp
(save-excursion
(newline)
(insert "}")
(indent-according-to-mode)
(newline-and-indent)
(when (not (or (looking-at-p "}")
(looking-at-p (concat ws-re "*$"))))
(newline-and-indent))))
(php-format-spacing t)
(indent-according-to-mode))))))
(defun php-insert-method-definition (name var-list scope staticp abstractp
finalp &optional confirm-constants
non-class)
"Inserts a properly formatted method definition using the
supplied parameters. You may optionally specify whether to
CONFIRM-CONSTANTS or whether the method is NON-CLASS."
(beginning-of-line)
(indent-according-to-mode)
(let ((insert-string ""))
(unless non-class
(dolist (mod '("abstract" "final" "static"))
(when (symbol-value (intern (concat mod "p")))
(setf insert-string (concat insert-string mod " "))))
(setf insert-string
(concat insert-string
(when scope (concat (symbol-name scope) " ") ))))
(setf insert-string (concat insert-string "function " name "("))
(when var-list
(dolist (var (php-sort-method-args var-list))
(let* ((arg-text "")
(name (rest (assq 'name var)))
(type (rest (assq 'type var)))
(type (or type "mixed"))
(value (rest (assq 'value var)))
(value (when value (format "%s" value))))
(when (and type
(or (string= type "array")
(let ((c (substring type 0 1)))
(string= c (upcase c)))))
(setq arg-text (concat arg-text type " ")))
(setq arg-text (concat arg-text name))
(when value
(setq arg-text
(concat arg-text " = "
(if (or (not (member type '("callback" "string")))
(php-is-a-constant-p value
(not confirm-constants))
(string= value "''")
(string= value "\"\""))
value
(php-single-quote-string value)))))
(setq arg-text (concat arg-text ", "))
(setf insert-string (concat insert-string arg-text))))
(setf insert-string (substring insert-string 0 -2)))
(setf insert-string (concat insert-string ")"))
(if abstractp
(setf insert-string (concat insert-string ";"))
(setf insert-string (concat insert-string " {")))
(unless (looking-at-p "[[:space:]]*$")
(save-excursion
(newline-and-indent)))
(insert insert-string)
(when (looking-back-p "{")
(backward-char))
(php-format-break-statement)
(php-skip-this-statement)
(when (looking-back-p "{")
(forward-line 1)
(indent-according-to-mode))))
(defun php-sort-method-args (var-list)
"Sorts a copy of VAR-LIST so that variables without default
arguments are placed after those with default arguments."
(let ((var-list (sort (copy-list var-list)
(lambda (x y)
(and (not (rest (assq 'value x)))
(rest (assq 'value y)))))))
var-list))
(defun php-extract-var-list-from-parse (parse)
"Given a method PHP parse, return a var-list that can be passed
to ``php-doc-method-insert'' or ``php-method-insert''."
(when (and (php-parse-p parse)
(eq (rest (assq 'type parse)) 'method))
(let ((arguments (rest (assq 'arguments parse)))
(params (rest (assq 'params (rest (assq 'documentation parse)))))
var-list)
(dolist (arg arguments var-list)
(let ((param (first (member-if (lambda (x)
(string= (rest (assq 'name arg))
(rest (assq 'name x))))
params))))
(add-to-list
'var-list
`(,(assq 'name arg)
(type . ,(or (rest (assq 'type arg)) (rest (assq 'type param))))
(value . ,(let ((value (rest (assq 'value arg))))
(when (stringp value)
(when (string= (substring value 0 1) "'")
(setf value (substring value 1)))
(when (string= (substring value -1) "'")
(setf value (substring value 0 -1))))
value))
,(assq 'desc param)) t))))))
(defun php-edit-thing (&optional commands thing-name)
"Slurp up the current
function/method/property/constant/class/interface definition,
apply COMMANDS and then re-insert the new definition. Commands
are alists of the form '((verb . verb) (type . type) (name
.name) (value . value))). Verbs currently supported are
'remove (for params only) and 'edit. Editing a non-existant
entry results in adding a new one. Types are currently
'short-desc 'long-desc 'author 'return, 'param and 'definition.
Values are single strings except in the case of 'param and
'definition. In the case of 'param, it is an alist '((name
. name) (type . type) (value . value) (desc . desc) (after
. after)). For 'definition, it is '((name . name) (visibility
. visibility) (staticp . staticp) (abstractp . abstractp) (finalp
. finalp) (extends . extends) (implements . implements)).
Specifiying abstractp or finalp for a constant or property will
be ignored. Specifying '(after . nil) in the value will result
in the param begin placed first. Optional params will be pushed
backwards always before generation, which may affect the order.
In the case of the verb 'edit, only supply values that will be
changed. Returns the change in text size. Leaves point at the
beginning of thing."
(when (or (not thing-name) (php-jump-to-thing thing-name))
(let ((parse (php-parse-current '(constant property method class
interface))))
(when (php-parse-p parse)
(let* ((doc (rest (assq 'documentation parse)))
(begin (rest (assq 'begin parse)))
(parse-type (rest (assq 'type parse)))
(parse-type
(if (and (eq parse-type 'method)
(not (php-parse-current '(class interface))))
'function
parse-type))
(def-end (save-excursion (php-jump-to-first-statement parse)))
(def-end (if (or (memq parse-type '(constant property))
(rest (assq 'abstractp parse))
(= def-end begin))
(rest (assq 'end parse))
def-end))
(var-list (when (memq parse-type '(function method))
(php-extract-var-list-from-parse parse))))
(dolist (command commands)
(let* ((verb (rest (assq 'verb command)))
(type (rest (assq 'type command)))
(name (rest (assq 'name command)))
(value (rest (assq 'value command)))
(name (if (and (not name) (listp value))
(rest (assq 'name value))
name))
thing-order
(thing (cond ((eq type 'param)
(let ((place (php-var-list-tail
name var-list)))
(setf thing-order
(- (length var-list) (length place)))
(first place)))
((eq type 'definition) parse)
(t (assq type doc))))
(list-to-edit (cond ((eq type 'param) 'var-list)
((eq type 'definition) 'parse)
(t 'doc)))
(list-value (symbol-value list-to-edit)))
(cond ((not (eq type 'definition))
(setf list-value (delete thing list-value))))
(cond ((eq verb 'edit)
(cond ((or (eq type 'param) (eq type 'definition))
(when (assq 'after value)
(let ((after (rest (assq 'after value))))
(setf thing-order
(if after
(- (1+ (length list-value))
(length (php-var-list-tail
after
list-value)))
0))))
(dolist (field (cond
((eq type 'param)
'(name type value desc))
((eq type 'definition)
(cond ((eq parse-type 'class)
'(name extends implements
abstractp finalp))
((eq parse-type 'interface)
'(name extends))
((eq parse-type 'function)
'(name))
((eq parse-type 'method)
'(name visibility staticp
abstractp finalp))
((eq parse-type 'constant)
'(name value))
((eq parse-type 'property)
'(name value visibility
staticp))))))
(when (assq field value)
(let* ((new-val (rest (assq field value))))
(when (eq field 'name)
(php-edit-thing-handle-name))
(setf thing (assq-delete-all field thing))
(setf thing
(append thing
`((,field . ,new-val))))))))
(t (setf thing `(,type . ,value))))
(cond ((eq type 'param)
(when (and (assoc 'name thing)
(assoc 'type thing))
(setf list-value
(append (butlast list-value
(- (length list-value)
thing-order))
`(,thing)
(nthcdr thing-order
list-value)))))
((eq type 'definition)
(setf list-value thing))
(t (setf list-value (append list-value
`(,thing)))))))
(setf (symbol-value list-to-edit) list-value)))
(goto-char begin)
(delete-region begin def-end)
(cond ((eq parse-type 'class)
(php-doc-class-insert (rest (assq 'short-desc doc))
(rest (assq 'long-desc doc))
(rest (assq 'category doc))
(rest (assq 'package doc))
(rest (assq 'subpackage doc))
(rest (assq 'author doc))
(rest (assq 'license doc))
(rest (assq 'link doc)))
(php-insert-class-definition (rest (assq 'name parse))
(rest (assq 'extends parse))
(rest (assq 'implements parse))
(rest (assq 'finalp parse))
(rest (assq 'abstractp parse))))
((memq parse-type '(function method))
(php-doc-method-insert (rest (assq 'short-desc doc))
(rest (assq 'long-desc doc))
(rest (assq 'author doc))
(rest (assq 'return doc))
:var-list var-list)
(php-insert-method-definition (rest (assq 'name parse))
var-list
(rest (assq 'visibility parse))
(rest (assq 'staticp parse))
(rest (assq 'abstractp parse))
(rest (assq 'finalp parse))
(eq parse-type 'function)))
((memq parse-type '(constant property))
(let ((doc-args `(,(rest (assq 'short-desc doc))
,(rest (assq 'var doc))))
(name (rest (assq 'name parse))))
(apply 'php-doc-property-insert doc-args)
(cond ((eq parse-type 'constant)
(php-insert-constant-definition
name (rest (assq 'value parse)) doc-args))
((eq parse-type 'property)
(php-insert-property-definition
name
(rest (assq 'visibility parse))
(rest (assq 'value parse))
(rest (assq 'staticp parse))
doc-args))))))
(let* ((parse-type (if (eq parse-type 'function) 'method parse-type))