b0o / SchemaStore.nvim

🛍 JSON schemas for Neovim
https://schemastore.org
Apache License 2.0
738 stars 18 forks source link

[Help] Trying to load schema for LSP to use with neoconf #42

Open Ajaymamtora opened 2 months ago

Ajaymamtora commented 2 months ago

Hi I can't get this plugin to suggest anything from local schema files in jsonls.

I'm loading the local schemas (1 of my own, the rest being these https://github.com/folke/neoconf.nvim/blob/main/schemas) via my setup:

modules["b0o/schemastore.nvim"] = {
  version = false,
  lazy = false,
  config = function()
    local schemastore_status_ok, _ = pcall(require, "schemastore")
    if not schemastore_status_ok then
      return
    end

    local extra_schemas = {
      {
        description = "Schema for nvim project config",
        fileMatch = { "nvim.json", ".nvim.json" },
        name = "nvim.json",
        url = "file://" .. _G.__os.home .. "/.config/json_schema/nvim.json",
      },
    }

    local schema_path = _G.__os.home .. "/.local/share/nvim/lazy/neoconf.nvim/schemas"

    local function add_schema(file)
      local name = file:match("(.+)%.json$")
      if name then
        table.insert(extra_schemas, {
          description = "Schema for " .. name,
          fileMatch = { "nvim.json", ".nvim.json" },
          name = "nvim.json",
          url = "file://" .. schema_path .. "/" .. file,
        })
      end
    end

    local handle = vim.loop.fs_scandir(schema_path)
    if handle then
      local name, type
      while true do
        name, type = vim.loop.fs_scandir_next(handle)
        if not name then
          break
        end
        if type == "file" and name:match("%.json$") then
          add_schema(name)
        end
      end
    end

    require("schemastore").json.schemas({
      extra = extra_schemas,
    })
  end,
}

-- LSP Setup:
M.jsonls_config = function(file_types)
  return {
    flags = {
      debounce_text_changes = default_debounce_time,
    },
    autostart = true,
    filetypes = file_types,
    on_attach = function(client, bufnr)
      setup_diagnostics.keymaps(client, bufnr)
      setup_diagnostics.omni(client, bufnr)
      setup_diagnostics.tag(client, bufnr)
      if client.server_capabilities.document_highlight then
        setup_diagnostics.document_highlight(client, bufnr)
      end
      setup_diagnostics.document_formatting(client, bufnr)
      setup_diagnostics.inlay_hint(client, bufnr)
      if client.server_capabilities.documentSymbolProvider then
        navic.attach(client, bufnr)
      end
    end,
    capabilities = setup_diagnostics.get_capabilities(),
    root_dir = function(fname)
      return util.find_git_ancestor(fname) or vim.fn.getcwd()
    end,
    settings = {
      json = {
        validate = { enable = true },
        schemas = require("schemastore").json.schemas({
          extra = {
            {
              description = "Schema for nvim project config",
              fileMatch = { "nvim.json", ".nvim.json" },
              name = "nvim.json",
              url = "file://" .. _G.__os.home .. "/.config/json_schema/nvim.json",
            },
          },
        }),
      },
    },
  }
end

It successfully adds to extras but nothing is suggested by the LSP at all.

I'm triggering nvim cmp in files named nvim.json and .nvim.json and neither works

Please can I have some help?

Ajaymamtora commented 2 months ago

In json files that don't match my patterns, the LSP suggests $schema and then a bunch of nonsense - not sure what happened here?

image

None of these exist in my local schemas

b0o commented 2 months ago

Calling schemastore.json.schemas() does not mutate the underlying catalog, it returns a deep copy of the catalog with the extra schemas added. Therefore, this will have no effect:

modules["b0o/schemastore.nvim"] = {
  version = false,
  lazy = false,
  config = function()
    -- ...
    require("schemastore").json.schemas({
      extra = extra_schemas,
    })
  end,
}

You will need to modify your config to pass extra_schemas here:

-- LSP Setup:
M.jsonls_config = function(file_types)
  return {
    -- ...
    settings = {
      json = {
        validate = { enable = true },
        schemas = require("schemastore").json.schemas({ -- pass extra_schemas to this call
          extra = {
            {
              description = "Schema for nvim project config",
              fileMatch = { "nvim.json", ".nvim.json" },
              name = "nvim.json",
              url = "file://" .. _G.__os.home .. "/.config/json_schema/nvim.json",
            },
          },
        }),
      },
    },
  }
end
Ajaymamtora commented 2 months ago

Thanks, I've updated it as you suggested and temporarily simplified it like so:

  local extra_schemas = {
    {
      description = "ASTRO",
      fileMatch = { "nvim.json", ".nvim.json" },
      name = "nvim.json",
      url = "/Users/ajay.mamtora/.local/share/nvim/lazy/neoconf.nvim/schemas/astro.json",
    },
  }
...
    settings = {
      json = {
        validate = { enable = true },
        schemas = require("schemastore").json.schemas({
          extra = extra_schemas,
        }),
      },
    },

And the jsonls lsp isn't suggesting properties defined in "https://github.com/folke/neoconf.nvim/blob/main/schemas/astro.json" -- am I misunderstanding something?

All I see is this: image

image