Skip to content

Backport ppss accessors, use setq-local & more #475

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Eldev
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,3 @@
(eldev-use-plugin 'undercover)

(eldev-add-loading-roots 'test "tests")

(eldev-add-extra-dependencies 'test 's)
2 changes: 1 addition & 1 deletion elixir-format.el
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
(require 'ansi-color)

(defcustom elixir-format-arguments nil
"Additional arguments to 'mix format'"
"Additional arguments to 'mix format'."
:type '(repeat string)
:group 'elixir
:group 'elixir-format)
Expand Down
64 changes: 33 additions & 31 deletions elixir-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@
(rx-to-string (car sexps) t))))))

(defsubst elixir-syntax-in-string-or-comment-p ()
(nth 8 (syntax-ppss)))
(elixir-ppss-comment-or-string-start (syntax-ppss)))

(defsubst elixir-syntax-count-quotes (quote-char &optional point limit)
"Count number of quotes around point (max is 3).
Expand All @@ -217,13 +217,12 @@ is used to limit the scan."
(defun elixir-syntax-stringify ()
"Put `syntax-table' property correctly on single/triple quotes."
(let* ((num-quotes (length (match-string-no-properties 1)))
(ppss (prog2
(backward-char num-quotes)
(syntax-ppss)
(forward-char num-quotes)))
(string-start (and (not (nth 4 ppss)) (nth 8 ppss)))
(quote-starting-pos (- (point) num-quotes))
(quote-ending-pos (point))
(ppss (save-excursion
(syntax-ppss quote-starting-pos)))
(string-start (and (not (elixir-ppss-comment-depth ppss))
(elixir-ppss-comment-or-string-start ppss)))
(num-closing-quotes
(and string-start
(elixir-syntax-count-quotes
Expand Down Expand Up @@ -253,7 +252,8 @@ is used to limit the scan."
(context (save-excursion (save-match-data (syntax-ppss beg)))))
(put-text-property beg (1+ beg) 'syntax-table (string-to-syntax "w"))
(put-text-property beg (1+ beg) 'elixir-interpolation
(cons (nth 3 context) (match-data)))))
(cons (elixir-ppss-string-terminator context)
(match-data)))))

(defconst elixir-sigil-delimiter-pair
'((?\( . ")")
Expand Down Expand Up @@ -294,7 +294,9 @@ is used to limit the scan."
(funcall
(syntax-propertize-rules
("\\(\\?\\)[\"']"
(1 (if (save-excursion (nth 3 (syntax-ppss (match-beginning 0))))
(1 (if (save-excursion
(elixir-ppss-string-terminator
(syntax-ppss (match-beginning 0))))
;; Within a string, skip.
(ignore
(goto-char (match-end 1)))
Expand Down Expand Up @@ -518,17 +520,19 @@ just return nil."
(forward-line 1)))))

(defun elixir--docstring-p (&optional pos)
"Check to see if there is a docstring at pos."
(let ((pos (or pos (nth 8 (parse-partial-sexp (point-min) (point))))))
"Check to see if there is a docstring at POS."
(let ((pos (or pos (elixir-ppss-comment-or-string-start
(parse-partial-sexp (point-min) (point))))))
(when pos
(save-excursion
(goto-char pos)
(and (looking-at "\"\"\"")(looking-back (rx "@" (or "moduledoc" "typedoc" "doc") (+ space))
(line-beginning-position)))))))
(and (looking-at "\"\"\"")
(looking-back (rx "@" (or "moduledoc" "typedoc" "doc") (+ space))
(line-beginning-position)))))))

(defun elixir-font-lock-syntactic-face-function (state)
(if (nth 3 state)
(if (elixir--docstring-p (nth 8 state))
(if (elixir-ppss-string-terminator state)
(if (elixir--docstring-p (elixir-ppss-comment-or-string-start state))
font-lock-doc-face
font-lock-string-face)
font-lock-comment-face))
Expand All @@ -547,29 +551,27 @@ just return nil."
"Major mode for editing Elixir code.

\\{elixir-mode-map}"
(set (make-local-variable 'font-lock-defaults)
'(elixir-font-lock-keywords
nil nil nil nil
(font-lock-syntactic-face-function
. elixir-font-lock-syntactic-face-function)))
(set (make-local-variable 'comment-start) "# ")
(set (make-local-variable 'comment-end) "")
(set (make-local-variable 'comment-start-skip) "#+ *")
(set (make-local-variable 'comment-use-syntax) t)
(set (make-local-variable 'syntax-propertize-function)
#'elixir-syntax-propertize-function)
(set (make-local-variable 'imenu-generic-expression)
elixir-imenu-generic-expression)

(set (make-local-variable 'beginning-of-defun-function) #'elixir-beginning-of-defun)
(set (make-local-variable 'end-of-defun-function) #'elixir-end-of-defun)
(setq-local font-lock-defaults
'(elixir-font-lock-keywords
nil nil nil nil
(font-lock-syntactic-face-function
. elixir-font-lock-syntactic-face-function)))
(setq-local comment-start "# ")
(setq-local comment-end "")
(setq-local comment-start-skip "#+ *")
(setq-local comment-use-syntax t)
(setq-local syntax-propertize-function #'elixir-syntax-propertize-function)
(setq-local imenu-generic-expression elixir-imenu-generic-expression)

(setq-local beginning-of-defun-function #'elixir-beginning-of-defun)
(setq-local end-of-defun-function #'elixir-end-of-defun)

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

;; Invoke elixir-mode when appropriate

Expand Down
65 changes: 47 additions & 18 deletions elixir-smie.el
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,38 @@
(rassoc next smie-closer-alist))))
(smie-indent-calculate))))))))

;; In Emacs 27, ppss became a structure and has proper accessors.

(defalias 'elixir-ppss-depth
(if (<= 27 emacs-major-version)
'ppss-depth
(lambda (parse-data) (nth 0 parse-data))))

(defalias 'elixir-ppss-innermost-start
(if (<= 27 emacs-major-version)
'ppss-innermost-start
(lambda (parse-data) (nth 1 parse-data))))

(defalias 'elixir-ppss-last-complete-sexp-start
(if (<= 27 emacs-major-version)
'ppss-last-complete-sexp-start
(lambda (parse-data) (nth 2 parse-data))))

(defalias 'elixir-ppss-string-terminator
(if (<= 27 emacs-major-version)
'ppss-string-terminator
(lambda (parse-data) (nth 3 parse-data))))

(defalias 'elixir-ppss-comment-depth
(if (<= 27 emacs-major-version)
'ppss-comment-depth
(lambda (parse-data) (nth 4 parse-data))))

(defalias 'elixir-ppss-comment-or-string-start
(if (<= 27 emacs-major-version)
'ppss-comment-or-string-start
(lambda (parse-data) (nth 8 parse-data))))

(defun elixir-smie-looking-around (back at)
"Check if looking backwards at BACK and forward at AT."
(and (looking-at-p at) (looking-back back)))
Expand Down Expand Up @@ -187,7 +219,7 @@
(looking-back elixir-smie--operator-regexp (- (point) 3) t))))

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

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

(defun elixir-smie-forward-token ()
Expand All @@ -279,7 +311,7 @@
(if (elixir-smie--semi-ends-match)
"MATCH-STATEMENT-DELIMITER"
(if (and (looking-at ".+,$")
(not (> (nth 0 (syntax-ppss)) 0)))
(not (> (elixir-ppss-depth (syntax-ppss)) 0)))
"COMMA"
";")))
((looking-at elixir-smie--block-operator-regexp)
Expand Down Expand Up @@ -308,7 +340,7 @@
(if (elixir-smie--semi-ends-match)
"MATCH-STATEMENT-DELIMITER"
(if (and (looking-back ",$" (- (point) 3) t)
(not (> (nth 0 (syntax-ppss)) 0)))
(not (> (elixir-ppss-depth (syntax-ppss)) 0)))
"COMMA"
";")))
((looking-back elixir-smie--block-operator-regexp (- (point) 3) t)
Expand Down Expand Up @@ -446,11 +478,11 @@
(not (smie-rule-hanging-p)))
0)
((and (not (smie-rule-sibling-p))
(nth 2 smie--parent)
(elixir-ppss-last-complete-sexp-start smie--parent)
(smie-rule-hanging-p))
(smie-rule-parent elixir-smie-indent-basic))
((and (not (smie-rule-sibling-p))
(not (nth 2 smie--parent))
(not (elixir-ppss-last-complete-sexp-start smie--parent))
(smie-rule-hanging-p))
(smie-rule-parent))))
(`(:after . "MATCH-STATEMENT-DELIMITER")
Expand Down Expand Up @@ -512,12 +544,12 @@
(save-excursion
(move-beginning-of-line 1)
(looking-at "^\s*do:.+$")))
(if (> (nth 0 (syntax-ppss)) 0)
(if (> (elixir-ppss-depth (syntax-ppss)) 0)
(smie-rule-parent (- 3))
(smie-rule-parent elixir-smie-indent-basic)))
((and (smie-rule-parent-p ";")
(not (smie-rule-hanging-p)))
(if (> (nth 0 (syntax-ppss)) 0)
(if (> (elixir-ppss-depth (syntax-ppss)) 0)
(smie-rule-parent (- elixir-smie-indent-basic))
(smie-rule-parent)))
((and (smie-rule-parent-p "OP")
Expand Down Expand Up @@ -567,7 +599,7 @@
(`(:before . "else:")
(cond
((smie-rule-parent-p ";")
(if (> (nth 0 (syntax-ppss)) 0)
(if (> (elixir-ppss-depth (syntax-ppss)) 0)
(smie-rule-parent elixir-smie-indent-basic)
(smie-rule-parent)))
((smie-rule-parent-p "if")
Expand Down Expand Up @@ -724,7 +756,7 @@
;; ... then indent the line after the `->' aligned with the
;; parent, offset by `elixir-smie-indent-basic'."
(if (and smie--parent (elixir-smie--same-line-as-parent
(nth 1 smie--parent)
(elixir-ppss-innermost-start smie--parent)
(point)))
(smie-rule-parent elixir-smie-indent-basic)
elixir-smie-indent-basic))
Expand Down Expand Up @@ -845,12 +877,10 @@
(defun elixir-smie--heredoc-at-current-point-p ()
"Return non-nil if cursor is at a string."
(save-excursion
(or (and (nth 3 (save-excursion
(let ((pos (point)))
(parse-partial-sexp 1 pos))))
(nth 8 (save-excursion
(let ((pos (point)))
(parse-partial-sexp 1 pos)))))
(or (save-excursion
(let ((parse-data (parse-partial-sexp 1 (point))))
(and (elixir-ppss-string-terminator parse-data)
(elixir-ppss-comment-or-string-start parse-data))))
(and (looking-at "\"\"\"")
(match-beginning 0)))))

Expand All @@ -876,8 +906,7 @@
Rules:
1. If the previous line is empty, indent as the basic indentation
at the beginning of the heredoc.
2. If the previous line is not empty, indent as the previous line.
"
2. If the previous line is not empty, indent as the previous line."
(if (eq major-mode 'elixir-mode)
(if (elixir-smie--heredoc-at-current-point-p)
(let ((indent
Expand Down
1 change: 0 additions & 1 deletion tests/test-helper.el
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

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

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

Expand Down