๐Ÿ’ YAML toolkit for Neovim users

Updates quickfix API to Lua

+46 -34
+18 -11
README.md
··· 1 1 # yaml.nvim [![Tests status](https://github.com/cuducos/yaml.nvim/actions/workflows/tests.yml/badge.svg)](https://github.com/cuducos/yaml.nvim/actions/workflows/tests.yml) 2 2 3 - Simple tools to help developers working YAML in [Neovim](https://neovim.io): 3 + Simple tools to help developers working YAML in [Neovim](https://neovim.io). 4 + 5 + Assuming `yaml = require("yaml_nvim")` for the Lua API: 4 6 5 - | Command | Description | 6 - |:--|:--| 7 - | `:YAMLView` | Shows the full path and value of the current key/value pair | 8 - | `:YAMLYank [register]` | Yanks the full path and value of the current key/value pair. The default register is the unnamed one (`"`) | 9 - | `:YAMLYankKey [register]` | Yanks the full path of the key for the current key/value pair. The default register is the unnamed one (`"`) | 10 - | `:YAMLYankValue [regster]` | Yanks the value of the current key/value pair. The default register is the unnamed one (`"`) | 11 - | `:YAMLQuickfix` | Generates a quickfix with key/value pairs | 12 - | `:YAMLTelescope` | Full path key/value fuzzy finder via [Telescope](https://github.com/nvim-telescope/telescope.nvim) **if installed** | 7 + | Command | Lua API | Description | 8 + |:--|:--|:--| 9 + | `:YAMLView` | `yaml.view()` | Shows the full path and value of the current key/value pair | 10 + | `: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 (`"`) | 11 + | `: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 (`"`) | 12 + | `:YAMLYankValue [regster]` | `yaml.yank_value([register])` | Yanks the value of the current key/value pair. The default register is the unnamed one (`"`) | 13 + | `:YAMLQuickfix` | `yaml.quickfix()` | Generates a quickfix with key/value pairs | 14 + | `:YAMLTelescope` | `yaml.telescope()` | Full path key/value fuzzy finder via [Telescope](https://github.com/nvim-telescope/telescope.nvim) **if installed** | 13 15 14 16 ![Example GIF](doc/demo.gif) 15 17 16 - It requires **Neovim 0.9** or newer, [`nvim-treesitter`](https://github.com/nvim-treesitter/nvim-treesitter) with [YAML support](https://github.com/ikatyang/tree-sitter-yaml). Telescope is **optional**. 18 + ## Requirements 19 + 20 + * **Neovim 0.9** or newer 21 + * [`nvim-treesitter`](https://github.com/nvim-treesitter/nvim-treesitter) with [YAML support](https://github.com/ikatyang/tree-sitter-yaml) 22 + 23 + Telescope is **optional**. 17 24 18 25 <details> 19 26 ··· 31 38 ```lua 32 39 use { 33 40 "cuducos/yaml.nvim", 34 - ft = {"yaml"}, -- optional 41 + ft = { "yaml" }, -- optional 35 42 requires = { 36 43 "nvim-treesitter/nvim-treesitter", 37 44 "nvim-telescope/telescope.nvim" -- optional
+20 -20
lua/yaml_nvim/init.lua
··· 29 29 restore_filetype(restore_to) 30 30 end 31 31 32 - -- created in _G so we can call it with v:lua 33 - _G.create_yaml_quickfix = function() 34 - local restore_to = set_yaml_as_filetype() 35 - local lines = {} 36 - 37 - for _, key in pairs(document.all_keys()) do 38 - if not document.is_value_a_block(key) then 39 - local parsed = pair.parse(key) 40 - table.insert(lines, parsed.errorformat) 41 - end 42 - end 43 - 44 - restore_filetype(restore_to) 45 - return lines 46 - end 47 - 48 32 local yank = function(key, value, register) 49 33 register = register or [["]] 50 34 if not key and not value then ··· 84 68 end) 85 69 end 86 70 87 - M.yank_all = function(register) 71 + M.yank = function(register) 88 72 assure_yaml_filetype(yank, true, true, register) 89 73 end 90 74 ··· 96 80 assure_yaml_filetype(yank, false, true, register) 97 81 end 98 82 83 + M.quickfix = function() 84 + local restore_to = set_yaml_as_filetype() 85 + local lines = {} 86 + 87 + for _, key in pairs(document.all_keys()) do 88 + if not document.is_value_a_block(key) then 89 + local parsed = pair.parse(key) 90 + table.insert(lines, parsed.quickfix) 91 + end 92 + end 93 + 94 + restore_filetype(restore_to) 95 + vim.fn.setqflist(lines) 96 + end 97 + 99 98 M.telescope = function() 100 99 if not has_telescope then 101 100 return 102 101 end 103 - vim.cmd("cex v:lua.create_yaml_quickfix()") 102 + 103 + M.quickfix() 104 104 require("telescope.builtin").quickfix() 105 105 end 106 106 107 107 vim.cmd("command! YAMLView lua require('yaml_nvim').view()") 108 - vim.cmd("command! -nargs=? YAMLYank lua require('yaml_nvim').yank_all(<f-args>)") 108 + vim.cmd("command! -nargs=? YAMLYank lua require('yaml_nvim').yank(<f-args>)") 109 109 vim.cmd("command! -nargs=? YAMLYankKey lua require('yaml_nvim').yank_key(<f-args>)") 110 110 vim.cmd("command! -nargs=? YAMLYankValue lua require('yaml_nvim').yank_value(<f-args>)") 111 - vim.cmd("command! YAMLQuickfix cex v:lua.create_yaml_quickfix()") 111 + vim.cmd("command! YAMLQuickfix lua require('yaml_nvim').quickfix()") 112 112 113 113 if has_telescope then 114 114 vim.cmd("command! YAMLTelescope lua require('yaml_nvim').telescope()")
+8 -3
lua/yaml_nvim/pair.lua
··· 89 89 local bufnr = vim.api.nvim_get_current_buf() 90 90 local key = get_keys(node, bufnr) 91 91 local value = get_value(node, bufnr) 92 - local line, _ = node:start() 93 - local path = vim.api.nvim_buf_get_name(bufnr) 92 + local line, col = node:start() 94 93 local cleaned_value = clean_up_block_value(value) 95 94 local human = string.format("%s = %s", key, cleaned_value) 96 95 ··· 98 97 key = key, 99 98 cleaned_value = cleaned_value, 100 99 human = human, 101 - errorformat = string.format("%s:%d: %s", path, line + 1, human), 100 + quickfix = { 101 + bufnr = bufnr, 102 + col = col, 103 + lnum = line, 104 + text = human, 105 + valid = 1, 106 + }, 102 107 } 103 108 end 104 109