Skip to content

Commit

Permalink
Fix indentation in the esup buffer
Browse files Browse the repository at this point in the history
Fixes #63 by adding a function to strip common leading whitespace from
a multiline string.
  • Loading branch information
benley committed Aug 13, 2020
1 parent c9c95e2 commit eee63ed
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cask
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
(files "esup-child.el")

(depends-on "cl-lib" "0.5")
(depends-on "s" "1.2")

(development
(depends-on "dash")
Expand Down
19 changes: 17 additions & 2 deletions esup-child.el
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
;; Version: 0.7.1
;; URL: https://github.com/jschaf/esup
;; Keywords: convenience, processes
;; Package-Requires: ((cl-lib "0.5") (emacs "25"))
;; Package-Requires: ((cl-lib "0.5") (s "1.2") (emacs "25.1"))

;; This file is NOT part of GNU Emacs.

Expand Down Expand Up @@ -36,6 +36,8 @@

(require 'benchmark)
(require 'eieio)
(require 's)
(require 'seq)

;; We don't use :accesssor for class slots because it cause a
;; byte-compiler error even if we use the accessor. This is fixed in
Expand Down Expand Up @@ -187,6 +189,19 @@ a complete result.")
(setq str (replace-match "" t t str)))
str)

(defun esup-child-unindent (str)
"Remove common leading whitespace from each line of STR.
If STR contains only whitespace, return an empty string."
(let* ((lines (s-lines str))
(non-whitespace-lines (seq-filter (lambda (s) (< 0 (length (s-trim-left s))))
lines))
(n-to-trim (apply #'min (mapcar (lambda (s) (- (length s) (length (s-trim-left s))))
(or non-whitespace-lines [""]))))
(result (s-join "\n"
(mapcar (lambda (s) (substring (s-pad-left n-to-trim " " s) n-to-trim))
lines))))
(if (= 0 (length (esup-child-chomp result))) "" result)))

(defmacro with-esup-child-increasing-depth (&rest body)
"Run BODY and with an incremented depth level.
Decrement the depth level after complete."
Expand Down Expand Up @@ -315,7 +330,7 @@ BUFFER defaults to the current buffer."
(defun esup-child-profile-sexp (start end)
"Profile the sexp between START and END in the current buffer.
Returns a list of class `esup-result'."
(let* ((sexp-string (esup-child-chomp (buffer-substring start end)))
(let* ((sexp-string (esup-child-unindent (buffer-substring start end)))
(line-number (line-number-at-pos start))
(file-name (buffer-file-name))
sexp
Expand Down
2 changes: 1 addition & 1 deletion esup.el
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
;; Version: 0.7.1
;; URL: https://github.com/jschaf/esup
;; Keywords: convenience, processes
;; Package-Requires: ((cl-lib "0.5") (emacs "25"))
;; Package-Requires: ((cl-lib "0.5") (emacs "25.1"))

;; This file is NOT part of GNU Emacs.

Expand Down
12 changes: 12 additions & 0 deletions test/test-esup.el
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
;; Maintainer: Serghei Iakovlev <[email protected]>
;; Version: 0.7.1
;; URL: https://github.com/jschaf/esup
;; Package-Requires: ((emacs "25.1"))

;; This file is NOT part of GNU Emacs.

Expand Down Expand Up @@ -79,6 +80,17 @@
(esup-child-run "/fake/foo-bar.el" -1)
(list)))))

(it "handles whitespace-only file"
(with-esup-mock
'(:load-path ("/fake")
:files (("/fake/foo-bar.el" . " ")))

(should
(esup-results-equal-p
'(:gc-time :exec-time)
(esup-child-run "/fake/foo-bar.el" -1)
(list)))))

(it "counts gc"
(with-esup-mock
'(:load-path ("/fake")
Expand Down

0 comments on commit eee63ed

Please sign in to comment.