diff options
author | Ludovic Courtès <ludo@gnu.org> | 2017-08-01 14:18:35 +0200 |
---|---|---|
committer | Ludovic Courtès <ludo@gnu.org> | 2017-08-01 15:32:07 +0200 |
commit | 9081a776ea917ca8beda2cf41e8f37575c00deee (patch) | |
tree | 32c3e93d079e829e5d6efb29fac2fbd0cf2842e8 | |
parent | 269504a797a3e950725e406edf550a03946900ad (diff) | |
download | guix-9081a776ea917ca8beda2cf41e8f37575c00deee.tar guix-9081a776ea917ca8beda2cf41e8f37575c00deee.tar.gz |
lint: formatting: Detect sexp boundaries.
* guix/scripts/lint.scm (report-formatting-issues)[last-line]: Remove.
[sexp-last-line]: New procedure.
Use it.
-rw-r--r-- | guix/scripts/lint.scm | 41 |
1 files changed, 28 insertions, 13 deletions
diff --git a/guix/scripts/lint.scm b/guix/scripts/lint.scm index 04ab852999..aceafc674d 100644 --- a/guix/scripts/lint.scm +++ b/guix/scripts/lint.scm @@ -878,24 +878,39 @@ move to the previous or next line") #:key (reporters %formatting-reporters)) "Report white-space issues in FILE starting from STARTING-LINE, and report them for PACKAGE." - (define last-line - ;; Number of the presumed last line. - ;; XXX: Ideally we'd stop at the boundaries of the surrounding sexp, but - ;; for now just use this simple heuristic. - (+ starting-line 60)) + (define (sexp-last-line port) + ;; Return the last line of the sexp read from PORT or an estimate thereof. + (define &failure (list 'failure)) + + (let ((start (ftell port)) + (start-line (port-line port)) + (sexp (catch 'read-error + (lambda () (read port)) + (const &failure)))) + (let ((line (port-line port))) + (seek port start SEEK_SET) + (set-port-line! port start-line) + (if (eq? sexp &failure) + (+ start-line 60) ;conservative estimate + line)))) (call-with-input-file file (lambda (port) - (let loop ((line-number 1)) + (let loop ((line-number 1) + (last-line #f)) (let ((line (read-line port))) (or (eof-object? line) - (> line-number last-line) - (begin - (unless (< line-number starting-line) - (for-each (lambda (report) - (report package line line-number)) - reporters)) - (loop (+ 1 line-number))))))))) + (and last-line (> line-number last-line)) + (if (and (= line-number starting-line) + (not last-line)) + (loop (+ 1 line-number) + (+ 1 (sexp-last-line port))) + (begin + (unless (< line-number starting-line) + (for-each (lambda (report) + (report package line line-number)) + reporters)) + (loop (+ 1 line-number) last-line))))))))) (define (check-formatting package) "Check the formatting of the source code of PACKAGE." |