Skip to content

Commit

Permalink
Move comments down to back end with view toward new info key
Browse files Browse the repository at this point in the history
Although we keep the racket-hash-lang-module-language-hook added in
the previous commit, for end users, we no longer use a default hook to
set comment-{start end padding}. Instead have the back end provide
those values. The goal is to have a new info key, e.g.
"drracket:comments", for langs to supply this. Meanwhile, any
fallbacks live down there (although a user could still use the hook to
add their own fallback or work-around).

For background on this commit and the previous commit see PR #661.
  • Loading branch information
greghendershott committed Sep 6, 2023
1 parent 74cc20f commit f61796f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 32 deletions.
43 changes: 15 additions & 28 deletions racket-hash-lang.el
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ re-tokenization has progressed sufficiently.")
(defvar-local racket-hash-lang-module-language nil
"The symbol for the module language, if any, else nil.")

(defvar racket-hash-lang-module-language-hook nil
"Hook run when the module language changes.
Your hook function may do additional customization based on the
the variable `racket-hash-lang-module-language'.")

;;;###autoload
(define-minor-mode racket-hash-lang-mode
"Use color-lexer, indent, and navigation supplied by a #lang.
Expand Down Expand Up @@ -238,8 +244,6 @@ lang's attributes that we care about have changed."
(widen)
(racket--hash-lang-remove-text-properties (point-min) (point-max))
(font-lock-flush (point-min) (point-max))
(setq racket-hash-lang-module-language (plist-get plist 'module-language))
(run-hooks 'racket-hash-lang-module-language-hook)
;; If the lang uses racket-grouping-position, i.e. it uses
;; s-expressions, then use racket-mode-syntax-table. That way
;; other Emacs features and packages are more likely to work.
Expand Down Expand Up @@ -269,7 +273,15 @@ lang's attributes that we care about have changed."
(setq-local racket-hash-lang-mode-lighter
(concat " #lang"
(when (plist-get plist 'racket-grouping) "()")
(when (plist-get plist 'range-indenter) ""))))))
(when (plist-get plist 'range-indenter) "")))
(pcase-let ((`(,start ,end ,padding) (plist-get plist 'comments)))
(setq-local comment-start start)
(setq-local comment-end end)
(setq-local comment-padding padding))
;; Finally run user's module language hooks.
(progn
(setq racket-hash-lang-module-language (plist-get plist 'module-language))
(run-hooks 'racket-hash-lang-module-language-hook)))))

(defun racket--hash-lang-on-changed-tokens (_gen beg end)
"The back end has processed a change that resulted in new tokens.
Expand Down Expand Up @@ -547,31 +559,6 @@ not a complete expression, in which case `newline-and-indent'."
(racket-repl-submit prefix)
(newline-and-indent)))

(defun racket-hash-lang-module-language-hook-default ()
"A default value for hook variable `racket-hash-lang-module-language-hook'.
Sets the variable `comment-start'. This is probably just a
work-around until a lang info key for this is defined and
implemented by various langs?"
(setq comment-start
(cl-case racket-hash-lang-module-language
((racket
racket/base)
";;")
((rhombus)
"//")
((scribble/base/lang
scribble/manual/lang)
"@;"))))

(defvar racket-hash-lang-module-language-hook
(list #'racket-hash-lang-module-language-hook-default)
"Hook run when the module language changes.
Your hook function may consult the variable
`racket-hash-lang-module-language' and do additional
customization based on the specific language.")

(provide 'racket-hash-lang)

;; racket-hash-lang.el ends here
3 changes: 2 additions & 1 deletion racket/hash-lang-bridge.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
'module-language (lang-info-module-language li)
'racket-grouping (lang-info-grouping-position-is-racket? li)
'range-indenter (and (lang-info-range-indenter li) #t)
'submit-predicate (and (lang-info-submit-predicate li) #t))))
'submit-predicate (and (lang-info-submit-predicate li) #t)
'comments (lang-info-comments li))))
(define/override (on-changed-tokens gen beg end)
(when (< beg end)
(async-channel-put hash-lang-notify-channel
Expand Down
20 changes: 18 additions & 2 deletions racket/hash-lang.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -671,23 +671,39 @@
racket-grouping-position
racket-amount-to-indent
#f
#f
#f))

(define (read-lang-info* in)
(define info (or (with-handlers ([values (λ _ #f)])
(read-language in (λ _ #f)))
(λ (_key default) default)))
(define-values (_line _col end-pos) (port-next-location in))
(values (lang-info (info 'module-language default-module-language)
(define mod-lang (info 'module-language default-module-language))
(values (lang-info mod-lang
(info 'color-lexer default-lexer)
(info 'drracket:paren-matches default-paren-matches)
(info 'drracket:quote-matches default-quote-matches)
(info 'drracket:grouping-position racket-grouping-position)
(info 'drracket:indentation racket-amount-to-indent)
(info 'drracket:range-indentation #f)
(info 'drracket:submit-predicate #f))
(info 'drracket:submit-predicate #f)
(or (info 'comments #f) ;; TODO: drracket:comments ?
(comments-fallback mod-lang)))
end-pos))

;; Fallback when langs don't support a comments info key.
(define (comments-fallback mod-lang-sym)
(define (root sym) ;e.g. 'racket and 'racket/base => racket
(match (and sym (symbol->string sym))
[(pregexp "^([^/]+)" (list _ str))
(string->symbol str)]
[_ #f]))
(case (root mod-lang-sym)
[(scribble) '("@;" "" " ")]
[(rhombus) '("//" "" " ")]
[else '(";;" "" " ")]))

(define (read-lang-info in)
(define-values (v _pos) (read-lang-info* in))
v)
Expand Down
3 changes: 2 additions & 1 deletion racket/lang-info.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
grouping-position
line-indenter
range-indenter
submit-predicate)
submit-predicate
comments)
#:transparent #:authentic)

(define racket-grouping-position
Expand Down

0 comments on commit f61796f

Please sign in to comment.