feat: Add first working telescope WIP

This commit is contained in:
Myzel394 2024-04-11 15:14:12 +02:00
parent d5a3615e1a
commit 2015ac5aef
No known key found for this signature in database
GPG Key ID: DEC4AAB876F73185
2 changed files with 60 additions and 21 deletions

View File

@ -316,7 +316,6 @@ local function grok_string(self, text, start, options)
end end
local function skip_whitespace(text, start) local function skip_whitespace(text, start)
local _, match_end = text:find("^[ \n\r\t]+", start) -- [http://www.ietf.org/rfc/rfc4627.txt] Section 2 local _, match_end = text:find("^[ \n\r\t]+", start) -- [http://www.ietf.org/rfc/rfc4627.txt] Section 2
if match_end then if match_end then
return match_end + 1 return match_end + 1
@ -325,6 +324,19 @@ local function skip_whitespace(text, start)
end end
end end
-- Count newlines in `text` up to `start`
local function count_newlines(text, start)
local _, count = text:sub(1, start):gsub('\n', '\n')
return count
end
-- Count characters from `i` to previous newline
local function get_relative_i(text, i)
local _, count = text:sub(1, i):gsub('\n', '\n')
return i - count
end
local grok_one -- assigned later local grok_one -- assigned later
local function grok_object(self, text, start, options) local function grok_object(self, text, start, options)
@ -345,6 +357,12 @@ local function grok_object(self, text, start, options)
while i <= text_len do while i <= text_len do
local key, new_i = grok_string(self, text, i, options) local key, new_i = grok_string(self, text, i, options)
---- Find start of JSON key
local key_start = i
local key_end = new_i - 2
local newlines = count_newlines(text, key_start)
local relative_start = get_relative_i(text, key_start)
i = skip_whitespace(text, new_i) i = skip_whitespace(text, new_i)
if text:sub(i, i) ~= ':' then if text:sub(i, i) ~= ':' then
@ -357,7 +375,7 @@ local function grok_object(self, text, start, options)
local new_val, new_i = grok_one(self, text, i, options) local new_val, new_i = grok_one(self, text, i, options)
---- Add start position so we can quickly jump to it ---- Add start position so we can quickly jump to it
VALUE[key] = {new_val, new_i} VALUE[key] = {new_val, key_start = key_start, key_end = key_end, newlines = newlines, relative_start = relative_start}
-- --
-- Expect now either '}' to end things, or a ',' to allow us to continue. -- Expect now either '}' to end things, or a ',' to allow us to continue.

View File

@ -2,16 +2,20 @@ local json = require"jsonfly.json"
local finders = require "telescope.finders" local finders = require "telescope.finders"
local pickers = require "telescope.pickers" local pickers = require "telescope.pickers"
local conf = require("telescope.config").values local conf = require("telescope.config").values
local make_entry = require "telescope.make_entry"
local function get_recursive_keys(t) local function get_recursive_keys(t)
local keys = {} local keys = {}
for k, v in pairs(t) do for k, raw_value in pairs(t) do
table.insert(keys, k) table.insert(keys, {key = k, entry = raw_value})
local v = raw_value[0]
if type(v) == "table" then if type(v) == "table" then
local subkeys = get_recursive_keys(v) local sub_keys = get_recursive_keys(v)
for _, subkey in ipairs(subkeys) do for _, sub_key in ipairs(sub_keys) do
table.insert(keys, k .. "." .. subkey) table.insert(keys, {key = k .. "." .. sub_key.key, entry = sub_key.entry})
end end
end end
end end
@ -26,18 +30,35 @@ return require"telescope".register_extension {
opts = opts or {} opts = opts or {}
local current_buf = vim.api.nvim_get_current_buf() local current_buf = vim.api.nvim_get_current_buf()
local filename = vim.api.nvim_buf_get_name(current_buf)
local content_lines = vim.api.nvim_buf_get_lines(current_buf, 0, -1, false) local content_lines = vim.api.nvim_buf_get_lines(current_buf, 0, -1, false)
local content = table.concat(content_lines, "") local content = table.concat(content_lines, "\n")
local parsed = json:decode(content) local parsed = json:decode(content)
local keys = get_recursive_keys(parsed)
print("keys" .. vim.inspect(parsed)) print(vim.inspect(keys))
pickers.new(opts, { pickers.new(opts, {
prompt_title = "colors", prompt_title = "colors",
finder = finders.new_table { finder = finders.new_table {
results = keys, results = keys,
entry_maker = function(entry)
return make_entry.set_default_entry_mt({
value = vim.inspect(entry.entry[0]),
ordinal = entry.key,
display = entry.key,
bufnr = current_buf,
filename = filename,
lnum = entry.entry.newlines + 1,
indicator = 0,
extra = 0,
}, opts)
end,
}, },
previewer = conf.grep_previewer(opts),
sorter = conf.generic_sorter(opts), sorter = conf.generic_sorter(opts),
}):find() }):find()
end end