-
Notifications
You must be signed in to change notification settings - Fork 2
/
as3-mode.el
2007 lines (1690 loc) · 76 KB
/
as3-mode.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
;;; as3-mode.el --- A simple mode for editing Actionscript 3 files
;; Copyright (C) 2007 Aemon Cannon
;; Author: Aemon Cannon
;; Keywords: language modes
;; 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., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Commentary:
;;
;;; Code:
(eval-when-compile (require 'cl))
(require 'fdb)
(require 'flyparse-mode)
(require 'font-lock)
(require 'yasnippet)
(require 'ido)
(defvar as3-flyparse-single-file-to-stdout-cmd-maker 'as3-flyparse-make-single-file-to-stdout-cmd
"The shell command maker for parsing a single as3 file and printing the resultant tree to stdout.")
(make-variable-buffer-local 'as3-flyparse-single-file-to-stdout-cmd-maker)
(defvar as3-flyparse-single-file-cmd-maker 'as3-flyparse-make-single-file-cmd
"The shell command maker for parsing a single as3 file.")
(make-variable-buffer-local 'as3-flyparse-single-file-cmd-maker)
(defvar as3-flyparse-recursive-cmd-maker 'as3-flyparse-make-recursive-cmd
"The shell command maker for parsing a directory recursively.")
(make-variable-buffer-local 'as3-flyparse-recursive-cmd-maker)
(defvar as3-flex-livedoc-url "http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/%s.html"
"The url used to browse to class documentation. See as3-open-livedoc-for-class")
;;
;; Some predefined flyparse queries, for conveniance.
;;
(defvar as3-flyparse-path-to-import-def
'("COMPILATION_UNIT" "PACKAGE_DECL" "IMPORT_DEF"))
(defvar as3-flyparse-path-to-class-def
'("COMPILATION_UNIT" "PACKAGE_DECL" "CLASS_DEF"))
(defvar as3-flyparse-path-to-interface-def
'("COMPILATION_UNIT" "PACKAGE_DECL" "INTERFACE_DEF"))
(defvar as3-flyparse-path-to-class-name
(append as3-flyparse-path-to-class-def '("CLASS_NAME" "NAME" *)))
(defvar as3-flyparse-path-to-interface-name
(append as3-flyparse-path-to-interface-def '("NAME" *)))
(defvar as3-flyparse-path-to-extends-clause
(append as3-flyparse-path-to-class-def '("EXTENDS_CLAUSE")))
(defvar as3-flyparse-path-to-implements-clause
(append as3-flyparse-path-to-class-def '("IMPLEMENTS_CLAUSE")))
(defvar as3-flyparse-path-to-extends-name
(append as3-flyparse-path-to-extends-clause '("NAME" *)))
(defvar as3-flyparse-path-to-class-block
(append as3-flyparse-path-to-class-def '("TYPE_BLOCK")))
(defvar as3-flyparse-path-to-class-member
(append as3-flyparse-path-to-class-block '("CLASS_MEMBER")))
(defvar as3-flyparse-path-to-method-def
(append as3-flyparse-path-to-class-member '("METHOD_DEF")))
(defvar as3-flyparse-path-to-method-name
(append as3-flyparse-path-to-method-def '("METHOD_NAME" "NAME")))
(defvar as3-flyparse-path-to-method-param
(append as3-flyparse-path-to-method-def '("PARAMS" "PARAM")))
(defvar as3-flyparse-path-to-method-return-type
(append as3-flyparse-path-to-method-def '("TYPE_SPEC" "TYPE")))
(defvar as3-flyparse-path-to-method-name-text
(append as3-flyparse-path-to-method-def '("METHOD_NAME" "NAME" *)))
(defvar as3-flyparse-path-to-method-def-block
(append as3-flyparse-path-to-method-def '("BLOCK")))
(defvar as3-flyparse-path-to-interface-member
(append as3-flyparse-path-to-interface-def '("TYPE_BLOCK") '("CLASS_MEMBER")))
(defvar as3-flyparse-path-to-interface-method-def
(append as3-flyparse-path-to-interface-member '("METHOD_DEF")))
(defvar as3-flyparse-path-to-variable-def
(append as3-flyparse-path-to-class-member '("VARIABLE_DEF")))
(defvar as3-flyparse-path-to-variable-def-name
(append as3-flyparse-path-to-variable-def '("VAR_DECLARATION" "NAME")))
(defvar as3-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c C-c") 'comment-region)
(define-key map (kbd "C-c m") 'as3-quick-menu)
(define-key map (kbd "C-c h") 'as3-show-help-at-point)
map)
"Keymap for `as3-mode'.")
(defvar as3-mode-syntax-table
(let ((st (make-syntax-table)))
(modify-syntax-entry ?\" "\"" st)
(modify-syntax-entry ?\' "\"" st)
(modify-syntax-entry ?_ "w" st)
(modify-syntax-entry ?\\ "\\" st)
(modify-syntax-entry ?{ "(" st)
(modify-syntax-entry ?} ")" st)
(modify-syntax-entry ?\[ "(" st)
(modify-syntax-entry ?\] ")" st)
(modify-syntax-entry ?\( "(" st)
(modify-syntax-entry ?\) ")" st)
(modify-syntax-entry ?+ "." st)
(modify-syntax-entry ?- "." st)
(modify-syntax-entry ?= "." st)
(modify-syntax-entry ?% "." st)
(modify-syntax-entry ?< "." st)
(modify-syntax-entry ?> "." st)
(modify-syntax-entry ?& "." st)
(modify-syntax-entry ?| "." st)
(modify-syntax-entry ?\240 "." st)
(modify-syntax-entry ?/ ". 124b" st)
(modify-syntax-entry ?* ". 23" st)
(modify-syntax-entry ?\n "> b" st)
(modify-syntax-entry ?\^m "> b" st)
st)
"Syntax table for `as3-mode'.")
(defvar as3-font-lock-default-face 'as3-font-lock-default-face)
(defconst as3-font-lock-keywords
(append
(list
'("\\<\\(get\\|set\\)\\>\\(?:\\s-+\\(\\sw+\\)\\)?"
(1 font-lock-type-face)
(2 font-lock-function-name-face nil t))
'("\\<\\(function\\)\\>\\(?:\\s-+\\(\\sw+\\)\\)?"
(1 font-lock-keyword-face)
(2 font-lock-function-name-face nil t))
'("\\<\\(function\\)\\>"
(1 font-lock-keyword-face))
'("\\<\\([A-Z_]+\\)\\>"
(1 font-lock-constant-face))
'("\\<\\([A-Z]\\sw+\\)\\>"
(1 font-lock-type-face))
'("\\<\\(var\\)\\>\\(?:\\s-+\\(_?\\sw+\\)\\)?"
(1 font-lock-keyword-face)
(2 font-lock-variable-name-face nil t))
'("\\(_\\sw+\\)"
(1 font-lock-variable-name-face nil t))
'("\\<\\(\\|this\\|super\\|debugger\\|delete\\|export\\|in\\|is\\|typeof\\|with\\)\\>"
(1 font-lock-builtin-face))
'("\\<\\(public\\|private\\|override\\|protected\\|import\\|package\\|static\\|class\\|const\\|extends\\|implements\\)\\>"
(1 font-lock-keyword-face))
'("\\<\\(return\\|new\\|if\\|else\\|while\\|for\\|throw\\)\\>"
(1 font-lock-keyword-face))
'("\\<\\(default\\)\\>\\(?:\\s-+\\(\\sw+\\)\\)?"
(1 font-lock-keyword-face)
(2 font-lock-variable-name-face nil t))
'("\\<\\(void\\)\\>\\(?:\\s-+\\(\\sw+\\)\\)?"
(1 font-lock-keyword-face t)
(2 as3-font-lock-default-face t t))
'("\\<\\(Infinity\\|NaN\\|undefined\\)\\>"
0 font-lock-constant-face t)
)
)
"Subdued level highlighting for As3 mode.")
(defvar as3-event-options
`(("MouseEvent.MOUSE_DOWN" . ("MouseEvent" "MOUSE_DOWN" "onMouseDown"))
("MouseEvent.MOUSE_UP" . ("MouseEvent" "MOUSE_UP" "onMouseUp"))
("MouseEvent.ROLL_OUT" . ("MouseEvent" "MOUSE_OUT" "onRollOut"))
("MouseEvent.ROLL_OVER" . ("MouseEvent" "MOUSE_OVER" "onRollOver"))
("KeyboardEvent.KEY_UP" . ("KeyboardEvent" "KEY_UP" "onKeyUp"))
("KeyboardEvent.KEY_DOWN" . ("KeyboardEvent" "KEY_DOWN" "onKeyDown"))
("TimerEvent.TIMER" . ("TimerEvent" "TIMER" "onTimer"))
("Event.ACTIVATE" . ("Event" "ACTIVATE" "onActivate"))
("Event.ADDED" . ("Event" "ADDED" "onAdded"))
("Event.ADDED_TO_STAGE" . ("Event" "ADDED_TO_STAGE" "onAddedToStage"))
("Event.CANCEL" . ("Event" "CANCEL" "onCancel"))
("Event.CHANGE" . ("Event" "CHANGE" "onChange"))
("Event.CLOSE" . ("Event" "CLOSE" "onClose"))
("Event.COMPLETE" . ("Event" "COMPLETE" "onComplete"))
("Event.CONNECT" . ("Event" "CONNECT" "onConnect"))
("Event.DEACTIVATE" . ("Event" "DEACTIVATE" "onDeactivate"))
("Event.ENTER_FRAME" . ("Event" "ENTER_FRAME" "onEnterFrame"))
("Event.FULLSCREEN" . ("Event" "FULLSCREEN" "onFullScreen"))
("Event.ID3" . ("Event" "ID3" "onId3"))
("Event.INIT" . ("Event" "INIT" "onInit"))
("Event.MOUSE_LEAVE" . ("Event" "MOUSE_LEAVE" "onMouseLeave"))
("Event.OPEN" . ("Event" "OPEN" "onOpen"))
("Event.REMOVED" . ("Event" "REMOVED" "onRemoved"))
("Event.REMOVED_FROM_STAGE" . ("Event" "REMOVED_FROM_STAGE" "onRemovedFromStage"))
("Event.RENDER" . ("Event" "RENDER" "onRender"))
("Event.RESIZE" . ("Event" "RESIZE" "onResize"))
("Event.SCROLL" . ("Event" "SCROLL" "onScroll"))
("Event.SELECT" . ("Event" "SELECT" "onSelect"))
("Event.SOUND_COMPLETE" . ("Event" "SOUND_COMPLETE" "onSoundComplete"))
("Event.TAB_CHILDREN_CHANGE" . ("Event" "TAB_CHILDREN_CHANGE" "onTabChildrenChange"))
("Event.TAB_ENABLED_CHANGE" . ("Event" "TAB_ENABLED_CHANGE" "onTabEnabledChange"))
("Event.TAB_INDEX_CHANGE" . ("Event" "TAB_INDEX_CHANGE" "onTabIndexChange"))
("Event.UNLOAD" . ("Event" "UNLOAD" "onUnload"))
)
"Information for common event types and their handlers."
)
(defvar as3-command-library
'(("hoist-constant" . "as3-hoist-as-constant")
("hoist-method" . "as3-hoist-as-method")
("import" . "as3-new-import")
("accessors" . "as3-getter-setter")
("flip-to-y" . "as3-copy-and-flip-line")
("create-getter" . "as3-create-getter-from-var-def-at-point")
("create-setter" . "as3-create-setter-from-var-def-at-point")
("create-getter-and-setter" . "as3-create-getter-and-setter-from-var-def-at-point")
("create-subclass" . "as3-create-subclass")
("create-from-template" . "as3-create-from-template")
("create-private-var" . "as3-create-private-var-at-point")
("switch-to-super" . "as3-switch-to-super")
("switch-to-subclass" . "as3-switch-to-subclass")
("organize-imports" . "as3-alphabetize-imports")
("asdoc-method" . "as3-asdoc-method")
("asdoc-class" . "as3-asdoc-class")
("add-event-listener" . "as3-insert-event-listener")
("describe-class" . "as3-describe-class-by-name")
("jump-to-class" . "as3-jump-to-class-by-name")
("override-method" . "as3-override-method-by-name")
("implement-interface-method" . "as3-implement-interface-method")
("organize-interface-implementation" . "as3-organize-interface-implementation")
("define-string-constant" . "as3-retroactively-define-string-constant")
("flashlog" . "as3-project-flashlog")
("help" . "as3-open-livedoc-for-class")
)
"Library of commands, accessible via as3-quick-menu."
)
(defun as3-flyparse-make-single-file-to-stdout-cmd (file-name)
"Create command for parsing a single file using flyparse and printing resultant tree to stdout."
(list "java" "emacs.flyparse.as3.AS3Driver" "-f" file-name))
(defun as3-flyparse-make-single-file-cmd (file-name result-file-name)
"Create command for parsing a single file using flyparse."
(list "java" "emacs.flyparse.as3.AS3Driver" "-f" file-name result-file-name))
(defun as3-flyparse-make-recursive-cmd (directory-names result-file-name)
"Create a command for parsing a directory recursively."
`("java" "emacs.flyparse.as3.AS3Driver" "-l" ,result-file-name ,@directory-names))
(defun as3-run-command-by-bookmark (command-library)
"Execute command associated with bookmark."
(let* ((key (ido-completing-read "Enter command bookmark: "
command-library
nil t nil))
(func (intern (cdr (assoc key command-library)))))
(call-interactively func)))
(defun as3-quick-menu ()
(interactive)
(as3-run-command-by-bookmark as3-command-library))
(define-derived-mode as3-mode fundamental-mode "as3-mode"
"A major mode for editing Actionscript 3 files."
:syntax-table as3-mode-syntax-table
(set (make-local-variable 'comment-start) "//")
(set (make-local-variable 'font-lock-defaults) (list as3-font-lock-keywords))
(set (make-local-variable 'indent-line-function) 'as3-indent-line)
(setq tab-width 4)
(as3-project-helper-load)
(setq flyparse-single-file-to-stdout-maker as3-flyparse-single-file-to-stdout-cmd-maker)
(setq flyparse-single-file-cmd-maker as3-flyparse-single-file-cmd-maker)
(setq flyparse-recursive-cmd-maker as3-flyparse-recursive-cmd-maker)
(flyparse-mode-on)
(yas/initialize)
(run-hooks 'as3-mode-hook)
)
;; Indentation
(defun as3-indent-line ()
"Indent current line of As3 code."
(interactive)
(indent-line-to (max 0 (as3-calculate-indentation))))
(defun as3-calculate-indentation ()
"Return the column to which the current line should be indented."
(save-excursion
(as3-maybe-skip-leading-close-delim)
(let ((pos (point)))
(beginning-of-line)
(if (not (search-backward-regexp "[^\n\t\r ]" 1 0))
0
(progn
(as3-maybe-skip-leading-close-delim)
(+ (current-indentation) (* 4 (as3-count-scope-depth (point) pos))))))))
(defun as3-maybe-skip-leading-close-delim ()
(beginning-of-line)
(forward-to-indentation 0)
(if (looking-at "\\s)")
(forward-char)
(beginning-of-line)))
(defun as3-face-at-point (pos)
"Return face descriptor for char at point."
(plist-get (text-properties-at pos) 'face))
(defun as3-count-scope-depth (rstart rend)
"Return difference between open and close scope delimeters."
;;Attempting Steve Yegge's solution..
;; (save-excursion
;; (let ((result (parse-partial-sexp rstart rend)))
;; (if (or (nth 3 result) (nth 4 result) (nth 7 result))
;; 0
;; (nth 0 result)))))
(save-excursion
(goto-char rstart)
(let ((open-count 0)
(close-count 0)
opoint)
(while (and (< (point) rend)
(progn (setq opoint (point))
(re-search-forward "\\s)\\|\\s(" rend t)))
(if (= opoint (point))
(forward-char 1)
(cond
;; Don't count if in string or comment.
((as3-face-at-point (- (point) 1)))
((looking-back "\\s)")
(incf close-count))
((looking-back "\\s(")
(incf open-count))
)))
(- open-count close-count))))
;; Define the structures and functionality for the AS3 Dom.
(defstruct as3-node "An AS3 program node." (tree nil) (file-path ""))
(defun as3-class-for-node (an-as3-node)
"Get the as3-class that defined this node."
(make-as3-class
:tree (flyparse-get-cached-tree (as3-node-file-path an-as3-node))
:file-path (as3-node-file-path an-as3-node)))
(defun as3-pretty-print-node (an-as3-node)
"Return a pretty string description of an-as3-node."
(cond ((as3-method-p an-as3-node) (as3-pretty-print-method an-as3-node))
((as3-member-var-p an-as3-node) (as3-pretty-print-member-var an-as3-node))
((as3-var-declaration-p an-as3-node) (as3-pretty-print-var-declaration an-as3-node))
((as3-formal-parameter-p an-as3-node) (as3-pretty-print-formal-parameter an-as3-node))
(t "An as3 node.")))
(defstruct (as3-interface (:include as3-node)) "An AS3 interface." )
(defun as3-interface-named (name)
"Return the interface with the given name."
(catch 'return-now
(flyparse-for-each-cached-tree
(lambda (path tree)
(let ((name-tree (flyparse-query-first
as3-flyparse-path-to-interface-name tree)))
(if name-tree
(let ((interface-name (flyparse-tree-as-text name-tree)))
(if (equal interface-name name)
(throw 'return-now
(make-as3-interface :tree tree :file-path path))))
))
))
))
(defun as3-interface-name (an-as3-interface)
"Return the name of the given interface."
(let* ((tree (as3-interface-tree an-as3-interface))
(name-tree (flyparse-query-first as3-flyparse-path-to-interface-name tree)))
(if name-tree
(flyparse-tree-as-text name-tree))))
(defun as3-interface-instance-methods (an-as3-interface)
"Get all instance methods of an-as3-interface."
(let ((interface-tree (as3-interface-tree an-as3-interface)))
(mapcar
(lambda (tree)
(make-as3-method
:tree tree
:file-path (as3-interface-file-path an-as3-interface)))
(flyparse-query-all
as3-flyparse-path-to-interface-method-def
interface-tree))
))
(defstruct (as3-class (:include as3-node)) "An AS3 class." )
(defun as3-all-classes ()
"Return a list of all known classes."
(let ((classes '()))
(flyparse-for-each-cached-tree
(lambda (path tree)
(if (as3-class-tree-p tree)
(push (make-as3-class :tree tree :file-path path) classes))))
classes))
(defun as3-class-named (name)
"Return the class with the given name."
(catch 'return-now
(flyparse-for-each-cached-tree
(lambda (path tree)
(let ((class-name-tree (flyparse-query-first
as3-flyparse-path-to-class-name tree)))
(if class-name-tree
(let ((class-name (flyparse-tree-as-text class-name-tree)))
(if (equal class-name name)
(throw 'return-now
(make-as3-class :tree tree :file-path path))))
))
))
))
(defun as3-current-class ()
"Get the class for the current buffer."
(if (as3-class-tree-p flyparse-newest-parse-tree)
(make-as3-class :tree flyparse-newest-parse-tree :file-path buffer-file-name)))
(defun as3-class-name (an-as3-class)
"Return the name of the given class."
(let* ((class-tree (as3-class-tree an-as3-class))
(class-name-tree (flyparse-query-first as3-flyparse-path-to-class-name class-tree)))
(if class-name-tree
(flyparse-tree-as-text class-name-tree))))
(defun as3-current-class-name ()
"Return the name of the current class in the active buffer."
(as3-class-name (as3-current-class)))
(defun as3-class-tree-p (tree)
"Return t or nil depending on whether 'tree' is an as3 class tree."
(not (null (flyparse-query-first as3-flyparse-path-to-class-def tree))))
(defun as3-super-class-for-class (an-as3-class)
"Return the super-class of given class."
(let* ((class-tree (as3-class-tree an-as3-class))
(extends-tree (flyparse-query-first as3-flyparse-path-to-extends-name class-tree)))
(if extends-tree
(as3-class-named (flyparse-tree-as-text extends-tree)))))
(defun as3-subclasses-for-class (an-as3-class)
"Return a list the direct subclasses for an-as3-class."
(let ((class-name (as3-class-name an-as3-class))
(subclasses '()))
(flyparse-for-each-cached-tree
(lambda (path tree)
(let ((subclass-name-tree (flyparse-query-first
as3-flyparse-path-to-class-name tree))
(extends-name-tree (flyparse-query-first
as3-flyparse-path-to-extends-name tree)))
(if (and extends-name-tree subclass-name-tree)
(let ((subclass-name (flyparse-tree-type subclass-name-tree))
(extends-name (flyparse-tree-type extends-name-tree)))
(if (equal class-name extends-name)
(push (make-as3-class :tree tree :file-path path) subclasses))))
)))
subclasses
))
(defun as3-class-chain-starting-at (an-as3-class)
"Return the inheritance chain starting at an-as3-class"
(let ((super (as3-super-class-for-class an-as3-class)))
(if super
(append (list an-as3-class) (as3-class-chain-starting-at super))
(list an-as3-class))))
(defun as3-class-instance-methods (an-as3-class)
"Get all instance methods of an-as3-class."
(let ((class-chain (as3-class-chain-starting-at an-as3-class)))
(apply 'append (mapcar (lambda (class)
(let ((class-tree (as3-class-tree class)))
(mapcar
(lambda (tree)
(make-as3-method
:tree tree
:file-path (as3-class-file-path class)))
(flyparse-query-all
as3-flyparse-path-to-method-def
class-tree))
))
class-chain))))
(defun as3-class-implemented-interface-names (an-as3-class)
"Returns the names of all implemented interfaces for an-as3-class."
(let ((implements-clause-tree
(flyparse-query-first
as3-flyparse-path-to-implements-clause
(as3-class-tree an-as3-class))))
(if implements-clause-tree
(mapcar (lambda (ea)
(flyparse-tree-as-text ea))
(flyparse-query-all '("IMPLEMENTS_CLAUSE" "NAME") implements-clause-tree))
)))
(defstruct (as3-method (:include as3-node)) "An AS3 instance method." )
(defun as3-method-name (an-as3-method)
(flyparse-tree-as-text (flyparse-query-first '("METHOD_DEF" "METHOD_NAME" "NAME") (as3-method-tree an-as3-method))))
(defun as3-method-class (an-as3-method)
(as3-class-for-node an-as3-method))
(defun as3-method-return-type (an-as3-method)
(let ((type-tree (flyparse-query-first '("METHOD_DEF" "TYPE_SPEC" "TYPE") (as3-method-tree an-as3-method))))
(if (null type-tree)
"void"
(flyparse-tree-as-text type-tree))))
(defun as3-method-accessor-role (an-as3-method)
(let ((role-tree (flyparse-query-first '("METHOD_DEF" "ACCESSOR_ROLE") (as3-method-tree an-as3-method))))
(if role-tree
(flyparse-tree-as-text role-tree))))
(defun as3-method-modifiers (an-as3-method)
(mapcar (lambda (ea) (flyparse-tree-as-text ea))
(flyparse-query-all '("METHOD_DEF" "MODIFIER_LIST" *) (as3-method-tree an-as3-method))))
(defun as3-method-parameters (an-as3-method)
(mapcar (lambda (ea) (make-as3-formal-parameter :tree ea :file-path (as3-method-file-path an-as3-method)))
(flyparse-query-all '("METHOD_DEF" "PARAMS" "PARAM") (as3-method-tree an-as3-method))))
(defun as3-method-parameter-types (an-as3-method)
(mapcar (lambda (ea) (flyparse-tree-as-text ea))
(flyparse-query-all '("METHOD_DEF" "PARAMS" "PARAM" "TYPE_SPEC" "TYPE") (as3-method-tree an-as3-method))))
(defun as3-pretty-print-method (an-as3-method)
"Return the a pretty stringified description of method."
(let* ((name (as3-method-name an-as3-method))
(type (as3-method-return-type an-as3-method))
(params (as3-method-parameters an-as3-method))
(modifiers (as3-method-modifiers an-as3-method))
(accessor-role (as3-method-accessor-role an-as3-method))
)
(format "%s function %s%s(%s):%s"
(mapconcat 'identity modifiers " ")
(if accessor-role (format "%s " accessor-role) "")
name
(mapconcat (lambda (ea) (format "%s:%s" (as3-formal-parameter-name ea) (as3-formal-parameter-type ea))) params ", ")
type
)
))
(defun as3-method-at-point (pos &optional an-as3-class)
"The method at pos in for current class or an-as3-class if provided. nil if not positioned in a method."
(let* ((class (or an-as3-class (as3-current-class)))
(tree (flyparse-query-first
(append as3-flyparse-path-to-class-block `(("CLASS_MEMBER" (at ,pos)) "METHOD_DEF"))
(as3-class-tree class)
)))
(if tree
(make-as3-method :tree tree :file-path buffer-file-name))))
(defun as3-all-methods-named (name)
"Get all methods with name, for type (if provided)"
(let ((methods '()))
(flyparse-for-each-cached-tree
(lambda (path tree)
(let* ((class (make-as3-class :tree tree :file-path path))
(class-name (as3-class-name class)))
(let* ((query (append as3-flyparse-path-to-class-member `(("METHOD_DEF" (has ("METHOD_DEF" "METHOD_NAME" "NAME" ,name))))))
(method-tree (flyparse-query-first query tree)))
(if method-tree
(push (make-as3-method :tree method-tree :file-path path) methods)
))
)))
methods
))
(defun as3-method-named (an-as3-class name)
"Return the first method in class for a method names 'name',
otherwise, if none is found, return nil."
(let* ((class-chain (as3-class-chain-starting-at an-as3-class)))
(catch 'return-now
(mapc
(lambda (class)
(let* ((query (append as3-flyparse-path-to-class-member `(("METHOD_DEF" (has ("METHOD_DEF" "METHOD_NAME" "NAME" ,name))))))
(method-tree (flyparse-query-first query (as3-class-tree class))))
(if method-tree
(throw 'return-now (make-as3-method :tree method-tree :file-path (as3-class-file-path class))))))
class-chain)
nil
)
))
(defstruct (as3-member-var (:include as3-node)) "An AS3 member variable." )
(defun as3-member-var-at-point ()
(let ((var-tree
(flyparse-query-first
(append as3-flyparse-path-to-class-block '(("CLASS_MEMBER" in) "VARIABLE_DEF")))))
(if var-tree
(make-as3-member-var :tree var-tree :file-path buffer-file-name))))
(defun as3-member-var-named (an-as3-class name)
"Return the first member variable named 'name' in class,
otherwise, if none is found, return nil."
(let* ((class-chain (as3-class-chain-starting-at an-as3-class)))
(catch 'return-now
(mapc
(lambda (class)
(let* ((query (append as3-flyparse-path-to-class-member `(("VARIABLE_DEF" (has ("VARIABLE_DEF" "VAR_DECLARATION" "NAME" ,name))))))
(var-tree (flyparse-query-first query (as3-class-tree class))))
(if var-tree
(throw 'return-now (make-as3-member-var :tree var-tree :file-path (as3-class-file-path class))))))
class-chain)
nil
)
))
(defun as3-member-vars-of (an-as3-class)
"Get all member vars of an-as3-class."
(let* ((class-chain (as3-class-chain-starting-at an-as3-class)))
(apply 'append (mapcar (lambda (class)
(let ((class-tree (as3-class-tree class)))
(mapcar
(lambda (tree)
(make-as3-member-var
:tree tree
:file-path (as3-class-file-path class)))
(flyparse-query-all
as3-flyparse-path-to-variable-def
class-tree))
))
class-chain))))
(defun as3-member-var-name (an-as3-member-var)
"Return name of member var definition."
(flyparse-tree-as-text (flyparse-query-first
'("VARIABLE_DEF" "VAR_DECLARATION" *)
(as3-member-var-tree an-as3-member-var))))
(defun as3-member-var-type (an-as3-member-var)
"Return type name for member var definition."
(flyparse-tree-as-text (flyparse-query-first
'("VARIABLE_DEF" "VAR_DECLARATION" "TYPE_SPEC" "TYPE")
(as3-member-var-tree an-as3-member-var))))
(defun as3-member-var-modifiers (an-as3-member-var)
(mapcar (lambda (ea) (flyparse-tree-as-text ea))
(flyparse-query-all '("VARIABLE_DEF" "MODIFIER_LIST" *) (as3-member-var-tree an-as3-member-var))))
(defun as3-member-var-class (an-as3-member-var)
"Return class where an-as3-member-var is defined."
(as3-class-for-node an-as3-member-var))
(defun as3-getter-for (an-as3-member-var)
"Return text of a variable getter for var tree."
(let ((type (as3-member-var-type an-as3-member-var))
(name (as3-member-var-name an-as3-member-var)))
(format "public function get %s():%s { return %s }" (replace-regexp-in-string "_" "" name) type name)))
(defun as3-setter-for (an-as3-member-var)
"Return text of a variable setter for var-def tree."
(let ((type (as3-member-var-type an-as3-member-var))
(name (as3-member-var-name an-as3-member-var)))
(format "public function set %s(val:%s):void { %s = val }"
(replace-regexp-in-string "_" "" name) type name)))
(defun as3-pretty-print-member-var (an-as3-member-var)
"Return a pretty stringified description of member-var."
(let* ((name (as3-member-var-name an-as3-member-var))
(type (as3-member-var-type an-as3-member-var))
(modifiers (as3-member-var-modifiers an-as3-member-var))
)
(format "%s var %s:%s"
(mapconcat 'identity modifiers " ")
name
type
)
))
(defstruct (as3-var-declaration (:include as3-node)) "An AS3 local variable declaration." )
(defun as3-var-declaration-named (an-as3-method name)
"Return the first variable definition for a variable named 'name' in method-tree,
otherwise, if none is found, return nil."
(let ((var-tree
(flyparse-search
`("VAR_DECLARATION" (has ("VAR_DECLARATION" "NAME" ,name))) (as3-method-tree an-as3-method))))
(if var-tree
(make-as3-var-declaration :tree var-tree :file-path (as3-method-file-path an-as3-method)))))
(defun as3-var-declaration-type (an-as3-var-declaration)
"Return the name of the variables's type declaration."
(let ((type-tree
(flyparse-search '("TYPE")
(as3-var-declaration-tree an-as3-var-declaration))))
(if type-tree
(flyparse-tree-as-text type-tree))))
(defun as3-var-declaration-name (an-as3-var-declaration)
"Return the name of the variables."
(let ((name-tree
(flyparse-query-first '("VAR_DECLARATION" "NAME") (as3-node-tree an-as3-var-declaration))))
(if name-tree
(flyparse-tree-as-text name-tree))))
(defun as3-pretty-print-var-declaration (an-as3-var-declaration)
"Return the a pretty stringified description of member-var."
(let* ((name (as3-var-declaration-name an-as3-var-declaration))
(type (as3-var-declaration-type an-as3-var-declaration))
)
(format "var %s:%s" name type)
))
(defstruct (as3-formal-parameter (:include as3-node)) "An AS3 formal-parameter." )
(defun as3-formal-parameter-named (an-as3-method name)
"Return the first method parameter with provided name."
(let ((param-tree
(flyparse-query-first `("METHOD_DEF" "PARAMS" ("PARAM" (has ("PARAM" ("NAME" (text-match ,name))))))
(as3-method-tree an-as3-method))))
(if param-tree
(make-as3-formal-parameter :tree param-tree :file-path (as3-method-file-path an-as3-method)))))
(defun as3-formal-parameter-type (an-as3-formal-parameter)
"Return the name of the parameter's type declaration."
(let ((type-tree
(flyparse-query-first '("PARAM" "TYPE_SPEC" "TYPE")
(as3-formal-parameter-tree an-as3-formal-parameter))))
(if type-tree
(flyparse-tree-as-text type-tree))))
(defun as3-formal-parameter-name (an-as3-formal-parameter)
"Return the parameter's name."
(let ((name-tree
(flyparse-query-first '("PARAM" "NAME")
(as3-formal-parameter-tree an-as3-formal-parameter))))
(if name-tree
(flyparse-tree-as-text name-tree))))
(defun as3-pretty-print-formal-parameter (an-as3-formal-parameter)
"Return the a pretty stringified description of member-var."
(let* ((name (as3-formal-parameter-name an-as3-formal-parameter))
(type (as3-formal-parameter-type an-as3-formal-parameter))
)
(format "%s:%s" name type)
))
;; General utilities
(defun as3-name-at-point (pos)
(let ((var-name-tree (flyparse-containing-tree-of-type '("NAME"))))
(if var-name-tree
(flyparse-tree-type (flyparse-query-first '("NAME" *) var-name-tree)))))
(defun as3-type-of-literal (constant-tree)
"Examine a flyparse tree of 'constant' type, and return its actionscript type.'"
(cond ((flyparse-has-subtree-of-type-p constant-tree "LITERAL_NUMBER") "Number")
((flyparse-has-subtree-of-type-p constant-tree "LITERAL_STRING") "String")
((flyparse-has-subtree-of-type-p constant-tree "LITERAL_REGEX") "RegExp")
((flyparse-has-subtree-of-type-p constant-tree "LITERAL_XML") "XML")
(t "Object")))
(defun as3-var-type-at-point (name point &optional an-as3-class)
"For the given name in the given class at the given buffer offset, what is the name of the type of the variable?"
(let ((class (or an-as3-class (as3-current-class))))
(if (equal name "this")
(as3-class-name class)
(let ((method (as3-method-at-point point class)))
(if (not method)
(message "Point is not in a method.")
(let* ((member-var-def (as3-member-var-named class name))
(local-var-def (as3-var-declaration-named method name))
(method-param (as3-formal-parameter-named method name)))
(cond
(local-var-def (as3-var-declaration-type local-var-def))
(method-param (as3-formal-parameter-type method-param))
(member-var-def (as3-member-var-type member-var-def))
)
))))))
(defun as3-point-after-last-var-def (&optional an-as3-class)
(flyparse-tree-end-offset
(flyparse-query-last as3-flyparse-path-to-variable-def
(if an-as3-class (as3-class-tree an-as3-class) flyparse-newest-parse-tree))))
(defun as3-point-after-last-const-def (&optional an-as3-class)
(flyparse-tree-end-offset
(flyparse-query-last (append as3-flyparse-path-to-class-member '(("VARIABLE_DEF" (has ("VARIABLE_DEF" "const")))))
(if an-as3-class (as3-class-tree an-as3-class) flyparse-newest-parse-tree))))
(defun as3-point-at-beginning-of-type-block (&optional an-as3-class)
(flyparse-tree-beg-offset
(flyparse-query-first as3-flyparse-path-to-class-block
(if an-as3-class (as3-class-tree an-as3-class) flyparse-newest-parse-tree))))
(defun as3-find-definitions-of (name &optional is-dot-accessed enclosing-method)
"Get all methods with name, for type (if provided)"
(let ((current-class (as3-current-class))
(methods '())
(member-vars '())
(local-vars '())
(params '()))
(if (and (not is-dot-accessed) enclosing-method)
(let ((member-var-def (as3-member-var-named current-class name))
(method (as3-method-named current-class name))
(local-var-def (as3-var-declaration-named enclosing-method name))
(method-param (as3-formal-parameter-named enclosing-method name)))
(if member-var-def (push member-var-def member-vars))
(if method (push method methods))
(if local-var-def (push local-var-def local-vars))
(if method-param (push method-param params))
)
(flyparse-for-each-cached-tree
(lambda (path tree)
(let* ((member-var-query (append as3-flyparse-path-to-class-member `(("VARIABLE_DEF" (has ("VARIABLE_DEF" "VAR_DECLARATION" "NAME" ,name))))))
(method-query (append as3-flyparse-path-to-class-member `(("METHOD_DEF" (has ("METHOD_DEF" "METHOD_NAME" "NAME" ,name))))))
(member-var-tree (flyparse-query-first member-var-query tree))
(method-tree (flyparse-query-first method-query tree))
)
(if method-tree
(push (make-as3-method :tree method-tree :file-path path) methods)
)
(if member-var-tree
(push (make-as3-member-var :tree member-var-tree :file-path path) member-vars)
)
)
)))
(list methods member-vars local-vars params)
))
(defun as3-make-code-link (start end file-path offset)
"Make an emacs button, from start to end in current buffer, linking to file-path and offset."
(make-button start end
'face font-lock-constant-face
'action `(lambda (x)
(find-file-other-window ,file-path)
(goto-char ,offset)
)))
;; Interactive commands
(defun as3-alphabetize-imports (pos)
"Alphabetize the imports in this class."
(interactive (list (point)))
(let ((imports (flyparse-query-all as3-flyparse-path-to-import-def)))
(if imports
(let ((beg-point (flyparse-tree-beg-offset (first imports))))
(goto-char beg-point)
(flyparse-kill-region imports)
(let ((alphabetized-imports
(sort imports (lambda (a b)
(string-lessp (flyparse-tree-as-text a)
(flyparse-tree-as-text b))))))
(mapc (lambda (ea)
(insert (flyparse-tree-as-text ea))
(end-of-line)
(newline))
alphabetized-imports)
(indent-region beg-point (point))
(kill-line)
))
(message "Could not find any imports."))))
(defun as3-switch-to-super ()
"Open the file containing this this class's superclass and switch to it."
(interactive)
(let* ((class (as3-current-class))
(super (as3-super-class-for-class (as3-current-class))))
(if super
(find-file-other-window (as3-class-file-path super))
(message "Sorry, could not locate superclass of %s." (as3-class-name class)))))
(defun as3-switch-to-subclass ()
"Offer all subclasses of current class as option, switch to chosen subclass."
(interactive)
(let* ((class (as3-current-class))
(subclasses (as3-subclasses-for-class class))
(choices (mapcar (lambda (c)
`(,(as3-class-name c) ,(as3-class-file-path c))
) subclasses)))
(if (not (null choices))
(let* ((key (ido-completing-read "Select a subclass: "
choices
nil t nil))
(path (cdr (assoc key choices))))
(find-file-other-window path))
(message "Sorry, did not find any subclasses for %s." (as3-class-name class))
)))
(defun as3-describe-class-by-name ()
"Find a class by name, Offer all subclasses of current class as option, switch to chosen subclass."
(interactive)
(let* ((classes (as3-all-classes))
(choices (mapcar (lambda (c)
`(,(as3-class-name c) . ,c)
) classes)))
(if (not (null choices))
(let* ((key (ido-completing-read
(format "Select a class (%s available): " (length choices))
choices
nil t nil))
(chosen-class (cdr (assoc key choices))))
(as3-show-members-of chosen-class))
)))
(defun as3-jump-to-class-by-name ()
"Find a class by name, open the class's file in a new buffer & window."
(interactive)
(let* ((classes (as3-all-classes))
(choices (mapcar (lambda (c)
`(,(as3-class-name c) . ,(as3-class-file-path c))
) classes)))
(if (not (null choices))
(let* ((key (ido-completing-read
(format "Select a class (%s available): " (length choices))
choices
nil t nil))
(path (cdr (assoc key choices))))
(find-file-other-window path))
)))
(defun as3-override-method-by-name ()
"Find a method by name, insert an override stub at point."
(interactive)
(let* ((start-point (point))
(class (as3-current-class))
(superclass (as3-super-class-for-class class))
(methods (if superclass (as3-class-instance-methods superclass) '()))
(choices (mapcar (lambda (m)