Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Could beancount-completion-at-point be possible to complete the current bean buffer with contents that are included by the include command? #58

Open
pinacle2000 opened this issue Nov 14, 2024 · 4 comments

Comments

@pinacle2000
Copy link

          Any update on this feature? I love the work beancount-completion-at-point does for the current bean buffer. Is it possible to complete the current bean buffer with contents that are included by the include command?

For example, if my.bean file has a head as below:

`include "account.bean"

include "old.bean"
`
then when I edit my.bean, I could get completion from account.bean and old.bean.

then I can e

Originally posted by @pinacle2000 in #24 (comment)

@dnicolodi
Copy link
Collaborator

I'm happy to review patches that implement it

@pinacle2000
Copy link
Author

I am not familiar with elisp... I tried with help from AI, but it didn't work. Following is what AI suggests for getting account from include files. Don't know whether it makes some sense:

(defun beancount-account-completion-table (string pred action)
  (if (eq action 'metadata) '(metadata (category . beancount-account))
    (let ((account-names (beancount-get-accounts-from-included-files)))
      (complete-with-action action account-names string pred))))

(defun beancount-get-accounts-from-included-files ()
  "Get all accounts from the current .bean file and included files."
  (let* ((current-file (buffer-file-name))
         (dir (file-name-directory current-file))
         (included-files (beancount-find-included-files current-file dir))
         (all-accounts (beancount-extract-accounts-from-file current-file)))
    (dolist (file included-files all-accounts)
      (setq all-accounts (append all-accounts (beancount-extract-accounts-from-file file))))))

(defun beancount-find-included-files (file dir)
  "Find all files included in the given .bean file."
  (with-temp-buffer
    (insert-file-contents file)
    (let (files)
      (while (re-search-forward "^\\s-*include\\s-+\"\$.*?\$\"\\s-*$" nil t)
        (let ((included-file (expand-file-name (match-string 1) dir)))
          (when (file-exists-p included-file)
            (push included-file files))))
      (nreverse files))))

(defun beancount-extract-accounts-from-file (file)
  "Extract all account names from the given .bean file using open directives."
  (with-temp-buffer
    (insert-file-contents file)
    (beancount-extract-accounts)))

(defun beancount-extract-accounts ()
  "Extract all account names from the current buffer using open directives."
  (save-excursion
    (goto-char (point-min))
    (let (accounts)
      (while (re-search-forward "^\\s-*open\\s-+\$.*?\$\\s-*$" nil t)
        (push (match-string 1) accounts))
      (nreverse accounts))))

@pinacle2000
Copy link
Author

I tried another AI but still failed. The way AI does is create a temp buffer and put the included bean files there:

(defun beancount-extract-includes ()
  "Extract all include statements from the current buffer."
  (let (includes)
    (save-excursion
      (goto-char (point-min))
      (while (re-search-forward "^include \"\\([^\"]+\\)\"" nil t)
        (push (match-string-no-properties 1) includes)))
    includes))

(defun beancount-load-includes (includes)
  "Load all included files and return their combined content."
  (let ((combined-content ""))
    (dolist (include includes)
      (with-temp-buffer
        (insert-file-contents include)
        (setq combined-content (concat combined-content (buffer-string)))))
    combined-content))

(defun beancount-completion-at-point-with-includes ()
  "Perform completion at point, including content from included files."
  (let* ((includes (beancount-extract-includes))
         (included-content (beancount-load-includes includes))
         (all-content (concat (buffer-string) included-content)))
    (with-temp-buffer
      (insert all-content)
      (beancount-completion-at-point))))

@blais
Copy link
Member

blais commented Nov 14, 2024

Hey @pinacle2000, I'm not against using AIs to generate code -- this is the new normal, I get it -- but you have to understand what it's doing. What's more, there's an unwritten expectation you should be able to maintain that part of the code moving forward, i.e., if there's a bug in that new part you added it might get assigned to you (or the patch reverted). We all can get Claude to spit out some code, that's not difficult, but it's not sufficient, it has to be code you agree with how it works (that is, you can read and understand it). We're not going to blindly merge patches of code spit out by AI. If you can't get it to work, how do you expect to debug issues with it later? (Or do you expect us to debug it?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants