refactor(lib): don't use smartparens' API

Toward our eventual goal of moving smartparens out of core, I've adapted
this from code provided by hpfr on Discord, which was adapted from
smartparen's syntax-ppss caching logic. `:config default` will need need
some attention before we can fully move smartparens to its own `:editor
smartparens` module.

Co-authored-by: hpfr <hpfr@users.noreply.github.com>
This commit is contained in:
Henrik Lissner 2024-07-09 19:27:36 -04:00
parent 1131d5b36d
commit e43d575caf
No known key found for this signature in database
GPG key ID: B60957CA074D39A3
6 changed files with 70 additions and 33 deletions

View file

@ -604,8 +604,6 @@ current buffer."
:hook (doom-first-buffer . smartparens-global-mode) :hook (doom-first-buffer . smartparens-global-mode)
:commands sp-pair sp-local-pair sp-with-modes sp-point-in-comment sp-point-in-string :commands sp-pair sp-local-pair sp-with-modes sp-point-in-comment sp-point-in-string
:config :config
(add-to-list 'doom-point-in-string-functions 'sp-point-in-string)
(add-to-list 'doom-point-in-comment-functions 'sp-point-in-comment)
;; smartparens recognizes `slime-mrepl-mode', but not `sly-mrepl-mode', so... ;; smartparens recognizes `slime-mrepl-mode', but not `sly-mrepl-mode', so...
(add-to-list 'sp-lisp-modes 'sly-mrepl-mode) (add-to-list 'sp-lisp-modes 'sly-mrepl-mode)
;; Load default smartparens rules for various languages ;; Load default smartparens rules for various languages

View file

@ -355,9 +355,8 @@ without needing to check if they are available."
(defun doom--help-current-module-str () (defun doom--help-current-module-str ()
(cond ((save-excursion (cond ((save-excursion
(require 'smartparens)
(ignore-errors (ignore-errors
(sp-beginning-of-sexp) (thing-at-point--beginning-of-sexp)
(unless (eq (char-after) ?\() (unless (eq (char-after) ?\()
(backward-char)) (backward-char))
(let ((sexp (sexp-at-point))) (let ((sexp (sexp-at-point)))

View file

@ -1,18 +1,32 @@
;;; lisp/lib/text.el -*- lexical-binding: t; -*- ;;; lisp/lib/text.el -*- lexical-binding: t; -*-
;;;###autoload (defvar-local doom--sppss-memo-last-point nil)
(defvar doom-point-in-comment-functions () (defvar-local doom--sppss-memo-last-result nil)
"List of functions to run to determine if point is in a comment.
Each function takes one argument: the position of the point. Stops on the first (defun doom--sppss-memo-reset-h (&rest _ignored)
function to return non-nil. Used by `doom-point-in-comment-p'.") "Reset memoization as a safety precaution.
IGNORED is a dummy argument used to eat up arguments passed from
the hook where this is executed."
(setq doom--sppss-memo-last-point nil
doom--sppss-memo-last-result nil))
;;;###autoload ;;;###autoload
(defvar doom-point-in-string-functions () (defun doom-syntax-ppss (&optional p)
"List of functions to run to determine if point is in a string. "Memoize the last result of `syntax-ppss'.
Each function takes one argument: the position of the point. Stops on the first P is the point at which we run `syntax-ppss'"
function to return non-nil. Used by `doom-point-in-string-p'.") (let ((p (or p (point)))
(mem-p doom--sppss-memo-last-point))
(if (and (eq p (nth 0 mem-p))
(eq (point-min) (nth 1 mem-p))
(eq (point-max) (nth 2 mem-p)))
doom--sppss-memo-last-result
;; Add hook to reset memoization if necessary
(unless doom--sppss-memo-last-point
(add-hook 'before-change-functions #'doom--sppss-memo-reset-h t t))
(setq doom--sppss-memo-last-point (list p (point-min) (point-max))
doom--sppss-memo-last-result (syntax-ppss p)))))
;;;###autoload ;;;###autoload
(defun doom-surrounded-p (pair &optional inline balanced) (defun doom-surrounded-p (pair &optional inline balanced)
@ -40,22 +54,49 @@ lines, above and below, with only whitespace in between."
(= (- pt nbeg) (- nend pt)))))))))))) (= (- pt nbeg) (- nend pt))))))))))))
;;;###autoload ;;;###autoload
(defun doom-point-in-comment-p (&optional pos) (defun doom-point-in-comment-p (&optional pt)
"Return non-nil if POS is in a comment. "Return non-nil if point is in a comment.
POS defaults to the current position." PT defaults to the current position."
(let ((pos (or pos (point)))) (let ((pt (or pt (point))))
(if doom-point-in-comment-functions (ignore-errors
(run-hook-with-args-until-success 'doom-point-in-comment-functions pos) (save-excursion
(nth 4 (syntax-ppss pos))))) ;; We cannot be in a comment if we are inside a string
(unless (nth 3 (doom-syntax-ppss pt))
(or (nth 4 (doom-syntax-ppss pt))
;; this also test opening and closing comment delimiters... we
;; need to chack that it is not newline, which is in "comment
;; ender" class in elisp-mode, but we just want it to be treated
;; as whitespace
(and (< pt (point-max))
(memq (char-syntax (char-after pt)) '(?< ?>))
(not (eq (char-after pt) ?\n)))
;; we also need to test the special syntax flag for comment
;; starters and enders, because `syntax-ppss' does not yet know if
;; we are inside a comment or not (e.g. / can be a division or
;; comment starter...).
(when-let ((s (car (syntax-after pt))))
(or (and (/= 0 (logand (ash 1 16) s))
(nth 4 (syntax-ppss (+ pt 2))))
(and (/= 0 (logand (ash 1 17) s))
(nth 4 (syntax-ppss (+ pt 1))))
(and (/= 0 (logand (ash 1 18) s))
(nth 4 (syntax-ppss (- pt 1))))
(and (/= 0 (logand (ash 1 19) s))
(nth 4 (syntax-ppss (- pt 2))))))))))))
;;;###autoload ;;;###autoload
(defun doom-point-in-string-p (&optional pos) (defun doom-point-in-string-p (&optional pt)
"Return non-nil if POS is in a string." "Return non-nil if point is inside string.
;; REVIEW Should we cache `syntax-ppss'?
(let ((pos (or pos (point)))) This function actually returns the 3rd element of `syntax-ppss'
(if doom-point-in-string-functions which can be a number if the string is delimited by that
(run-hook-with-args-until-success 'doom-point-in-string-functions pos) character or t if the string is delimited by general string
(nth 3 (syntax-ppss pos))))) fences.
If optional argument PT is present test this instead of point."
(ignore-errors
(save-excursion
(nth 3 (doom-syntax-ppss pt)))))
;;;###autoload ;;;###autoload
(defun doom-point-in-string-or-comment-p (&optional pos) (defun doom-point-in-string-or-comment-p (&optional pos)

View file

@ -78,10 +78,9 @@ more information on modifiers."
(defun +evil--insert-newline (&optional above _noextranewline) (defun +evil--insert-newline (&optional above _noextranewline)
(let ((pos (save-excursion (beginning-of-line-text) (point))) (let ((pos (save-excursion (beginning-of-line-text) (point)))
comment-auto-fill-only-comments) comment-auto-fill-only-comments)
(require 'smartparens)
(evil-narrow-to-field (evil-narrow-to-field
(if above (if above
(if (save-excursion (nth 4 (sp--syntax-ppss pos))) (if (save-excursion (nth 4 (doom-syntax-ppss pos)))
(evil-save-goal-column (evil-save-goal-column
(setq evil-auto-indent nil) (setq evil-auto-indent nil)
(goto-char pos) (goto-char pos)
@ -103,7 +102,7 @@ more information on modifiers."
(forward-line -1) (forward-line -1)
(back-to-indentation)) (back-to-indentation))
(evil-move-end-of-line) (evil-move-end-of-line)
(cond ((sp-point-in-comment pos) (cond ((doom-point-in-comment-p pos)
(setq evil-auto-indent nil) (setq evil-auto-indent nil)
(if comment-line-break-function (if comment-line-break-function
(funcall comment-line-break-function nil) (funcall comment-line-break-function nil)

View file

@ -18,7 +18,7 @@
(if (eq char 27) (if (eq char 27)
(cons "" "") (cons "" "")
(let* ((pair (+evil--embrace-get-pair (string char))) (let* ((pair (+evil--embrace-get-pair (string char)))
(escape (if (sp-point-in-string) "\\\\" "\\")) (escape (if (doom-point-in-string-p) "\\\\" "\\"))
(escape (format "\\1%s" (regexp-quote escape)))) (escape (format "\\1%s" (regexp-quote escape))))
(cons (replace-regexp-in-string "^\\( *\\)" escape (car pair)) (cons (replace-regexp-in-string "^\\( *\\)" escape (car pair))
(replace-regexp-in-string "^\\( *\\)" escape (cdr pair))))))) (replace-regexp-in-string "^\\( *\\)" escape (cdr pair)))))))

View file

@ -57,13 +57,13 @@ See `+evil/next-preproc-directive' for details."
(require 'newcomment) (require 'newcomment)
(dotimes (_ (abs count)) (dotimes (_ (abs count))
(cond ((> count 0) (cond ((> count 0)
(while (and (not (eobp)) (sp-point-in-comment)) (while (and (not (eobp)) (doom-point-in-comment-p))
(forward-line 1)) (forward-line 1))
(unless (comment-search-forward (point-max) 'noerror) (unless (comment-search-forward (point-max) 'noerror)
(goto-char orig-pt) (goto-char orig-pt)
(user-error "No comment after point"))) (user-error "No comment after point")))
(t (t
(while (and (not (bobp)) (sp-point-in-comment)) (while (and (not (bobp)) (doom-point-in-comment-p))
(forward-line -1)) (forward-line -1))
(unless (comment-search-backward nil 'noerror) (unless (comment-search-backward nil 'noerror)
(goto-char orig-pt) (goto-char orig-pt)