Forked from the original repository someone-stole-my-name/yaml-companion.nvim and expanded for a bit more modularity to work with multiple language servers like yaml-language-server
and helm-ls
at the same time as well as automatic Kubernetes CRD detection. I have been happily using the plugin with some caveats, so I wanted to refactor it a bit to match my current mostly Kubernetes working environment.
Currently in the dogfooding stage with matching all the resources, but the following features are available.
- Ability to add any kind of matcher, that can detect schema based on content.
- Kubernetes resources can be matched utilizing the repository yannh/kubernetes-json-schema.
- Kubernetes CRD definitions can be matched utilizing the repository datreeio/CRDs-catalog.
- Change matcher variables on the fly, like the Kubernetes version. Do not be stuck with whatever
yaml-language-server
has hardcoded at the given time. - Select one from multiple matchers for the current buffer to not have any collisions in the
yaml-language-server
.
return {
"cenk1cenk2/schema-companion.nvim",
dependencies = {
{ "nvim-lua/plenary.nvim" },
{ "nvim-telescope/telescope.nvim" },
},
config = function()
require("schema-companion").setup({
-- if you have telescope you can register the extension
enable_telescope = true,
matchers = {
-- add your matchers
require("schema-companion.matchers.kubernetes").setup({ version = "master" }),
},
})
end,
}
Plugin has to be configured once, and the language servers can be added by extending the LSP configuration.
The default plugin configuration for the setup function is as below.
require("schema-companion").setup({
log_level = vim.log.levels.INFO,
enable_telescope = false,
matchers = {},
schemas = {},
}
You can automatically extend your configuration of the yaml-language-server
or helm-ls
with the following configuration.
require("lspconfig").yamlls.setup(require("schema-companion").setup_client({
-- your yaml language server configuration
}))
You can add custom schemas that can be activated manually through the telescope picker.
schemas = {
{
name = "Kubernetes master",
uri = "https://raw.githubusercontent.com/yannh/kubernetes-json-schema/master/master-standalone-strict/all.json",
},
{
name = "Kubernetes v1.30",
uri = "https://raw.githubusercontent.com/yannh/kubernetes-json-schema/master/v1.30.3-standalone-strict/all.json",
},
}
Adding custom matchers are as easy as defining a function that detects and handles a schema.
You can map the telescope
picker to any keybinding of your choice.
require("telescope").extensions.yaml_schema.select_schema()
If there are multiple matches for the buffer, you can select the schema manually from the ones that matches.
require("telescope").extensions.yaml_schema.select_from_matching_schemas()
local schema = require("schema-companion").get_buf_schema(vim.api.nvim_get_current_buf())
This can be further utilized in lualine
as follows.
-- your lualine configuration
require("lualine").setup({
sections = {
lualine_c = {
{
function()
return ("%s"):format(require("schema-companion.context").get_buffer_schema(0).name)
end,
cond = function()
return package.loaded["schema-companion"]
end,
},
},
},
})