Skip to content

Commit

Permalink
Adds setup method to handle accaptable YAML file types
Browse files Browse the repository at this point in the history
  • Loading branch information
cuducos committed Nov 7, 2023
1 parent 134f533 commit 15c4cc1
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 3 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ Plug 'cuducos/yaml.nvim'

## Configuration

### File types

The plugin ignores other file types than YAML. By now the list of YAML file types includes `yaml` and `eruby.yaml` — we're are open to enhance this list, so PRs are welcomed.

If you want to manually change this list, you can pass a custom config:

```lua
require("yaml_nvim").setup({ ft = { "yaml", "other yaml filetype" } })
```

### Showing the YAML path and value

#### Neovim's winbar
Expand Down
45 changes: 44 additions & 1 deletion lua/yaml_nvim/init.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
local has_telescope, _ = pcall(require, "telescope")
local document = require("yaml_nvim.document")
local pair = require("yaml_nvim.pair")
local M = {}

local is_yaml = function()
local curr = vim.bo.filetype
for _, ft in ipairs(M.config.ft) do
if curr == ft then
return true
end
end

return false
end

local set_yaml_as_filetype = function()
local expected = "yaml"
Expand All @@ -22,6 +32,10 @@ local restore_filetype = function(restore_to)
end

local get_current_yaml_node = function()
if not is_yaml() then
return
end

local restore_to = set_yaml_as_filetype()

local node = document.get_key_relevant_to_cursor()
Expand Down Expand Up @@ -60,6 +74,29 @@ local yank = function(node, key, value, register)
vim.fn.setreg(register, contents)
end

-- Public API

M = {}

M.default_config = {
ft = { "yaml", "eruby.yaml" },
}

M.config = vim.deepcopy(M.default_config)

M.setup = function(opts)
if opts == nil then
return
end

for k, _ in pairs(M.default_config) do
local custom = opts[k]
if custom ~= nil then
M.config[k] = custom
end
end
end

M.view = function()
local node = get_current_yaml_node()
if node == nil then
Expand Down Expand Up @@ -99,6 +136,10 @@ M.yank_value = function(register)
end

M.quickfix = function()
if not is_yaml() then
return
end

local restore_to = set_yaml_as_filetype()
local lines = {}

Expand All @@ -122,6 +163,8 @@ M.telescope = function()
require("telescope.builtin").quickfix()
end

-- Commands

vim.cmd("command! YAMLView lua require('yaml_nvim').view()")
vim.cmd("command! -nargs=? YAMLYank lua require('yaml_nvim').yank(<f-args>)")
vim.cmd("command! -nargs=? YAMLYankKey lua require('yaml_nvim').yank_key(<f-args>)")
Expand Down
29 changes: 27 additions & 2 deletions tests/yaml_nvim/functions_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ local stub = require("luassert.stub")
describe("Lua functions with sample YAML:", function()
before_each(function()
vim.cmd(":e tests/sample.yaml")
vim.cmd("set ft=not-a-yaml-file")
vim.cmd(":norm 10j^")
end)

Expand Down Expand Up @@ -68,9 +67,10 @@ end)

describe("Lua functions with simple YAML:", function()
before_each(function()
vim.cmd("set ft=not-a-yaml-file")
vim.cmd("set ft=eruby.yaml")
vim.cmd(":norm ianswer: 42")
vim.cmd(":norm oquestion: null")
vim.cmd(":norm 0d^")
end)

after_each(function()
Expand Down Expand Up @@ -99,3 +99,28 @@ describe("Lua functions with simple YAML:", function()
})
end)
end)

describe("Lua functions with non-YAML files:", function()
before_each(function()
vim.cmd("set ft=lua")
vim.cmd(":norm iprint(42)")
vim.cmd(":norm oreturn {}")
end)

after_each(function()
vim.cmd(":bw!")
end)

it("loading YAML node ignores filetypes that are not YAML", function()
local yaml = require("yaml_nvim")
assert.is_nil(yaml.get_yaml_key_and_value())
assert.is_nil(yaml.get_yaml_key())
end)

it("quickfix ignores filetypes that are not YAML", function()
local yaml = require("yaml_nvim")
local setqflist = spy.on(vim.fn, "setqflist")
yaml.quickfix()
assert.spy(setqflist).was_not.called()
end)
end)
29 changes: 29 additions & 0 deletions tests/yaml_nvim/setup_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,33 @@ describe("setup", function()
assert(contains(cmds, cmd), cmd .. " not found")
end
end)

it("uses default config when required", function()
local yaml = require("yaml_nvim")
assert.are.same(yaml.config, yaml.default_config)
end)

it("uses default config when setup is not called", function()
local yaml = require("yaml_nvim")
assert.are.same(yaml.config, yaml.default_config)
end)

it("uses default config when setup is called with no arguments", function()
local yaml = require("yaml_nvim")
yaml.setup()
assert.are.same(yaml.config, yaml.default_config)
end)

it("uses default config when setup is called with empty table", function()
local yaml = require("yaml_nvim")
yaml.setup({})
assert.are.same(yaml.config, yaml.default_config)
end)

it("set up uses custom config", function()
local yaml = require("yaml_nvim")
local custom_config = { ft = { "not-my-yaml" } }
yaml.setup(custom_config)
assert.are.same(yaml.config, custom_config)
end)
end)

0 comments on commit 15c4cc1

Please sign in to comment.