From 3f57caef632c0eefce3ce7cd1fe10fb30d06330e Mon Sep 17 00:00:00 2001 From: Eduardo Cuducos <4732915+cuducos@users.noreply.github.com> Date: Tue, 31 Oct 2023 17:35:36 -0400 Subject: [PATCH 1/3] Refactors: replaces assure_yaml_filetype by get_current_yaml_node --- lua/yaml_nvim/init.lua | 47 ++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/lua/yaml_nvim/init.lua b/lua/yaml_nvim/init.lua index ef28a0e..6d50533 100644 --- a/lua/yaml_nvim/init.lua +++ b/lua/yaml_nvim/init.lua @@ -21,33 +21,37 @@ local restore_filetype = function(restore_to) end end -local assure_yaml_filetype = function(func, ...) +local get_current_yaml_node = function() local restore_to = set_yaml_as_filetype() - func(...) + local node = document.get_key_relevant_to_cursor() + if node == nil then + return + end + local parsed = pair.parse(node) restore_filetype(restore_to) + + return parsed end -local yank = function(key, value, register) - register = register or [["]] - if not key and not value then +local yank = function(node, key, value, register) + if node == nil then return end - local node = document.get_key_relevant_to_cursor() - if node == nil then + register = register or [["]] + if not key and not value then return end - local parsed = pair.parse(node) local contents = "" if key and value then - contents = parsed.human + contents = node.human elseif key then - contents = parsed.key + contents = node.key elseif value then - contents = parsed.cleaned_value + contents = node.cleaned_value end contents = string.gsub(contents, "'", "''") @@ -57,30 +61,33 @@ local yank = function(key, value, register) end M.view = function() - vim.notify(M.get_yaml_key_and_value()) + local node = get_current_yaml_node() + if node == nil then + return + end + + vim.notify(node.human) end M.get_yaml_key_and_value = function() - local restore_to = set_yaml_as_filetype() - local node = document.get_key_relevant_to_cursor() + local node = get_current_yaml_node() if node == nil then return end - local parsed = pair.parse(node) - restore_filetype(restore_to) - return parsed.human + + return node.human end M.yank = function(register) - assure_yaml_filetype(yank, true, true, register) + yank(get_current_yaml_node(), true, true, register) end M.yank_key = function(register) - assure_yaml_filetype(yank, true, false, register) + yank(get_current_yaml_node(), true, false, register) end M.yank_value = function(register) - assure_yaml_filetype(yank, false, true, register) + yank(get_current_yaml_node(), false, true, register) end M.quickfix = function() From b1fd34f03df84b0de925af1700c5e8be80dc2b48 Mon Sep 17 00:00:00 2001 From: Eduardo Cuducos <4732915+cuducos@users.noreply.github.com> Date: Tue, 31 Oct 2023 17:55:22 -0400 Subject: [PATCH 2/3] Adds instructions to see YAML info in the statusline Thanks for the inspiration, @joshzcold :) --- README.md | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b87b3b9..af11364 100644 --- a/README.md +++ b/README.md @@ -69,7 +69,9 @@ Plug 'cuducos/yaml.nvim' ## Configuration -### Showing the YAML patch and value in Neovim's winbar +### Showing the YAML path and value + +#### Neovim's winbar ```lua vim.api.nvim_create_autocmd({ "FileType" }, { @@ -80,6 +82,18 @@ vim.api.nvim_create_autocmd({ "FileType" }, { }) ``` +#### Neovim's statusline (with [`lualine.nvim`](https://github.com/nvim-lualine/lualine.nvim)) + +```lua +require("lualine").setup({ + sections = { + lualine_x = { require("yaml_nvim").get_yaml_key_and_value }, + -- etc + } +}) + +``` + ## Reporting bugs and contributing There is a mini toolchain to help you test the plugin in isolation using a container. It requires: From 647cfec449354b70d81b86fae144b4810af2cb41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Vicente=20Gon=C3=A7alves?= <4732915+cuducos@users.noreply.github.com> Date: Wed, 1 Nov 2023 16:35:21 -0400 Subject: [PATCH 3/3] Adds tests to yanking values --- tests/yaml_nvim/commands_spec.lua | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/yaml_nvim/commands_spec.lua b/tests/yaml_nvim/commands_spec.lua index 0ae0028..445d35b 100644 --- a/tests/yaml_nvim/commands_spec.lua +++ b/tests/yaml_nvim/commands_spec.lua @@ -16,4 +16,24 @@ describe("commands", function() assert(vim.fn.getreg("7"), "worldcup") end) + + it("yanks value to the default register", function() + require("yaml_nvim") + + vim.cmd(":e sample.yaml") + vim.cmd("norm 10j") + vim.cmd(":YAMLYankValue") + + assert(vim.fn.getreg('"'), "7") + end) + + it("yanks value to a custom register", function() + require("yaml_nvim") + + vim.cmd(":e sample.yaml") + vim.cmd("norm 10j") + vim.cmd(":YAMLYankValue 7") + + assert(vim.fn.getreg("7"), "7") + end) end)