Minor work and documentation.
This commit is contained in:
parent
f9e9f91663
commit
0b0c2330ec
46
pretty.lua
46
pretty.lua
|
@ -4,6 +4,38 @@
|
|||
|
||||
-- TODO: Maybe move table formatting into its own file?
|
||||
|
||||
|
||||
--[=[ Thoughts on displaying tables in an intuitive way.
|
||||
|
||||
Lua's table datastructure is likely to be the most consise data structure ever
|
||||
invented. (If not, please send me a link!) Lists, maps, objects, classes,
|
||||
proxies, etc. This obviously brings about it some difficulty when attempting to
|
||||
represent these tables. What do we want to highlight, and what do we choose to
|
||||
avoid?
|
||||
|
||||
One notable issue is whether to show every key that a table answers to, or to
|
||||
just display those it contains. That is, do we think about `__index` and what it
|
||||
returns, or do we ignore `__index`? For cases where `__index` is a function,
|
||||
we cannot say anything about the keys that the table answers to. If `__index` is
|
||||
a table, we have a better idea, but then the output would be cluttered.
|
||||
|
||||
1. Native representation: Lua's native representation includes the type and
|
||||
address of the table. It allows for distinguishing between unique tables,
|
||||
but won't tell about the contents.
|
||||
2. It continues: By representing tables as the pseudo-parsable `{...}`, it's
|
||||
clear we are talking about a table. We disregard the ability to
|
||||
distinguish between tables.
|
||||
3. Special case for 2. If the table is empty, we could represent it as `{}`.
|
||||
But what if the table has a metatable with `__index` defined? We could
|
||||
continue to represent it as `{}`, but `{...}` would be more "honest".
|
||||
4. Single-line: TODO
|
||||
5. Multi-line: TODO
|
||||
6. ls style: TODO
|
||||
7. Tabular: TODO
|
||||
8. Special cases: (Array-tree, Table-Tree, Linked-List) TODO
|
||||
|
||||
--]=]
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
-- Ensure loading library, if it exists, no matter where pretty.lua was loaded from.
|
||||
|
@ -337,12 +369,12 @@ local function format_table (t, depth, l)
|
|||
local already_visited = l.visited[t]
|
||||
l.visited[t] = true
|
||||
|
||||
if table_info.type == TABLE_TYPE.EMPTY then
|
||||
-- Empty Map
|
||||
return l('{'..( l.options._table_addr_comment and (' --[['..table_info.address..']] ') or '')..'}')
|
||||
elseif depth >= l.options.max_depth or already_visited then
|
||||
-- Already visited or above max depth
|
||||
return l('{'..( l.options._table_addr_comment and (' --[['..table_info.address..']] ') or '')..'...}')
|
||||
-- If empty, visited or above max-depth, give a small represetation: `{...}`
|
||||
if table_info.type == TABLE_TYPE.EMPTY or depth >= l.options.max_depth or already_visited then
|
||||
l '{'
|
||||
if l.options._table_addr_comment then l[#l+1] = ' --[[' .. table_info.address .. ']] ' end
|
||||
if table_info.type ~= TABLE_TYPE.EMPTY then l[#l+1] = '...' end
|
||||
return l '}'
|
||||
end
|
||||
|
||||
-- Get key-value pairs, and possibly fill holes.
|
||||
|
@ -351,7 +383,7 @@ local function format_table (t, depth, l)
|
|||
fill_holes_in_key_value_pairs(key_value_pairs)
|
||||
end
|
||||
|
||||
-- Find format function
|
||||
-- Find pair formatting function
|
||||
local pair_format_func = TABLE_TYPE_TO_PAIR_FORMAT[table_info.type]
|
||||
local start_of_table_i = #l + 1
|
||||
assert(pair_format_func)
|
||||
|
|
|
@ -79,13 +79,13 @@ format_test {
|
|||
expect = '{ 1, nil, 3 }',
|
||||
}
|
||||
|
||||
if HAS_UNICODE_IDEN then
|
||||
--[[ Sorting is hard in unicode, and I can't be bothered.
|
||||
format_test {
|
||||
name = 'øl kommer tydeligt før ål',
|
||||
name = 'Unicode: ø comes before å in danish',
|
||||
input = loadstring 'return { øl = 1, ål = 2 }' (),
|
||||
expect = '{ øl = 1, ål = 2 }',
|
||||
}
|
||||
end
|
||||
--]]
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
-- Alphanum Algorithm Example 1
|
||||
|
|
Loading…
Reference in New Issue
Block a user