vermiculus / sx.el

Stack Exchange for Emacs
http://stackapps.com/q/3950
709 stars 40 forks source link

Answers are not displayed after sorting #358

Closed dvzubarev closed 5 years ago

dvzubarev commented 5 years ago
  1. open a question (e.g. https://stackoverflow.com/questions/1912434/how-do-i-parse-xml-in-python)
  2. there should be 14 answers
  3. invoke sx-question-mode-order-by ( O )
  4. choose e.g. "newer first"
  5. accepted answer is not in the buffer now
  6. invoke sx-question-mode-order-by again and choose "More active first"
  7. only one answer is displayed now.
vermiculus commented 5 years ago

Can you try this definition of sx-question-mode--print-question? I think the bug is in this use of sx--deleted-p.

(defun sx-question-mode--print-question (question)
  "Print a buffer describing QUESTION.
QUESTION must be a data structure returned by `json-read'."
  (when (sx--deleted-p question)
    (sx-user-error "This is a deleted question"))
  (setq sx-question-mode--data question)
  ;; Clear the overlays
  (mapc #'delete-overlay sx--overlays)
  (setq sx--overlays nil)
  ;; Print everything
  (sx-assoc-let question
    (when .closed_reason
      (add-to-list 'mode-line-format sx-question-mode--closed-mode-line-string)
      (sx-question-mode--print-close-reason .closed_reason .closed_date .closed_details))
    (sx-question-mode--print-section question)
    (mapc #'sx-question-mode--print-section
          (cl-sort .answers sx-question-mode-answer-sort-function)))
  (insert "\n\n                       ")
  (insert-text-button "Write an Answer" :type 'sx-button-answer)
  (run-hooks 'sx-question-mode-after-print-hook)
  ;; Go up
  (goto-char (point-min))
  (sx-question-mode-next-section))
dvzubarev commented 5 years ago

Unfortunately, this code didn't solve the problem. I noticed that some answers are removed from the list after cl-sort. I'm not sure why but making a copy of answers before sorting, fixes the problem.

          (cl-sort (copy-sequence .answers)
                   sx-question-mode-answer-sort-function))
vermiculus commented 5 years ago

Hm, is cl-sort destructive? That would be surprising. I can try to look tonight.

dvzubarev commented 5 years ago

Yes, cl-sort is destructive, from the docs This is a destructive function; it reuses the storage of SEQ if possible. I can make a PR, if you want.

vermiculus commented 5 years ago

Re cl-sort, yes, but I imagine it would still return the sorted list. Ahh, except that .answers is direct reference to the question object! It's not sorting it the second time that's the problem, it's sorting it the first time!

Does this patch (also in the PR above) work for you?

@@ -244,7 +244,7 @@ QUESTION must be a data structure returned by `json-read'."
     (mapc #'sx-question-mode--print-section
           (cl-remove-if
            #'sx--deleted-p
-           (cl-sort .answers sx-question-mode-answer-sort-function))))
+           (sort .answers sx-question-mode-answer-sort-function))))
   (insert "\n\n                       ")
   (insert-text-button "Write an Answer" :type 'sx-button-answer)
   (run-hooks 'sx-question-mode-after-print-hook)

Note I left in the check for #'sx--deleted-p; it shouldn't be doing any harm. A separate patch could make it work for answers here (assuming I'm correct and that it's not working right now).

dvzubarev commented 5 years ago

It seems that sort is destructive too. https://www.emacswiki.org/emacs/DestructiveOperations So it doesn't work.

vermiculus commented 5 years ago

Damn I need to read the docs more carefully.

Ok, we'll just copy the sequence before as you suggested.

vermiculus commented 5 years ago

Should be fixed once MELPA updates in a few hours