From f61796f085d00c9b8e490e9c6def69fdd6236582 Mon Sep 17 00:00:00 2001 From: Greg Hendershott Date: Sat, 26 Aug 2023 11:20:30 -0400 Subject: [PATCH] Move comments down to back end with view toward new info key 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. --- racket-hash-lang.el | 43 +++++++++++++------------------------ racket/hash-lang-bridge.rkt | 3 ++- racket/hash-lang.rkt | 20 +++++++++++++++-- racket/lang-info.rkt | 3 ++- 4 files changed, 37 insertions(+), 32 deletions(-) diff --git a/racket-hash-lang.el b/racket-hash-lang.el index 1d42ca26..cb09b656 100644 --- a/racket-hash-lang.el +++ b/racket-hash-lang.el @@ -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. @@ -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. @@ -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. @@ -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 diff --git a/racket/hash-lang-bridge.rkt b/racket/hash-lang-bridge.rkt index 660eaf57..42fc80fc 100644 --- a/racket/hash-lang-bridge.rkt +++ b/racket/hash-lang-bridge.rkt @@ -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 diff --git a/racket/hash-lang.rkt b/racket/hash-lang.rkt index 7defdb94..5edd8c89 100644 --- a/racket/hash-lang.rkt +++ b/racket/hash-lang.rkt @@ -671,6 +671,7 @@ racket-grouping-position racket-amount-to-indent #f + #f #f)) (define (read-lang-info* in) @@ -678,16 +679,31 @@ (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) diff --git a/racket/lang-info.rkt b/racket/lang-info.rkt index 3d95b62e..06c5bca4 100644 --- a/racket/lang-info.rkt +++ b/racket/lang-info.rkt @@ -17,7 +17,8 @@ grouping-position line-indenter range-indenter - submit-predicate) + submit-predicate + comments) #:transparent #:authentic) (define racket-grouping-position