diff --git a/.gitignore b/.gitignore index 1725e36..6fc14e7 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /.sandbox/ +**/*.elc diff --git a/org-roam-utils.el b/org-roam-utils.el index 6c929b2..a1bcf76 100644 --- a/org-roam-utils.el +++ b/org-roam-utils.el @@ -136,6 +136,33 @@ ITEM is of the form: (:from from-path :to to-path :content preview-content)." (org-element-property :value kw))) :first-match t)) +(defun org-roam--build-cache (dir) + "Build the org-roam caches in DIR." + (let ((backward-links (make-hash-table :test #'equal)) + (forward-links (make-hash-table :test #'equal)) + (file-titles (make-hash-table :test #'equal))) + (let* ((org-roam-files (org-roam--find-files dir)) + (file-items (mapcar (lambda (file) + (with-temp-buffer + (insert-file-contents file) + (org-roam--parse-content file))) org-roam-files))) + (dolist (items file-items) + (dolist (item items) + (org-roam--insert-item + item + :forward forward-links + :backward backward-links))) + (dolist (file org-roam-files) + (with-temp-buffer + (insert-file-contents file) + (when-let ((title (org-roam--extract-title))) + (puthash file title file-titles))) + org-roam-files)) + (list + :forward forward-links + :backward backward-links + :titles file-titles))) + (provide 'org-roam-utils) ;;; org-roam-utils.el ends here diff --git a/org-roam.el b/org-roam.el index f739da3..8350581 100644 --- a/org-roam.el +++ b/org-roam.el @@ -304,9 +304,9 @@ If not provided, derive the title from the file name." (org-roam--make-file absolute-file-path title-or-slug)) (find-file absolute-file-path))) -;;; Building the org-roam cache (asynchronously) +;;; Building the org-roam cache (defun org-roam--build-cache-async () - "Builds the cache asychronously, saving it into the org-roam caches." + "Builds the caches asychronously." (interactive) (async-start `(lambda () @@ -314,30 +314,7 @@ If not provided, derive the title from the file name." (package-initialize) (require 'org-roam-utils) ,(async-inject-variables "org-roam-directory") - (let ((backward-links (make-hash-table :test #'equal)) - (forward-links (make-hash-table :test #'equal)) - (file-titles (make-hash-table :test #'equal))) - (let* ((org-roam-files (org-roam--find-files org-roam-directory)) - (file-items (mapcar (lambda (file) - (with-temp-buffer - (insert-file-contents file) - (org-roam--parse-content file))) org-roam-files))) - (dolist (items file-items) - (dolist (item items) - (org-roam--insert-item - item - :forward forward-links - :backward backward-links))) - (mapcar (lambda (file) - (with-temp-buffer - (insert-file-contents file) - (when-let ((title (org-roam--extract-title))) - (puthash file title file-titles)))) - org-roam-files)) - (list - :forward forward-links - :backward backward-links - :titles file-titles))) + (org-roam--build-cache org-roam-directory)) (lambda (cache) (setq org-roam-forward-links-cache (plist-get cache :forward)) (setq org-roam-backward-links-cache (plist-get cache :backward)) @@ -346,6 +323,14 @@ If not provided, derive the title from the file name." (message "Org-roam cache built!")))) (defun org-roam--clear-cache () + "Clears all entries in the caches." + (interactive) + (setq org-roam-cache-initialized nil) + (setq org-roam-forward-links-cache (make-hash-table :test #'equal)) + (setq org-roam-backward-links-cache (make-hash-table :test #'equal)) + (setq org-roam-titles-cache (make-hash-table :test #'equal))) + +(defun org-roam--clear-file-from-cache () "Remove any related links to the file. This is equivalent to removing the node from the graph." @@ -372,7 +357,7 @@ This is equivalent to removing the node from the graph." (defun org-roam--update-cache () "Update org-roam caches for the current buffer file." (save-excursion - (org-roam--clear-cache) + (org-roam--clear-file-from-cache) ;; Insert into title cache (org-roam--update-cache-title) ;; Insert new items diff --git a/shell.nix b/shell.nix deleted file mode 100644 index 655b7be..0000000 --- a/shell.nix +++ /dev/null @@ -1,9 +0,0 @@ -let - pkgs = import {}; -in -pkgs.mkShell { - name = "docs"; - buildInput = with pkgs; [ - mkdocs - ]; -} diff --git a/tests/test-org-roam.el b/tests/test-org-roam.el index 084afae..b43362a 100644 --- a/tests/test-org-roam.el +++ b/tests/test-org-roam.el @@ -3,7 +3,7 @@ ;; Copyright (C) 2020 Jethro Kuan ;; Author: Jethro Kuan -;; Package-Requires: ((buttercup)) +;; Package-Requires: ((buttercup) (with-simulated-input)) ;; This program is free software; you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by @@ -27,17 +27,38 @@ ;;;; Requirements (require 'buttercup) +(require 'with-simulated-input) (require 'org-roam) (defun abs-path (file-path) (file-truename (expand-file-name file-path org-roam-directory))) -;;; Tests -(describe "org-roam" - (before-all - (setq org-roam-directory (file-truename (concat default-directory "tests/roam-files")))) +(defun org-roam--test-find-new-file (path) + (let ((path (abs-path path))) + (make-directory (file-name-directory path) t) + (find-file path))) - (it "org-roam--build-cache-async" +(defvar org-roam--tests-directory (file-truename (concat default-directory "tests/roam-files")) + "Directory containing org-roam test org files.") + +(defun org-roam--test-init () + (let ((original-dir org-roam--tests-directory) + (new-dir (expand-file-name (make-temp-name "org-roam") temporary-file-directory))) + (copy-directory original-dir new-dir) + (setq org-roam-directory new-dir))) + +(defun org-roam--test-build-cache () + "Builds the caches synchronously." + (let ((cache (org-roam--build-cache org-roam-directory))) + (setq org-roam-forward-links-cache (plist-get cache :forward)) + (setq org-roam-backward-links-cache (plist-get cache :backward)) + (setq org-roam-titles-cache (plist-get cache :titles)) + (setq org-roam-cache-initialized t))) + +;;; Tests +(describe "org-roam--build-cache-async" + (it "initializes correctly" + (org-roam--test-init) (expect org-roam-cache-initialized :to-be nil) (expect (hash-table-count org-roam-forward-links-cache) :to-be 0) (expect (hash-table-count org-roam-backward-links-cache) :to-be 0) @@ -89,3 +110,41 @@ (expect (gethash (abs-path "nested/f1.org") org-roam-titles-cache) :to-equal "Nested File 1") (expect (gethash (abs-path "nested/f2.org") org-roam-titles-cache) :to-equal "Nested File 2") (expect (gethash (abs-path "no-title.org") org-roam-titles-cache) :to-be nil))) + +(describe "org-roam-insert" + (before-each + (org-roam--test-init) + (org-roam--clear-cache) + (org-roam--test-build-cache)) + + (it "temp1 -> f1" + (let ((buf (org-roam--test-find-new-file "temp1.org"))) + (with-current-buffer buf + (with-simulated-input + "File SPC 1 RET" + (org-roam-insert)))) + (expect (buffer-string) :to-match (regexp-quote "file:f1.org"))) + + (it "temp2 -> nested/f1" + (let ((buf (org-roam--test-find-new-file "temp2.org"))) + (with-current-buffer buf + (with-simulated-input + "Nested SPC File SPC 1 RET" + (org-roam-insert)))) + (expect (buffer-string) :to-match (regexp-quote "file:nested/f1.org"))) + + (it "nested/temp3 -> f1" + (let ((buf (org-roam--test-find-new-file "nested/temp3.org"))) + (with-current-buffer buf + (with-simulated-input + "File SPC 1 RET" + (org-roam-insert)))) + (expect (buffer-string) :to-match (regexp-quote "file:../f1.org"))) + + (it "a/b/temp4 -> nested/f1" + (let ((buf (org-roam--test-find-new-file "a/b/temp4.org"))) + (with-current-buffer buf + (with-simulated-input + "Nested SPC File SPC 1 RET" + (org-roam-insert)))) + (expect (buffer-string) :to-match (regexp-quote "file:../../nested/f1.org"))))