···1010| `:YAMLYank [register]` | `yaml.yank_all([register])` | Yanks the full path and value of the current key/value pair. The default register is the unnamed one (`"`) |
1111| `:YAMLYankKey [register]` | `yaml.yank_key([register])` | Yanks the full path of the key for the current key/value pair. The default register is the unnamed one (`"`) |
1212| `:YAMLYankValue [register]` | `yaml.yank_value([register])` | Yanks the value of the current key/value pair. The default register is the unnamed one (`"`) |
1313+| `:YAMLHighlight <key>` | `yaml.highlight(key)` | Highlights the line(s) of an YAML `key` |
1414+| `:YAMLRemoveHighlight` | `yaml.remove_highlight()` | Removes the highlight created by `:YAMLHighlight`/`yaml.highlight(key)` |
1315| `:YAMLQuickfix` | `yaml.quickfix()` | Generates a quickfix with key/value pairs |
1416| `:YAMLSnacks` | `yaml.snacks()` | Full path key/value fuzzy finder via [Snacks](https://github.com/folke/snacks.nvim) **if installed** |
1517| `:YAMLTelescope` | `yaml.telescope()` | Full path key/value fuzzy finder via [Telescope](https://github.com/nvim-telescope/telescope.nvim) **if installed** |
+9
doc/help.txt
···3535 Yanks the value of the key/value pair under the cursor to the selected
3636 register. The default register is the unnamed one (see |:quotequote|).
37373838+:YAMLHighlight <key> *:YAMLHighlight*
3939+4040+ Highlights the line(s) of the provided YAML key. The key should match
4141+ the output format used in |:YAMLViem|.
4242+4343+:YAMLRemoveHighlight *:YAMLRemoveHighlight*
4444+4545+ Removes the highlight created by |:YAMLHighlight|.
4646+3847:YAMLQuickfix *YAMLQuickfix*
39484049 Creates a |:quickfix| list with the contents of the YAML, showing the full
+45
lua/yaml_nvim/init.lua
···137137 yank(get_current_yaml_node(), false, true, register)
138138end
139139140140+M.current_highlight = nil
141141+142142+M.highlight = function(key)
143143+ if not is_yaml() then
144144+ return
145145+ end
146146+147147+ local restore_to = set_yaml_as_filetype()
148148+149149+ local target = nil
150150+ for _, node in pairs(document.all_keys()) do
151151+ if not document.is_value_a_block(node) then
152152+ local parsed = pair.parse(node)
153153+ if parsed.key == key then
154154+ target = parsed
155155+ break
156156+ end
157157+ end
158158+ end
159159+ if target == nil then
160160+ vim.notify(key .. " not found")
161161+ return
162162+ end
163163+164164+ local pattern = "\\%" .. target.lines[1] .. "l"
165165+ if target.lines[2] > target.lines[1] then
166166+ pattern = "\\%>" .. target.lines[1] .. "l\\%<" .. target.lines[2] .. "l"
167167+ end
168168+169169+ M.remove_highlight()
170170+ M.current_highlight = vim.fn.matchadd("Visual", pattern)
171171+ restore_filetype(restore_to)
172172+end
173173+174174+M.remove_highlight = function()
175175+ if M.current_highlight == nil then
176176+ return
177177+ end
178178+179179+ vim.fn.matchdelete(M.current_highlight)
180180+ M.current_highlight = nil
181181+end
182182+140183M.quickfix = function()
141184 if not is_yaml() then
142185 return
···189232vim.cmd("command! -nargs=? YAMLYank lua require('yaml_nvim').yank(<f-args>)")
190233vim.cmd("command! -nargs=? YAMLYankKey lua require('yaml_nvim').yank_key(<f-args>)")
191234vim.cmd("command! -nargs=? YAMLYankValue lua require('yaml_nvim').yank_value(<f-args>)")
235235+vim.cmd("command! -nargs=? YAMLHighlight lua require('yaml_nvim').highlight(<f-args>)")
236236+vim.cmd("command! YAMLRemoveHighlight lua require('yaml_nvim').remove_highlight()")
192237vim.cmd("command! YAMLQuickfix lua require('yaml_nvim').quickfix()")
193238194239if has_snacks then
+14-7
lua/yaml_nvim/pair.lua
···2828 return trim(string.gsub(value:sub(2, #value), "%s+", " "))
2929end
30303131-local function get_value(node, bufnr)
3131+local function get_value_node(node)
3232 while node ~= nil do
3333 if node:type() == "block_mapping_pair" then
3434 if node:field("value")[1] ~= nil then
3535- local value = node:field("value")[1]
3636- return table.concat({ vim.treesitter.get_node_text(value, bufnr) }, "\n")
3535+ return node
3736 end
3837 end
3938 node = node:parent()
4039 end
4040+end
4141+4242+local function get_value(node, bufnr)
4343+ local value = node:field("value")[1]
4444+ return table.concat({ vim.treesitter.get_node_text(value, bufnr) }, "\n")
4145end
42464347local function is_sequence_block(value)
···96100M.parse = function(node)
97101 local bufnr = vim.api.nvim_get_current_buf()
98102 local key = get_keys(node, bufnr)
9999- local value = get_value(node, bufnr)
100100- local line, col = node:start()
103103+ local value_node = get_value_node(node)
104104+ local value = get_value(value_node, bufnr)
101105 local cleaned_value = clean_up_block_value(value)
102106 local human = string.format("%s = %s", key, cleaned_value)
107107+ local start_line, start_col = node:start()
108108+ local end_line, _ = value_node:end_()
103109104110 return {
105111 key = key,
106112 cleaned_value = cleaned_value,
107113 human = human,
114114+ lines = { start_line + 1, end_line + 1 },
108115 quickfix = {
109116 bufnr = bufnr,
110110- col = col,
111111- lnum = line + 1,
117117+ col = start_col,
118118+ lnum = start_line + 1,
112119 text = human,
113120 valid = 1,
114121 },
+12
tests/yaml_nvim/commands_spec.lua
···5151 assert.stub(yank_value).was_called_with("8")
5252 end)
53535454+ it("YAMLHighlight calls Lua function for highlighting", function()
5555+ local highlight = stub(require("yaml_nvim"), "highlight")
5656+ vim.cmd("YAMLHighlight worldcup.intro")
5757+ assert.stub(highlight).was_called_with("worldcup.intro")
5858+ end)
5959+6060+ it("YAMLRemoveHighlight calls Lua function for removing highlight", function()
6161+ local remove = stub(require("yaml_nvim"), "remove_highlight")
6262+ vim.cmd("YAMLRemoveHighlight")
6363+ assert.stub(remove).was_called_with()
6464+ end)
6565+5466 it("YAMLQuickfix calls Lua function", function()
5567 local qf = stub(require("yaml_nvim"), "quickfix")
5668 vim.cmd("YAMLQuickfix")
+30
tests/yaml_nvim/functions_spec.lua
···5757 assert.are.equal("7", vim.fn.getreg("7"))
5858 end)
59596060+ it("highlight calls the notify function with unknown key", function()
6161+ local matchadelete = stub(vim.fn, "matchdelete")
6262+ local notify = stub(vim, "notify")
6363+ require("yaml_nvim").highlight("this.key.does.not.exist")
6464+ assert.spy(notify).was.called_with("this.key.does.not.exist not found")
6565+ assert.spy(matchadelete).was_not.called()
6666+ end)
6767+6868+ it("highlight calls matchadd with the proper line", function()
6969+ local matchadd = stub(vim.fn, "matchadd")
7070+ require("yaml_nvim").highlight("worldcup.host")
7171+ assert.spy(matchadd).was.called_with("Visual", "\\%2l")
7272+ end)
7373+7474+ it("highlight calls matchadd with the proper lines", function()
7575+ local matchadd = stub(vim.fn, "matchadd")
7676+ require("yaml_nvim").highlight("worldcup.intro")
7777+ assert.spy(matchadd).was.called_with("Visual", "\\%>5l\\%<7l")
7878+ end)
7979+8080+ it("remove_highlight calls matchdelete", function()
8181+ local matchadelete = stub(vim.fn, "matchdelete")
8282+ local yaml = require("yaml_nvim")
8383+ yaml.current_highlight = 42
8484+ yaml.highlight("worldcup.host")
8585+ yaml.remove_highlight()
8686+ assert.spy(matchadelete).was.called_with(42)
8787+ assert.are.equal(nil, yaml.current_highlight)
8888+ end)
8989+6090 it("snacks calls Snacks's native qflist function", function()
6191 stub(vim.fn, "setqflist")
6292 local snacks = require("snacks")