nvim-config

Log | Files | Refs | Submodules | README

shadautls.lua (1414B)


      1 local M = {}
      2 
      3 local function iter_packed(str)
      4   local unpacker = vim.mpack.Unpacker()
      5   local current = 1
      6   return function()
      7     if current > #str then
      8       return
      9     end
     10 
     11     local res, newoff = unpacker(str, current)
     12     current = newoff
     13     return res
     14   end
     15 end
     16 
     17 local function get_path()
     18   return vim.fn.stdpath "state" .. "/shada/main.shada"
     19 end
     20 
     21 local function shada_iterator()
     22   local path = get_path()
     23   local content = io.open(path):read "*a"
     24   local mpack_iter = iter_packed(content)
     25   return function()
     26     -- see |shada-format| to get the actual format of this
     27     local type = mpack_iter()
     28     local timestamp = mpack_iter()
     29     -- Drop the length of the entry
     30     mpack_iter()
     31     local data = mpack_iter()
     32 
     33     if type and timestamp and data then
     34       return type, timestamp, data
     35     else
     36       return nil, nil, nil
     37     end
     38   end
     39 end
     40 
     41 function M.read()
     42   local path = get_path()
     43   local content = io.open(path):read "*a"
     44   local acc = {}
     45   for thing in iter_packed(content) do
     46     acc[#acc + 1] = thing
     47   end
     48   return acc
     49 end
     50 
     51 function M.mark_for_path(path, mname)
     52   local timenow = vim.fn.localtime()
     53   mname = vim.fn.char2nr(mname or '"')
     54 
     55   for type, timestamp, data in shada_iterator() do
     56     if type == 10 and data.f == path and (data.n or 34) == mname then
     57       return {
     58         time = timenow - timestamp,
     59         line = data.l,
     60         col = data.c
     61       }
     62     end
     63   end
     64 end
     65 
     66 return M