Skip to content

Commit 17983be

Browse files
committed
Date completion: Add trailing space even if point was inside incomplete date
1 parent d0afe36 commit 17983be

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

ledger-complete.el

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ an alist (ACCOUNT-ELEMENT . NODE)."
224224
(defvar ledger-complete--current-time-for-testing nil
225225
"Internal, used for testing only.")
226226

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

252252
(defun ledger-complete-effective-date
253253
(tx-year-string tx-month-string tx-day-string
254-
month-string day-string)
254+
month-string day-string
255+
date-at-eol-p)
255256
"Complete an effective date."
256257
(let* ((tx-year (string-to-number tx-year-string))
257258
(tx-month (string-to-number tx-month-string))
@@ -270,7 +271,7 @@ an alist (ACCOUNT-ELEMENT . NODE)."
270271
(let ((collection
271272
(list (concat (ledger-format-date
272273
(cl-find-if (lambda (date) (not (time-less-p date tx-date))) dates))
273-
(and (= (point) (line-end-position)) " ")))))
274+
(when date-at-eol-p " ")))))
274275
(lambda (string predicate action)
275276
(if (eq action 'metadata)
276277
'(metadata (category . ledger-date))
@@ -286,7 +287,9 @@ an alist (ACCOUNT-ELEMENT . NODE)."
286287
(save-excursion
287288
(skip-chars-forward "0-9/-")
288289
(looking-back (concat "^" ledger-incomplete-date-regexp) (line-beginning-position)))
289-
(setq collection (ledger-complete-date (match-string 1) (match-string 2))
290+
(setq collection (ledger-complete-date (match-string 1)
291+
(match-string 2)
292+
(= (line-end-position) (match-end 0)))
290293
start (match-beginning 0)
291294
delete-suffix (save-match-data
292295
(when (looking-at (rx (one-or-more (or digit (any ?/ ?-)))))
@@ -299,7 +302,8 @@ an alist (ACCOUNT-ELEMENT . NODE)."
299302
(setq start (line-beginning-position))
300303
(setq collection (ledger-complete-effective-date
301304
(match-string 2) (match-string 3) (match-string 4)
302-
(match-string 5) (match-string 6))))
305+
(match-string 5) (match-string 6)
306+
(= (line-end-position) (match-end 0)))))
303307
(;; Payees
304308
(eq 'transaction
305309
(save-excursion

test/complete-test.el

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -417,9 +417,8 @@ https://github.com/ledger/ledger-mode/issues/419"
417417
;; completion uses whole day number, not just the part before point
418418
(completion-at-point)
419419
(should
420-
;; TODO: Ideally, this should include a trailing space too.
421420
(equal (buffer-string)
422-
"2023-12-23")))))
421+
"2023-12-23 ")))))
423422

424423
(provide 'complete-test)
425424

0 commit comments

Comments
 (0)