-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharei-syntax.el
78 lines (61 loc) · 2.98 KB
/
arei-syntax.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
;;; arei-syntax.el --- Tools for navigating and manipulating code -*- lexical-binding: t; coding:utf-8 -*-
;; Copyright © 2023, 2024 Andrew Tropin
;; Author: Andrew Tropin <[email protected]>
;; 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 <https://www.gnu.org/licenses/>.
;;; Commentary:
;; Tools for navigating and manipulating code
;;; Code:
(defconst arei-syntax--module-re
"^[[:blank:]]*([[:blank:]\n]*define-module[[:blank:]\n]+\\(([^)]+)\\)"
"Regular expression matching a top-level definition of the module.")
(defconst arei-syntax--library-re
"^[[:blank:]]*([[:blank:]\n]*\\(?:define-\\)?library[[:blank:]\n]+\\(([^)]+)\\)"
"Regular expression matching a top-level definition of the library.")
(defun arei-current-module ()
"Find current buffer's module. It goes backwards and looking for
`arei--module-re' or `arei--library-re' to match, after that it
goes the opposite direction. This logic is not perfect, but
works good enough for this amount of code. Also, it's similar
to geiser and cider approaches. In the future it would be better
to extract this information from tree-sitter or some other more
preciese way."
(save-excursion
(when (or (re-search-backward arei-syntax--module-re nil t)
(re-search-backward arei-syntax--library-re nil t)
(re-search-forward arei-syntax--module-re nil t)
(re-search-forward arei-syntax--library-re nil t))
(match-string-no-properties 1))))
(defun arei-syntax-last-sexp-bounds ()
"Return bounds of last S-expression."
(save-excursion
(let* ((start (progn (backward-sexp)
(point)))
(end (progn (forward-sexp)
(point))))
(cons start end))))
(defun arei-syntax-current-top-level-form ()
"Return bounds of the current top-level-form."
(save-excursion
(let ((end (progn (end-of-defun) (backward-char) (point)))
(start (progn (beginning-of-defun) (point))))
(cons start end))))
(defun arei-syntax-list-at-point ()
"Return bounds of the list (compound form) at point, otherwise nil."
(bounds-of-thing-at-point 'list))
(defun arei-syntax-buffer-bounds ()
"Return bounds of the buffer."
(cons (point-min) (point-max)))
;; TODO: [Andrew Tropin, 2024-07-15] Implement
;; arei-syntax-thing-at-point, it's necessary for easier migration to
;; tree-sitter in the future, more precise selection of a thing at
;; point and support for other languages.
(provide 'arei-syntax)