From d00c1ed972778b0e77071f123d203a2c785ed019 Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Sun, 21 Apr 2024 15:04:03 +0200 Subject: [PATCH 1/4] chore: Improve names --- lua/telescope/_extensions/jsonfly.lua | 30 +++++++++++++++++++-------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/lua/telescope/_extensions/jsonfly.lua b/lua/telescope/_extensions/jsonfly.lua index add1faa..77715b2 100644 --- a/lua/telescope/_extensions/jsonfly.lua +++ b/lua/telescope/_extensions/jsonfly.lua @@ -20,8 +20,8 @@ ---@field other string - Highlight group for other types, Default: "@label.json" local parsers = require"jsonfly.parsers" -local json = require"jsonfly.json" local utils = require"jsonfly.utils" +local cache = require"jsonfly.cache" local json = require"jsonfly.json" local finders = require "telescope.finders" @@ -50,9 +50,9 @@ local opts = { backend = "lsp", } ----@param results Entry[] +---@param entries Entry[] ---@param buffer number -local function show_picker(results, buffer) +local function show_picker(entries, buffer) local filename = vim.api.nvim_buf_get_name(buffer) local displayer = entry_display.create { @@ -75,7 +75,7 @@ local function show_picker(results, buffer) pickers.new(opts, { prompt_title = opts.prompt_title, finder = finders.new_table { - results = results, + results = entries, ---@param entry Entry entry_maker = function(entry) local _, raw_depth = entry.key:gsub("%.", ".") @@ -133,14 +133,25 @@ return require"telescope".register_extension { exports = { jsonfly = function(xopts) local current_buf = vim.api.nvim_get_current_buf() + + local cached_entries = cache:get_cache(current_buf) + + if cached_entries ~= nil then + print("Using cached entries") + show_picker(cached_entries, current_buf) + return + end + local content_lines = vim.api.nvim_buf_get_lines(current_buf, 0, -1, false) local content = table.concat(content_lines, "\n") - function run_lua_parser() + local function run_lua_parser() local parsed = json:decode(content) - local keys = parsers:get_entries_from_lua_json(parsed) + local entries = parsers:get_entries_from_lua_json(parsed) - show_picker(keys, current_buf) + cache:cache_buffer(current_buf, entries) + + show_picker(entries, current_buf) end if opts.backend == "lsp" then @@ -156,9 +167,10 @@ return require"telescope".register_extension { return end - local result = parsers:get_entries_from_lsp_symbols(lsp_response) + local entries = parsers:get_entries_from_lsp_symbols(lsp_response) + cache:cache_buffer(current_buf, entries) - show_picker(result, current_buf) + show_picker(entries, current_buf) end ) else From 1455d877cd1f998b743f2676228afc991af78cde Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Mon, 22 Apr 2024 21:44:56 +0200 Subject: [PATCH 2/4] feat: Add cache support --- lua/jsonfly/cache.lua | 47 +++++++++++++++++++++++++++ lua/telescope/_extensions/jsonfly.lua | 3 +- 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 lua/jsonfly/cache.lua diff --git a/lua/jsonfly/cache.lua b/lua/jsonfly/cache.lua new file mode 100644 index 0000000..42551de --- /dev/null +++ b/lua/jsonfly/cache.lua @@ -0,0 +1,47 @@ +local M = {}; + +local _cache = {}; + +---@param buffer integer +function M:cache_buffer(buffer, value) + _cache[buffer] = value; +end + +---@param buffer integer +function M:invalidate_buffer(buffer) + _cache[buffer] = nil; +end + +---@param buffer integer +---@return string[]|nil +function M:get_cache(buffer) + return _cache[buffer]; +end + +local _listening_buffers = {}; + +---@param buffer integer +function M:register_listeners(buffer) + if _listening_buffers[buffer] then + return; + end + + _listening_buffers[buffer] = true; + + vim.api.nvim_buf_attach( + buffer, + false, + { + on_lines = function() + self:invalidate_buffer(buffer) + end, + on_detach = function() + self:invalidate_buffer(buffer) + _listening_buffers[buffer] = nil; + end, + } + ); +end + +return M; + diff --git a/lua/telescope/_extensions/jsonfly.lua b/lua/telescope/_extensions/jsonfly.lua index 77715b2..b3b7e2b 100644 --- a/lua/telescope/_extensions/jsonfly.lua +++ b/lua/telescope/_extensions/jsonfly.lua @@ -137,11 +137,12 @@ return require"telescope".register_extension { local cached_entries = cache:get_cache(current_buf) if cached_entries ~= nil then - print("Using cached entries") show_picker(cached_entries, current_buf) return end + cache:register_listeners(current_buf) + local content_lines = vim.api.nvim_buf_get_lines(current_buf, 0, -1, false) local content = table.concat(content_lines, "\n") From a36cb5b0b151dbd41076226fd2c3aa3ddbe780ec Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Mon, 22 Apr 2024 21:49:52 +0200 Subject: [PATCH 3/4] feat: Add option for cache --- lua/telescope/_extensions/jsonfly.lua | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/lua/telescope/_extensions/jsonfly.lua b/lua/telescope/_extensions/jsonfly.lua index b3b7e2b..cd0c19f 100644 --- a/lua/telescope/_extensions/jsonfly.lua +++ b/lua/telescope/_extensions/jsonfly.lua @@ -11,6 +11,7 @@ ---@field jump_behavior "key_start"|"value_start" - Behavior for jumping to the location, "key_start" == Jump to the start of the key, "value_start" == Jump to the start of the value, Default: "key_start" ---@field subkeys_display "normal"|"waterfall" - Display subkeys in a normal or waterfall style, Default: "normal" ---@field backend "lua"|"lsp" - Backend to use for parsing JSON, "lua" = Use our own Lua parser to parse the JSON, "lsp" = Use your LSP to parse the JSON (currently only https://github.com/Microsoft/vscode-json-languageservice is supported). If the "lsp" backend is selected but the LSP fails, it will fallback to the "lua" backend, Default: "lsp" +---@field use_cache number - Whether to use cache the parsed JSON. The cache will be activated if the number of lines is greater or equal to this value, By default, the cache is activate when the file if 1000 lines or more; `0` to disable the cache, Default: 500 --- ---@class Highlights ---@field number string - Highlight group for numbers, Default: "@number.json" @@ -48,6 +49,7 @@ local opts = { jump_behavior = "key_start", subkeys_display = "normal", backend = "lsp", + use_cache = 500, } ---@param entries Entry[] @@ -141,16 +143,21 @@ return require"telescope".register_extension { return end - cache:register_listeners(current_buf) - local content_lines = vim.api.nvim_buf_get_lines(current_buf, 0, -1, false) local content = table.concat(content_lines, "\n") + local allow_cache = opts.use_cache > 0 and #content_lines >= opts.use_cache + + if allow_cache then + cache:register_listeners(current_buf) + end local function run_lua_parser() local parsed = json:decode(content) local entries = parsers:get_entries_from_lua_json(parsed) - cache:cache_buffer(current_buf, entries) + if allow_cache then + cache:cache_buffer(current_buf, entries) + end show_picker(entries, current_buf) end @@ -169,7 +176,10 @@ return require"telescope".register_extension { end local entries = parsers:get_entries_from_lsp_symbols(lsp_response) - cache:cache_buffer(current_buf, entries) + + if allow_cache then + cache:cache_buffer(current_buf, entries) + end show_picker(entries, current_buf) end From 7a6aedb8408408f839cdfc206e0d1fde927bbf3e Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Mon, 22 Apr 2024 21:52:06 +0200 Subject: [PATCH 4/4] fix: Improve docs --- lua/jsonfly/parsers.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/jsonfly/parsers.lua b/lua/jsonfly/parsers.lua index ef775e5..eee1f5d 100644 --- a/lua/jsonfly/parsers.lua +++ b/lua/jsonfly/parsers.lua @@ -40,7 +40,7 @@ local function get_contents_from_json_value(entry) end end ----@param t table|nil|string|number +---@param t table|nil|string|number|boolean ---@return Entry[] function M:get_entries_from_lua_json(t) if PRIMITIVE_TYPES[type(t)] or t == nil then