robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Lewis RussellMerge pull request #38486 from lewis6991/testharnes9e80f63c30

master
4.0 KiB166 linesraw
1local M = {}
2
3local SUBTBL = {
4  '\\000',
5  '\\001',
6  '\\002',
7  '\\003',
8  '\\004',
9  '\\005',
10  '\\006',
11  '\\007',
12  '\\008',
13  '\\t',
14  '\\n',
15  '\\011',
16  '\\012',
17  '\\r',
18  '\\014',
19  '\\015',
20  '\\016',
21  '\\017',
22  '\\018',
23  '\\019',
24  '\\020',
25  '\\021',
26  '\\022',
27  '\\023',
28  '\\024',
29  '\\025',
30  '\\026',
31  '\\027',
32  '\\028',
33  '\\029',
34  '\\030',
35  '\\031',
36}
37
38--- @param v any
39--- @return string
40local function format_float(v)
41  -- On windows exponent appears to have three digits and not two
42  local ret = ('%.6e'):format(v)
43  local l, f, es, e = ret:match('^(%-?%d)%.(%d+)e([+%-])0*(%d%d+)$')
44  return l .. '.' .. f .. 'e' .. es .. e
45end
46
47-- Formats Lua value `v`.
48--
49-- TODO(justinmk): redundant with vim.inspect() ?
50--
51-- "Nice table formatting similar to screen:snapshot_util()".
52-- Commit: 520c0b91a528
53function M.format_luav(v, indent, opts)
54  opts = opts or {}
55  local linesep = '\n'
56  local next_indent_arg = nil
57  local indent_shift = opts.indent_shift or '  '
58  local next_indent
59  local nl = '\n'
60  if indent == nil then
61    indent = ''
62    linesep = ''
63    next_indent = ''
64    nl = ' '
65  else
66    next_indent_arg = indent .. indent_shift
67    next_indent = indent .. indent_shift
68  end
69  local ret = ''
70  if type(v) == 'string' then
71    if opts.literal_strings then
72      ret = v
73    else
74      local quote = opts.dquote_strings and '"' or "'"
75      ret = quote
76        .. tostring(v)
77          :gsub(opts.dquote_strings and '["\\]' or "['\\]", '\\%0')
78          :gsub('[%z\1-\31]', function(match)
79            return SUBTBL[match:byte() + 1]
80          end)
81        .. quote
82    end
83  elseif type(v) == 'table' then
84    if v == vim.NIL then
85      ret = 'REMOVE_THIS'
86    else
87      local processed_keys = {}
88      ret = '{' .. linesep
89      local non_empty = false
90      local format_luav = M.format_luav
91      for i, subv in ipairs(v) do
92        ret = ('%s%s%s,%s'):format(ret, next_indent, format_luav(subv, next_indent_arg, opts), nl)
93        processed_keys[i] = true
94        non_empty = true
95      end
96      for k, subv in pairs(v) do
97        if not processed_keys[k] then
98          if type(k) == 'string' and k:match('^[a-zA-Z_][a-zA-Z0-9_]*$') then
99            ret = ret .. next_indent .. k .. ' = '
100          else
101            ret = ('%s%s[%s] = '):format(ret, next_indent, format_luav(k, nil, opts))
102          end
103          ret = ret .. format_luav(subv, next_indent_arg, opts) .. ',' .. nl
104          non_empty = true
105        end
106      end
107      if nl == ' ' and non_empty then
108        ret = ret:sub(1, -3)
109      end
110      ret = ret .. indent .. '}'
111    end
112  elseif type(v) == 'number' then
113    if v % 1 == 0 then
114      ret = ('%d'):format(v)
115    else
116      ret = format_float(v)
117    end
118  elseif type(v) == 'nil' then
119    ret = 'nil'
120  elseif type(v) == 'boolean' then
121    ret = (v and 'true' or 'false')
122  else
123    print(type(v))
124    -- Not implemented yet
125    assert(false)
126  end
127  return ret
128end
129
130-- Like Python repr(), "{!r}".format(s)
131--
132-- Commit: 520c0b91a528
133function M.format_string(fmt, ...)
134  local i = 0
135  local args = { ... }
136  local function getarg()
137    i = i + 1
138    return args[i]
139  end
140  local ret = fmt:gsub('%%[0-9*]*%.?[0-9*]*[cdEefgGiouXxqsr%%]', function(match)
141    local subfmt = match:gsub('%*', function()
142      return tostring(getarg())
143    end)
144    local arg = nil
145    if subfmt:sub(-1) ~= '%' then
146      arg = getarg()
147    end
148    if subfmt:sub(-1) == 'r' or subfmt:sub(-1) == 'q' then
149      -- %r is like built-in %q, but it is supposed to single-quote strings and
150      -- not double-quote them, and also work not only for strings.
151      -- Builtin %q is replaced here as it gives invalid and inconsistent with
152      -- luajit results for e.g. "\e" on lua: luajit transforms that into `\27`,
153      -- lua leaves as-is.
154      arg = M.format_luav(arg, nil, { dquote_strings = (subfmt:sub(-1) == 'q') })
155      subfmt = subfmt:sub(1, -2) .. 's'
156    end
157    if subfmt == '%e' then
158      return format_float(arg)
159    else
160      return subfmt:format(arg)
161    end
162  end)
163  return ret
164end
165
166return M