mirror of https://github.com/Chizi123/.emacs.d.git

Chizi123
2018-11-21 5ddac8bd2392ec5b64392e8750d725029bf5aa79
commit | author | age
5cb5f7 1 ;;; smartparens-crystal.el --- Additional configuration for Crystal based modes.  -*- lexical-binding: t; -*-
C 2
3 ;; Copyright (C) 2018 Brantou
4
5 ;; Author: Brantou <brantou89@gmail.com>
6 ;; Maintainer: Brantou <brantou89@gmail.com>
7 ;; Created: 5 March 2018
8 ;; Keywords: abbrev convenience editing
9 ;; URL: https://github.com/Fuco1/smartparens
10
11 ;; Based on smartparens-ruby.el
12
13 ;; This file is not part of GNU Emacs.
14
15 ;;; License:
16
17 ;; This file is part of Smartparens.
18
19 ;; Smartparens is free software; you can redistribute it and/or modify
20 ;; it under the terms of the GNU General Public License as published by
21 ;; the Free Software Foundation, either version 3 of the License, or
22 ;; (at your option) any later version.
23
24 ;; Smartparens is distributed in the hope that it will be useful,
25 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
26 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27 ;; GNU General Public License for more details.
28
29 ;; You should have received a copy of the GNU General Public License
30 ;; along with Smartparens.  If not, see <http://www.gnu.org/licenses/>.
31
32 ;;; Commentary:
33
34 ;; This file provides some additional configuration for Crystal based
35 ;; modes.  To use it, simply add:
36 ;;
37 ;; (require 'smartparens-crystal)
38 ;;
39 ;; into your configuration.  You can use this in conjunction with the
40 ;; default config or your own configuration.
41 ;;
42
43 ;; If you have good ideas about what should be added please file an
44 ;; issue on the github tracker.
45
46 ;; For more info, see github readme at
47 ;; https://github.com/Fuco1/smartparens
48
49 ;;; Code:
50
51 (require 'smartparens)
52 (require 'smartparens-ruby)
53
54 (declare-function crystal-forward-sexp "crystal")
55 (declare-function crystal-backward-sexp "crystal")
56
57 (defun sp-crystal-forward-sexp ()
58   "Wrapper for `crystal-forward-sexp'."
59   (interactive)
60   (crystal-forward-sexp))
61
62 (defun sp-crystal-backward-sexp ()
63   "Wrapper for `crystal-backward-sexp'."
64   (interactive)
65   (crystal-backward-sexp))
66
67 (defun sp-crystal-inline-p (id)
68   "Test if ID is inline."
69   (save-excursion
70     (when (looking-back id nil)
71       (backward-word))
72     (when (not (or (looking-back "^[[:blank:]]*" nil)
73                    (looking-back "= *" nil)))
74       (or (save-excursion
75             (forward-symbol -1)
76             (forward-symbol 1)
77             (looking-at-p (concat " *" id)))
78           (save-excursion
79             ;; This does not seem to make emacs snapshot happy
80             (ignore-errors
81               (sp-crystal-backward-sexp)
82               (sp-crystal-forward-sexp)
83               (looking-at-p (concat "[^[:blank:]]* *" id))))))))
84
85 (defun sp-crystal-skip-inline-match-p (ms _mb _me)
86   "If non-nil, skip inline match.
87 MS, MB, ME."
88   (or (sp-ruby-method-p ms)
89       (sp-crystal-inline-p ms)))
90
91 (defun sp-crystal-in-string-word-or-inline-p (id action context)
92   "Test if point is inside string, word or inline.
93 ID, ACTION, CONTEXT."
94   (or (sp-ruby-in-string-or-word-p id action context)
95       (and (looking-back id nil)
96            (sp-crystal-inline-p id))))
97
98 (add-to-list 'sp-navigate-skip-match
99              '((crystal-mode) . sp--ruby-skip-match))
100
101 (dolist (mode '(crystal-mode))
102   (add-to-list 'sp-sexp-suffix `(,mode syntax "")))
103
104 (sp-with-modes 'crystal-mode
105   (sp-local-pair "do" "end"
106                  :when '(("SPC" "RET" "<evil-ret>"))
107                  :unless '(sp-ruby-in-string-or-word-p sp-in-comment-p)
108                  :actions '(insert navigate)
109                  :pre-handlers '(sp-ruby-pre-handler)
110                  :post-handlers '(sp-ruby-block-post-handler)
111                  :skip-match 'sp-ruby-skip-method-p
112                  :suffix "")
113
114   (sp-local-pair "{" "}"
115                  :pre-handlers '(sp-ruby-pre-handler)
116                  :post-handlers '(sp-ruby-post-handler)
117                  :suffix "")
118
119   (sp-local-pair "begin" "end"
120                  :when '(("SPC" "RET" "<evil-ret>"))
121                  :unless '(sp-ruby-in-string-or-word-p sp-in-comment-p)
122                  :actions '(insert navigate)
123                  :pre-handlers '(sp-ruby-pre-handler)
124                  :post-handlers '(sp-ruby-block-post-handler)
125                  :skip-match 'sp-ruby-skip-method-p
126                  :suffix "")
127
128   (sp-local-pair "def" "end"
129                  :when '(("SPC" "RET" "<evil-ret>"))
130                  :unless '(sp-ruby-in-string-or-word-p sp-in-comment-p)
131                  :actions '(insert navigate)
132                  :pre-handlers '(sp-ruby-pre-handler)
133                  :post-handlers '(sp-ruby-def-post-handler)
134                  :skip-match 'sp-ruby-skip-method-p
135                  :suffix "")
136
137   (sp-local-pair "class" "end"
138                  :when '(("SPC" "RET" "<evil-ret>"))
139                  :unless '(sp-ruby-in-string-or-word-p sp-in-comment-p)
140                  :actions '(insert navigate)
141                  :pre-handlers '(sp-ruby-pre-handler)
142                  :post-handlers '(sp-ruby-def-post-handler)
143                  :skip-match 'sp-ruby-skip-method-p
144                  :suffix "")
145
146   (sp-local-pair "struct" "end"
147                  :when '(("SPC" "RET" "<evil-ret>"))
148                  :unless '(sp-ruby-in-string-or-word-p sp-in-comment-p)
149                  :actions '(insert navigate)
150                  :pre-handlers '(sp-ruby-pre-handler)
151                  :post-handlers '(sp-ruby-def-post-handler)
152                  :skip-match 'sp-ruby-skip-method-p
153                  :suffix "")
154
155   (sp-local-pair "lib" "end"
156                  :when '(("SPC" "RET" "<evil-ret>"))
157                  :unless '(sp-ruby-in-string-or-word-p sp-in-comment-p)
158                  :actions '(insert navigate)
159                  :pre-handlers '(sp-ruby-pre-handler)
160                  :post-handlers '(sp-ruby-def-post-handler)
161                  :skip-match 'sp-ruby-skip-method-p
162                  :suffix "")
163
164   (sp-local-pair "fun" "end"
165                  :when '(("SPC" "RET" "<evil-ret>"))
166                  :unless '(sp-ruby-in-string-or-word-p sp-in-comment-p)
167                  :actions '(insert navigate)
168                  :pre-handlers '(sp-ruby-pre-handler)
169                  :post-handlers '(sp-ruby-def-post-handler)
170                  :skip-match 'sp-ruby-skip-method-p
171                  :suffix "")
172
173   (sp-local-pair "enum" "end"
174                  :when '(("SPC" "RET" "<evil-ret>"))
175                  :unless '(sp-ruby-in-string-or-word-p sp-in-comment-p)
176                  :actions '(insert navigate)
177                  :pre-handlers '(sp-ruby-pre-handler)
178                  :post-handlers '(sp-ruby-def-post-handler)
179                  :skip-match 'sp-ruby-skip-method-p
180                  :suffix "")
181
182   (sp-local-pair "union" "end"
183                  :when '(("SPC" "RET" "<evil-ret>"))
184                  :unless '(sp-ruby-in-string-or-word-p sp-in-comment-p)
185                  :actions '(insert navigate)
186                  :pre-handlers '(sp-ruby-pre-handler)
187                  :post-handlers '(sp-ruby-def-post-handler)
188                  :skip-match 'sp-ruby-skip-method-p
189                  :suffix "")
190
191   (sp-local-pair "module" "end"
192                  :when '(("SPC" "RET" "<evil-ret>"))
193                  :unless '(sp-ruby-in-string-or-word-p sp-in-comment-p)
194                  :actions '(insert navigate)
195                  :pre-handlers '(sp-ruby-pre-handler)
196                  :post-handlers '(sp-ruby-def-post-handler)
197                  :skip-match 'sp-ruby-skip-method-p
198                  :suffix "")
199
200   (sp-local-pair "macro" "end"
201                  :when '(("SPC" "RET" "<evil-ret>"))
202                  :unless '(sp-ruby-in-string-or-word-p sp-in-comment-p)
203                  :actions '(insert navigate)
204                  :pre-handlers '(sp-ruby-pre-handler)
205                  :post-handlers '(sp-ruby-def-post-handler)
206                  :skip-match 'sp-ruby-skip-method-p
207                  :suffix "")
208
209   (sp-local-pair "case" "end"
210                  :when '(("SPC" "RET" "<evil-ret>"))
211                  :unless '(sp-ruby-in-string-or-word-p sp-in-comment-p)
212                  :actions '(insert navigate)
213                  :pre-handlers '(sp-ruby-pre-handler)
214                  :post-handlers '(sp-ruby-def-post-handler)
215                  :skip-match 'sp-ruby-skip-method-p
216                  :suffix "")
217
218   (sp-local-pair "if" "end"
219                  :when '(("SPC" "RET" "<evil-ret>"))
220                  :unless '(sp-crystal-in-string-word-or-inline-p sp-in-comment-p)
221                  :actions '(insert navigate)
222                  :pre-handlers '(sp-ruby-pre-handler)
223                  :post-handlers '(sp-ruby-def-post-handler)
224                  :skip-match 'sp-crystal-skip-inline-match-p
225                  :suffix "")
226
227   (sp-local-pair "unless" "end"
228                  :when '(("SPC" "RET" "<evil-ret>"))
229                  :unless '(sp-crystal-in-string-word-or-inline-p sp-in-comment-p)
230                  :actions '(insert navigate)
231                  :pre-handlers '(sp-ruby-pre-handler)
232                  :post-handlers '(sp-ruby-def-post-handler)
233                  :skip-match 'sp-crystal-skip-inline-match-p
234                  :suffix "")
235
236   (sp-local-pair "while" "end"
237                  :when '(("SPC" "RET" "<evil-ret>"))
238                  :unless '(sp-crystal-in-string-word-or-inline-p sp-in-comment-p)
239                  :actions '(insert navigate)
240                  :pre-handlers '(sp-ruby-pre-handler)
241                  :post-handlers '(sp-ruby-def-post-handler)
242                  :skip-match 'sp-crystal-skip-inline-match-p
243                  :suffix "")
244
245   (sp-local-pair "until" "end"
246                  :when '(("SPC" "RET" "<evil-ret>"))
247                  :unless '(sp-crystal-in-string-word-or-inline-p sp-in-comment-p)
248                  :actions '(insert navigate)
249                  :pre-handlers '(sp-ruby-pre-handler)
250                  :post-handlers '(sp-ruby-def-post-handler)
251                  :skip-match 'sp-crystal-skip-inline-match-p
252                  :suffix "")
253
254   (sp-local-pair "|" "|"
255                  :when '(sp-ruby-should-insert-pipe-close)
256                  :pre-handlers '(sp-ruby-pre-pipe-handler)
257                  :suffix ""))
258
259 (provide 'smartparens-crystal)
260
261 ;;; smartparens-crystal.el ends here