aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.md4
-rw-r--r--apheleia.el34
2 files changed, 29 insertions, 9 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index ee2768f..1766698 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,9 @@ The format is based on [Keep a Changelog].
## Unreleased
### Enhancements
* Prettier is now enabled in `json-mode`.
+* Formatters are now run by default in the root directory of your
+ project (as defined by `project.el` or Projectile, defaulting to the
+ working directory). See [#30].
### Bugs fixed
* Prettier now respects `.prettierignore` ([#21]).
@@ -14,6 +17,7 @@ The format is based on [Keep a Changelog].
[#21]: https://github.com/raxod502/apheleia/issues/21
[#27]: https://github.com/raxod502/apheleia/issues/27
+[#30]: https://github.com/raxod502/apheleia/issues/30
## 1.1.1 (released 2020-07-16)
### Formatters
diff --git a/apheleia.el b/apheleia.el
index 8f1e195..20d0b96 100644
--- a/apheleia.el
+++ b/apheleia.el
@@ -25,14 +25,29 @@
(require 'cl-lib)
(require 'map)
+(require 'project)
(require 'subr-x)
+(declare-function projectile-project-root "ext:projectile")
+
(defgroup apheleia nil
"Reformat buffer without moving point."
:group 'external
:link '(url-link :tag "GitHub" "https://github.com/raxod502/apheleia")
:link '(emacs-commentary-link :tag "Commentary" "apheleia"))
+(defun apheleia--project-root ()
+ "Return the directory containing the current project.
+This is an absolute path ending in a slash. It uses `project' and
+then `projectile' (if the latter is installed), before falling
+back to `default-directory'."
+ (expand-file-name
+ (if-let ((project (project-current)))
+ (car (project-roots project))
+ (or (and (require 'projectile nil 'noerror)
+ (projectile-project-root))
+ default-directory))))
+
(cl-defun apheleia--edit-distance-table (s1 s2)
"Align strings S1 and S2 for minimum edit distance.
Return the dynamic programming table as has table which maps cons
@@ -437,15 +452,16 @@ modified from what is written to disk, then don't do anything."
output-fname
arg))
command)))
- (apheleia--make-process
- :command command
- :stdin (unless input-fname
- (current-buffer))
- :callback (lambda (stdout)
- (when output-fname
- (erase-buffer)
- (insert-file-contents-literally output-fname))
- (funcall callback stdout))))))
+ (let ((default-directory (apheleia--project-root)))
+ (apheleia--make-process
+ :command command
+ :stdin (unless input-fname
+ (current-buffer))
+ :callback (lambda (stdout)
+ (when output-fname
+ (erase-buffer)
+ (insert-file-contents-literally output-fname))
+ (funcall callback stdout)))))))
(defcustom apheleia-formatters
'((black . ("black" "-"))