From 2015ac5aefaa1aa05d38fbcb693db600e23a8d24 Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Thu, 11 Apr 2024 15:14:12 +0200 Subject: [PATCH] feat: Add first working telescope WIP --- lua/jsonfly/json.lua | 22 +++++++++- lua/telescope/_extensions/jsonfly.lua | 59 ++++++++++++++++++--------- 2 files changed, 60 insertions(+), 21 deletions(-) diff --git a/lua/jsonfly/json.lua b/lua/jsonfly/json.lua index 2751e55..20ad726 100644 --- a/lua/jsonfly/json.lua +++ b/lua/jsonfly/json.lua @@ -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. diff --git a/lua/telescope/_extensions/jsonfly.lua b/lua/telescope/_extensions/jsonfly.lua index 494dc7b..5b0b0f6 100644 --- a/lua/telescope/_extensions/jsonfly.lua +++ b/lua/telescope/_extensions/jsonfly.lua @@ -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 } }