From 13d9d0a6fd1fdc8811a8e87047c818da6c664e60 Mon Sep 17 00:00:00 2001 From: Joshua Cold Date: Mon, 30 Oct 2023 11:03:41 -0600 Subject: [PATCH 1/3] Add function to return the yaml path to populate the winbar - .view() uses .get() to call its returned value - Add lua example to docs --- README.md | 13 +++++++++++++ lua/yaml_nvim/init.lua | 12 +++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 9056281..a603032 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,19 @@ Plug 'nvim-treesitter/nvim-treesitter' Plug 'cuducos/yaml.nvim' ``` +## Configuration + +### Showing the YAML patch and value in Neovim's winbar + +```lua +vim.api.nvim_create_autocmd({ "FileType" }, { + pattern = { "yaml" }, + callback = function() + vim.opt_local.winbar = [[%{%v:lua.require("yaml_nvim").get()%}]] + end, +}) +``` + ## Reporting bugs and contributing There is a mini toolchain to help you test the plugin in isolation using a container. It requires: diff --git a/lua/yaml_nvim/init.lua b/lua/yaml_nvim/init.lua index f074a38..43ab048 100644 --- a/lua/yaml_nvim/init.lua +++ b/lua/yaml_nvim/init.lua @@ -24,9 +24,10 @@ end local assure_yaml_filetype = function(func, ...) local restore_to = set_yaml_as_filetype() - func(...) + local out = func(...) restore_filetype(restore_to) + return out end local yank = function(key, value, register) @@ -57,15 +58,20 @@ local yank = function(key, value, register) end M.view = function() - assure_yaml_filetype(function() + vim.notify(M.get()) +end + +M.get = function() + local out = assure_yaml_filetype(function() local node = document.get_key_relevant_to_cursor() if node == nil then return end local parsed = pair.parse(node) - vim.notify(parsed.human) + return parsed.human end) + return out end M.yank = function(register) From b67b5811e576e0ccc4054eb66c5b209e4627986e Mon Sep 17 00:00:00 2001 From: Joshua Cold Date: Mon, 30 Oct 2023 14:26:26 -0600 Subject: [PATCH 2/3] make the get function for yaml key be less generic get_yaml_key_and_value --- README.md | 2 +- lua/yaml_nvim/init.lua | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a603032..b87b3b9 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,7 @@ Plug 'cuducos/yaml.nvim' vim.api.nvim_create_autocmd({ "FileType" }, { pattern = { "yaml" }, callback = function() - vim.opt_local.winbar = [[%{%v:lua.require("yaml_nvim").get()%}]] + vim.opt_local.winbar = [[%{%v:lua.require("yaml_nvim").get_yaml_key_and_value()%}]] end, }) ``` diff --git a/lua/yaml_nvim/init.lua b/lua/yaml_nvim/init.lua index 43ab048..60bb774 100644 --- a/lua/yaml_nvim/init.lua +++ b/lua/yaml_nvim/init.lua @@ -58,10 +58,10 @@ local yank = function(key, value, register) end M.view = function() - vim.notify(M.get()) + vim.notify(M.get_yaml_key_and_value()) end -M.get = function() +M.get_yaml_key_and_value = function() local out = assure_yaml_filetype(function() local node = document.get_key_relevant_to_cursor() if node == nil then From 99243d2ae157629ffb4cbc82cd29fe95916fdd68 Mon Sep 17 00:00:00 2001 From: Joshua Cold Date: Tue, 31 Oct 2023 09:57:26 -0600 Subject: [PATCH 3/3] Set the assure_yaml_filetype back to default get_yaml_key_and_value does the assure_yaml_filetype functions manually --- lua/yaml_nvim/init.lua | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/lua/yaml_nvim/init.lua b/lua/yaml_nvim/init.lua index 60bb774..ef28a0e 100644 --- a/lua/yaml_nvim/init.lua +++ b/lua/yaml_nvim/init.lua @@ -24,10 +24,9 @@ end local assure_yaml_filetype = function(func, ...) local restore_to = set_yaml_as_filetype() - local out = func(...) + func(...) restore_filetype(restore_to) - return out end local yank = function(key, value, register) @@ -62,16 +61,14 @@ M.view = function() end M.get_yaml_key_and_value = function() - local out = assure_yaml_filetype(function() - local node = document.get_key_relevant_to_cursor() - if node == nil then - return - end - - local parsed = pair.parse(node) - return parsed.human - end) - return out + local restore_to = set_yaml_as_filetype() + 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.human end M.yank = function(register)