From 9aecd3f084018b6c08c42f21ae9c1538ac254810 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kan=20Th=C3=B6rngren?= Date: Tue, 14 May 2019 04:46:49 -0700 Subject: [PATCH] feat(pretty) force ordered output in pretty.write (#293) Having consistent output makes it easier to use it for test purposes, such as when dumping tables and comparing it to some expected output. --- lua/pl/pretty.lua | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/lua/pl/pretty.lua b/lua/pl/pretty.lua index a9abe0ea..fbf60a9b 100644 --- a/lua/pl/pretty.lua +++ b/lua/pl/pretty.lua @@ -248,7 +248,14 @@ function pretty.write (tbl,space,not_clever) used[i] = true end end - for key,val in pairs(t) do + local ordered_keys = {} + for k,v in pairs(t) do + if type(k) ~= 'number' then + ordered_keys[#ordered_keys + 1] = k + end + end + table.sort(ordered_keys) + local function write_entry (key, val) local tkey = type(key) local numkey = tkey == 'number' if not_clever then @@ -268,6 +275,16 @@ function pretty.write (tbl,space,not_clever) end end end + for i = 1, #ordered_keys do + local key = ordered_keys[i] + local val = t[key] + write_entry(key, val) + end + for key,val in pairs(t) do + if type(key) == 'number' then + write_entry(key, val) + end + end tables[t] = nil eat_last_comma() putln(oldindent..'},')