guix-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

01/05: lint: formatting: Detect sexp boundaries.


From: Ludovic Courtès
Subject: 01/05: lint: formatting: Detect sexp boundaries.
Date: Tue, 1 Aug 2017 09:35:47 -0400 (EDT)

civodul pushed a commit to branch master
in repository guix.

commit 9081a776ea917ca8beda2cf41e8f37575c00deee
Author: Ludovic Courtès <address@hidden>
Date:   Tue Aug 1 14:18:35 2017 +0200

    lint: formatting: Detect sexp boundaries.
    
    * guix/scripts/lint.scm (report-formatting-issues)[last-line]: Remove.
    [sexp-last-line]: New procedure.
    Use it.
---
 guix/scripts/lint.scm | 41 ++++++++++++++++++++++++++++-------------
 1 file changed, 28 insertions(+), 13 deletions(-)

diff --git a/guix/scripts/lint.scm b/guix/scripts/lint.scm
index 04ab852..aceafc6 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."



reply via email to

[Prev in Thread] Current Thread [Next in Thread]