From a5a4e25379f777d9418fb3ce93c6b2b23e25c57c Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Fri, 12 Apr 2024 21:24:28 +0200 Subject: [PATCH] feat: Add configurable jump behavior --- lua/jsonfly/json.lua | 15 ++++++--------- lua/telescope/_extensions/jsonfly.lua | 7 ++++++- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/lua/jsonfly/json.lua b/lua/jsonfly/json.lua index 159a3a5..fb58ddd 100644 --- a/lua/jsonfly/json.lua +++ b/lua/jsonfly/json.lua @@ -370,7 +370,6 @@ local function grok_object(self, text, start, 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) @@ -388,10 +387,9 @@ local function grok_object(self, text, start, options) ---- Add start position so we can quickly jump to it VALUE[key] = { value = new_val, - key_start = key_start, - key_end = key_end, newlines = newlines or 0, - relative_start = relative_start, + key_start = relative_start + 1, + value_start = get_relative_i(text, i), } -- @@ -437,18 +435,17 @@ local function grok_array(self, text, start, options) local newlines = count_newlines(text, i) local relative_start = get_relative_i(text, i) + i = skip_whitespace(text, new_i) + -- can't table.insert(VALUE, val) here because it's a no-op if val is nil VALUE[VALUE_INDEX] = { value = val, - key_start = new_i, - key_end = new_i, newlines = newlines or 0, - relative_start = relative_start, + value_start = relative_start, + key_start = relative_start, } VALUE_INDEX = VALUE_INDEX + 1 - i = skip_whitespace(text, new_i) - -- -- 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 15d0a6d..6c29397 100644 --- a/lua/telescope/_extensions/jsonfly.lua +++ b/lua/telescope/_extensions/jsonfly.lua @@ -6,6 +6,7 @@ ---@field conceal boolean|"auto" - Whether to conceal strings, If `true` strings will be concealed, If `false` strings will be displayed as they are, If `"auto"` strings will be concealed if `conceallevel` is greater than 0, Default: "auto" ---@field prompt_title string - Title for the prompt, Default: "JSON(fly)" ---@field highlights Highlights - Highlight groups for different types +---@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" --- ---@class Highlights ---@field number string - Highlight group for numbers, Default: "@number.json" @@ -100,6 +101,7 @@ return require"telescope".register_extension { null = "@constant.builtin.json", other = "@label.json", } + opts.jump_behavior = opts.jump_behavior or "key_start" if opts.conceal == nil then opts.conceal = "auto" @@ -153,7 +155,10 @@ return require"telescope".register_extension { bufnr = current_buf, filename = filename, lnum = entry.entry.newlines + 1, - col = entry.entry.relative_start, + col = opts.jump_behavior == "key_start" + and entry.entry.key_start + -- Use length ("#" operator) as vim jumps to the bytes, not characters + or entry.entry.value_start }, opts) end, },