forked from mickeynp/combobulate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombobulate.el
193 lines (166 loc) · 7.79 KB
/
combobulate.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
;;; combobulate.el --- edit and navigate text by syntactic constructs -*- lexical-binding: t; -*-
;; Copyright (C) 2021-23 Mickey Petersen
;; Author: Mickey Petersen <mickey at masteringemacs.org>
;; Package-Requires: ((emacs "29"))
;; Version: 0.1
;; Homepage: https://www.github.com/mickeynp/combobulate
;; Keywords: convenience, tools, languages
;; 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:
;;
;; Navigate and transform source code using the concrete syntax tree
;; provided by the Emacs 29's builtin support for tree-sitter.
;;
;;; Code:
;; Requirements:
(require 'seq)
(require 'subr-x)
(require 'tempo)
(require 'map)
(eval-when-compile
(require 'cl-lib))
(require 'combobulate-settings)
(defvar-local combobulate-options-envelope-key-map
(make-sparse-keymap "Combobulate Envelopes")
"Dynamically set key map of Combobulate envelopes.")
(defvar combobulate-edit-key-map
(let ((map (make-sparse-keymap "Combobulate Edit")))
(define-key map (kbd "c") #'combobulate-edit-cluster-dwim)
(define-key map (kbd "t") #'combobulate-edit-node-type-dwim)
(define-key map (kbd "x") #'combobulate-edit-node-by-text-dwim)
map))
(defvar combobulate-options-key-map
(let ((map (make-sparse-keymap "Combobulate Options")))
(define-key map (kbd "j") #'combobulate-avy-jump)
(define-key map (kbd "t") combobulate-edit-key-map)
(define-key map (kbd "o") #'combobulate)
(define-key map (kbd "c") #'combobulate-clone-node-dwim)
(define-key map (kbd "v") #'combobulate-vanish-node)
map))
(defvar combobulate-key-map
(let ((map (make-sparse-keymap "Combobulate")))
(define-key map (kbd "C-M-a") #'combobulate-navigate-beginning-of-defun)
(define-key map (kbd "C-M-d") #'combobulate-navigate-down-list-maybe)
(define-key map (kbd "C-M-e") #'combobulate-navigate-end-of-defun)
(define-key map (kbd "C-M-h") #'combobulate-mark-defun)
(define-key map (kbd "C-M-n") #'combobulate-navigate-next)
(define-key map (kbd "C-M-p") #'combobulate-navigate-previous)
(define-key map (kbd "C-M-t") #'combobulate-transpose-sexps)
(define-key map (kbd "C-M-u") #'combobulate-navigate-up-list-maybe)
(define-key map (kbd "M-<up>") #'combobulate-splice-up)
(define-key map (kbd "M-<down>") #'combobulate-splice-down)
(define-key map (kbd "M-<left>") #'combobulate-yeet-forward)
(define-key map (kbd "M-<right>") #'combobulate-yoink-forward)
(define-key map (kbd "M-N") #'combobulate-drag-down)
(define-key map (kbd "M-P") #'combobulate-drag-up)
(define-key map (kbd "M-a") #'combobulate-navigate-logical-previous)
(define-key map (kbd "M-e") #'combobulate-navigate-logical-next)
(define-key map (kbd "M-h") #'combobulate-mark-node-dwim)
(define-key map (kbd "M-k") #'combobulate-kill-node-dwim)
map))
(when combobulate-key-prefix
(define-key combobulate-key-map (kbd combobulate-key-prefix) combobulate-options-key-map))
(make-variable-buffer-local 'forward-sexp-function)
(defun combobulate--setup-envelopes (envelopes)
"Prepare ENVELOPES for interactive use.
Each envelope is read and an interactive function for it
created."
(mapcar
(lambda (envelope)
(map-let (:description :name :template :point-placement) envelope
(let ((fn-name (intern (string-replace
" " "-"
(concat
combobulate-envelope-symbol-prefix
(symbol-name major-mode)
"-"
name)))))
(set fn-name template)
;; Store the function symbol for later recall in things like
;; transient.
(setf envelopes
(plist-put envelope
:function
(defalias fn-name
`(lambda () ,description
(interactive)
(combobulate-execute-envelope ,name)))))
(setf envelopes (plist-put envelope :point-placement (or point-placement 'start))))))
envelopes))
(defun combobulate-setup ()
"Setup combobulate in the current buffer.
This can be used to reinitialize mode-specific setups if they
have changed."
(interactive)
(if-let* ((lang (car-safe (treesit-parser-list)))
(parser-lang (treesit-parser-language lang))
(setup-fn (alist-get parser-lang combobulate-setup-functions-alist)))
(progn
;; load the production rules
(setq combobulate-navigation-rules-overrides nil)
(setq combobulate-navigation-rules-overrides-inverted nil)
(setq combobulate-navigation-rules
(symbol-value (intern (format "combobulate-rules-%s" parser-lang))))
(setq combobulate-navigation-rules-inverted
(symbol-value (intern (format "combobulate-rules-%s-inverted" parser-lang))))
;; prepare the sexp functions so they use our version
(setq-local forward-sexp-function #'combobulate-forward-sexp-function)
(setq-local transpose-sexps-function #'combobulate-transpose-sexp-function)
(funcall setup-fn parser-lang)
;; this should come after the funcall to `setup-fn' as we need
;; the procedures setup and ready before we continue.
(setq-local combobulate-navigation-editable-nodes
(combobulate-procedure-get-activation-nodes combobulate-manipulation-edit-procedures))
(when combobulate-key-prefix
(local-set-key
(kbd (format "%s e" combobulate-key-prefix))
;; todo: this should be a single-shot setup per mode.
(let ((map (make-sparse-keymap)))
(dolist (envelope (combobulate--setup-envelopes
(append combobulate-manipulation-envelopes
(alist-get parser-lang combobulate-manipulation-envelopes-custom))))
(map-let (:function :key :extra-key) envelope
(define-key map (kbd key) function)
(when extra-key
(define-key combobulate-key-map (kbd extra-key) function))))
map)))
(run-hooks 'combobulate-after-setup-hook))
(user-error "Combobulate cannot find a setup function for this tree sitter language.
Customize `combobulate-setup-functions-alist' to change the language setup alist.")))
(define-minor-mode combobulate-mode "Navigate and edit text by syntactic constructs
\\{combobulate-key-map}"
:init-value nil :lighter "©" :keymap combobulate-key-map
(condition-case nil
(when combobulate-mode
(combobulate-setup))
(user-error
(combobulate-message "There is either no tree sitter language in this buffer, or Combobulate does not support it.")
(combobulate-mode -1))))
;;; internal
(require 'combobulate-rules)
(require 'combobulate-navigation)
(require 'combobulate-manipulation)
(require 'combobulate-envelope)
(require 'combobulate-contrib)
(require 'combobulate-display)
(require 'combobulate-ui)
(require 'combobulate-misc)
;;; end internal
;;; language support
(require 'combobulate-html)
(require 'combobulate-python)
(require 'combobulate-js-ts)
(require 'combobulate-css)
(require 'combobulate-yaml)
;;; end language support
(provide 'combobulate)
;;; combobulate.el ends here