mirror of
https://github.com/Myzel394/jsonfly.nvim.git
synced 2025-06-18 12:15:25 +02:00
feat: Add first working telescope WIP
This commit is contained in:
parent
d5a3615e1a
commit
2015ac5aef
@ -316,7 +316,6 @@ local function grok_string(self, text, start, options)
|
||||
end
|
||||
|
||||
local function skip_whitespace(text, start)
|
||||
|
||||
local _, match_end = text:find("^[ \n\r\t]+", start) -- [http://www.ietf.org/rfc/rfc4627.txt] Section 2
|
||||
if match_end then
|
||||
return match_end + 1
|
||||
@ -325,6 +324,19 @@ local function skip_whitespace(text, start)
|
||||
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 function grok_object(self, text, start, options)
|
||||
@ -345,6 +357,12 @@ local function grok_object(self, text, start, options)
|
||||
while i <= text_len do
|
||||
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)
|
||||
|
||||
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)
|
||||
|
||||
---- 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.
|
||||
|
@ -2,44 +2,65 @@ local json = require"jsonfly.json"
|
||||
local finders = require "telescope.finders"
|
||||
local pickers = require "telescope.pickers"
|
||||
local conf = require("telescope.config").values
|
||||
local make_entry = require "telescope.make_entry"
|
||||
|
||||
local function get_recursive_keys(t)
|
||||
local keys = {}
|
||||
local keys = {}
|
||||
|
||||
for k, v in pairs(t) do
|
||||
table.insert(keys, k)
|
||||
if type(v) == "table" then
|
||||
local subkeys = get_recursive_keys(v)
|
||||
for _, subkey in ipairs(subkeys) do
|
||||
table.insert(keys, k .. "." .. subkey)
|
||||
end
|
||||
for k, raw_value in pairs(t) do
|
||||
table.insert(keys, {key = k, entry = raw_value})
|
||||
|
||||
local v = raw_value[0]
|
||||
|
||||
if type(v) == "table" then
|
||||
local sub_keys = get_recursive_keys(v)
|
||||
for _, sub_key in ipairs(sub_keys) do
|
||||
table.insert(keys, {key = k .. "." .. sub_key.key, entry = sub_key.entry})
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return keys
|
||||
return keys
|
||||
end
|
||||
|
||||
return require"telescope".register_extension {
|
||||
setup = function() end,
|
||||
exports = {
|
||||
jsonfly = function(opts)
|
||||
opts = opts or {}
|
||||
opts = opts or {}
|
||||
|
||||
local current_buf = vim.api.nvim_get_current_buf()
|
||||
local content_lines = vim.api.nvim_buf_get_lines(current_buf, 0, -1, false)
|
||||
local content = table.concat(content_lines, "")
|
||||
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 = 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",
|
||||
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),
|
||||
}):find()
|
||||
}):find()
|
||||
end
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user