Skip to content

Commit d4cf005

Browse files
committed
Avoid repeated thing-at-point call in indent function
clojure-indent-function called thing-at-point 'symbol twice: once indirectly via clojure--find-indent-spec and again in its own nil branch. Compute it once and pass it through.
1 parent 0d8e314 commit d4cf005

1 file changed

Lines changed: 47 additions & 45 deletions

File tree

clojure-mode.el

Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1360,29 +1360,6 @@ will align the values like this:
13601360
:safe #'booleanp
13611361
:type 'boolean)
13621362

1363-
(defvar clojure--align-search-regexp-cache nil
1364-
"Cached regexp for `clojure--find-sexp-to-align'.
1365-
A cons of (KEY . REGEXP) where KEY captures the inputs used to build it.")
1366-
1367-
(defun clojure--align-search-regexp ()
1368-
"Return the cached regexp for alignment search.
1369-
Rebuilds the regexp only when the inputs change."
1370-
(let ((key (list clojure-align-reader-conditionals
1371-
clojure-align-binding-forms
1372-
clojure-align-cond-forms)))
1373-
(unless (equal key (car clojure--align-search-regexp-cache))
1374-
(setq clojure--align-search-regexp-cache
1375-
(cons key
1376-
(concat (when clojure-align-reader-conditionals
1377-
(concat clojure--beginning-of-reader-conditional-regexp
1378-
"\\|"))
1379-
"{\\|("
1380-
(regexp-opt
1381-
(append clojure-align-binding-forms
1382-
clojure-align-cond-forms)
1383-
'symbols)))))
1384-
(cdr clojure--align-search-regexp-cache)))
1385-
13861363
(defcustom clojure-align-binding-forms
13871364
'("let" "when-let" "when-some" "if-let" "if-some" "binding" "loop"
13881365
"doseq" "for" "with-open" "with-local-vars" "with-redefs")
@@ -1456,6 +1433,29 @@ construct."
14561433
;; Return non-nil.
14571434
t)))))
14581435

1436+
(defvar clojure--align-search-regexp-cache nil
1437+
"Cached regexp for `clojure--find-sexp-to-align'.
1438+
A cons of (KEY . REGEXP) where KEY captures the inputs used to build it.")
1439+
1440+
(defun clojure--align-search-regexp ()
1441+
"Return the cached regexp for alignment search.
1442+
Rebuilds the regexp only when the inputs change."
1443+
(let ((key (list clojure-align-reader-conditionals
1444+
clojure-align-binding-forms
1445+
clojure-align-cond-forms)))
1446+
(unless (equal key (car clojure--align-search-regexp-cache))
1447+
(setq clojure--align-search-regexp-cache
1448+
(cons key
1449+
(concat (when clojure-align-reader-conditionals
1450+
(concat clojure--beginning-of-reader-conditional-regexp
1451+
"\\|"))
1452+
"{\\|("
1453+
(regexp-opt
1454+
(append clojure-align-binding-forms
1455+
clojure-align-cond-forms)
1456+
'symbols)))))
1457+
(cdr clojure--align-search-regexp-cache)))
1458+
14591459
(defun clojure--find-sexp-to-align (end)
14601460
"Non-nil if there's a sexp ahead to be aligned before END.
14611461
Place point as in `clojure--position-for-alignment'."
@@ -1641,16 +1641,18 @@ Implementation function for `clojure--find-indent-spec'."
16411641
(message "Invalid indent spec for `%s': %s" function method)
16421642
nil))))))
16431643

1644-
(defun clojure--find-indent-spec ()
1644+
(defun clojure--find-indent-spec (&optional function-name)
16451645
"Return the indent spec that applies to current sexp.
16461646
If `clojure-use-backtracking-indent' is non-nil, also do
16471647
backtracking up to a higher-level sexp in order to find the
1648-
spec."
1648+
spec. FUNCTION-NAME, if provided, is used instead of computing
1649+
the symbol at point."
16491650
(if clojure-use-backtracking-indent
16501651
(save-excursion
16511652
(clojure--find-indent-spec-backtracking))
1652-
(let ((function (thing-at-point 'symbol)))
1653-
(clojure--get-indent-method function))))
1653+
(let ((function (or function-name (thing-at-point 'symbol))))
1654+
(when function
1655+
(clojure--get-indent-method function)))))
16541656

16551657
(defun clojure--keyword-to-symbol (keyword)
16561658
"Convert KEYWORD to symbol."
@@ -1755,10 +1757,11 @@ This function also returns nil meaning don't specify the indentation."
17551757
(1+ (current-column))
17561758
;; Function or macro call.
17571759
(forward-char 1)
1758-
(let ((method (and clojure-enable-indent-specs
1759-
(clojure--find-indent-spec)))
1760-
(last-sexp calculate-lisp-indent-last-sexp)
1761-
(containing-form-column (1- (current-column))))
1760+
(let* ((function (thing-at-point 'symbol))
1761+
(method (and clojure-enable-indent-specs
1762+
(clojure--find-indent-spec function)))
1763+
(last-sexp calculate-lisp-indent-last-sexp)
1764+
(containing-form-column (1- (current-column))))
17621765
(pcase method
17631766
((or (and (pred integerp) method) `(,method))
17641767
(let ((pos -1))
@@ -1789,20 +1792,19 @@ This function also returns nil meaning don't specify the indentation."
17891792
(funcall method indent-point state))
17901793
;; No indent spec, do the default.
17911794
(`nil
1792-
(let ((function (thing-at-point 'symbol)))
1793-
(cond
1794-
;; Preserve useful alignment of :require (and friends) in `ns' forms.
1795-
((and function (string-match "^:" function))
1796-
(clojure--normal-indent last-sexp clojure-indent-keyword-style))
1797-
;; This should be identical to the :defn above.
1798-
((and function
1799-
(string-match "\\`\\(?:\\S +/\\)?\\(def[a-z]*\\|with-\\)"
1800-
function)
1801-
(not (string-match "\\`default" (match-string 1 function))))
1802-
(+ lisp-body-indent containing-form-column))
1803-
;; Finally, nothing special here, just respect the user's
1804-
;; preference.
1805-
(t (clojure--normal-indent last-sexp clojure-indent-style)))))))))
1795+
(cond
1796+
;; Preserve useful alignment of :require (and friends) in `ns' forms.
1797+
((and function (string-match "^:" function))
1798+
(clojure--normal-indent last-sexp clojure-indent-keyword-style))
1799+
;; This should be identical to the :defn above.
1800+
((and function
1801+
(string-match "\\`\\(?:\\S +/\\)?\\(def[a-z]*\\|with-\\)"
1802+
function)
1803+
(not (string-match "\\`default" (match-string 1 function))))
1804+
(+ lisp-body-indent containing-form-column))
1805+
;; Finally, nothing special here, just respect the user's
1806+
;; preference.
1807+
(t (clojure--normal-indent last-sexp clojure-indent-style))))))))
18061808

18071809
;;; Setting indentation
18081810
(defun put-clojure-indent (sym indent)

0 commit comments

Comments
 (0)