Skip to content

Commit

Permalink
Add read-line-any rule
Browse files Browse the repository at this point in the history
Also, fix `in-syntax-identifiers` to skip quoted identifiers.
  • Loading branch information
jackfirth committed Sep 18, 2024
1 parent 966ce9a commit 0bad0a7
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 6 deletions.
3 changes: 3 additions & 0 deletions default-recommendations.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
(all-from-out resyntax/default-recommendations/boolean-shortcuts
resyntax/default-recommendations/comparison-shortcuts
resyntax/default-recommendations/conditional-shortcuts
resyntax/default-recommendations/console-io-suggestions
resyntax/default-recommendations/contract-shortcuts
resyntax/default-recommendations/definition-shortcuts
resyntax/default-recommendations/file-io-suggestions
Expand Down Expand Up @@ -36,6 +37,7 @@
resyntax/default-recommendations/boolean-shortcuts
resyntax/default-recommendations/comparison-shortcuts
resyntax/default-recommendations/conditional-shortcuts
resyntax/default-recommendations/console-io-suggestions
resyntax/default-recommendations/contract-shortcuts
resyntax/default-recommendations/definition-shortcuts
resyntax/default-recommendations/file-io-suggestions
Expand Down Expand Up @@ -66,6 +68,7 @@
#:suites (boolean-shortcuts
comparison-shortcuts
conditional-shortcuts
console-io-suggestions
contract-shortcuts
definition-shortcuts
file-io-suggestions
Expand Down
30 changes: 30 additions & 0 deletions default-recommendations/console-io-suggestions-test.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#lang resyntax/testing/refactoring-test


require: resyntax/default-recommendations console-io-suggestions


header:
- #lang racket/base


test: "should suggest 'any linemode with read-line when linemode not specified"
----------------------------------------
(define (foo in)
(read-line in))
----------------------------------------
----------------------------------------
(define (foo in)
(read-line in 'any))
----------------------------------------


test: "should suggest 'any linemode with read-line when linemode and port not specified"
----------------------------------------
(define (foo)
(read-line))
----------------------------------------
----------------------------------------
(define (foo)
(read-line (current-input-port) 'any))
----------------------------------------
32 changes: 32 additions & 0 deletions default-recommendations/console-io-suggestions.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#lang racket/base


(require racket/contract/base)


(provide
(contract-out
[console-io-suggestions refactoring-suite?]))


(require racket/file
racket/list
rebellion/private/static-name
resyntax/base
syntax/parse)


;@----------------------------------------------------------------------------------------------------


(define-refactoring-rule read-line-any
#:description
(string-append "Specify a line mode of `'any` with `read-line` to avoid differences between "
"Windows and other platforms.")
#:literals (read-line)
(read-line (~optional port))
(read-line (~? port (current-input-port)) 'any))


(define-refactoring-suite console-io-suggestions
#:rules (read-line-any))
17 changes: 11 additions & 6 deletions default-recommendations/private/syntax-identifier-sets.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,24 @@
racket/sequence
racket/set
racket/stream
syntax/id-set)
syntax/id-set
syntax/parse)


;@----------------------------------------------------------------------------------------------------


(define (in-syntax-identifiers stx)
(stream*
(guarded-block
(guard (not (identifier? stx)) #:else (stream stx))
(guard-match (or (cons head tail) (? syntax? (app syntax-e (cons head tail)))) stx #:else
(stream))
(stream-append (in-syntax-identifiers head) (in-syntax-identifiers tail)))))
(syntax-parse stx
#:datum-literals (quote)
[(quote _) (stream)]
[(subform ...) (apply stream-append (map in-syntax-identifiers (attribute subform)))]
[(subform ...+ . tail-form)
(stream-append (apply stream-append (map in-syntax-identifiers (attribute subform)))
(in-syntax-identifiers (attribute tail-form)))]
[id:id (stream (attribute id))]
[_ (stream)])))


(define (syntax-identifiers stx)
Expand Down

0 comments on commit 0bad0a7

Please sign in to comment.