-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
vhdl-ts-mode.el
1431 lines (1256 loc) · 55.9 KB
/
vhdl-ts-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
;;; vhdl-ts-mode.el --- VHDL Tree-sitter major mode -*- lexical-binding: t -*-
;; Copyright (C) 2022-2024 Gonzalo Larumbe
;; Author: Gonzalo Larumbe <gonzalomlarumbe@gmail.com>
;; URL: https://github.com/gmlarumbe/vhdl-ts-mode
;; Version: 0.2.0
;; Keywords: VHDL, IDE, Tools
;; Package-Requires: ((emacs "29.1"))
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Major mode to navigate and edit VHDL files with tree-sitter.
;;
;; Provides tree-sitter based implementations for the following features:
;; - Syntax highlighting
;; - Indentation
;; - `imenu'
;; - `which-func'
;; - Navigation functions
;; - Completion at point
;;
;;
;; Contributions:
;; This major mode is still under active development!
;; Check contributing guidelines:
;; - https://github.com/gmlarumbe/vhdl-ts-mode#contributing
;;
;; For some highlight queries examples, check the link below:
;; - https://github.com/alemuller/tree-sitter-vhdl/blob/main/queries/highlights.scm
;;; Code:
;;; Requirements
(require 'treesit)
(require 'vhdl-mode)
(declare-function treesit-parser-create "treesit.c")
(declare-function treesit-induce-sparse-tree "treesit.c")
(declare-function treesit-node-parent "treesit.c")
(declare-function treesit-node-start "treesit.c")
(declare-function treesit-node-end "treesit.c")
(declare-function treesit-node-child "treesit.c")
(declare-function treesit-node-child-by-field-name "treesit.c")
(declare-function treesit-node-type "treesit.c")
;;; Customization
(defgroup vhdl-ts nil
"VHDL tree-sitter mode."
:group 'languages)
(defcustom vhdl-ts-indent-level 4
"Tree-sitter indentation of VHDL statements with respect to containing block."
:group 'vhdl-ts
:type 'integer)
(defcustom vhdl-ts-file-extension-re "\\.vhdl?\\'"
"VHDL file extensions.
Defaults to .vhd and .vhdl."
:group 'vhdl-ts
:type 'string)
(defcustom vhdl-ts-imenu-style 'tree
"Style of generated Imenu index for current VHDL buffer.
- Simple: default basic grouping into categories.
- Tree: tree-like structure without further processing.
- Tree-group: tree-like structure with some processing to group same type
elements into subcategories (useful for display with third party
packages such as `imenu-list')."
:type '(choice (const :tag "simple" simple)
(const :tag "tree" tree)
(const :tag "tree-group" tree-group))
:group 'vhdl-ts)
(defcustom vhdl-ts-which-func-style 'custom
"Style of `which-func' display for current VHDL buffer.
- Simple: show last element of current Imenu entry
- Breadcrumb: display hierarchy of current Imenu entry
- Custom: use custom `vhdl-ts-mode' implementation for `which-func':
- Format A:B
- A represents the type of current node, B its name
- For instances, A is the instance name and B the instance type."
:type '(choice (const :tag "simple" simple)
(const :tag "breadcrumb" breadcrumb)
(const :tag "custom" custom))
:group 'vhdl-ts)
(defcustom vhdl-ts-beautify-align-ports-and-params nil
"Align all ports and params of instances when beautifying."
:type 'boolean
:group 'vhdl-ts)
;;; Utils
;;;; Core
(defconst vhdl-ts-identifier-re "\\(identifier\\|simple_name\\)")
(defconst vhdl-ts-instance-re "\\_<component_instantiation_statement\\_>")
(defun vhdl-ts--node-at-point ()
"Return tree-sitter node at point."
(treesit-node-at (point) 'vhdl))
(defun vhdl-ts--node-has-parent-recursive (node node-type)
"Return non-nil if NODE is part of NODE-TYPE in the parsed tree."
(treesit-parent-until
node
(lambda (node) ; Third argument must be a function
(string-match node-type (treesit-node-type node)))))
(defun vhdl-ts--node-has-child-recursive (node node-type)
"Return first node of type NODE-TYPE that is a child of NODE in the parsed tree.
If none is found, return nil."
(treesit-search-subtree node node-type))
(defun vhdl-ts--node-identifier-name (node)
"Return identifier name of NODE."
(let (temp-node)
(when node
(cond ((string-match vhdl-ts-instance-re (treesit-node-type node))
(cond ((setq temp-node (treesit-search-subtree node "\\_<component_instantiation\\_>"))
(treesit-node-text (or (treesit-node-child-by-field-name (treesit-search-subtree temp-node "selected_name") "suffix")
(treesit-node-child-by-field-name temp-node "component"))
:no-prop))
((setq temp-node (treesit-search-subtree node "entity_instantiation"))
(treesit-node-text (treesit-node-child-by-field-name (treesit-node-child temp-node 1) "suffix") :no-props))
(t (error "Unexpected component_instantiation_statement subnode!"))))
((string-match "function_\\(declaration\\|body\\)" (treesit-node-type node))
(treesit-node-text (treesit-search-subtree node (concat "\\(operator_symbol\\|" vhdl-ts-identifier-re "\\)")) :no-prop)) ; Overloaded functions
(t
(treesit-node-text (treesit-search-subtree node vhdl-ts-identifier-re) :no-prop))))))
(defun vhdl-ts--node-instance-name (node)
"Return identifier name of NODE.
Node must be of type `vhdl-ts-instance-re'. Otherwise return nil."
(unless (and node
(string-match vhdl-ts-instance-re (treesit-node-type node)))
(error "Wrong node type: %s" (treesit-node-type node)))
(treesit-node-text (treesit-search-subtree node "identifier") :no-props))
(defun vhdl-ts--highest-node-at-pos (pos)
"Return highest node starting at POS in the parsed tree.
Only might work as expected if point is at the beginning of a symbol.
Snippet fetched from `treesit--indent-1'."
(let* ((smallest-node (vhdl-ts--node-at-point))
(node (treesit-parent-while
smallest-node
(lambda (node)
(eq pos (treesit-node-start node))))))
node))
(defun vhdl-ts--highest-node-at-symbol ()
"Return highest node in the hierarchy for symbol at point.
Check also `treesit-thing-at-point' for similar functionality."
(vhdl-ts--highest-node-at-pos (car (bounds-of-thing-at-point 'symbol))))
(defun vhdl-ts--node-at-bol ()
"Return node at first non-blank character of current line.
Snippet fetched from `treesit--indent-1'."
(let* ((bol (save-excursion
(forward-line 0)
(skip-chars-forward " \t")
(point)))
(smallest-node
(cond ((null (treesit-parser-list)) nil)
((eq 1 (length (treesit-parser-list)))
(treesit-node-at bol))
((treesit-language-at (point))
(treesit-node-at bol (treesit-language-at (point))))
(t (treesit-node-at bol))))
(node (treesit-parent-while
smallest-node
(lambda (node)
(eq bol (treesit-node-start node))))))
node))
(defun vhdl-ts--node-parents-list (node node-type)
"Return NODE parents that match NODE-TYPE as a list of nodes."
(let (parent parent-list)
(while (setq parent (vhdl-ts--node-has-parent-recursive node node-type))
(push parent parent-list)
(setq node parent))
(nreverse parent-list)))
(defun vhdl-ts-nodes (pred &optional start)
"Return current buffer NODES that match PRED.
If optional arg START is non-nil, use it as the initial node to search in the
tree."
(let ((root-node (or start (treesit-buffer-root-node))))
(mapcar #'car (cdr (treesit-induce-sparse-tree root-node pred)))))
(defun vhdl-ts-nodes-props (pred &optional start)
"Return nodes and properties that satisfy PRED in current buffer.
If optional arg START is non-nil, use it as the initial node to search in the
tree.
Returned properties are a property list that include node name, start position
and end position."
(mapcar (lambda (node)
`(,node :name ,(vhdl-ts--node-identifier-name node)
:start-pos ,(treesit-node-start node)
:end-pos ,(treesit-node-end node)))
(vhdl-ts-nodes pred start)))
;;;; Context
(defconst vhdl-ts-block-at-point-re
(eval-when-compile
(regexp-opt
'("entity_declaration"
"architecture_body"
"process_statement"
"procedure_body"
"function_body"
"generate_statement_body"
"block_statement"
"component_instantiation_statement")
'symbols)))
(defun vhdl-ts-entity-at-point ()
"Return node of entity at point."
(let ((entity (vhdl-ts--node-has-parent-recursive (vhdl-ts--node-at-point) "entity_declaration"))
(pos (point)))
(when (and entity
(>= pos (treesit-node-start entity))
(<= pos (treesit-node-end entity)))
entity)))
(defun vhdl-ts-arch-at-point ()
"Return node of architectre body at point."
(let ((arch (vhdl-ts--node-has-parent-recursive (vhdl-ts--node-at-point) "architecture_body"))
(pos (point)))
(when (and arch
(>= pos (treesit-node-start arch))
(<= pos (treesit-node-end arch)))
arch)))
(defun vhdl-ts-instance-at-point ()
"Return node of instance at point."
(let ((instance (vhdl-ts--node-has-parent-recursive (vhdl-ts--node-at-point) vhdl-ts-instance-re))
(pos (point)))
(when (and instance
(>= pos (treesit-node-start instance))
(<= pos (treesit-node-end instance)))
instance)))
(defun vhdl-ts--block-at-point (regex)
"Return deepest node of block at point that matches REGEX."
(let ((blocks (vhdl-ts--node-parents-list (vhdl-ts--node-at-point) regex))
(pos (point)))
(catch 'block
(mapc (lambda (block)
(when (and block
(>= pos (treesit-node-start block))
(<= pos (treesit-node-end block)))
(throw 'block block)))
blocks)
nil)))
(defun vhdl-ts-block-at-point ()
"Return node of block at point."
(vhdl-ts--block-at-point vhdl-ts-block-at-point-re))
(defun vhdl-ts-nodes-block-at-point (pred)
"Return block at point NODES that match PRED."
(mapcar #'car (cdr (treesit-induce-sparse-tree (vhdl-ts-block-at-point) pred))))
(defun vhdl-ts-search-node-block-at-point (pred &optional backward all)
"Search forward for node matching PRED inside block at point.
By default, only search for named nodes, but if ALL is non-nil, search
for all nodes. If BACKWARD is non-nil, search backwards."
(treesit-search-forward (vhdl-ts--node-at-point)
(lambda (node)
(and (string-match pred (treesit-node-type node))
(< (treesit-node-end node) (treesit-node-end (vhdl-ts-block-at-point)))))
backward
all))
;; Some examples using previous API
(defun vhdl-ts-entity-declarations-nodes-current-buffer ()
"Return entity declaration nodes of current file."
(vhdl-ts-nodes "entity_declaration"))
(defun vhdl-ts-entity-declarations-current-buffer ()
"Return entity declaration names of current file."
(mapcar (lambda (node-and-props)
(plist-get (cdr node-and-props) :name))
(vhdl-ts-nodes-props "entity_declaration")))
(defun vhdl-ts-arch-body-nodes-current-buffer ()
"Return architecture body nodes of current file."
(vhdl-ts-nodes "architecture_body"))
(defun vhdl-ts-arch-body-current-buffer ()
"Return architecture body names of current file."
(mapcar (lambda (node-and-props)
(plist-get (cdr node-and-props) :name))
(vhdl-ts-nodes-props "architecture_body")))
(defun vhdl-ts-arch-instances-nodes (arch-node)
"Return instance nodes of ARCH-NODE."
(unless (and arch-node (string= "architecture_body" (treesit-node-type arch-node)))
(error "Wrong arch-node: %s" arch-node))
(vhdl-ts-nodes vhdl-ts-instance-re arch-node))
(defun vhdl-ts-arch-instances (arch-node)
"Return instances of ARCH-NODE."
(unless (and arch-node (string= "architecture_body" (treesit-node-type arch-node)))
(error "Wrong arch-node: %s" arch-node))
(mapcar (lambda (node-and-props)
(plist-get (cdr node-and-props) :name))
(vhdl-ts-nodes-props vhdl-ts-instance-re arch-node)))
(defun vhdl-ts-arch-process-blocks (arch-node)
"Return process blocks of ARCH-NODE."
(unless (and arch-node (string= "architecture_body" (treesit-node-type arch-node)))
(error "Wrong arch-node: %s" arch-node))
(mapcar (lambda (node-and-props)
(plist-get (cdr node-and-props) :name))
(vhdl-ts-nodes-props "process_statement" arch-node)))
(defun vhdl-ts-arch-concurrent-assignments (arch-node)
"Return concurrent assignments of ARCH-NODE."
(unless (and arch-node (string= "architecture_body" (treesit-node-type arch-node)))
(error "Wrong arch-node: %s" arch-node))
(mapcar (lambda (node-and-props)
(plist-get (cdr node-and-props) :name))
(vhdl-ts-nodes-props "simple_concurrent_signal_assignment" arch-node)))
(defun vhdl-ts-arch-entity-name (arch-node)
"Return associated entity name of ARCH-NODE."
(unless (and arch-node (string= "architecture_body" (treesit-node-type arch-node)))
(error "Wrong arch-node: %s" arch-node))
(treesit-node-text (treesit-node-child-by-field-name arch-node "entity") :no-props))
;;;; Navigation
(defun vhdl-ts-forward-sexp (&optional arg)
"Move forward across S-expressions.
With `prefix-arg', move ARG expressions."
(interactive "p")
(if (member (following-char) '(?\( ?\{ ?\[))
(if (and arg (< arg 0))
(backward-sexp arg)
(forward-sexp arg))
(let* ((node (or (vhdl-ts--highest-node-at-symbol)
(vhdl-ts--node-at-point)))
(beg (treesit-node-start node))
(end (treesit-node-end node)))
(if (and arg (< arg 0))
(goto-char beg)
(goto-char end)))))
(defun vhdl-ts-backward-sexp (&optional arg)
"Move backward across S-expressions.
With `prefix-arg', move ARG expressions."
(interactive "p")
(if (member (preceding-char) '(?\) ?\} ?\]))
(if (and arg (< arg 0))
(forward-sexp arg)
(backward-sexp arg))
(let* ((node (treesit-node-parent (vhdl-ts--node-at-point)))
(beg (treesit-node-start node))
(end (treesit-node-end node)))
(if (and arg (< arg 0))
(goto-char end)
(goto-char beg)))))
;;; Font-lock
;;;; Faces
(defgroup vhdl-ts-faces nil
"VHDL-ts faces."
:group 'vhdl-ts)
(defvar vhdl-ts-font-lock-then-face 'vhdl-ts-font-lock-then-face)
(defface vhdl-ts-font-lock-then-face
'((t (:inherit font-lock-keyword-face)))
"Face for if-else grouping keyword: then."
:group 'vhdl-ts-faces)
(defvar vhdl-ts-font-lock-punctuation-face 'vhdl-ts-font-lock-punctuation-face)
(defface vhdl-ts-font-lock-punctuation-face
'((t (:inherit font-lock-punctuation-face)))
"Face for punctuation symbols:
!,;:?'=<>*"
:group 'vhdl-ts-faces)
(defvar vhdl-ts-font-lock-operator-face 'vhdl-ts-font-lock-operator-face)
(defface vhdl-ts-font-lock-operator-face
'((t (:inherit font-lock-operator-face)))
"Face for operator symbols, such as &^~+-/|."
:group 'vhdl-ts-faces)
(defvar vhdl-ts-font-lock-parenthesis-face 'vhdl-ts-font-lock-parenthesis-face)
(defface vhdl-ts-font-lock-parenthesis-face
'((t (:inherit font-lock-bracket-face)))
"Face for parenthesis ()."
:group 'vhdl-ts-faces)
(defvar vhdl-ts-font-lock-brackets-content-face 'vhdl-ts-font-lock-brackets-content-face)
(defface vhdl-ts-font-lock-brackets-content-face
'((t (:inherit font-lock-number-face)))
"Face for content between brackets: arrays, bit vector width and indexing."
:group 'vhdl-ts-faces)
(defvar vhdl-ts-font-lock-port-connection-face 'vhdl-ts-font-lock-port-connection-face)
(defface vhdl-ts-font-lock-port-connection-face
'((t (:inherit font-lock-constant-face)))
"Face for port connections of instances.
portA => signalA,
portB => signalB
);"
:group 'vhdl-ts-faces)
(defvar vhdl-ts-font-lock-entity-face 'vhdl-ts-font-lock-entity-face)
(defface vhdl-ts-font-lock-entity-face
'((t (:inherit font-lock-function-call-face)))
"Face for entity names."
:group 'vhdl-ts-faces)
(defvar vhdl-ts-font-lock-instance-face 'vhdl-ts-font-lock-instance-face)
(defface vhdl-ts-font-lock-instance-face
'((t (:inherit font-lock-variable-use-face)))
"Face for instance names."
:group 'vhdl-ts-faces)
(defvar vhdl-ts-font-lock-instance-lib-face 'vhdl-ts-font-lock-instance-lib-face)
(defface vhdl-ts-font-lock-instance-lib-face
'((t (:inherit font-lock-property-name-face)))
"Face for instances lib prefix."
:group 'vhdl-ts-faces)
(defvar vhdl-ts-font-lock-translate-off-face 'vhdl-ts-font-lock-translate-off-face)
(defface vhdl-ts-font-lock-translate-off-face
'((t (:slant italic)))
"Face for pragmas between comments, e.g:
* translate_off / * translate_on"
:group 'vhdl-ts-faces)
;;;; Functions
(defun vhdl-ts--fontify-error (node _override start end &rest _)
"Fontify a syntax error with a red wavy underline.
For NODE,OVERRIDE, START, END, and ARGS, see `treesit-font-lock-rules'."
(treesit-fontify-with-override (treesit-node-start node)
(treesit-node-end node)
'(:underline (:style wave :color "Red1"))
'append
start end))
;;;; Keywords
(defconst vhdl-ts-keywords (append vhdl-02-keywords vhdl-08-keywords))
(defconst vhdl-ts-types (append vhdl-02-types vhdl-08-types vhdl-math-types))
(defconst vhdl-ts-types-regexp (regexp-opt vhdl-ts-types 'symbols))
(defconst vhdl-ts-attributes (append vhdl-02-attributes vhdl-08-attributes))
(defconst vhdl-ts-enum-values vhdl-02-enum-values)
(defconst vhdl-ts-constants vhdl-math-constants)
(defconst vhdl-ts-functions (append vhdl-02-functions vhdl-08-functions vhdl-math-functions))
(defconst vhdl-ts-functions-regexp (regexp-opt vhdl-ts-functions 'symbols))
(defconst vhdl-ts-packages (append vhdl-02-packages vhdl-08-packages vhdl-math-packages))
(defconst vhdl-ts-directives vhdl-08-directives)
(defconst vhdl-ts-operators-relational '("=" "/=" "<" ">"
"<=" ; Less or equal/signal assignment
">=" ; Greater or equal
":=" ; Variable assignment (not an operator, but falls better here)
"=>")) ; Port connection (not an operator, but falls better here)
(defconst vhdl-ts-operators-arithmetic '("+" "-" "*" "/" "**" "&"))
(defconst vhdl-ts-punctuation '(";" ":" "," "'" "|" "." "!" "?"))
(defconst vhdl-ts-parenthesis '("(" ")" "[" "]"))
;;;; Treesit-settings
(defvar vhdl-ts--font-lock-settings
(treesit-font-lock-rules
:feature 'comment
:language 'vhdl
'((comment) @font-lock-comment-face)
:feature 'string
:language 'vhdl
'([(string_literal)
(bit_string_literal)
(character_literal)]
@font-lock-string-face)
:feature 'keyword
:language 'vhdl
`((["downto" "to"] @vhdl-ts-font-lock-instance-lib-face)
(["then"] @vhdl-ts-font-lock-then-face)
([,@vhdl-ts-keywords] @font-lock-keyword-face)
(attribute_name ; clk'event / s'range
prefix: (simple_name) @font-lock-builtin-face
(predefined_designator) @font-lock-builtin-face))
:feature 'punctuation
:language 'vhdl
`(([,@vhdl-ts-punctuation] @vhdl-ts-font-lock-punctuation-face)
([,@vhdl-ts-parenthesis] @vhdl-ts-font-lock-parenthesis-face))
:feature 'operator
:language 'vhdl
`(([,@vhdl-ts-operators-relational] @vhdl-ts-font-lock-punctuation-face)
([,@vhdl-ts-operators-arithmetic] @vhdl-ts-font-lock-operator-face))
:feature 'declaration
:language 'vhdl
'(;; Entity
(entity_declaration
name: (identifier) @font-lock-function-name-face)
(entity_declaration
at_end: (simple_name) @font-lock-function-name-face)
;; Architecture
(architecture_body
(identifier) @font-lock-function-name-face
(simple_name) @font-lock-function-name-face)
;; Component
(component_declaration
name: (identifier) @font-lock-function-name-face)
;; Package
(package_declaration
(identifier) @font-lock-function-name-face)
(package_body
(simple_name) @font-lock-function-name-face)
;; Function
(procedure_declaration (identifier) @font-lock-function-name-face)
(procedure_body (identifier) @font-lock-function-name-face)
(function_declaration (identifier) @font-lock-function-name-face)
(function_body (identifier) @font-lock-function-name-face)
;; Function Overloading
(function_declaration (operator_symbol) @font-lock-function-name-face)
(function_body (operator_symbol) @font-lock-function-name-face)
;; Constants
(constant_declaration
(identifier_list (identifier) @font-lock-constant-face))
;; Alias
(alias_declaration
designator : (identifier) @font-lock-constant-face))
:feature 'type
:language 'vhdl
`((full_type_declaration
name: (identifier) @font-lock-type-face)
(subtype_declaration
name: (identifier) @font-lock-type-face)
((ambiguous_name
prefix: (simple_name) @font-lock-type-face)
(:match ,vhdl-ts-types-regexp @font-lock-type-face))
(subtype_indication
(type_mark
(simple_name) @font-lock-type-face)))
:feature 'instance
:language 'vhdl
'((component_instantiation_statement
(label (identifier) @vhdl-ts-font-lock-instance-face)
(entity_instantiation
(selected_name
prefix: (simple_name) @vhdl-ts-font-lock-instance-lib-face
suffix: (simple_name) @vhdl-ts-font-lock-entity-face)))
(component_instantiation_statement
(label (identifier) @vhdl-ts-font-lock-instance-face)
(component_instantiation
(selected_name
prefix: (selected_name) @vhdl-ts-font-lock-instance-lib-face
suffix: (simple_name) @vhdl-ts-font-lock-entity-face)))
(component_instantiation_statement
(label (identifier) @vhdl-ts-font-lock-instance-face)
(entity_instantiation (simple_name) @vhdl-ts-font-lock-entity-face))
(component_instantiation_statement
(label (identifier) @vhdl-ts-font-lock-instance-face)
(component_instantiation (simple_name) @vhdl-ts-font-lock-entity-face)))
:feature 'builtin
:language 'vhdl
`(((ambiguous_name
prefix: (simple_name) @font-lock-builtin-face)
(:match ,vhdl-ts-functions-regexp @font-lock-builtin-face)))
:feature 'array
:language 'vhdl
:override t
'((descending_range
high: (simple_expression) @vhdl-ts-font-lock-brackets-content-face)
(descending_range
low: (simple_expression) @vhdl-ts-font-lock-brackets-content-face)
(ascending_range
high: (simple_expression) @vhdl-ts-font-lock-brackets-content-face)
(ascending_range
low: (simple_expression) @vhdl-ts-font-lock-brackets-content-face)
(expression_list
(expression (integer_decimal) @vhdl-ts-font-lock-brackets-content-face))
(expression_list
(expression (simple_name) @vhdl-ts-font-lock-brackets-content-face)))
:feature 'misc
:language 'vhdl
'(;; Library
(library_clause
(logical_name_list (simple_name) @font-lock-builtin-face))
(use_clause
(selected_name
(selected_name (simple_name) @font-lock-function-name-face)))
;; Generate
(if_generate_statement
(label (identifier) @font-lock-constant-face))
(for_generate_statement
(label (identifier) @font-lock-constant-face))
;; Block
(block_statement
(label (identifier) @font-lock-constant-face))
;; Process
(process_statement
(label (identifier) @font-lock-constant-face))
(process_statement
(sensitivity_list (simple_name) @font-lock-constant-face))
;; Port connections
(association_list
(named_association_element
formal_part: (simple_name) @vhdl-ts-font-lock-port-connection-face))
(association_list
(named_association_element
formal_part: (selected_name
prefix: (simple_name) @vhdl-ts-font-lock-instance-lib-face
suffix: (simple_name) @vhdl-ts-font-lock-port-connection-face)))
(association_list
(named_association_element
formal_part:
(ambiguous_name
(simple_name) @vhdl-ts-font-lock-port-connection-face)))
(association_list
(named_association_element
formal_part:
(slice_name
(simple_name) @vhdl-ts-font-lock-port-connection-face)))
;; Enum labels
(enumeration_type_definition
literal: (identifier) @font-lock-constant-face)
;; Record members
(selected_name
prefix: (simple_name) @vhdl-ts-font-lock-instance-lib-face))
:feature 'error
:language 'vhdl
:override t
'((ERROR) @vhdl-ts--fontify-error)))
;;; Indent
;;;; Matchers
(defun vhdl-ts--matcher-blank-line (node &rest _)
"A tree-sitter simple indent matcher for NODE.
Matches if point is at a blank line."
(unless node
t))
(defun vhdl-ts--matcher-generic-or-port (node &rest _)
"A tree-sitter simple indent matcher for NODE.
Matches if point is at generic/port declaration."
(let* ((node-type (treesit-node-type node))
(entity-or-comp-node (vhdl-ts--node-has-parent-recursive node "\\(entity\\|component\\)_declaration")))
(when (and entity-or-comp-node
(string-match "\\(\\(generic\\|port\\)_clause\\|\\(entity\\|component\\)_header\\)" node-type))
(treesit-node-start entity-or-comp-node))))
(defun vhdl-ts--matcher-keyword (node &rest _)
"A tree-sitter simple indent matcher for NODE.
Matches if point is at a VHDL keyword, somehow as a fallback."
(member (treesit-node-type node) vhdl-ts-keywords))
(defun vhdl-ts--matcher-punctuation (node &rest _)
"A tree-sitter simple indent matcher for NODE.
Matches if point is at a punctuation/operator char, somehow as a fallback."
(member (treesit-node-type node) `(,@vhdl-ts-punctuation ,@vhdl-ts-parenthesis)))
(defun vhdl-ts--matcher-default (&rest _)
"A tree-sitter simple indent matcher."
t)
;;;; Anchors
(defun vhdl-ts--anchor-point-min (&rest _)
"A tree-sitter simple indent anchor."
(save-excursion
(point-min)))
(defun vhdl-ts--anchor-instance-port (node &rest _)
"A tree-sitter simple indent anchor for NODE."
(let ((label-node (vhdl-ts--node-has-parent-recursive node "\\(component_instantiation\\|call\\)_statement")))
(treesit-node-start label-node)))
(defun vhdl-ts--anchor-concurrent-signal-assignment (node parent &rest _)
"A tree-sitter simple indent anchor for NODE and PARENT."
(let ((gen-node (vhdl-ts--node-has-parent-recursive node "\\(for\\|if\\)_generate_statement")))
(if gen-node
(treesit-node-start gen-node)
(treesit-node-start (treesit-node-parent parent)))))
;;;; Rules
(defconst vhdl-ts--indent-zero-parent-node-re
(eval-when-compile
(regexp-opt '("design_file" "context_clause" "design_unit") 'symbols)))
(defvar vhdl-ts--treesit-indent-rules
`((vhdl
;; Comments
((and (node-is "comment")
(parent-is ,vhdl-ts--indent-zero-parent-node-re))
parent-bol 0)
((node-is "comment") grand-parent vhdl-ts-indent-level)
;; Zero-indent
((or (node-is "\\(library\\|use\\)_clause")
(node-is "design_unit") ; architecture_body
(node-is "entity_declaration")
(node-is "architecture_body")
(node-is "package_\\(declaration\\|body\\)"))
parent-bol 0)
;; Procedure parameter types
(vhdl-ts--matcher-generic-or-port grand-parent vhdl-ts-indent-level)
((node-is "\\(constant\\|variable\\|signal\\)_interface_declaration") parent-bol vhdl-ts-indent-level)
;; Declarations
((node-is "declarative_part") parent-bol vhdl-ts-indent-level) ; First declaration of the declarative part
((or (node-is "\\(component\\|signal\\|constant\\|full_type\\|element\\|variable\\|procedure\\|function\\)_declaration")
(node-is "\\(function\\|procedure\\)_body"))
grand-parent vhdl-ts-indent-level)
;; Block
((node-is "block_header") parent-bol vhdl-ts-indent-level)
((or (node-is "block_statement")
(parent-is "block_header"))
grand-parent vhdl-ts-indent-level)
;; Concurrent & generate
((or (node-is "concurrent_statement_part") ; First signal declaration of a declarative part
(node-is "generate_statement_body"))
parent-bol vhdl-ts-indent-level)
((node-is "\\(for\\|if\\)_generate_statement") grand-parent vhdl-ts-indent-level)
((node-is "\\(simple\\|conditional\\)_concurrent_signal_assignment") vhdl-ts--anchor-concurrent-signal-assignment vhdl-ts-indent-level) ; Parent is (concurrent_statement_part)
((and (node-is "waveforms")
(parent-is "alternative_\\(conditional\\|selected\\)_waveforms"))
grand-parent 0) ; when else on next line or select multiple lines
((node-is "process_statement") grand-parent vhdl-ts-indent-level) ; Grandparent is architecture_body
((node-is "selected_waveforms") parent-bol vhdl-ts-indent-level)
((and (node-is "simple_name")
(parent-is "selected_concurrent_signal_assignment"))
parent-bol vhdl-ts-indent-level)
;; Instances
((node-is "component_\\(instantiation_statement\\|map_aspect\\)") grand-parent vhdl-ts-indent-level)
((node-is "port_map_aspect") parent-bol 0) ; Port map only when there are generics
((node-is "association_list") parent-bol vhdl-ts-indent-level)
((node-is "named_association_element") parent-bol 0)
;; Procedural
((node-is "sequence_of_statements") parent-bol vhdl-ts-indent-level) ; Statements inside process
((parent-is "sequence_of_statements") grand-parent vhdl-ts-indent-level)
((node-is "\\(if\\|else\\|elsif\\)") parent-bol 0)
((node-is "case_statement") grand-parent vhdl-ts-indent-level)
((node-is "case_statement_alternative") parent-bol vhdl-ts-indent-level)
;; Others
((node-is "aggregate") grand-parent vhdl-ts-indent-level) ; Aggregates/array elements
((node-is "positional_element_association") parent-bol 0) ; Check test/files/common/indent_misc.vhd:42
;; Opening & closing
((node-is "\\(begin\\|end\\|)\\)") parent-bol 0)
;; Fallbacks/default
((and vhdl-ts--matcher-blank-line (parent-is ,vhdl-ts--indent-zero-parent-node-re)) parent-bol 0)
((and vhdl-ts--matcher-blank-line
(not (parent-is "\\(concurrent_statement\\|declarative\\)_part"))
(not (parent-is "association_list")))
parent-bol vhdl-ts-indent-level)
((or vhdl-ts--matcher-keyword vhdl-ts--matcher-punctuation) parent-bol vhdl-ts-indent-level)
(vhdl-ts--matcher-default parent 0))))
;;; Imenu
(defconst vhdl-ts-imenu-create-index-re
(eval-when-compile
(regexp-opt
'(;; 3.2 Entity declarations
"entity_declaration"
;; 3.3 Architecture bodies
"architecture_body"
;; 3.4 Configuration declarations
"configuration_declaration"
;; 3.4.3 Component configuration
"component_configuration"
;; 4.2.1 Subprogram declarations
"procedure_declaration"
"function_declaration"
;; 4.3 Subprogram bodies
"procedure_body"
"function_body"
;; 4.4 Subprogram instantiation declarations
"procedure_instantiation_declaration"
"function_instantiation_declaration"
;; 4.7 Package declarations
"package_declaration"
;; 4.8 Package bodies
"package_body"
;; 4.9 Package instantiation declarations
"package_instantiation_declaration"
;; 6.8 Component declarations
"component_declaration"
;; 11 Concurrent statements
"block_statement"
"process_statement"
"component_instantiation_statement"
"for_generate_statement"
"if_generate_statement"
"case_generate_statement")
'symbols)))
(defvar vhdl-ts-imenu-format-item-label-function
'vhdl-ts-imenu-format-item-label
"Imenu function used to format an item label.
It must be a function with two arguments: TYPE and NAME.")
(defvar vhdl-ts-imenu-format-parent-item-label-function
'vhdl-ts-imenu-format-parent-item-label
"Imenu function used to format a parent item label.
It must be a function with two arguments: TYPE and NAME.")
(defvar vhdl-ts-imenu-format-parent-item-jump-label-function
'vhdl-ts-imenu-format-parent-item-jump-label
"Imenu function used to format a parent jump item label.
It must be a function with two arguments: TYPE and NAME.")
(defun vhdl-ts-imenu-format-item-label (_type name)
"Return Imenu label for single node using TYPE and NAME."
(format "%s" name))
(defun vhdl-ts-imenu-format-parent-item-label (_type name)
"Return Imenu label for parent node using TYPE and NAME."
(format "%s" name))
(defun vhdl-ts-imenu-format-parent-item-jump-label (type _name)
"Return Imenu label for parent node jump using TYPE and NAME."
(format "*%s*" type))
(defun vhdl-ts-imenu-treesit-create-index-tree (node)
"Given a sparse tree, create an imenu alist.
NODE is the root node of the tree returned by
`treesit-induce-sparse-tree' (not a tree-sitter node, its car is
a tree-sitter node). Walk that tree and return an imenu alist.
Return a list of ENTRY where
ENTRY := (NAME . MARKER)
| (NAME . ((JUMP-LABEL . MARKER)
ENTRY
...)
NAME is the function/class's name, JUMP-LABEL is like \"*function
definition*\".
Copied from Python's `python--imenu-treesit-create-index-1' and adapted to
VHDL parser."
(let* ((ts-node (car node))
(children (cdr node))
(subtrees (mapcan #'vhdl-ts-imenu-treesit-create-index-tree
children))
(type (treesit-node-type ts-node))
;; The root of the tree could have a nil ts-node.
(name (when ts-node
(or (vhdl-ts--node-identifier-name ts-node)
"Anonymous")))
(marker (when ts-node
(set-marker (make-marker)
(treesit-node-start ts-node)))))
(cond
;; Root node
((null ts-node)
subtrees)
;; Non-leaf node
(subtrees
(let ((parent-label (funcall vhdl-ts-imenu-format-parent-item-label-function type name))
(jump-label (funcall vhdl-ts-imenu-format-parent-item-jump-label-function type name)))
`((,parent-label
,(cons jump-label marker)
,@subtrees))))
;; Leaf node
(t (let ((label (funcall vhdl-ts-imenu-format-item-label-function type name)))
(list (cons label marker)))))))
(defun vhdl-ts--imenu-treesit-create-index-tree-group-process (subtree)
"Utility function to process SUBTREE and group leaves into categories."
(let (instances processes procedures functions components default)
(mapc
(lambda (elm)
(if (and (listp elm) (listp (cdr elm)) (listp (cddr elm)) ; Basic checks due to custom imenu entry format for grouping
(markerp (cadr elm)) ; Element can be grouped because it was added ...
(stringp (caddr elm))) ; ... a third field, indicating tree-sitter type
(let ((type (caddr elm))
(entry (cons (car elm) (cadr elm))))
(pcase type
("component_instantiation_statement" (push entry instances))
("process_statement" (push entry processes))
((or "procedure_declaration" "procedure_body") (push entry procedures))
((or "function_declaration" "function_body") (push entry functions))
("component_declaration" (push entry components))
(_ (push entry default))))
;; Otherwise entry cannot be grouped because it already was, or because it was a leaf node
(push elm default)))
subtree)
;; Populate return value
(if (or processes instances procedures functions components) ; Avoid processing when no grouping is required
(progn
(when instances
(setq instances (nreverse instances))
(setq default `(("Instances" ,@instances) ,@default)))
(when processes
(setq processes (nreverse processes))
(setq default `(("Processes" ,@processes) ,@default)))
(when procedures
(setq procedures (nreverse procedures))
(setq default `(("Procedures" ,@procedures) ,@default)))
(when functions
(setq functions (nreverse functions))
(setq default `(("Functions" ,@functions) ,@default)))
(when components
(setq components (nreverse components))
(setq default `(("Components" ,@components) ,@default)))
default)
;; Else it might be processing of the leaf nodes of top subtree and reordering is required
(nreverse default))))
(defun vhdl-ts-imenu-treesit-create-index-tree-group (node)
"Given a sparse tree, create an imenu alist.
NODE is the root node of the tree returned by
`treesit-induce-sparse-tree' (not a tree-sitter node, its car is
a tree-sitter node). Walk that tree and return an imenu alist.
Return a list of ENTRY where
ENTRY := (NAME . MARKER)
| (NAME . ((JUMP-LABEL . MARKER)
ENTRY
...)
NAME is the function/class's name, JUMP-LABEL is like \"*function
definition*\".
Copied from Python's `python--imenu-treesit-create-index-1' and adapted to
VHDL parser."
(let* ((ts-node (car node))
(children (cdr node))
(subtrees (mapcan #'vhdl-ts-imenu-treesit-create-index-tree-group
children))
(type (treesit-node-type ts-node))
;; The root of the tree could have a nil ts-node.
(name (when ts-node
(or (vhdl-ts--node-identifier-name ts-node)
"Anonymous")))
(marker (when ts-node
(set-marker (make-marker)
(treesit-node-start ts-node)))))
(cond
;; Root node
((null ts-node)
(vhdl-ts--imenu-treesit-create-index-tree-group-process subtrees))
;; Non-leaf node
(subtrees
(let ((parent-label (funcall vhdl-ts-imenu-format-parent-item-label-function type name))
(jump-label (funcall vhdl-ts-imenu-format-parent-item-jump-label-function type name)))
`((,parent-label
,(cons jump-label marker)
,@(vhdl-ts--imenu-treesit-create-index-tree-group-process subtrees)))))
;; Leaf node
(t (let ((label (funcall vhdl-ts-imenu-format-item-label-function type name)))
(if (member type '("component_instantiation_statement"
"process_statement"
"procedure_declaration"
"function_declaration"
"procedure_body"
"function_body"
"component_declaration"))
(list (list label marker type))
(list (cons label marker))))))))
(defun vhdl-ts--imenu-create-index (func &optional node)
"Imenu index builder function for `vhdl-ts-mode'.