Skip to content

Commit

Permalink
racket-expand-xxx: Various changes
Browse files Browse the repository at this point in the history
Provide richer defcustom type for racket-expand-hiding.

Unless overridden by a command prefix, show the racket-expand-hiding
name and value in the buffer, and button-ize the former to be
customize-variable.

Add a racket-stepper-refresh command. So if someone has customized the
hiding policy, they can just hit "g" to effect the new value.
  • Loading branch information
greghendershott committed Jan 26, 2024
1 parent a931610 commit be2dce3
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 83 deletions.
14 changes: 2 additions & 12 deletions doc/racket-mode.texi
Original file line number Diff line number Diff line change
Expand Up @@ -2782,6 +2782,8 @@ Used by the commands @ref{racket-expand-file},
@multitable {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa} {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa}
@item Key
@tab Binding
@item @kbd{g}
@tab @code{racket-stepper-refresh}
@item @kbd{k}
@tab @code{racket-stepper-previous-item}
@item @kbd{p}
Expand Down Expand Up @@ -3156,18 +3158,6 @@ string should be a properly encoded URL@.

The macro hiding policy for commands like @ref{racket-expand-file}.

Although any macro-debugger/model/hiding-policies value may be
used, frequently used values include:

@itemize
@item
'standard
@item
'disabled
@item
(list 'custom hide-racket? hide-libs? hide-contracts? hide-phase1? rules)
@end itemize

@node Hash lang variables
@section Hash lang variables

Expand Down
19 changes: 10 additions & 9 deletions racket-custom.el
Original file line number Diff line number Diff line change
Expand Up @@ -573,16 +573,17 @@ ignore POS. Examples: `racket-show-echo-area' and
:group 'racket-other)

(defcustom racket-expand-hiding 'standard
"The macro hiding policy for commands like `racket-expand-file'.
Although any macro-debugger/model/hiding-policies value may be
used, frequently used values include:
- \\='standard
- \\='disabled
- (list \\='custom hide-racket? hide-libs? hide-contracts? hide-phase1? rules)"
"The macro hiding policy for commands like `racket-expand-file'."
:tag "Racket Expand Hiding"
:type 'sexpr
:type '(choice (const :tag "Disable" disable)
(const :tag "Standard" standard)
(list :tag "Custom" :value (list t t t t nil)
(boolean :tag "Hide racket syntax")
(boolean :tag "Hide library syntax")
(boolean :tag "Hide contracts")
(boolean :tag "Hide phase>0")
(repeat :tag "More rules (see macro-debugger/model/hiding-policies \"Entry\" and \"Condition\")"
sexp)))
:group 'racket-other)

;;; Faces
Expand Down
99 changes: 59 additions & 40 deletions racket-stepper.el
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@
;; Need to define this before racket-stepper-mode
(defvar racket-stepper-mode-map
(racket--easy-keymap-define
'((("C-m") racket-stepper-step)
(("n" "j") racket-stepper-next-item)
(("p" "k") racket-stepper-previous-item))))
`((("C-m") ,#'racket-stepper-step)
(("n" "j") ,#'racket-stepper-next-item)
(("p" "k") ,#'racket-stepper-previous-item)
("g" ,#'racket-stepper-refresh))))

(easy-menu-define racket-stepper-mode-menu racket-stepper-mode-map
"Menu for Racket stepper mode."
'("Racket"
["Step" racket-stepper-step]
["Next" racket-stepper-next-item]
["Previous" racket-stepper-previous-item]))
`("Racket"
["Step" ,#'racket-stepper-step]
["Next" ,#'racket-stepper-next-item]
["Previous" ,#'racket-stepper-previous-item]
["Refresh" ,#'racket-stepper-refresh]))

(defconst racket-stepper-font-lock-keywords
(eval-when-compile
Expand Down Expand Up @@ -76,8 +78,7 @@ were \\='disabled."
(interactive "P")
(racket--assert-edit-mode)
(racket--save-if-changed)
(racket-stepper--start nil
no-hiding))
(racket-stepper--start nil no-hiding))

(defun racket-expand-region (&optional no-hiding)
"Expand the active region using `racket-stepper-mode'.
Expand Down Expand Up @@ -134,11 +135,12 @@ were \\='disabled."
(racket-stepper--start (buffer-substring-no-properties beg end)
no-hiding))))

(defvar racket--stepper-repl-session-id nil
"The REPL session used when stepping.
May be nil for \"file\" stepping, but must be valid for \"expr\"
stepping.")
;; When starting, save the essential parameters in these vars, to
;; support a refresh command.
(defvar racket--stepper-repl-session-id nil)
(defvar racket--stepper-path nil)
(defvar racket--stepper-expr nil)
(defvar racket--stepper-no-hiding nil)

(defun racket-stepper--start (expression-str no-hiding)
"Ensure buffer and issue initial command.
Expand All @@ -148,46 +150,63 @@ STR should be the expression or nil for file expansion."
(setq racket--stepper-repl-session-id (racket--repl-session-id))
(unless (or (not expression-str)
racket--stepper-repl-session-id)
(error "Expression expansion only works when the edit buffer has a REPL buffer, and, you should racket-run first"))
(let ((path (racket-file-name-front-to-back (racket--buffer-file-name))))
;; Create buffer if necessary
(let ((name (racket--stepper-buffer-name)))
(unless (get-buffer name)
(with-current-buffer (get-buffer-create name)
(racket-stepper-mode)))
;; Give it a window if necessary
(unless (get-buffer-window name)
(pop-to-buffer (get-buffer name)))
;; Select the stepper window and insert
(select-window (get-buffer-window name))
(let ((inhibit-read-only t))
(delete-region (point-min) (point-max))
(insert "Starting macro expansion stepper... please wait...\n"))
(racket--cmd/async racket--stepper-repl-session-id
`(macro-stepper ,path
,expression-str
,(if no-hiding
'disable
racket-expand-hiding))
#'racket-stepper--insert))))

(defun racket-stepper--insert (nothing-or-steps)
(if (eq nothing-or-steps 'nothing)
(error "Expression expansion only works when the edit buffer has a REPL buffer, and, you already did a racket-run"))
(setq racket--stepper-path (racket-file-name-front-to-back (racket--buffer-file-name)))
(setq racket--stepper-expr expression-str)
(setq racket--stepper-no-hiding no-hiding)
;; Create buffer if necessary
(let ((name (racket--stepper-buffer-name)))
(unless (get-buffer name)
(with-current-buffer (get-buffer-create name)
(racket-stepper-mode)))
;; Give it a window if necessary
(unless (get-buffer-window name)
(pop-to-buffer (get-buffer name)))
;; Select the stepper window and start.
(select-window (get-buffer-window name))
(racket-stepper-refresh)))

(defun racket-stepper-refresh ()
(interactive)
(let ((inhibit-read-only t))
(delete-region (point-min) (point-max))
(insert "Starting macro expansion stepper... please wait...\n"))
(racket--cmd/async racket--stepper-repl-session-id
`(macro-stepper ,racket--stepper-path
,racket--stepper-expr
,(if racket--stepper-no-hiding
'disable
racket-expand-hiding))
#'racket-stepper--insert))

(defun racket-stepper--insert (steps)
(if (null steps)
(message "Nothing to expand")
(with-current-buffer (racket--stepper-buffer-name)
(let ((inhibit-read-only t))
(goto-char (point-max))
(dolist (step nothing-or-steps)
(dolist (step steps)
(pcase step
(`(original . ,text)
(delete-region (point-min) (point-max))
(if racket--stepper-no-hiding
(insert "macro hiding disabled by command prefix")
(insert-text-button "racket-expand-hiding"
'action #'racket-stepper-customize-hiding)
(insert ": ")
(princ (if racket--stepper-no-hiding 'disable racket-expand-hiding)
(current-buffer)))
(insert "\n\n")
(insert "Original\n" text "\n" "\n"))
(`(final . ,text) (insert "Final\n" text "\n"))
(`(,label . ,diff) (insert label "\n" diff "\n"))))
(racket-stepper-previous-item)
(when (equal (selected-window) (get-buffer-window (current-buffer)))
(recenter))))))

(defun racket-stepper-customize-hiding (_btn)
(customize-variable 'racket-expand-hiding))

(defun racket-stepper-step (prefix)
(interactive "P")
(racket--cmd/async racket--stepper-repl-session-id
Expand Down
27 changes: 13 additions & 14 deletions racket/commands/macro.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@
macro-stepper/next)

(define step/c (cons/c (or/c 'original string? 'final) string?))
(define step-proc/c (-> (or/c 'next 'all)
(or/c 'nothing (listof step/c))))
(define step-proc #f)
(define (nothing-step-proc _) 'nothing)
(define step-proc/c (-> (or/c 'next 'all) (listof step/c)))

(define (nothing-step-proc _) null)

(define step-proc nothing-step-proc)

(define/contract (macro-stepper path expression-str hiding-policy)
(-> (and/c path-string? complete-path?) any/c any/c
(list/c step/c))
(assert-macro-debugger-stepper-works)
(define-values (stx ns)
(cond
[(string? expression-str)
Expand All @@ -56,7 +58,6 @@
(define/contract (make-stepper path stx ns elisp-hiding-policy)
(-> (and/c path-string? complete-path?) syntax? namespace? any/c
step-proc/c)
(assert-macro-debugger-stepper-works)
(define dir (path-only path))
(define policy (elisp-policy->policy elisp-hiding-policy))
(define predicate (policy->predicate policy))
Expand Down Expand Up @@ -94,7 +95,6 @@
(list (cons 'final step-last-after))])]))
step)


(define (elisp-policy->policy e)
;; See macro-debugger/model/hiding-policies.rkt):
;;
Expand All @@ -108,8 +108,7 @@
;; should be fine.
(match e
[(or 'disable 'standard) e]
[(list 'custom
(app as-racket-bool hide-racket?)
[(list (app as-racket-bool hide-racket?)
(app as-racket-bool hide-libs?)
(app as-racket-bool hide-contracts?)
(app as-racket-bool hide-phase1?)
Expand All @@ -128,12 +127,6 @@
(pretty-format #:mode 'write before)
(pretty-format #:mode 'write after))]))

(define (assert-macro-debugger-stepper-works)
(define step (stepper-text #'(module example racket/base 42)))
(unless (step 'next)
(error 'macro-debugger/stepper-text
"does not work in your version of Racket.\nPlease try an older or newer version.")))

(define (diff-text before-text after-text #:unified [-U 3])
(define template "racket-mode-syntax-diff-~a")
(define (make-temporary-file-with-text str)
Expand All @@ -160,3 +153,9 @@

(define (pretty-format-syntax stx)
(pretty-format #:mode 'write (syntax->datum stx)))

(define (assert-macro-debugger-stepper-works)
(define step (stepper-text #'(module example racket/base 42)))
(unless (step 'next)
(error 'macro-debugger/stepper-text
"does not work in your version of Racket.\nPlease try an older or newer version.")))
31 changes: 23 additions & 8 deletions test/racket-tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -419,13 +419,17 @@ want to use the value of `racket-program' at run time."
(defconst racket-tests/expand-mod-name "foo")

(defconst racket-tests/expand-shallow-0
"«f:Original»
"«:button:racket-expand-hiding»: standard
«f:Original»
(module foo racket/base (#%module-begin (define x 42) x))
")

(defconst racket-tests/expand-shallow-1
"«f:Original»
"«:button:racket-expand-hiding»: standard
«f:Original»
(module foo racket/base (#%module-begin (define x 42) x))
«f:Final»
Expand All @@ -441,7 +445,8 @@ want to use the value of `racket-program' at run time."
(code "#lang racket/base\n(define x 42)\nx"))
(write-region code nil path nil 'no-wrote-file-message)
(find-file path)
(racket-expand-file)
(let ((racket-expand-hiding 'standard))
(racket-expand-file))
(set-buffer "*Racket Stepper </>*")
(should (eq major-mode 'racket-stepper-mode))
(should (equal header-line-format "Press RET to step. C-u RET to step all. C-h m to see help."))
Expand All @@ -456,13 +461,17 @@ want to use the value of `racket-program' at run time."
;;; Macro stepper: File "deep"

(defconst racket-tests/expand-deep-0
"«f:Original»
"«f:macro hiding disabled by command prefix»
«f:Original»
(module foo racket/base (#%module-begin (define x 42) x))
")

(defconst racket-tests/expand-deep-1
"«f:Original»
"«f:macro hiding disabled by command prefix»
«f:Original»
(module foo racket/base (#%module-begin (define x 42) x))
«f:1: Macro transformation»
Expand All @@ -481,7 +490,9 @@ want to use the value of `racket-program' at run time."
")

(defconst racket-tests/expand-deep-2
"«f:Original»
"«f:macro hiding disabled by command prefix»
«f:Original»
(module foo racket/base (#%module-begin (define x 42) x))
«f:1: Macro transformation»
Expand Down Expand Up @@ -549,13 +560,17 @@ want to use the value of `racket-program' at run time."
;;; Macro stepper: Expression

(defconst racket-tests/expand-expression-original
"«f:Original»
"«f:macro hiding disabled by command prefix»
«f:Original»
(cond ((< 1 2) #t) (else #f))
")

(defconst racket-tests/expand-expression-final
"«f:Original»
"«f:macro hiding disabled by command prefix»
«f:Original»
(cond ((< 1 2) #t) (else #f))
«f:1: Macro transformation»
Expand Down

0 comments on commit be2dce3

Please sign in to comment.