diff options
| author | Mohsin Kaleem <mohkale@kisara.moe> | 2023-04-20 10:55:13 +0100 |
|---|---|---|
| committer | Mohsin Kaleem <mohkalsin@gmail.com> | 2023-04-20 11:24:43 +0100 |
| commit | 2d797a52e6e4b778f03324e288be74a425b533a5 (patch) | |
| tree | 073f7df5cacff57ff271cbc5326ada5b4fee5ed0 /apheleia-utils.el | |
| parent | 04742734965aa39d5939bbc69c333e400ac489fb (diff) | |
Move apheleia-formatters to apheleia-utils
Diffstat (limited to 'apheleia-utils.el')
| -rw-r--r-- | apheleia-utils.el | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/apheleia-utils.el b/apheleia-utils.el new file mode 100644 index 0000000..e903185 --- /dev/null +++ b/apheleia-utils.el @@ -0,0 +1,102 @@ +;;; apheleia-utils.el --- Formatter helpers. -*- lexical-binding: t -*- + +;;; Commentary: + +;; Helper functions for defining apheleia formatters. + +;;; Code: + +(require 'cl-lib) +(require 'subr-x) + +(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." + (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)))) + +(defcustom apheleia-formatters-respect-fill-column nil + "Whether formatters should set `fill-column' related flags." + :type 'boolean + :group 'apheleia) + +(defun apheleia-formatters-fill-column (fill-flag) + "Set flag for wrap column. +Helper function to set a flag based on `fill-column'. When `fill-column' is set +and `apheleia-formatters-respect-fill-column' return a list of FILL-FLAG and +`fill-column'." + (when (and apheleia-formatters-respect-fill-column + (bound-and-true-p fill-column)) + (list fill-flag (number-to-string fill-column)))) + +(defun apheleia-formatters-locate-file (file-flag file-name) + "Set a flag based on a dominating-file. +Look for a file up recursively from the current directory until FILE-NAME is +found. If found return a list of FILE-FLAG and the absolute path to the located +FILE-NAME." + (when-let ((file (locate-dominating-file default-directory file-name))) + (list file-flag (concat (expand-file-name file) file-name)))) + +(defun apheleia-formatters-extension-p (&rest exts) + "Assert whether current buffer has an extension in EXTS." + (when-let ((name buffer-file-name) + (ext (file-name-extension name))) + (cl-find-if (apply-partially #'string-equal ext) + exts))) + +(defcustom apheleia-formatters-mode-extension-assoc + '((c-mode . ".c") + (c-ts-mode . ".c") + (c++-mode . ".cpp") + (c++-ts-mode . ".cpp") + (glsl-mode . ".glsl") + (java-mode . ".java") + (java-ts-mode . ".java")) + "Association list between major-modes and common file extensions for them." + :type 'alist + :group 'apheleia) + +(defun apheleia-formatters-mode-extension (&optional flag) + "Get a file-extension based on the current `major-mode'. +If FLAG is set this function returns a list of FLAG and then the extension. +Otherwise return the extension only." + (when-let ((ext + (alist-get major-mode apheleia-formatters-mode-extension-assoc))) + (if flag + (list flag ext) + ext))) + +(defun apheleia-formatters-local-buffer-file-name () + "Get variable `buffer-file-name' without any remote components." + (when-let ((name buffer-file-name)) + (let ((remote (file-remote-p name))) + (if remote + (substring name (length remote)) + name)))) + +(provide 'apheleia-utils) + +;;; apheleia-utils.el ends here |
