-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
verilog-ext-beautify.el
182 lines (155 loc) · 6.23 KB
/
verilog-ext-beautify.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
;;; verilog-ext-beautify.el --- Verilog-ext Beautify -*- lexical-binding: t -*-
;; Copyright (C) 2022-2024 Gonzalo Larumbe
;; Author: Gonzalo Larumbe <[email protected]>
;; URL: https://github.com/gmlarumbe/verilog-ext
;; 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:
;; Indent, align parameters and ports:
;; - Interactively for module at point
;; - Interactively for current buffer
;; - In batch for list of files and files of current directory
;;; Code:
(require 'verilog-ext-nav)
(defun verilog-ext-beautify-block-at-point-indent ()
"Indent current block at point."
(let ((data (verilog-ext-block-at-point :return-pos))
start-pos end-pos block name)
(unless data
(user-error "Not inside a block"))
(save-excursion
(setq block (alist-get 'type data))
(setq name (alist-get 'name data))
(goto-char (alist-get 'beg-point data))
(setq start-pos (line-beginning-position))
(goto-char (alist-get 'end-point data))
(setq end-pos (line-end-position))
(verilog-ext-indent-region start-pos end-pos)
(message "Indented %s : %s" block name))))
(defun verilog-ext-beautify--module-at-point-indent ()
"Indent current module."
(let ((case-fold-search nil)
(current-ids (verilog-ext-instance-at-point))
current-module beg end)
(unless current-ids
(user-error "Not inside an instance!"))
(setq current-module (car current-ids))
(save-excursion
(goto-char (match-beginning 0))
(beginning-of-line)
(setq beg (point))
(goto-char (match-end 0))
(end-of-line)
(setq end (point)))
(verilog-ext-indent-region beg end)
(message "Indented %s" current-module)))
(defun verilog-ext-beautify--module-at-point-align (thing)
"Align THING of current module at point (ports/parameters)."
(let ((case-fold-search nil)
(re "\\(\\s-*\\)(")
(current-ids (verilog-ext-instance-at-point))
(idx (cond ((eq thing 'parameters) 1)
((eq thing 'ports) 2)
(t (error "Invalid thing to align"))))
current-module beg end)
(unless current-ids
(user-error "Not inside an instance!"))
(setq current-module (car current-ids))
(save-excursion
(goto-char (match-beginning idx))
(verilog-re-search-forward "(" nil t)
(verilog-forward-syntactic-ws)
(setq beg (point))
(verilog-backward-up-list -1)
(backward-char)
(verilog-backward-syntactic-ws)
(setq end (point)))
(align-regexp beg end re 1 1 nil)
(if (eq idx 1)
(message "Parameters of %s aligned..." current-module)
(message "Ports of %s aligned..." current-module))))
(defun verilog-ext-beautify--module-at-point-align-ports ()
"Align ports of current module."
(verilog-ext-beautify--module-at-point-align 'ports))
(defun verilog-ext-beautify--module-at-point-align-params ()
"Align parameters of current module."
(verilog-ext-beautify--module-at-point-align 'parameters))
(defun verilog-ext-beautify-module-at-point ()
"Beautify current module:
- Indent
- Align ports
- Align parameters"
(interactive)
(save-excursion
(verilog-ext-beautify--module-at-point-indent)
(verilog-ext-beautify--module-at-point-align-ports)
(verilog-ext-beautify--module-at-point-align-params)))
(defun verilog-ext-beautify-block-at-point ()
"Beautify/indent block at point.
If block is an instance, also align parameters and ports."
(interactive)
;; Precedence is relevant in the subsequent conditional clause
(cond (;; Tree-sitter implementation
(eq major-mode 'verilog-ts-mode)
(verilog-ts-beautify-block-at-point))
(;; Module instance
(and verilog-ext-file-allows-instances
(verilog-ext-instance-at-point))
(verilog-ext-beautify-module-at-point))
(t
(verilog-ext-beautify-block-at-point-indent))))
(defun verilog-ext-beautify-current-buffer ()
"Beautify current buffer:
- Indent whole buffer
- Beautify every instantiated module
- Untabify and delete trailing whitespace"
(interactive)
(if (eq major-mode 'verilog-ts-mode)
;; `verilog-ts-mode' based beautifying
(verilog-ts-beautify-current-buffer)
;; `verilog-mode' based beautifying
(verilog-ext-indent-region (point-min) (point-max))
(save-excursion
(goto-char (point-min))
(while (verilog-ext-find-module-instance-fwd)
(verilog-ext-beautify-module-at-point)))
(untabify (point-min) (point-max))
(delete-trailing-whitespace (point-min) (point-max))))
(defun verilog-ext-beautify-files (files ts-mode)
"Beautify Verilog FILES.
FILES is a list of strings containing the filepaths.
If TS-MODE is non-nil use tree-sitter implementation if `verilog-ts-mode' is
available."
(dolist (file files)
(unless (file-exists-p file)
(error "File %s does not exist! Aborting!" file)))
(save-window-excursion
(dolist (file files)
(message "Processing %s..." file)
(with-temp-file file
(insert-file-contents file)
(if ts-mode
(progn
(verilog-ts-mode)
(verilog-ts-beautify-current-buffer))
(verilog-ext-with-no-hooks
(verilog-mode))
(verilog-ext-beautify-current-buffer))))))
(defun verilog-ext-beautify-dir-files (dir &optional ts-mode)
"Beautify Verilog files on DIR.
Include subdirectory files recursively.
With prefix arg, or if TS-MODE is non-nil, use `verilog-ts-mode' beautifying
implementation."
(interactive "DDirectory: \nP")
(let ((files (verilog-ext-dir-files dir :recursive)))
(verilog-ext-beautify-files files ts-mode)))
(provide 'verilog-ext-beautify)
;;; verilog-ext-beautify.el ends here