From f57d21ef1f36f00930001fc3a22bd7e4a9fa16c9 Mon Sep 17 00:00:00 2001 From: Radon Rosborough Date: Thu, 12 Oct 2023 20:19:14 -0700 Subject: [PATCH] [#208] Allow ignoring Emacs indentation settings (#211) For https://github.com/radian-software/apheleia/discussions/208#discussioncomment-7086819 --- CHANGELOG.md | 7 +++++++ apheleia-utils.el | 11 ++++++++++- apheleia.el | 14 ++++++++------ 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f089a9..bcc032e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,12 @@ The format is based on [Keep a Changelog]. * Disable formatting of go module files with gofmt. This was never supported ([#214]). +### Features +* New user option `apheleia-formatters-respect-indent-level`, + defaulting to `t`. You can set this to `nil` to disable Apheleia + configuring formatters to use the same indent settings as the Emacs + major mode is using ([#208]). + ### Enhancements * Use the `prettier-json` formatter for `js-json-mode` ([#209]). * Prettier is now enabled in `svelte-mode`. @@ -68,6 +74,7 @@ The format is based on [Keep a Changelog]. [#182]: https://github.com/radian-software/apheleia/pull/182 [#187]: https://github.com/radian-software/apheleia/pull/187 [#196]: https://github.com/radian-software/apheleia/pull/196 +[#208]: https://github.com/radian-software/apheleia/discussions/208 [#209]: https://github.com/radian-software/apheleia/pull/209 [#213]: https://github.com/radian-software/apheleia/pull/213 [#214]: https://github.com/radian-software/apheleia/pull/214 diff --git a/apheleia-utils.el b/apheleia-utils.el index e903185..391d6a5 100644 --- a/apheleia-utils.el +++ b/apheleia-utils.el @@ -9,14 +9,23 @@ (require 'cl-lib) (require 'subr-x) +(defcustom apheleia-formatters-respect-indent-level t + "Whether formatters should respect Emacs' indent configuration." + :type 'boolean + :group 'apheleia) + (defun apheleia-formatters-indent (tab-flag indent-flag indent-var) "Set flag for indentation. Helper function for `apheleia-formatters' which allows you to supply alternating flags based on the current buffers indent configuration. If the buffer is indented with tabs then returns TAB-FLAG. Otherwise if INDENT-VAR is set in the buffer return INDENT-FLAG and the value of INDENT-VAR. Use this -to easily configure the indentation level of a formatter." +to easily configure the indentation level of a formatter. + +If `apheleia-formatters-respect-indent-level' is nil then this +always returns nil to defer to the formatter." (cond + ((not apheleia-formatters-respect-indent-level) nil) (indent-tabs-mode tab-flag) (indent-var (when-let ((indent (and (boundp indent-var) diff --git a/apheleia.el b/apheleia.el index 1453e05..7c23125 100644 --- a/apheleia.el +++ b/apheleia.el @@ -138,12 +138,14 @@ "-ln" (cl-case (bound-and-true-p sh-shell) (sh "posix") (t "bash")) - "-i" (number-to-string - (cond - (indent-tabs-mode 0) - ((boundp 'sh-basic-offset) - sh-basic-offset) - (t 4))) + (when apheleia-formatters-respect-indent-level + (list + "-i" (number-to-string + (cond + (indent-tabs-mode 0) + ((boundp 'sh-basic-offset) + sh-basic-offset) + (t 4))))) "-")) (rufo . ("rufo" "--filename" filepath "--simple-exit")) (stylua . ("stylua" "-"))