-
Notifications
You must be signed in to change notification settings - Fork 8
/
ember-mode.el
1526 lines (1352 loc) · 70.6 KB
/
ember-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
;;; ember-mode.el --- Ember navigation mode for emacs
;;;;;;;;;;;;;;;;
;;;; MIT License
;; Copyright (C) 2014 Aad Versteden
;;
;; Permission is hereby granted, free of charge, to any person
;; obtaining a copy of this software and associated documentation
;; files (the "Software"), to deal in the Software without
;; restriction, including without limitation the rights to use, copy,
;; modify, merge, publish, distribute, sublicense, and/or sell copies
;; of the Software, and to permit persons to whom the Software is
;; furnished to do so, subject to the following conditions:
;;
;; The above copyright notice and this permission notice shall be
;; included in all copies or substantial portions of the Software.
;;
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
;; BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
;; ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
;; CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
;; SOFTWARE.
;;;;;;;;;;;;;;;
;;;; Accounting
;; Version: 0.3.1
;; Author: Aad Versteden <[email protected]>
;; Keywords: ember ember.js emberjs
;; License: MIT
;; Package-Requires: ((cl-lib "0.5"))
;;; Commentary:
;; This is a proof of concept for ember-mode. ember-mode helps you
;; navigate through the files in your emberjs project. A bunch of
;; bindings have been created to quickly jump to the relevant sources
;; given that you're visiting a recognised file (*) in the ember project.
;;
;; In the current state, you can quickly jump to the:
;; - model
;; - controller
;; - route
;; - router
;; - view
;; - component
;; - template
;;
;; ember-mode is currently geared towards ember-cli, however the
;; folder structure is similar in similar build systems for ember so
;; it will probably work there as well.
;;
;;
;; (*) There is a base implementation for the file recognition, but it
;; needs improvement so you can always jump back from a found file.
;; Some (somewhat) less common files are not recognised yet.
;;; Code:
(require 'cl-lib)
(defgroup ember nil
"Ember-mode customizations."
:prefix "ember-"
:group 'tools)
;;;;;;;;;;;;;;;;;;;
;;;; Debug printing
(defvar ember--debug-ember-mode nil)
(defvar ember--debug-output-buffer (generate-new-buffer "*ember-mode-debug*"))
(defun ember--debug-princ (&rest args)
"Prints content to the ember debug output using princ"
(when ember--debug-ember-mode
(princ args ember--debug-output-buffer)
(princ "\n" ember--debug-output-buffer)))
(defun ember--debug-print (&rest args)
"Prints content to the ember debug output using print"
(when ember--debug-ember-mode
(print args ember--debug-output-buffer)))
(ember--debug-princ "*This buffer contains debug s-expressions from ember-mode*")
;;;;;;;;;;;;;;;;;;
;;;; Opening files
(defun ember--find-file (path)
"Opens the supplied file and pushes the current location on the xref marker stack if there is support"
(when (functionp #'xref-push-marker-stack)
(xref-push-marker-stack))
(find-file path))
;;;;;;;;;;;;
;;;; plurals
;;
;; This should really be replaced by a Snowball or Porter2 stemmer.
;; It seems to be good enough for a proof of concept of ember-mode.
(defcustom ember-pluralization-irregular-nouns
'(("child" . "children") ("woman" . "women") ("man" . "men") ("mouse" . "mice") ("goose" . "geese"))
"Contain irregular pluralizations which ember-mode considers."
:type '(alist :key-type string :value-type string)
:group 'ember)
(defun ember--pluralize-noun (noun)
"Pluralizes NOUN."
(save-match-data
(cond ((cl-find noun ember-pluralization-irregular-nouns :key #'car :test #'string=)
(cdr (cl-find noun ember-pluralization-irregular-nouns :key #'car :test #'string=)))
((string-match-p "[yo]$" noun)
(message "Don't know how to translate %s" noun)
noun)
((or (string-match "ch$" noun)
(string-match "[xs]$" noun))
(concat noun "es"))
((string-match "^\\(.*\\)fe?$" noun)
(concat (match-string 1 noun) "ves"))
(t (concat noun "s")))))
(defun ember--singularize-noun (noun)
"Singularizes NOUN."
(save-match-data
(cond ((cl-find noun ember-pluralization-irregular-nouns :key #'cdr :test #'string=)
(car (cl-find noun ember-pluralization-irregular-nouns :key #'cdr :test #'string=)))
((string-match "^\\(.*ch\\)es$" noun)
(match-string 1 noun))
((string-match "^\\(.*[xs]\\)es$" noun)
(match-string 1 noun))
((string-match "^\\(.*\\)ves$" noun)
(concat (match-string 1 noun) "f")) ;; this is just a wild guess, it might as well be fe
((string-match "^\\(.*\\)s$" noun)
(match-string 1 noun))
(t noun))))
;;;;;;;;;;;;;;;;;;;;
;;; General Settings
(defcustom ember-script-file-types
'("coffee" "js" "ts")
"Filetypes used for script files. These are the javascript and the coffeescript file.
The first item in this list is used as the 'default', indicating
the preference to look up this type of file."
:type '(repeat string)
:group 'ember)
(defcustom ember-template-file-types
'("hbs" "html" "handlebars")
"Filetypes used for snippet files. These are the handlebars and html source files.
The first item in this list is used as the 'default', used when creating files."
:type '(repeat string)
:group 'ember)
(defcustom ember-keymap-prefix (kbd "C-c .")
"Ember keymap prefix."
:group 'ember
:type 'key-sequence
:set
(lambda (option value)
(when (boundp 'ember-mode-keymap)
(define-key ember-mode-keymap ember-keymap-prefix nil)
(define-key ember-mode-keymap value 'ember-command-prefix))
(set-default 'ember-keymap-prefix value)))
(defcustom ember-completion-system 'ido
"Which completion system ember-mode should use."
:group 'ember
:type '(radio
(const :tag "Ido" ido)
(const :tag "Helm" helm)
(const :tag "Default" default)))
(defcustom ember-command "ember"
"Ember command"
:group 'ember
:type 'string)
;;;;;;;;;;;;;;;;;;;;;;;;;
;;; POD structure support
(defvar ember-use-pods 'unset
"Default to not using the POD structure for now.")
(defun ember--dot-ember-cli-has-pods-p ()
"Returns non-nil iff the .ember-cli file in the root sets
usePods to true. Very basic detection is performed to see if the
line with usePods is commented out."
(let ((ember-cli-filename (concat (ember--current-project-root)
"/.ember-cli")))
(when (file-exists-p ember-cli-filename)
(with-temp-buffer
(insert-file-contents ember-cli-filename)
(re-search-forward "^[^/]*\"usePods\".*:.*true" nil t)))))
;;;;;;;;;;;;;;;;;;;;;;;;
;;; MU structure support
(defvar ember-use-mu 'unset
"Defaults to not using the MU structure for now.")
(defun ember--dot-ember-cli-has-mu-p ()
"Returns non-nil iff the .ember-cli file in the root sets
usePods to true. Very basic detection is performed to see if the
line with usePods is commented out."
(let ((ember-cli-filename (concat (ember--current-project-root)
"/.ember-cli.js")))
(when (file-exists-p ember-cli-filename)
(or
(with-temp-buffer
(insert-file-contents ember-cli-filename)
(re-search-forward ".*EMBER_CLI_MODULE_UNIFICATION.*true.*" nil t))
(with-temp-buffer
(insert-file-contents ember-cli-filename)
(re-search-forward "\\s*setEdition([\\s'\"]*octane[\\s'\"]*)" nil t))))))
(defun ember--has-mu-p ()
"Returns t iff mu is enabled"
(interactive)
(message (if ember-use-mu "mu use flag on" "mu use flag off"))
(message (if (ember--dot-ember-cli-has-mu-p) "mu in .ember-cli.js" "mu not in .ember-cli.js"))
(or ember-use-mu (ember--dot-ember-cli-has-mu-p)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Shared support for MU and PODs
(defun ember--get-matcher-map-name ()
"Returns the name of the map for the current matcher.
Returns either 'pod and 'no-pod."
(cond
((eq ember-use-mu t) 'mu)
((eq ember-use-pods t) 'pod)
((eq ember-use-pods nil) 'no-pod)
(t (cond ((ember--dot-ember-cli-has-mu-p) 'mu)
((ember--dot-ember-cli-has-pods-p) 'pod)
(t 'no-pod)))))
(defun base-prefixes ()
'("app" "src" "addon"))
;;;;;;;;;;;;;;
;;; Navigation
(defvar *ember--matcher-templates* (make-hash-table)
"Contains a hash with lists of file templates. The key of the
list is the name of the map (no-pod or pod).
A file template is a list containing:
- the base type (eg: component)
- the target kind (eg: source)
- the file path (eg: (list \"app/components/\" :class \".\" :extension))
It is assumed that the base-type and target-kind regexes don't
contain parens, `ember--relative-file-components' makes use of this
assumption.
From the string base, a type can be built.")
(setf *ember--matcher-templates* (make-hash-table))
(cl-defun ember--define-matcher (map-name base-type-regex target-kind base-location &optional (base-type base-type-regex))
"Adds a matcher to the end of the list of *EMBER--MATCHER-TEMPLATES* for map-name"
(setf (gethash map-name *ember--matcher-templates*)
(append (gethash map-name *ember--matcher-templates*)
(list (list base-type-regex target-kind base-location base-type)))))
(defmacro ember--define-matchers (matcher-map-name &rest matchers)
`(progn ,@(cl-loop for matcher in matchers
collect
`(ember--define-matcher ',matcher-map-name
,(car matcher) ,(cadr matcher)
(list ,@(cl-caddr matcher))
,@(cl-cdddr matcher)))))
(ember--define-matchers no-pod
;; BEGIN contains the definition for each matcher
;; the first two columns are a regexp, the rest is executed as code
;; base-type | target-kind | concatenation lambda body | override base-type
("router" ".*" (:prefix "/router" "." :jsext))
("^route$" "source" (:prefix "/routes/" :class "." :jsext) "route")
("model" "source" (:prefix "/models/" :class "." :jsext))
("view" "source" (:prefix "/views/" :class "." :jsext))
("component" "source" (:prefix "/components/" :class "." :jsext))
("controller" "source" (:prefix "/controllers/" :class "." :jsext))
("mixin" "source" (:prefix "/mixins/" :class "." :jsext))
("initializer" "source" (:prefix "/initializers/" :class "." :jsext))
("util" "source" (:prefix "/utils/" :class "." :jsext))
("helper" "source" (:prefix "/helpers/" :class "." :jsext))
("service" "source" (:prefix "/services/" :class "." :jsext))
("component" "template" (:prefix "/templates/components/" :class "." :hbext))
("component" "template" (:prefix "/components/" :class "." :hbext))
("template" ".*" (:prefix "/templates/" :class "." :hbext))
(".*" "template" (:prefix "/templates/" :class "." :hbext) "template")
;; END contains the definition of each matcher
)
(ember--define-matchers pod
;; BEGIN contains the definition for each matcher
;; the first two columns are a regexp, the rest is executed as code
;; base-type | target-kind | concatenation lambda body | override base-type
("router" ".*" (:prefix "/router" "." :jsext))
("^route$" "source" (:prefix "/" :class "/route" "." :jsext) "route")
("model" "source" (:prefix "/" :class "/model" "." :jsext))
("view" "source" (:prefix "/" :class "/view" "." :jsext))
("controller" "source" (:prefix "/" :class "/controller" "." :jsext))
("service" "source" (:prefix "/" :class "/service" "." :jsext))
("component" "source" (:prefix "/components/" :class "/component" "." :jsext))
("mixin" "source" (:prefix "/mixins/" :class "." :jsext))
("initializer" "source" (:prefix "/initializers/" :class "." :jsext))
("util" "source" (:prefix "/utils/" :class "." :jsext))
("helper" "source" (:prefix "/helpers/" :class "." :jsext))
("service" "source" (:prefix "/services/" :class "." :jsext))
("component" "template" (:prefix "/components/" :class "/template" "." :hbext))
("template" "source" (:prefix "/" :class "/template" "." :hbext))
(".*" "template" (:prefix "/" :class "/template" "." :hbext) "template")
;; END contains the definition of each matcher
)
(ember--define-matchers mu
;; BEGIN contains the definition for each matcher
;; the first two columns are a regexp, the rest is executed as code
;; base-type | target-kind | concatenation lambda body | override base-type
("router" ".*" (:prefix "/router" "." :jsext))
("^route$" "source" (:prefix "/ui/routes/" :class "/route" "." :jsext) "route")
("model" "source" (:prefix "/data/models/" :class "/model" "." :jsext))
("controller" "source" (:prefix "/controllers/" :class "." :jsext))
("service" "source" (:prefix "/services/" :class "." :jsext))
("component" "source" (:prefix "/ui/components/" :class "/component" "." :jsext))
("mixin" "source" (:prefix "/mixins/" :class "." :jsext))
("initializer" "source" (:prefix "/init/initializers/" :class "." :jsext))
("util" "source" (:prefix "/utils/" :class "." :jsext))
("helper" "source" (:prefix "/helpers/" :class "." :jsext))
("service" "source" (:prefix "/services/" :class "." :jsext))
("component" "template" (:prefix "/ui/components/" :class "/template" "." :hbext))
("template" "source" (:prefix "/ui/routes/" :class "/template" "." :hbext))
(".*" "template" (:prefix "/ui/routes/" :class "/template" "." :hbext) "template")
;; END contains the definition of each matcher
)
(defun ember--current-matcher-templates ()
"Returns the contents of the matcher templates given the current
POD setting."
(gethash (ember--get-matcher-map-name) *ember--matcher-templates*))
(defun ember--matcher-partial-fill (matcher-template &rest options)
"Fills in the parts of MATCHER-TEMPLATE which could be filled in
with the supplied OPTIONS.
OPTIONS is expected to be a plist containing the keywords in which
the :prefix keyword is required and :extension and :class are
optional."
(cl-loop for item in matcher-template
for substitution = (cl-getf options item)
if substitution
collect substitution
else
collect item))
(defun ember--matcher-relative-path (matcher-template &rest options)
"Constructs the relative path for MATCHER-TEMPLATE, given the
options in OPTIONS.
OPTIONS should be an alist containing the keywords :PREFIX, :CLASS
and :EXTENSION. Some matchers may not require all to be supplied."
(apply #'concat
(cl-loop for item in
(apply #'ember--matcher-partial-fill matcher-template options)
if (stringp item) collect item
else collect "")))
(defun ember--matcher-matches-p (matcher base-type target-kind)
"Returns non-nil iff MATCHER matches BASE-TYPE and TARGET-KIND."
(cl-destructuring-bind (base-type-regexp target-kind-regexp)
matcher
(and (string-match base-type-regexp base-type)
(string-match target-kind-regexp target-kind))))
(defun ember--matcher-templates-for (base-type target-kind)
"Returns the matcher templates which match BASE-TYPE and
TARGET-KIND in the order in which the matchers have been
defined."
(cl-loop for (base-type-regexp target-kind-regexp matcher-template)
in (ember--current-matcher-templates)
if (and (string-match base-type-regexp (or base-type ""))
(string-match target-kind-regexp (or target-kind "")))
collect matcher-template))
(defun ember--matchers-for (base-type target-kind)
"Similar to ember--matcher-templates-for, but returning the the
whole matcher"
(cl-loop for matcher in (ember--current-matcher-templates)
for (base-type-regexp target-kind-regexp matcher-template) = matcher
if (and (string-match base-type-regexp (or base-type ""))
(string-match target-kind-regexp (or target-kind "")))
collect matcher))
(defun ember--matcher-hbs-template-p (matcher-template)
"Returns non-nil iff the matcher-template has a handlebars-extension"
(cl-find :hbext matcher-template))
(defun ember--matcher-js-template-p (matcher-template)
"Returns non-nil iff the matcher-template has a javascript-extension"
(cl-find :jsext matcher-template))
(defun ember--matcher-template-map-extensions (matcher-template)
"Returns a new matcher-template for each of the file-types which fit
the matcher-template"
(cond ((ember--matcher-js-template-p matcher-template)
(cl-loop for ext in ember-script-file-types collect
(ember--matcher-partial-fill matcher-template :jsext ext)))
((ember--matcher-hbs-template-p matcher-template)
(cl-loop for ext in ember-template-file-types collect
(ember--matcher-partial-fill matcher-template :hbext ext)))
(t (list matcher-template))))
(defun ember--matcher-template-map-prefixes (matcher-template)
"Returns a new matcher-template for each prefix"
(list (ember--matcher-partial-fill matcher-template :prefix "app")
(ember--matcher-partial-fill matcher-template :prefix "addon")
(ember--matcher-partial-fill matcher-template :prefix "src")))
(defun ember--regex-escape-matcher-template (matcher-template)
"Returns the same matcher but in which the components can be used in
a regular expression. The most common regex patterns will have been
replaced."
(let ((symbols-to-replace (list "." "+" "*")))
(cl-loop for char in symbols-to-replace
do (setf matcher-template
(cl-loop for component in matcher-template
if (stringp component)
collect (replace-regexp-in-string (concat "\\" char)
(concat "\\\\" char)
component)
else
collect component)))
matcher-template))
(defun ember--matcher-matches-file-p (matcher relative-path)
"Returns non-nil iff MATCHER matches RELATIVE-PATH.
If this returns non-nil, a PLIST is returned which maps the variables in the
template to their corresponding values in RELATIVE-PATH."
(let ((matcher-template (ember--get-matcher-template matcher)))
(let ((component-symbols (cl-loop for component in matcher-template
if (symbolp component)
collect component)))
(let ((template-regex
(concat
"^"
(apply #'ember--matcher-relative-path
(ember--regex-escape-matcher-template matcher-template)
(cl-loop for symbol in component-symbols append
(let ((regex (if (eq symbol :class)
"\\(.+\\)"
"\\([^/]+\\)")))
(list symbol regex))))
"$")))
(save-match-data
(when (string-match template-regex relative-path)
(cl-loop for component-symbol in component-symbols
for index from 1
append
(list component-symbol (match-string index relative-path)))))))))
(defun ember--relative-ember-source-path (base-prefix base-class base-type target-kind)
"Supplies a list of plausible paths to an ember source file given
its core components. The paths are returned as a list of strings,
starting from the app's root.
Sources are specified in ember by a few orthogonal factors:
- BASE-CLASS :: The base class of the element we're talking about.
For instance:
- A UserRoute would have a base class of User.
- A UserModel would have a base class of User.
- A LoginRoute would have a base class of Login.
- BASE-TYPE :: The type of the class we're talking about.
For instance:
- A UserRoute would have a base type of Route.
- A UserModel would have a base type of Model.
- A LoginRoute would have a base type of Route.
- The template of a UserRoute would be app/templates/user.hbs
but this could also be specified as a base-type of 'user'
and a target-kind of 'template'.
Possible values are:
- router
- route
- model
- controller
- view
- component
- template
- index (the index template) ;; !the index no longer exists
- (blank)
- TARGET-KIND :: The target kind is the kind of source file you
expect to receive. This is either 'source', 'template', or blank.
For instance:
- The coffeescript file for a UserRoute would be 'source'
- The handlebars file for a UserController would be 'template'
- The UserComponent's handlebars file would be 'template'
- The UserComponent's coffeescript file would be 'source'
Possible values are:
- template
- source
- (blank)"
(let ((templates (ember--matcher-templates-for base-type target-kind)))
(cl-loop
for template in templates
append
(mapcar #'ember--matcher-relative-path
(ember--matcher-template-map-extensions
(ember--matcher-partial-fill template
:prefix base-prefix
:class base-class
:base-type base-type
:target-kind target-kind))))))
(defun ember--current-project-root ()
"Returns the root folder of the current ember project."
;; for the current implementation this basically walks up the tree until
;; it sees an app folder and assumes the folder containing the app folder
;; is the root of the ember project.
(ember--file-project-root (or load-file-name buffer-file-name default-directory)))
(defun ember--file-project-root (file)
(or (locate-dominating-file file ".ember-cli")
(locate-dominating-file file ".ember-cli.js")
(locate-dominating-file file "ember-cli-build.js")))
(defun ember--relative-file-components (file)
"Returns a list containing the components which make up this ember source
file.
The components are defined in `ember--relative-ember-source-path'. This function
returns the base-class, the base-type and the target-kind of the current
file."
(let ((components-and-matcher
(cl-loop for matcher in (ember--current-matcher-templates)
for components = (ember--matcher-matches-file-p matcher file)
if components
return (list components matcher))))
(when components-and-matcher
(cl-destructuring-bind (components matcher) components-and-matcher
(let ((base-class (cl-getf components :class))
(base-type (cl-fourth matcher))
(base-prefix (cl-getf components :prefix))
(target-kind (cond
((cl-find (cl-getf components :jsext) ember-script-file-types :test #'equal)
"source")
((cl-find (cl-getf components :hbext) ember-template-file-types :test #'equal)
"template"))))
(let ((response (list base-prefix base-class base-type target-kind)))
response))))))
(defun ember--file-relative-to-root (file)
"Returns the pathname of FILE relative to the current project's
root."
(file-relative-name file (ember--file-project-root file)))
(defun ember--current-file-components ()
"Returns a list containing the components which make up this
ember source file."
(or (ember--relative-file-components
(ember--file-relative-to-root (or load-file-name buffer-file-name default-directory)))
(list nil nil nil nil)))
(cl-defun ember-open-file-by-type (type &optional (assume-js t))
"Opens an ember file for TYPE with all base values assumed from
the currently open file.
ASSUME-JS is an override. If this is true, it is assumed that a
javascript (or coffeescript) source file should be opened."
(cl-destructuring-bind (base-prefix base-class base-type target-kind)
(ember--current-file-components)
(let ((new-target-kind (if assume-js "source" target-kind)))
(if (and (equal type base-type)
(equal target-kind new-target-kind))
(ember--select-file-by-type-and-kind (concat "Open " type ": ") base-type new-target-kind)
(ember-generic-open-file base-prefix base-class type new-target-kind)))))
(defun ember-open-file-by-kind (kind)
"Opens an ember file for KIND.
Kind should be one of \"template\" or \"source\"."
(cl-destructuring-bind (base-prefix base-class base-type target-kind)
(ember--current-file-components)
(message "Current file components: %s" (list base-prefix base-class base-type target-kind))
(if (equal kind target-kind)
(ember--select-file-by-type-and-kind (concat "Open " base-type ": ") base-type kind)
(ember-generic-open-file base-prefix base-class base-type kind))))
(defun ember--get-matcher-template (matcher)
"Returns the matcher template for MATCHER."
(cl-third matcher))
(defmacro ember--appendf (list-location appended-list)
"Appends APPENDED-LIST to the list on LIST-LOCATION and stores
the resulting list in LIST-LOCATION."
`(setf ,list-location (append ,list-location ,appended-list)))
(defun ember--list-files-by-type-and-kind (base-type target-kind)
"List files in DIRECTORY and in its sub-directories.
Returns files that match the regular expression MATCH but ignore
files and directories that match IGNORE (IGNORE is tested before
MATCH. Recurse only to depth MAXDEPTH. Does not recurse if
MAXDEPTH is zero or negative."
(let ((matchers (ember--matchers-for base-type target-kind))
(walk-dirs (cl-loop for prefix in '("app" "addon" "src")
for dir = (concat (ember--current-project-root) prefix)
if (file-directory-p dir)
collect dir))
matching-files)
(cl-flet ((walk-directory
(dir)
(dolist (f (directory-files dir t "[A-Za-z]"))
(cond ((file-regular-p f)
(let ((relative-file (ember--file-relative-to-root f)))
(when (cl-some (lambda (m) (ember--matcher-matches-file-p m relative-file)) matchers)
(push f matching-files))))
((file-directory-p f)
(push f walk-dirs))))))
(while walk-dirs
(let ((walk-now walk-dirs))
(setf walk-dirs nil)
(dolist (dir walk-now)
(walk-directory dir)))))
(setf matching-files (mapcar #'ember--file-relative-to-root matching-files))
(cl-remove-if #'ember--temporary-file-p matching-files)))
(defun ember--last-char (string)
"Returns the last character of STRING."
(string (elt string (1- (length string)))))
(defun ember--temporary-file-p (filename)
"Returns non-nil iff FILENAME is a temporary file."
(message (format "checking filename %s" filename))
(or (equal (ember--last-char filename) "~")
(equal (string (elt filename 0)) "#")
(and (>= (length filename) 2)
(equal (string (elt filename 0)) ".")
(equal (string (elt filename 1)) "#"))))
(defun ember--completing-read (question matches)
"A smarter completing-read which poses QUESTION with matches being MATCHES.
This replacement uses a completion system according to
`ember-completion-system'."
(cond
((eq ember-completion-system 'ido)
(ido-completing-read question matches))
((and (eq ember-completion-system 'helm)
(fboundp 'helm-comp-read))
(helm-comp-read question matches
:must-match t))
(t (completing-read question matches))))
(defun ember--select-file-by-type-and-kind (question base-type target-kind)
"Lets the user select an ember file based on its kind and type.
- QUESTION is the question which will be asked to the user.
- BASE-TYPE is the type of the resource.
- TARGET-KIND is the kind of the resource."
(let ((potential-matches (ember--list-files-by-type-and-kind base-type target-kind)))
(let ((relative-file (ember--completing-read question potential-matches)))
(when relative-file
(ember--find-file (concat (ember--current-project-root) relative-file))))))
(defun ember-generic-open-file (base-prefix base-class base-type target-kind)
"Tries to open the ember file specified by BASE-CLASS, BASE-TYPE and TARGET-KIND.
If no such file was found, it tries to find related files or
requests the user if the file should be created."
(let ((prefix-list
(cond ((equal base-prefix "src")
'("src" "app"))
((and (equal base-prefix "app") (ember--has-mu-p))
'("app" "src"))
(t
(list base-prefix)))))
(unless base-class
(setf base-class ""))
(let ((ember-root (ember--current-project-root))
(file-list
;; pick the files and their alternatives, so we have a good list
;; to search for an existing file.
(cl-loop for prefix in prefix-list append
(append (ember--relative-ember-source-path prefix base-class base-type target-kind)
(ember--relative-ember-source-path prefix (ember--pluralize-noun base-class) base-type target-kind)
(ember--relative-ember-source-path prefix (ember--singularize-noun base-class) base-type target-kind)))))
(cl-block found-file
(let ((start-buffer (current-buffer)))
(cl-loop for relative-file in file-list
for absolute-file = (concat ember-root relative-file)
;; if the buffer exists, and moving to it positions
;; us in a different buffer than where we are, then
;; we're done.
if (and (file-exists-p absolute-file)
(progn (ember--find-file absolute-file)
(not (eq start-buffer (current-buffer)))))
do
(message "returning file %s" absolute-file)
(cl-return-from found-file absolute-file)))
(when (string= target-kind "template")
(setf base-type "template"))
(ember--select-file-by-type-and-kind "Not found, alternatives: " base-type target-kind)))))
(defun ember-open-component ()
"Opens a component file based on the currently opened file."
(interactive)
(ember-open-file-by-type "component"))
(defun ember-open-router ()
"Opens the Router file based on the currently opened file."
(interactive)
(ember-open-file-by-type "router"))
(defun ember-open-controller ()
"Opens an ember Controller file based on the currently opened file."
(interactive)
(ember-open-file-by-type "controller"))
(defun ember-open-model ()
"Opens an ember Model file based on the currently opened file."
(interactive)
(ember-open-file-by-type "model"))
(defun ember-open-route ()
"Opens an ember Route file based on the currently opened file."
(interactive)
(ember-open-file-by-type "route"))
(defun ember-open-mixin ()
"Opens an ember Mixin file based on the currently opened file."
(interactive)
(ember-open-file-by-type "mixin"))
(defun ember-open-initializer ()
"Opens an ember Initializer file based on the currently opened file."
(interactive)
(ember-open-file-by-type "initializer"))
(defun ember-open-util ()
"Opens an ember Utility file based on the currently opened file."
(interactive)
(ember-open-file-by-type "util"))
(defun ember-open-helper ()
"Opens an ember Helper file based on the currently opened file."
(interactive)
(ember-open-file-by-type "helper"))
(defun ember-open-service ()
"Opens an ember Service file based on the currently opened file."
(interactive)
(ember-open-file-by-type "service"))
(defun ember-open-template ()
"Opens an ember Template file based on the currently opened file."
(interactive)
(ember-open-file-by-kind "template"))
(defun ember-open-javascript ()
"Opens an ember Javascript file based on the currently opened file.
This may be handy if you are visiting a template and want to open
the corresponding source."
(interactive)
(ember-open-file-by-kind "source"))
(defun ember-open-view ()
"Opens an ember View file based on the currently opened file."
(interactive)
(ember-open-file-by-type "view"))
(defun ember-toggle-addon ()
"Toggles between the native view and the ember addon view."
(interactive)
;; TODO fix for mu
(cl-destructuring-bind (base-prefix base-class base-type target-kind)
(ember--current-file-components)
(let ((new-base-prefix (cond ((string= base-prefix "app")
"addon")
((string= base-prefix "addon")
"app")
(t (message "Not sure if I'm in app or addon")))))
(ember-generic-open-file new-base-prefix base-class base-type target-kind))))
;;;;;;;;;;;;;;
;;; Generators
(defun ember--match-by-index (regex string index)
(save-match-data
(and (string-match regex string)
(match-string index string))))
(defun ember-generate (generator kind options)
"Runs an ember generator."
(interactive (ember--interactive-generator-options))
(let ((default-directory (ember--current-project-root)))
(let ((command (concat ember-command " generate " generator " " kind " " (or options ""))))
(message command)
(let ((response (shell-command-to-string command)))
(message response)
;; open the first file that was created
(ember--find-file (concat default-directory "/"
(ember--match-by-index "\s+create\s+\\(.*\\)" response 1)))))))
(defun ember-generate-controller (kind options)
"Generates a controller."
(interactive (ember--interactive-generator-options "controller"))
(ember-generate "controller" kind options))
(defun ember-generate-component (kind options)
"Generates a component."
(interactive (ember--interactive-generator-options "component"))
(ember-generate "component" kind options))
(defun ember-generate-model (kind options)
"Generates a model."
(interactive (ember--interactive-generator-options "model"))
(ember-generate "model" kind options))
(defun ember-generate-route (kind options)
"Generates a route."
(interactive (ember--interactive-generator-options "route"))
(ember-generate "route" kind options))
(defun ember-generate-mixin (kind options)
"Generates a mixin."
(interactive (ember--interactive-generator-options "mixin"))
(ember-generate "mixin" kind options))
(defun ember-generate-initializer (kind options)
"Generates a initializer."
(interactive (ember--interactive-generator-options "initializer"))
(ember-generate "initializer" kind options))
(defun ember-generate-util (kind options)
"Generates a utility."
(interactive (ember--interactive-generator-options "util"))
(ember-generate "util" kind options))
(defun ember-generate-helper (kind options)
"Generates a helper."
(interactive (ember--interactive-generator-options "helper"))
(ember-generate "helper" kind options))
(defun ember-generate-service (kind options)
"Generates a service."
(interactive (ember--interactive-generator-options "service"))
(ember-generate "service" kind options))
(defun ember-generate-template (kind options)
"Generates a template."
(interactive (ember--interactive-generator-options "template"))
(ember-generate "template" kind options))
(defun ember-generate-view (kind options)
"Generates a view."
(interactive (ember--interactive-generator-options "view"))
(ember-generate "view" kind options))
(defun ember--generators ()
"Returns a list of all generators."
(split-string
(shell-command-to-string
(concat ember-command
" help generate | grep -o -E -e '^ ([^ ]+)' | grep -E -o -e '[a-z\\-]+' | sort | uniq"))))
(defun ember--interactive-generator-options
(&optional supplied-generator supplied-kind destroy-p)
"Generates a function interactive statement which ensures the
arguments for the generator are known.
The interative statement will try to find all unknown values from
`ember--current-file-compononents'.
If a value was supplied to this macro directly, then that value
will be assumed to be the final value.
If destroy-p is true, the user will be informed the requests are
regarding the destruction process, rather than regarding the
generate-process.
The user will be queried for all values which weren't supplied
and which could not be found by `ember--current-file-components'
or if the user has supplied a prefix-argument. In the case of a
prefix argument all values not supplied by SUPPLIED-GENERATOR or
SUPPLIED-KIND will be queried with the default being the value
found by `ember--current-file-components'."
(cl-destructuring-bind (current-base-prefix current-base-class current-base-kind current-target-kind)
;; fetch current values from current-file-components
(condition-case err
(ember--current-file-components)
('error (list nil nil nil nil)))
;; ask the user to override
(cl-destructuring-bind (new-generator new-kind new-options)
(if current-prefix-arg
(let* ((generator (or supplied-generator current-base-kind
(ember--completing-read "Generator: " (ember--generators))))
(kind (or supplied-kind current-base-class
(read-string (concat (if destroy-p "Destroying" "Generating") " "
generator " for kind: ")))))
(list generator kind ""))
(let* ((generator (or supplied-generator
(ember--completing-read "Generator: " (ember--generators))))
(kind (or supplied-kind (read-string (concat (if destroy-p "Destroying" "Generating") " "
generator " for kind: ")
current-base-class)))
(options (read-string "Options: " )))
(list generator kind options)))
;; figure out which values we should return
(let ((result (list new-options)))
(unless supplied-kind
(push new-kind result))
(unless supplied-generator
(push new-generator result))
result))))
;;; destroy
(defun ember-destroy (generator kind options)
"Runs an ember generator."
(interactive (ember--interactive-generator-options nil nil t))
(let ((default-directory (ember--current-project-root)))
(let ((response
(shell-command-to-string (concat ember-command " destroy " generator " " kind " " options))))
(message response)
;; open the first file that was created
(ember--find-file (concat default-directory "/"
(ember--match-by-index "\s+create\s+\\(.*\\)" response 1))))))
(defun ember-destroy-controller (kind options)
"Destroys a controller."
(interactive (ember--interactive-generator-options "controller" nil t))
(ember-destroy "controller" kind options))
(defun ember-destroy-component (kind options)
"Destroys a component."
(interactive (ember--interactive-generator-options "component" nil t))
(ember-destroy "component" kind options))
(defun ember-destroy-model (kind options)
"Destroys a model."
(interactive (ember--interactive-generator-options "model" nil t))
(ember-destroy "model" kind options))
(defun ember-destroy-route (kind options)
"Destroys a route."
(interactive (ember--interactive-generator-options "route" nil t))
(ember-destroy "route" kind options))
(defun ember-destroy-mixin (kind options)
"Destroys a mixin."
(interactive (ember--interactive-generator-options "mixin" nil t))
(ember-destroy "mixin" kind options))
(defun ember-destroy-initializer (kind options)
"Destroys a initializer."
(interactive (ember--interactive-generator-options "initializer" nil t))
(ember-destroy "initializer" kind options))
(defun ember-destroy-util (kind options)
"Destroys a utility."
(interactive (ember--interactive-generator-options "util" nil t))
(ember-destroy "util" kind options))
(defun ember-destroy-helper (kind options)
"Destroys a helper."
(interactive (ember--interactive-generator-options "helper" nil t))
(ember-destroy "helper" kind options))
(defun ember-destroy-service (kind options)
"Destroys a service."
(interactive (ember--interactive-generator-options "service" nil t))
(ember-destroy "service" kind options))
(defun ember-destroy-template (kind options)
"Destroys a template."
(interactive (ember--interactive-generator-options "template" nil t))
(ember-destroy "template" kind options))
(defun ember-destroy-view (kind options)
"Destroys a view."
(interactive (ember--interactive-generator-options "view" nil t))
(ember-destroy "view" kind options))
;;;;;;;;;;;;;;;;;;;;;
;;; Compilation modes
(defcustom ember-serve-command
"ember serve"
"Default command for running ember serve with `ember-serve-or-display'."
:type 'string