Skip to content

Commit

Permalink
Date completion: Add trailing space even if point was inside incomple…
Browse files Browse the repository at this point in the history
…te date
  • Loading branch information
bcc32 committed Jul 9, 2024
1 parent d0afe36 commit 17983be
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
16 changes: 10 additions & 6 deletions ledger-complete.el
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ an alist (ACCOUNT-ELEMENT . NODE)."
(defvar ledger-complete--current-time-for-testing nil
"Internal, used for testing only.")

(defun ledger-complete-date (month-string day-string)
(defun ledger-complete-date (month-string day-string date-at-eol-p)
"Complete a date."
(let* ((now (or ledger-complete--current-time-for-testing (current-time)))
(decoded (decode-time now))
Expand All @@ -243,15 +243,16 @@ an alist (ACCOUNT-ELEMENT . NODE)."
(let ((collection
(list (concat (ledger-format-date
(cl-find-if (lambda (date) (not (time-less-p now date))) dates))
(and (= (point) (line-end-position)) " ")))))
(when date-at-eol-p " ")))))
(lambda (string predicate action)
(if (eq action 'metadata)
'(metadata (category . ledger-date))
(complete-with-action action collection string predicate))))))

(defun ledger-complete-effective-date
(tx-year-string tx-month-string tx-day-string
month-string day-string)
month-string day-string
date-at-eol-p)
"Complete an effective date."
(let* ((tx-year (string-to-number tx-year-string))
(tx-month (string-to-number tx-month-string))
Expand All @@ -270,7 +271,7 @@ an alist (ACCOUNT-ELEMENT . NODE)."
(let ((collection
(list (concat (ledger-format-date
(cl-find-if (lambda (date) (not (time-less-p date tx-date))) dates))
(and (= (point) (line-end-position)) " ")))))
(when date-at-eol-p " ")))))
(lambda (string predicate action)
(if (eq action 'metadata)
'(metadata (category . ledger-date))
Expand All @@ -286,7 +287,9 @@ an alist (ACCOUNT-ELEMENT . NODE)."
(save-excursion
(skip-chars-forward "0-9/-")
(looking-back (concat "^" ledger-incomplete-date-regexp) (line-beginning-position)))
(setq collection (ledger-complete-date (match-string 1) (match-string 2))
(setq collection (ledger-complete-date (match-string 1)
(match-string 2)
(= (line-end-position) (match-end 0)))
start (match-beginning 0)
delete-suffix (save-match-data
(when (looking-at (rx (one-or-more (or digit (any ?/ ?-)))))
Expand All @@ -299,7 +302,8 @@ an alist (ACCOUNT-ELEMENT . NODE)."
(setq start (line-beginning-position))
(setq collection (ledger-complete-effective-date
(match-string 2) (match-string 3) (match-string 4)
(match-string 5) (match-string 6))))
(match-string 5) (match-string 6)
(= (line-end-position) (match-end 0)))))
(;; Payees
(eq 'transaction
(save-excursion
Expand Down
3 changes: 1 addition & 2 deletions test/complete-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -417,9 +417,8 @@ https://github.com/ledger/ledger-mode/issues/419"
;; completion uses whole day number, not just the part before point
(completion-at-point)
(should
;; TODO: Ideally, this should include a trailing space too.
(equal (buffer-string)
"2023-12-23")))))
"2023-12-23 ")))))

(provide 'complete-test)

Expand Down

0 comments on commit 17983be

Please sign in to comment.