Skip to content

Instantly share code, notes, and snippets.

@bennn
Created August 1, 2020 16:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bennn/52713a3c002eba9928f3b7ec256f1dc7 to your computer and use it in GitHub Desktop.
Save bennn/52713a3c002eba9928f3b7ec256f1dc7 to your computer and use it in GitHub Desktop.
quickscript: count lines in a file, even if unsaved or with unmatched parentheses
#lang racket/base
;; Count lines
;; Use the selection, if any, otherwise count lines across all definitions.
;;
;; Other ideas:
;; - if the file is read-able, use `syntax-sloc`
;; - find way to count types, contracts, etc.
(require quickscript
(only-in racket/class send))
(define count-linebreaks
(let ((newline-char (integer->char 10)))
(lambda (str)
(+ 1 ;; for the final line, because it's hard to select the newline at the end of it
(for/sum ((c (in-string str))
#:when (eq? c newline-char))
1)))))
(define-script line-count
#:label "line-count"
#:help-string "Count lines in the current selection, or for all definitions."
#:output-to message-box
(λ (selection #:definitions def)
(define-values [kind num-lines]
(if (< 0 (string-length selection))
(values "selection" (count-linebreaks selection))
(values "definitions" (count-linebreaks (send def get-flattened-text)))))
(format "Lines in ~a : ~a" kind num-lines)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment