Skip to content

Commit

Permalink
fix: Support TOML tables specified as plists
Browse files Browse the repository at this point in the history
  • Loading branch information
kaushalmodi committed Apr 29, 2022
1 parent ad8366d commit 4c419bc
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 39 deletions.
16 changes: 12 additions & 4 deletions test/tplist.el
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,14 @@
:str_list ("a" "b" "c")
:bool_list (t :false t :false)
:list_of_lists [(1 2) (3 4 5)]
;; TODO plist specification of TOML tables is not yet supported.
;; :map (:key1 123
;; :key2 "xyz")
:map (:key1 123
:key2 "xyz")
;; TODO plist specification of TOML tables arrays is
;; not yet supported.
;; :list_of_maps [(:key1 123
;; :key2 "xyz")
;; (:key1 567
;; :key2 "klm")]
)))
(ref '("int = 123
str = \"abc\"
Expand All @@ -49,7 +54,10 @@ bool_true = true
int_list = [ 1, 2, 3 ]
str_list = [ \"a\", \"b\", \"c\" ]
bool_list = [ true, false, true, false ]
list_of_lists = [ [ 1, 2 ], [ 3, 4, 5 ] ]"))
list_of_lists = [ [ 1, 2 ], [ 3, 4, 5 ] ]
[map]
key1 = 123
key2 = \"xyz\""))
out)
(dolist (el inp)
(push (tomelr-encode el) out))
Expand Down
76 changes: 41 additions & 35 deletions tomelr.el
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

;;; Code:

(require 'json)
(require 'map)
(require 'subr-x) ;For `string-trim' on Emacs versions 27.2 and older

Expand Down Expand Up @@ -208,43 +209,46 @@ returns the TOML representation as a string."
(defun tomelr--print-stringlike (object &optional type)
"Insert OBJECT encoded as a TOML string at point.
TYPE is set to `table' if OBJECT is a TOML Table key.
Possible value of TYPE are `table', `table-array' or nil.
Return nil if OBJECT cannot be encoded as a TOML string."
(cond ((stringp object)
;; (message "[tomelr--print-stringlike DBG] %S is string" object)
(tomelr--print-string object))
((keywordp object) ;Symbol beginning with `:', like `:some_key'
;; (message "[tomelr--print-stringlike DBG] %S is keyword" object)
(tomelr--print-string
(string-trim-left (symbol-name object) ":")
'keyword))
((symbolp 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 "."))))
((equal type 'table-array)
(let ((tta-name (format "[[%s]]" sym-name)))
(setq tomelr--print-table-array-key tta-name)
(princ tta-name)))
(t
(princ sym-name)))))))
(let ((sym-name (cond ((stringp object)
object)
;; Symbol beginning with `:', like `:some_key'
((keywordp object)
(string-trim-left (symbol-name object) ":"))
((symbolp object)
(symbol-name object)))))
(cond
((equal type 'table)
;; (message "[tomelr--print-stringlike DBG] %S is symbol, type = %S, depth = %d"
;; object type tomelr--print-indentation-depth)
(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 "."))))
((equal type 'table-array)
(let ((tta-name (format "[[%s]]" sym-name)))
(setq tomelr--print-table-array-key tta-name)
(princ tta-name)))
((stringp object)
;; (message "[tomelr--print-stringlike DBG] %S is string" object)
(tomelr--print-string sym-name))
((keywordp object)
;; (message "[tomelr--print-stringlike DBG] %S is keyword" object)
(tomelr--print-string sym-name 'keyword))
(sym-name
(princ sym-name)))))

(defun tomelr--print-key (key &optional type)
"Insert a TOML key representation of KEY at point.
Expand Down Expand Up @@ -273,6 +277,8 @@ Definition of a TOML Table (TT):
;; (car object) (type-of (car object)))
(setq tablep
(cond
((json-plist-p object)
t)
((seq-every-p
;; Ensure that every element in the `object' is a (KEY
;; . VAL) kind of cons.
Expand Down

0 comments on commit 4c419bc

Please sign in to comment.