Skip to content

Instantly share code, notes, and snippets.

@Calvin-LL
Last active October 21, 2021 02:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Calvin-LL/8c463dac8e4167f67d543d954f5f48c8 to your computer and use it in GitHub Desktop.
Save Calvin-LL/8c463dac8e4167f67d543d954f5f48c8 to your computer and use it in GitHub Desktop.
DrRacket Code Formatting Keybinding
#lang s-exp framework/keybinding-lang
(require fmt)
(define (format editor event)
(define text (send editor get-text))
;; for more options, see https://docs.racket-lang.org/fmt/
(define formatted-text (program-format text #:max-blank-lines +inf.0))
(send editor insert formatted-text 0 (string-length text) #f))
(define (format-selection editor event)
(define start-pos (send editor get-start-position))
(define end-pos (send editor get-end-position))
(define text (send editor get-text start-pos end-pos))
;; for more options, see https://docs.racket-lang.org/fmt/
(define formatted-text (program-format text #:max-blank-lines +inf.0))
(send editor insert formatted-text start-pos end-pos #f))
;; d is for the command key on Macs
;; s is for the shift key
;; a is for the option key
;; f is for the f key
(keybinding "d:s:f" format)
(keybinding "d:s:g" format-selection)
;; use this keybinding instead if you're not using a Mac
;; c is for the control key
;; s is for the shift key
;; a is for the alt key
;; f is for the f key
; (keybinding "c:s:f" format)
; (keybinding "c:s:g" format-selection)
;; for more details, see https://docs.racket-lang.org/drracket/Keyboard_Shortcuts.html
@Calvin-LL
Copy link
Author

Calvin-LL commented Oct 21, 2021

This keybinding formats the current Racket file when you press control+shift+f or command+shift+f and formats the current selection with control+shift+g or command+shift+g

To add this binding

  1. Install fmt
    • Go to DrRacket's menu bar -> File -> Install Package
    • For "Package Source", type fmt
    • Click "Show Details"
    • For "Dependencies Mode", select Auto
    • Click Install
  2. Install this binding
    • Download this file
    • If you're not using a Mac, comment out line 25 and 26 and uncomment line 32 and 33
    • Move the file to a safe place, you'll need to keep it on your computer
    • Go to DrRacket's menu bar -> Edit -> Keybindings -> Add User-defined Keybindings, and select this file

formatter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment