feat: Add configurable jump behavior

This commit is contained in:
Myzel394 2024-04-12 21:24:28 +02:00
parent d4176879f2
commit a5a4e25379
No known key found for this signature in database
GPG Key ID: DEC4AAB876F73185
2 changed files with 12 additions and 10 deletions

View File

@ -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.
--

View File

@ -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,
},