Skip to content

Commit

Permalink
Refactors: replaces assure_yaml_filetype by get_current_yaml_node
Browse files Browse the repository at this point in the history
  • Loading branch information
cuducos committed Oct 31, 2023
1 parent b642efd commit 3f57cae
Showing 1 changed file with 27 additions and 20 deletions.
47 changes: 27 additions & 20 deletions lua/yaml_nvim/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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, "'", "''")
Expand All @@ -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()
Expand Down

0 comments on commit 3f57cae

Please sign in to comment.