Skip to content
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

utils: Use editorconfig to set formatter indentation #188

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
44 changes: 23 additions & 21 deletions apheleia-utils.el
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,36 @@
(require 'cl-lib)
(require 'subr-x)

(defun apheleia-formatters-indent (tab-flag indent-flag indent-var)
(require 'editorconfig)

(defun apheleia-formatters-indent (tab-flag indent-flag &optional 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."
alternating flags based on the current buffers indent configuration.
If the buffer is indented with tabs then returns TAB-FLAG. Otherwise
look for an indentation variable associated with the current buffers
MAJOR-MODE and return it alongside INDENT-FLAG. If INDENT-VAR is set
then INDENT-VAR will be the only variable queried for INDENT-FLAG.

Use this helper to easily configure the indentation level of a formatter."
(cond
(indent-tabs-mode tab-flag)
(indent-var
(when-let ((indent (and (boundp indent-var)
(symbol-value indent-var))))
(list indent-flag (number-to-string indent))))))

(defun apheleia-formatters-js-indent (tab-flag indent-flag)
"Variant of `apheleia-formatters-indent' for JavaScript like modes.
See `apheleia-formatters-indent' for a description of TAB-FLAG and
INDENT-FLAG."
(apheleia-formatters-indent
tab-flag indent-flag
(cl-case major-mode
(json-mode 'js-indent-level)
(json-ts-mode 'json-ts-mode-indent-offset)
(js-mode 'js-indent-level)
(js-jsx-mode 'js-indent-level)
(js2-mode 'js2-basic-offset)
(js2-jsx-mode 'js2-basic-offset)
(js3-mode 'js3-indent-level))))
(list indent-flag (number-to-string indent))))
(t
(seq-find
(lambda (indent-var)
(and (boundp indent-var)
(symbol-value indent-var)))
(ensure-list
(alist-get major-mode editorconfig-indentation-alist))))))

(define-obsolete-function-alias
'apheleia-formatters-js-indent
'apheleia-formatters-indent
"3.3")

(defcustom apheleia-formatters-respect-fill-column nil
"Whether formatters should set `fill-column' related flags."
Expand Down
4 changes: 2 additions & 2 deletions apheleia.el
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
;; Created: 7 Jul 2019
;; Homepage: https://github.com/raxod502/apheleia
;; Keywords: tools
;; Package-Requires: ((emacs "26"))
;; Package-Requires: ((emacs "26") (editorconfig "0.9"))
;; SPDX-License-Identifier: MIT
;; Version: 3.2
;; Version: 3.3
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we only want to bump the version here when we actually release the new version.


;;; Commentary:

Expand Down