branch: externals/tomelr commit a1f434f03a761c50cd9813e27d5441d6b2c2902d Author: Kaushal Modi <kaushal.m...@gmail.com> Commit: Kaushal Modi <kaushal.m...@gmail.com>
feat: Support nested TOML tables --- README.org | 15 +++++++++++---- test/ttable.el | 18 ++++++++++++++++++ tomelr.el | 30 ++++++++++++++++++++++++------ 3 files changed, 53 insertions(+), 10 deletions(-) diff --git a/README.org b/README.org index 2acd5d41be..6a43d4df76 100644 --- a/README.org +++ b/README.org @@ -18,7 +18,7 @@ the Emacs core library [[https://git.savannah.gnu.org/cgit/emacs.git/tree/lisp/j It will then be gradually refactored so that it meets the specification defined below. -* Library Completion Status [4/7] +* Library Completion Status [5/7] - [X] Scalar - [X] Boolean - [X] Integer @@ -29,9 +29,9 @@ specification defined below. - [X] Nil - [X] Arrays - [X] Array of Arrays -- [-] Tables +- [X] Tables - [X] Basic Tables - - [ ] Nested Tables + - [X] Nested Tables - [ ] Array of Tables - [ ] Basic Array of Tables - [ ] Nested Array of Tables @@ -436,7 +436,8 @@ CLOSED: [2022-04-29 Fri 13:41] } } #+end_example -*** Nested TOML Tables +*** DONE Nested TOML Tables +CLOSED: [2022-04-29 Fri 14:30] **** S-expression #+begin_src emacs-lisp :eval no :noweb-ref nested-tables '((table-1 . ((table-1a . ((key1 . "some string") @@ -445,6 +446,12 @@ CLOSED: [2022-04-29 Fri 13:41] (key2 . 98765)))))) #+end_src **** TOML +#+begin_src emacs-lisp :noweb yes :exports results :wrap src toml +(tomelr-encode + <<nested-tables>>) +#+end_src + +#+RESULTS: #+begin_src toml [table-1] [table-1.table-1a] diff --git a/test/ttable.el b/test/ttable.el index fdcafa87f4..3898b187cd 100644 --- a/test/ttable.el +++ b/test/ttable.el @@ -41,5 +41,23 @@ (push (tomelr-encode el) out)) (should (equal ref (nreverse out))))) +;;;; Nested tables +(ert-deftest test-nested-table () + (let ((inp '(((table-1 . ((table-1a . ((key1 . "some string") + (key2 . 123))) + (table-1b . ((key1 . "foo") + (key2 . 98765)))))))) + (ref '("[table-1] + [table-1.table-1a] + key1 = \"some string\" + key2 = 123 + [table-1.table-1b] + key1 = \"foo\" + key2 = 98765")) + out) + (dolist (el inp) + (push (tomelr-encode el) out)) + (should (equal ref (nreverse out))))) + (provide 'ttable) diff --git a/tomelr.el b/tomelr.el index 4cbfbe45dc..55b3b07d73 100644 --- a/tomelr.el +++ b/tomelr.el @@ -52,6 +52,9 @@ ordered alphabetically.") "Current indentation level during encoding. Dictates repetitions of `tomelr-encoding-default-indentation'.") +(defvar tomelr--print-table-hierarchy () + "Internal variable used to save the TOML table hierarchy.") + (defvar tomelr--print-keyval-separator " = " "String used to separate key-value pairs during encoding.") @@ -211,12 +214,27 @@ Return nil if OBJECT cannot be encoded as a TOML string." ;; (message "[tomelr--print-stringlike DBG] %S is keyword" object) (tomelr--print-string (symbol-name object) 1)) ((symbolp object) - ;; (message "[tomelr--print-stringlike DBG] %S is symbol" object) - (cond - ((equal type 'table) - (princ (format "[%s]" (symbol-name object)))) - (t - (princ (symbol-name object))))))) + (let ((sym-name (symbol-name object))) + ;; (message "[tomelr--print-stringlike DBG] %S is symbol, type = %S, depth = %d" + ;; object type tomelr--print-indentation-depth) + (cond + ((equal type 'table) + (if (null (nth tomelr--print-indentation-depth tomelr--print-table-hierarchy)) + (progn + (push sym-name tomelr--print-table-hierarchy) + (setq tomelr--print-table-hierarchy (nreverse tomelr--print-table-hierarchy))) + ;; Throw away table keys collected at higher depths, if + ;; any, from earlier runs of this function. + (setq tomelr--print-table-hierarchy + (seq-take tomelr--print-table-hierarchy + (1+ tomelr--print-indentation-depth))) + (setf (nth tomelr--print-indentation-depth tomelr--print-table-hierarchy) + sym-name)) + ;; (message "[tomelr--print-stringlike DBG] table hier: %S" + ;; tomelr--print-table-hierarchy) + (princ (format "[%s]" (string-join tomelr--print-table-hierarchy ".")))) + (t + (princ sym-name))))))) (defun tomelr--print-key (key &optional type) "Insert a TOML key representation of KEY at point.