From 7bdbc39721259a576c3debce3450c91036d8e955 Mon Sep 17 00:00:00 2001 From: Markku Rontu Date: Sat, 17 Nov 2018 14:07:31 +0200 Subject: [PATCH 1/5] Indent "let", "when" and "while" as function form if not at start "let", "when" and "while" are considered to be a macro form normally: (when foo bar) Also you can introduce macros that start with "let", "when" or "while" that will be indented like macro forms: (when-foo-bar 1 2 3) But when "let", "when" and "while" are not in the beginning of a symbol they are now threated as function forms: (foo-when-bar 1 2 3) --- CHANGELOG.md | 1 + clojure-mode.el | 2 +- test/clojure-mode-indentation-test.el | 3 ++- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 05f489a3..e3731ae8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ * [#481](https://github.com/clojure-emacs/clojure-mode/issues/481): Support vertical alignment even in the presence of blank lines, with the new `clojure-align-separator` user option. * [#483](https://github.com/clojure-emacs/clojure-mode/issues/483): Support alignment for reader conditionals, with the new `clojure-align-reader-conditionals` user option. +- Indent "let", "when" and "while" as function form if not at start of a symbol ### Bugs fixed diff --git a/clojure-mode.el b/clojure-mode.el index 603b5ed4..21019e09 100644 --- a/clojure-mode.el +++ b/clojure-mode.el @@ -1304,7 +1304,7 @@ symbol properties." 'clojure-indent-function) (get (intern-soft (match-string 1 function-name)) 'clojure-backtracking-indent))) - (when (string-match (rx (or "let" "when" "while") (syntax symbol)) + (when (string-match (rx string-start (or "let" "when" "while") (syntax symbol)) function-name) (clojure--get-indent-method (substring (match-string 0 function-name) 0 -1))))) diff --git a/test/clojure-mode-indentation-test.el b/test/clojure-mode-indentation-test.el index 6f22503f..bb0d1374 100644 --- a/test/clojure-mode-indentation-test.el +++ b/test/clojure-mode-indentation-test.el @@ -421,7 +421,8 @@ values of customisable variables." (def-full-indent-test let-when-while-forms "(let-alist [x 1]\n ())" "(while-alist [x 1]\n ())" - "(when-alist [x 1]\n ())") + "(when-alist [x 1]\n ())" + "(indents-like-fn-when-let-while-are-not-the-start [x 1]\n ())") (defun indent-cond (indent-point state) (goto-char (elt state 1)) From 73c47223984f25b98381fd9538637c67dfb6e065 Mon Sep 17 00:00:00 2001 From: Markku Rontu Date: Sat, 17 Nov 2018 16:53:34 +0200 Subject: [PATCH 2/5] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e3731ae8..df8180b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ * [#481](https://github.com/clojure-emacs/clojure-mode/issues/481): Support vertical alignment even in the presence of blank lines, with the new `clojure-align-separator` user option. * [#483](https://github.com/clojure-emacs/clojure-mode/issues/483): Support alignment for reader conditionals, with the new `clojure-align-reader-conditionals` user option. -- Indent "let", "when" and "while" as function form if not at start of a symbol +* [#497](https://github.com/clojure-emacs/clojure-mode/pull/497) Indent "let", "when" and "while" as function form if not at start of a symbol ### Bugs fixed From 7e3fb2b99c0a3921a70d72e0cdb447a227941190 Mon Sep 17 00:00:00 2001 From: Markku Rontu Date: Sat, 17 Nov 2018 17:10:05 +0200 Subject: [PATCH 3/5] Handle if-let --- clojure-mode.el | 2 +- test/clojure-mode-indentation-test.el | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/clojure-mode.el b/clojure-mode.el index 21019e09..694f2026 100644 --- a/clojure-mode.el +++ b/clojure-mode.el @@ -1304,7 +1304,7 @@ symbol properties." 'clojure-indent-function) (get (intern-soft (match-string 1 function-name)) 'clojure-backtracking-indent))) - (when (string-match (rx string-start (or "let" "when" "while") (syntax symbol)) + (when (string-match (rx string-start (or "let" "when" "while" "if") (syntax symbol)) function-name) (clojure--get-indent-method (substring (match-string 0 function-name) 0 -1))))) diff --git a/test/clojure-mode-indentation-test.el b/test/clojure-mode-indentation-test.el index bb0d1374..62bc4882 100644 --- a/test/clojure-mode-indentation-test.el +++ b/test/clojure-mode-indentation-test.el @@ -422,6 +422,7 @@ values of customisable variables." "(let-alist [x 1]\n ())" "(while-alist [x 1]\n ())" "(when-alist [x 1]\n ())" + "(if-alist [x 1]\n ())" "(indents-like-fn-when-let-while-are-not-the-start [x 1]\n ())") (defun indent-cond (indent-point state) From a7cebff4e23bad530f9342106772eed8925b7dc1 Mon Sep 17 00:00:00 2001 From: Markku Rontu Date: Sat, 17 Nov 2018 17:12:45 +0200 Subject: [PATCH 4/5] Test also for if handling in the middle of symbol --- test/clojure-mode-indentation-test.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/clojure-mode-indentation-test.el b/test/clojure-mode-indentation-test.el index 62bc4882..51af55c0 100644 --- a/test/clojure-mode-indentation-test.el +++ b/test/clojure-mode-indentation-test.el @@ -423,7 +423,7 @@ values of customisable variables." "(while-alist [x 1]\n ())" "(when-alist [x 1]\n ())" "(if-alist [x 1]\n ())" - "(indents-like-fn-when-let-while-are-not-the-start [x 1]\n ())") + "(indents-like-fn-when-let-while-if-are-not-the-start [x 1]\n ())") (defun indent-cond (indent-point state) (goto-char (elt state 1)) From 646cd42ef4fc80d2246b562924121a917610bb05 Mon Sep 17 00:00:00 2001 From: Markku Rontu Date: Sat, 17 Nov 2018 17:43:33 +0200 Subject: [PATCH 5/5] Document indentation behavior --- clojure-mode.el | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/clojure-mode.el b/clojure-mode.el index 694f2026..9f758436 100644 --- a/clojure-mode.el +++ b/clojure-mode.el @@ -1304,7 +1304,10 @@ symbol properties." 'clojure-indent-function) (get (intern-soft (match-string 1 function-name)) 'clojure-backtracking-indent))) - (when (string-match (rx string-start (or "let" "when" "while" "if") (syntax symbol)) + ;; indent symbols starting if, when, ... + ;; such as if-let, when-let, ... + ;; like if, when, ... + (when (string-match (rx string-start (or "if" "when" "let" "while") (syntax symbol)) function-name) (clojure--get-indent-method (substring (match-string 0 function-name) 0 -1)))))