~niklaseklund/egerrit

d608c5f60f5409ef4e7a63670593a8790816006a — Niklas Eklund 3 months ago 7bb7f4f
Introduce a config for info buffer
1 files changed, 65 insertions(+), 40 deletions(-)

M egerrit-info.el
M egerrit-info.el => egerrit-info.el +65 -40
@@ 37,51 37,76 @@

(defvar egerrit--current-change)

;;;; Variables

(defcustom egerrit-info-config
  '(egerrit--change-info-section
    egerrit--requirement-section
    egerrit--relation-section)
  "This list describes section functions for the info buffer."
  :type '(repeat symbol)
  :group 'egerrit)

;;;; Functions

(defun egerrit--info-str (change revision)
  "Generate an info string about CHANGE's REVISION."
  (let ((info-heading "Change info")
        (requirements-heading "Submit requirements")
        (relation-heading "Relation chain")
(defun egerrit--change-info-section (change _revision)
  "Return change info section for CHANGE."
  (let ((heading "Change info")
        (has-merge-conflicts
         (not (egerrit-get-mergeable change)))
        (merge-conflict-str "Merge Conflict"))

    (put-text-property 0 (length heading) 'face 'egerrit-heading-face heading)
    (put-text-property 0 (length merge-conflict-str) 'face 'egerrit-inv-error-face merge-conflict-str)

    `(,(format "%s\n\n" heading)
      ,(when has-merge-conflicts
         (format "%s\n" merge-conflict-str))
      ,(egerrit--info-entry-str "Subject" (egerrit--subject-str change))
      ,(egerrit--info-entry-str "Owner" (egerrit--owner-str change))
      ,(egerrit--info-entry-str "Reviewers"
                                (string-join (seq-map
                                              (lambda (it)
                                                (alist-get 'name it))
                                              (alist-get 'REVIEWER (egerrit--change-reviewers change)))
                                             ", ")
                                'egerrit-unknown-face)
      ,(egerrit--info-entry-str "CC"
                                (string-join (seq-map
                                              (lambda (it)
                                                (alist-get 'name it))
                                              (alist-get 'CC (egerrit--change-reviewers change)))
                                             ", ")
                                'egerrit-unknown-face))))

(defun egerrit--requirement-section (change _revision)
  "Return requirement section for CHANGE."
  (let ((heading "Submit requirements"))

    (put-text-property 0 (length heading) 'face 'egerrit-heading-face heading)

    `(,(format "\n%s\n\n" heading)
      ,(format "%s\n" (egerrit--change-requirements-str change)))))

(defun egerrit--relation-section (change revision)
  "Return the relation section for CHANGE's REVISION."
  (let ((heading "Relation chain")
        (related-changes
         (egerrit-get-related-changes change revision))
        (content))

    (put-text-property 0 (length info-heading) 'face 'egerrit-heading-face info-heading)
    (put-text-property 0 (length relation-heading) 'face 'egerrit-heading-face relation-heading)
    (put-text-property 0 (length requirements-heading) 'face 'egerrit-heading-face requirements-heading)

    (setq content
          `(,(format "%s\n\n" info-heading)
            ,(when has-merge-conflicts
               (let ((str "Merge Conflict"))
                 (put-text-property 0 (length str) 'face 'egerrit-inv-error-face str)
                 (format "%s\n" str)))
            ,(egerrit--info-entry-str "Subject" (egerrit--subject-str change))
            ,(egerrit--info-entry-str "Owner" (egerrit--owner-str change))
            ,(egerrit--info-entry-str "Reviewers"
                                      (string-join (seq-map
                                                    (lambda (it)
                                                      (alist-get 'name it))
                                                    (alist-get 'REVIEWER (egerrit--change-reviewers change)))
                                                   ", ")
                                      'egerrit-unknown-face)
            ,(egerrit--info-entry-str "CC"
                                      (string-join (seq-map
                                                    (lambda (it)
                                                      (alist-get 'name it))
                                                    (alist-get 'CC (egerrit--change-reviewers change)))
                                                   ", ")
                                      'egerrit-unknown-face)
            ,(format "\n%s\n\n" requirements-heading)
            ,(format "%s" (egerrit--change-requirements-str change))
            ,(when related-changes
               (format "\n%s\n\n%s\n" relation-heading (string-join related-changes "\n")))))

    (string-join (seq-remove #'null content) "")))
         (egerrit-get-related-changes change revision)))

    (put-text-property 0 (length heading) 'face 'egerrit-heading-face heading)

    (when related-changes
      `(,(format "\n%s\n\n%s\n" heading (string-join related-changes "\n"))))))

(defun egerrit--info-str (change revision)
  "Generate an info string about CHANGE's REVISION."
  (string-join
   (thread-last egerrit-info-config
                (seq-map (lambda (it) (funcall it change revision)))
                (seq-remove #'null)
                (flatten-list))
   ""))

(defun egerrit-get-related-changes (change revision)
  "Get a list of related change(s) for CHANGE's REVISION."