Skip to content

Commit 6bbc1e5

Browse files
authored
Backport ppss accessors, use setq-local & more (#475)
* Replace set+make-local-variable with setq-local * Backport ppss accessors * Fix formatting, use save-excursion instead of saving point manually * Fix docstring formatting * Remove s
1 parent 0212b06 commit 6bbc1e5

File tree

5 files changed

+81
-53
lines changed

5 files changed

+81
-53
lines changed

Eldev

-2
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,3 @@
77
(eldev-use-plugin 'undercover)
88

99
(eldev-add-loading-roots 'test "tests")
10-
11-
(eldev-add-extra-dependencies 'test 's)

elixir-format.el

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
(require 'ansi-color)
2727

2828
(defcustom elixir-format-arguments nil
29-
"Additional arguments to 'mix format'"
29+
"Additional arguments to 'mix format'."
3030
:type '(repeat string)
3131
:group 'elixir
3232
:group 'elixir-format)

elixir-mode.el

+33-31
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@
200200
(rx-to-string (car sexps) t))))))
201201

202202
(defsubst elixir-syntax-in-string-or-comment-p ()
203-
(nth 8 (syntax-ppss)))
203+
(elixir-ppss-comment-or-string-start (syntax-ppss)))
204204

205205
(defsubst elixir-syntax-count-quotes (quote-char &optional point limit)
206206
"Count number of quotes around point (max is 3).
@@ -217,13 +217,12 @@ is used to limit the scan."
217217
(defun elixir-syntax-stringify ()
218218
"Put `syntax-table' property correctly on single/triple quotes."
219219
(let* ((num-quotes (length (match-string-no-properties 1)))
220-
(ppss (prog2
221-
(backward-char num-quotes)
222-
(syntax-ppss)
223-
(forward-char num-quotes)))
224-
(string-start (and (not (nth 4 ppss)) (nth 8 ppss)))
225220
(quote-starting-pos (- (point) num-quotes))
226221
(quote-ending-pos (point))
222+
(ppss (save-excursion
223+
(syntax-ppss quote-starting-pos)))
224+
(string-start (and (not (elixir-ppss-comment-depth ppss))
225+
(elixir-ppss-comment-or-string-start ppss)))
227226
(num-closing-quotes
228227
(and string-start
229228
(elixir-syntax-count-quotes
@@ -253,7 +252,8 @@ is used to limit the scan."
253252
(context (save-excursion (save-match-data (syntax-ppss beg)))))
254253
(put-text-property beg (1+ beg) 'syntax-table (string-to-syntax "w"))
255254
(put-text-property beg (1+ beg) 'elixir-interpolation
256-
(cons (nth 3 context) (match-data)))))
255+
(cons (elixir-ppss-string-terminator context)
256+
(match-data)))))
257257

258258
(defconst elixir-sigil-delimiter-pair
259259
'((?\( . ")")
@@ -294,7 +294,9 @@ is used to limit the scan."
294294
(funcall
295295
(syntax-propertize-rules
296296
("\\(\\?\\)[\"']"
297-
(1 (if (save-excursion (nth 3 (syntax-ppss (match-beginning 0))))
297+
(1 (if (save-excursion
298+
(elixir-ppss-string-terminator
299+
(syntax-ppss (match-beginning 0))))
298300
;; Within a string, skip.
299301
(ignore
300302
(goto-char (match-end 1)))
@@ -518,17 +520,19 @@ just return nil."
518520
(forward-line 1)))))
519521

520522
(defun elixir--docstring-p (&optional pos)
521-
"Check to see if there is a docstring at pos."
522-
(let ((pos (or pos (nth 8 (parse-partial-sexp (point-min) (point))))))
523+
"Check to see if there is a docstring at POS."
524+
(let ((pos (or pos (elixir-ppss-comment-or-string-start
525+
(parse-partial-sexp (point-min) (point))))))
523526
(when pos
524527
(save-excursion
525528
(goto-char pos)
526-
(and (looking-at "\"\"\"")(looking-back (rx "@" (or "moduledoc" "typedoc" "doc") (+ space))
527-
(line-beginning-position)))))))
529+
(and (looking-at "\"\"\"")
530+
(looking-back (rx "@" (or "moduledoc" "typedoc" "doc") (+ space))
531+
(line-beginning-position)))))))
528532

529533
(defun elixir-font-lock-syntactic-face-function (state)
530-
(if (nth 3 state)
531-
(if (elixir--docstring-p (nth 8 state))
534+
(if (elixir-ppss-string-terminator state)
535+
(if (elixir--docstring-p (elixir-ppss-comment-or-string-start state))
532536
font-lock-doc-face
533537
font-lock-string-face)
534538
font-lock-comment-face))
@@ -547,29 +551,27 @@ just return nil."
547551
"Major mode for editing Elixir code.
548552
549553
\\{elixir-mode-map}"
550-
(set (make-local-variable 'font-lock-defaults)
551-
'(elixir-font-lock-keywords
552-
nil nil nil nil
553-
(font-lock-syntactic-face-function
554-
. elixir-font-lock-syntactic-face-function)))
555-
(set (make-local-variable 'comment-start) "# ")
556-
(set (make-local-variable 'comment-end) "")
557-
(set (make-local-variable 'comment-start-skip) "#+ *")
558-
(set (make-local-variable 'comment-use-syntax) t)
559-
(set (make-local-variable 'syntax-propertize-function)
560-
#'elixir-syntax-propertize-function)
561-
(set (make-local-variable 'imenu-generic-expression)
562-
elixir-imenu-generic-expression)
563-
564-
(set (make-local-variable 'beginning-of-defun-function) #'elixir-beginning-of-defun)
565-
(set (make-local-variable 'end-of-defun-function) #'elixir-end-of-defun)
554+
(setq-local font-lock-defaults
555+
'(elixir-font-lock-keywords
556+
nil nil nil nil
557+
(font-lock-syntactic-face-function
558+
. elixir-font-lock-syntactic-face-function)))
559+
(setq-local comment-start "# ")
560+
(setq-local comment-end "")
561+
(setq-local comment-start-skip "#+ *")
562+
(setq-local comment-use-syntax t)
563+
(setq-local syntax-propertize-function #'elixir-syntax-propertize-function)
564+
(setq-local imenu-generic-expression elixir-imenu-generic-expression)
565+
566+
(setq-local beginning-of-defun-function #'elixir-beginning-of-defun)
567+
(setq-local end-of-defun-function #'elixir-end-of-defun)
566568

567569
(smie-setup elixir-smie-grammar 'verbose-elixir-smie-rules
568570
:forward-token 'elixir-smie-forward-token
569571
:backward-token 'elixir-smie-backward-token)
570572
;; https://github.com/elixir-editors/emacs-elixir/issues/363
571573
;; http://debbugs.gnu.org/cgi/bugreport.cgi?bug=35496
572-
(set (make-local-variable 'smie-blink-matching-inners) nil))
574+
(setq-local smie-blink-matching-inners nil))
573575

574576
;; Invoke elixir-mode when appropriate
575577

elixir-smie.el

+47-18
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,38 @@
6767
(rassoc next smie-closer-alist))))
6868
(smie-indent-calculate))))))))
6969

70+
;; In Emacs 27, ppss became a structure and has proper accessors.
71+
72+
(defalias 'elixir-ppss-depth
73+
(if (<= 27 emacs-major-version)
74+
'ppss-depth
75+
(lambda (parse-data) (nth 0 parse-data))))
76+
77+
(defalias 'elixir-ppss-innermost-start
78+
(if (<= 27 emacs-major-version)
79+
'ppss-innermost-start
80+
(lambda (parse-data) (nth 1 parse-data))))
81+
82+
(defalias 'elixir-ppss-last-complete-sexp-start
83+
(if (<= 27 emacs-major-version)
84+
'ppss-last-complete-sexp-start
85+
(lambda (parse-data) (nth 2 parse-data))))
86+
87+
(defalias 'elixir-ppss-string-terminator
88+
(if (<= 27 emacs-major-version)
89+
'ppss-string-terminator
90+
(lambda (parse-data) (nth 3 parse-data))))
91+
92+
(defalias 'elixir-ppss-comment-depth
93+
(if (<= 27 emacs-major-version)
94+
'ppss-comment-depth
95+
(lambda (parse-data) (nth 4 parse-data))))
96+
97+
(defalias 'elixir-ppss-comment-or-string-start
98+
(if (<= 27 emacs-major-version)
99+
'ppss-comment-or-string-start
100+
(lambda (parse-data) (nth 8 parse-data))))
101+
70102
(defun elixir-smie-looking-around (back at)
71103
"Check if looking backwards at BACK and forward at AT."
72104
(and (looking-at-p at) (looking-back back)))
@@ -187,7 +219,7 @@
187219
(looking-back elixir-smie--operator-regexp (- (point) 3) t))))
188220

189221
(defun elixir-smie-current-line-contains-built-in-keyword-p ()
190-
"Return non-nil if the current line contains built in keywords with a `.'"
222+
"Return non-nil if the current line contains built in keywords with a \".\"."
191223
(save-excursion
192224
(beginning-of-line)
193225
(looking-at ".+\\.\\(case\\|try\\|if\\|rescue\\)")))
@@ -260,7 +292,7 @@
260292
(not (looking-back ".+fn.+")))))))))
261293

262294
(defun elixir-smie--same-line-as-parent (parent-pos child-pos)
263-
"Return non-nil if `child-pos' is on same line as `parent-pos'."
295+
"Return non-nil if CHILD-POS is on same line as PARENT-POS."
264296
(= (line-number-at-pos parent-pos) (line-number-at-pos child-pos)))
265297

266298
(defun elixir-smie-forward-token ()
@@ -279,7 +311,7 @@
279311
(if (elixir-smie--semi-ends-match)
280312
"MATCH-STATEMENT-DELIMITER"
281313
(if (and (looking-at ".+,$")
282-
(not (> (nth 0 (syntax-ppss)) 0)))
314+
(not (> (elixir-ppss-depth (syntax-ppss)) 0)))
283315
"COMMA"
284316
";")))
285317
((looking-at elixir-smie--block-operator-regexp)
@@ -308,7 +340,7 @@
308340
(if (elixir-smie--semi-ends-match)
309341
"MATCH-STATEMENT-DELIMITER"
310342
(if (and (looking-back ",$" (- (point) 3) t)
311-
(not (> (nth 0 (syntax-ppss)) 0)))
343+
(not (> (elixir-ppss-depth (syntax-ppss)) 0)))
312344
"COMMA"
313345
";")))
314346
((looking-back elixir-smie--block-operator-regexp (- (point) 3) t)
@@ -446,11 +478,11 @@
446478
(not (smie-rule-hanging-p)))
447479
0)
448480
((and (not (smie-rule-sibling-p))
449-
(nth 2 smie--parent)
481+
(elixir-ppss-last-complete-sexp-start smie--parent)
450482
(smie-rule-hanging-p))
451483
(smie-rule-parent elixir-smie-indent-basic))
452484
((and (not (smie-rule-sibling-p))
453-
(not (nth 2 smie--parent))
485+
(not (elixir-ppss-last-complete-sexp-start smie--parent))
454486
(smie-rule-hanging-p))
455487
(smie-rule-parent))))
456488
(`(:after . "MATCH-STATEMENT-DELIMITER")
@@ -512,12 +544,12 @@
512544
(save-excursion
513545
(move-beginning-of-line 1)
514546
(looking-at "^\s*do:.+$")))
515-
(if (> (nth 0 (syntax-ppss)) 0)
547+
(if (> (elixir-ppss-depth (syntax-ppss)) 0)
516548
(smie-rule-parent (- 3))
517549
(smie-rule-parent elixir-smie-indent-basic)))
518550
((and (smie-rule-parent-p ";")
519551
(not (smie-rule-hanging-p)))
520-
(if (> (nth 0 (syntax-ppss)) 0)
552+
(if (> (elixir-ppss-depth (syntax-ppss)) 0)
521553
(smie-rule-parent (- elixir-smie-indent-basic))
522554
(smie-rule-parent)))
523555
((and (smie-rule-parent-p "OP")
@@ -567,7 +599,7 @@
567599
(`(:before . "else:")
568600
(cond
569601
((smie-rule-parent-p ";")
570-
(if (> (nth 0 (syntax-ppss)) 0)
602+
(if (> (elixir-ppss-depth (syntax-ppss)) 0)
571603
(smie-rule-parent elixir-smie-indent-basic)
572604
(smie-rule-parent)))
573605
((smie-rule-parent-p "if")
@@ -724,7 +756,7 @@
724756
;; ... then indent the line after the `->' aligned with the
725757
;; parent, offset by `elixir-smie-indent-basic'."
726758
(if (and smie--parent (elixir-smie--same-line-as-parent
727-
(nth 1 smie--parent)
759+
(elixir-ppss-innermost-start smie--parent)
728760
(point)))
729761
(smie-rule-parent elixir-smie-indent-basic)
730762
elixir-smie-indent-basic))
@@ -845,12 +877,10 @@
845877
(defun elixir-smie--heredoc-at-current-point-p ()
846878
"Return non-nil if cursor is at a string."
847879
(save-excursion
848-
(or (and (nth 3 (save-excursion
849-
(let ((pos (point)))
850-
(parse-partial-sexp 1 pos))))
851-
(nth 8 (save-excursion
852-
(let ((pos (point)))
853-
(parse-partial-sexp 1 pos)))))
880+
(or (save-excursion
881+
(let ((parse-data (parse-partial-sexp 1 (point))))
882+
(and (elixir-ppss-string-terminator parse-data)
883+
(elixir-ppss-comment-or-string-start parse-data))))
854884
(and (looking-at "\"\"\"")
855885
(match-beginning 0)))))
856886

@@ -876,8 +906,7 @@
876906
Rules:
877907
1. If the previous line is empty, indent as the basic indentation
878908
at the beginning of the heredoc.
879-
2. If the previous line is not empty, indent as the previous line.
880-
"
909+
2. If the previous line is not empty, indent as the previous line."
881910
(if (eq major-mode 'elixir-mode)
882911
(if (elixir-smie--heredoc-at-current-point-p)
883912
(let ((indent

tests/test-helper.el

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
(require 'ert-x) ; `ert-with-test-buffer'
99
(require 'cl-lib) ; `cl-defmacro'
10-
(require 's)
1110

1211
(message "Running tests on Emacs %s" emacs-version)
1312

0 commit comments

Comments
 (0)