<commit_before>(in-package :next)

(defstruct variable-candidate
  (name))
(defmethod object-string ((variable variable-candidate))
  (string-downcase (format nil "~s" (variable-candidate-name variable))))
(defmethod object-display ((variable variable-candidate))
  (object-string variable))

(defstruct function-candidate
  (name))
(defmethod object-string ((fun function-candidate))
  (string-downcase (format nil "~s" (function-candidate-name fun))))
(defmethod object-display ((fun function-candidate))
  (object-string fun))

(defstruct class-candidate
  (name))
(defmethod object-string ((class class-candidate))
  (string-downcase (format nil "~s" (class-candidate-name class))))
(defmethod object-display ((class class-candidate))
  (object-string class))

(defun variable-completion-filter ()
  (let* ((variables (mapcar (lambda (v) (make-variable-candidate :name v))
                            (package-variables))))
    (lambda (minibuffer)
      (fuzzy-match (input-buffer minibuffer) variables))))

(defun function-completion-filter ()
  (let ((functions (mapcar (lambda (v) (make-function-candidate :name v))
                           (package-functions))))
    (lambda (minibuffer)
      (fuzzy-match (input-buffer minibuffer) functions))))

(defun class-completion-filter ()
  (let ((classes (mapcar (lambda (v) (make-class-candidate :name v))
                         (package-classes))))
    (lambda (minibuffer)
      (fuzzy-match (input-buffer minibuffer) classes))))

(defun slot-completion-filter ()
  (let ((slots (package-slots)))
    (lambda (minibuffer)
      (fuzzy-match (input-buffer minibuffer) slots))))

(define-command describe-variable ()
  "Inspect a variable and show it in a help buffer."
  (with-result (input (read-from-minibuffer
                       (make-minibuffer
                        :completion-function (variable-completion-filter)
                        :input-prompt "Describe variable")))
    (let* ((input (variable-candidate-name input))
           (help-buffer (next/help-mode:help-mode
                         :activate t
                         :buffer (make-buffer
                                  :title (str:concat "*Help-"
                                                     (symbol-name input)
                                                     "*"))))
           (help-contents (markup:markup
                           (:h1 (format nil "~s" input)) ; Use FORMAT to keep package prefix.
                           (:p (documentation input 'variable))
                           (:h2 "Current Value:")
                           (:p (write-to-string (symbol-value input)))))
           (insert-help (ps:ps (setf (ps:@ document Body |innerHTML|)
                                     (ps:lisp help-contents)))))
      (ffi-buffer-evaluate-javascript help-buffer insert-help)
      (set-current-buffer help-buffer))))

(declaim (ftype (function (command)) describe-command*))
(defun describe-command* (command)
  "Display NAME command documentation in a new focused buffer."
  (let* ((title (str:concat "*Help-" (symbol-name (sym command)) "*"))
         (help-buffer (next/help-mode:help-mode
                       :activate t
                       :buffer (make-buffer :title title)))
         (key-keymap-pairs (nth-value 1 (keymap:binding-keys
                                         (sym command)
                                         (all-keymaps))))
         (key-keymapname-pairs (mapcar (lambda (pair)
                                         (list (first pair)
                                               (keymap:name (second pair))))
                                       key-keymap-pairs))
         (source-file (getf (getf (swank:find-definition-for-thing (command-function command))
                                  :location)
                            :file))
         (help-contents (markup:markup
                         (:h1 (symbol-name (sym command))
                              (unless (eq (find-package :next)
                                          (symbol-package (sym command)))
                                (format nil " (~a)"
                                        (package-name (symbol-package (sym command))))))
                         (:p (:pre ; See describe-slot* for why we use :pre.
                              ;; TODO: This only displays the first method,
                              ;; i.e. the first command of one of the modes.
                              ;; Ask for modes instead?
                                   (documentation (command-function command) t)))
                         (:h2 "Bindings")
                         (:p (format nil "~:{ ~S (~a)~:^, ~}" key-keymapname-pairs))
                         (:h2 (format nil "Source (~a): " source-file))
                         (:pre (:code (write-to-string (sexp command))))))
         (insert-help (ps:ps (setf (ps:@ document Body |innerHTML|)
                                   (ps:lisp help-contents)))))
    (ffi-buffer-evaluate-javascript help-buffer insert-help)
    (set-current-buffer help-buffer)))

(define-command describe-function ()
  "Inspect a function and show it in a help buffer.
For generic functions, describe all the methods."
  (with-result (input (read-from-minibuffer
                       (make-minibuffer
                        :input-prompt "Describe function"
                        :completion-function (function-completion-filter))))
    (setf input (function-candidate-name input))
    (flet ((method-desc (method)
             (markup:markup
              (:h1 (symbol-name input) " " (write-to-string (mopu:method-specializers method)))
              (:pre (documentation method 't))
              (:h2 "Argument list")
              (:p (write-to-string (closer-mop:method-lambda-list method))))))
      (let* ((help-buffer (next/help-mode:help-mode
                           :activate t
                           :buffer (make-buffer
                                    :title (str:concat "*Help-"
                                                       (symbol-name input)<commit_msg>help: Print command source in lower case.<commit_after>(in-package :next)

(defstruct variable-candidate
  (name))
(defmethod object-string ((variable variable-candidate))
  (string-downcase (format nil "~s" (variable-candidate-name variable))))
(defmethod object-display ((variable variable-candidate))
  (object-string variable))

(defstruct function-candidate
  (name))
(defmethod object-string ((fun function-candidate))
  (string-downcase (format nil "~s" (function-candidate-name fun))))
(defmethod object-display ((fun function-candidate))
  (object-string fun))

(defstruct class-candidate
  (name))
(defmethod object-string ((class class-candidate))
  (string-downcase (format nil "~s" (class-candidate-name class))))
(defmethod object-display ((class class-candidate))
  (object-string class))

(defun variable-completion-filter ()
  (let* ((variables (mapcar (lambda (v) (make-variable-candidate :name v))
                            (package-variables))))
    (lambda (minibuffer)
      (fuzzy-match (input-buffer minibuffer) variables))))

(defun function-completion-filter ()
  (let ((functions (mapcar (lambda (v) (make-function-candidate :name v))
                           (package-functions))))
    (lambda (minibuffer)
      (fuzzy-match (input-buffer minibuffer) functions))))

(defun class-completion-filter ()
  (let ((classes (mapcar (lambda (v) (make-class-candidate :name v))
                         (package-classes))))
    (lambda (minibuffer)
      (fuzzy-match (input-buffer minibuffer) classes))))

(defun slot-completion-filter ()
  (let ((slots (package-slots)))
    (lambda (minibuffer)
      (fuzzy-match (input-buffer minibuffer) slots))))

(define-command describe-variable ()
  "Inspect a variable and show it in a help buffer."
  (with-result (input (read-from-minibuffer
                       (make-minibuffer
                        :completion-function (variable-completion-filter)
                        :input-prompt "Describe variable")))
    (let* ((input (variable-candidate-name input))
           (help-buffer (next/help-mode:help-mode
                         :activate t
                         :buffer (make-buffer
                                  :title (str:concat "*Help-"
                                                     (symbol-name input)
                                                     "*"))))
           (help-contents (markup:markup
                           (:h1 (format nil "~s" input)) ; Use FORMAT to keep package prefix.
                           (:p (documentation input 'variable))
                           (:h2 "Current Value:")
                           (:p (write-to-string (symbol-value input)))))
           (insert-help (ps:ps (setf (ps:@ document Body |innerHTML|)
                                     (ps:lisp help-contents)))))
      (ffi-buffer-evaluate-javascript help-buffer insert-help)
      (set-current-buffer help-buffer))))

(declaim (ftype (function (command)) describe-command*))
(defun describe-command* (command)
  "Display NAME command documentation in a new focused buffer."
  (let* ((title (str:concat "*Help-" (symbol-name (sym command)) "*"))
         (help-buffer (next/help-mode:help-mode
                       :activate t
                       :buffer (make-buffer :title title)))
         (key-keymap-pairs (nth-value 1 (keymap:binding-keys
                                         (sym command)
                                         (all-keymaps))))
         (key-keymapname-pairs (mapcar (lambda (pair)
                                         (list (first pair)
                                               (keymap:name (second pair))))
                                       key-keymap-pairs))
         (source-file (getf (getf (swank:find-definition-for-thing (command-function command))
                                  :location)
                            :file))
         (help-contents (markup:markup
                         (:h1 (symbol-name (sym command))
                              (unless (eq (find-package :next)
                                          (symbol-package (sym command)))
                                (format nil " (~a)"
                                        (package-name (symbol-package (sym command))))))
                         (:p (:pre ; See describe-slot* for why we use :pre.
                              ;; TODO: This only displays the first method,
                              ;; i.e. the first command of one of the modes.
                              ;; Ask for modes instead?
                                   (documentation (command-function command) t)))
                         (:h2 "Bindings")
                         (:p (format nil "~:{ ~S (~a)~:^, ~}" key-keymapname-pairs))
                         (:h2 (format nil "Source (~a): " source-file))
                         (:pre (:code (let ((*print-case* :downcase))
                                        (write-to-string (sexp command)))))))
         (insert-help (ps:ps (setf (ps:@ document Body |innerHTML|)
                                   (ps:lisp help-contents)))))
    (ffi-buffer-evaluate-javascript help-buffer insert-help)
    (set-current-buffer help-buffer)))

(define-command describe-function ()
  "Inspect a function and show it in a help buffer.
For generic functions, describe all the methods."
  (with-result (input (read-from-minibuffer
                       (make-minibuffer
                        :input-prompt "Describe function"
                        :completion-function (function-completion-filter))))
    (setf input (function-candidate-name input))
    (flet ((method-desc (method)
             (markup:markup
              (:h1 (symbol-name input) " " (write-to-string (mopu:method-specializers method)))
              (:pre (documentation method 't))
              (:h2 "Argument list")
              (:p (write-to-string (closer-mop:method-lambda-list method))))))
      (let* ((help-buffer (next/help-mode:help-mode
                           :activate t
                           :buffer (make-buffer
                                    :title (str:concat "*Help-"
                                                       (symbol-name input)
{'fraction_non_alphanumeric': 0.12424070624443184, 'fraction_numerical': 0.0012958613428363166, 'mean_word_length': 1.6006739679865207, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 55, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '607417', 'n_tokens_mistral': 3231, 'n_tokens_neox': 3079, 'n_words': 816}