SchemaStore.nvim icon indicating copy to clipboard operation
SchemaStore.nvim copied to clipboard

Support `vim.lsp.config()`

Open ofseed opened this issue 8 months ago • 11 comments

This plugin could provide lsp/* files to avoid dependency on lspconfig.

ofseed avatar Apr 24 '25 12:04 ofseed

It actually supports vim.lp.config(...), maybe just not lazy loading:

vim.lsp.config("jsonls", {
    settings = {
        json = {
            schemas = require("schemastore").json.schemas(),
            validate = { enable = true },
        },
    },
})

carlos-algms avatar May 14 '25 13:05 carlos-algms

My approach is to harness the ability of on_init hook: That's the content of my lsp/jsonls.lua:

return {
    -- use on_init hook to defer the schema load as third-party plugins are not loaded
    on_init = function (client)
        client.config.settings.json = vim.tbl_deep_extend("force", client.config.settings.json, {
            schemas = require('schemastore').json.schemas(),
            validate = { enable = true },
        })
    end
}

Here's my full configuration: nvim config

ZWindL avatar May 16 '25 03:05 ZWindL

My approach is to harness the ability of on_init hook: That's the content of my lsp/jsonls.lua:

return { -- use on_init hook to defer the schema load as third-party plugins are not loaded on_init = function (client) client.config.settings.json = vim.tbl_deep_extend("force", client.config.settings.json, { schemas = require('schemastore').json.schemas(), validate = { enable = true }, }) end }

Here's my full configuration: nvim config

@ZWindL, it doesn't work for me. It seems that injecting the config directly into the client instance doesn't have any effect.

carlos-algms avatar May 16 '25 06:05 carlos-algms

@carlos-algms , have you enabled the lsp client by vim.lsp.enable('jsonls')?

ZWindL avatar May 16 '25 07:05 ZWindL

Yeah, it's working without the on_init you suggested. By moving this line schemas = require('schemastore').json.schemas(),, to the on_init, it stops working.

carlos-algms avatar May 16 '25 07:05 carlos-algms

Wait, mine doesn't work either now, I need a closer look. 🤔

ZWindL avatar May 16 '25 08:05 ZWindL

It needs to be set before the language server starts. Try this:

---@type vim.lsp.Config
return {
	before_init = function(_, client_config)
		client_config.settings.json = require('schemastore').json.schemas()
	end
}

Bahex avatar May 18 '25 04:05 Bahex

It needs to be set before the language server starts. Try this:

---@type vim.lsp.Config return { before_init = function(_, client_config) client_config.settings.json = require('schemastore').json.schemas() end }

there's a typo, but it works, thanks !!!!

- client_config.settings.json = require('schemastore').json.schemas()
+ client_config.settings.json.schemas = require('schemastore').json.schemas()

carlos-algms avatar May 18 '25 09:05 carlos-algms

it will not work if the settings table is nil:

---@diagnostic disable: missing-fields, inject-field

---@type vim.lsp.ClientConfig
return {
    settings = {
        json = {
            validate = {
                enable = true,
            }
        }
    },
    before_init = function(_, config)
        -- can't assign new table because of
        -- https://github.com/neovim/neovim/issues/27740#issuecomment-1978629315
        config.settings.json.schemas = require("schemastore").json.schemas()
    end,
}

remvn avatar Jun 14 '25 11:06 remvn

My approach is to harness the ability of on_init hook: That's the content of my lsp/jsonls.lua:

return { -- use on_init hook to defer the schema load as third-party plugins are not loaded on_init = function (client) client.config.settings.json = vim.tbl_deep_extend("force", client.config.settings.json, { schemas = require('schemastore').json.schemas(), validate = { enable = true }, }) end }

Here's my full configuration: nvim config

but how to write dependencies if I use lazy.nvim or packer.nvim

cxwx avatar Aug 20 '25 14:08 cxwx

Here's how I do it for globally installed json-ls/yaml-ls: https://github.com/polyzen/dotfiles/blob/18c6ffde934da6161b718aa0dd9cf04b068cdcf1/base/.config/nvim/lua/plugins.lua#L532-L542

polyzen avatar Aug 20 '25 22:08 polyzen