b0o / SchemaStore.nvim

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

Get current schema for buffer #20

Closed gegoune closed 11 months ago

gegoune commented 12 months ago

Hey! I would like to display current schema in status line. Is it possible to access it somehow? Thanks!

b0o commented 12 months ago

I like that idea. However, this plugin only provides the list of schemas to jsonls/yamlls, it doesn’t know what schema is active. That would be dependent on jsonls/yamlls.

yogeshlonkar commented 7 months ago

In case someone stumbles upon this issue

local method = 'yaml/get/jsonSchema'
local sync_timeout = 5000

local function current_schema()
  local bufnr = vim.api.nvim_get_current_buf()
  if not bufnr then
    print('no current buffer')
    return
  elseif vim.bo[bufnr].filetype ~= 'yaml' then
    print('current buffer is not yaml')
    return
  end
  local clients = vim.lsp.get_active_clients({name = 'yamlls', bufnr = bufnr})
  if not clients or #clients == 0 or not clients[1] then
    print('no yamlls client')
    return
  end
  local client = clients[1]
  local response, error = client.request_sync(method, {vim.uri_from_bufnr(bufnr)}, sync_timeout, bufnr)
  if error then
    print('bufnr=%d error=%s', bufnr, error)
    return
  elseif not response then
    print('bufnr=%d response=nil', bufnr)
    return
  elseif response.err then
    print('bufnr=%d response.err=%s', bufnr, response.err)
    return
  elseif #response.result == 0 then
    print('no schema for current buffer')
    return
  end
  local schema = response.result[1]
  print('bufnr=%d schema=%s', bufnr, vim.inspect(schema))
  -- comment above line and uncomment blow if you have nvim-notify
  vim.notify(schema.description .. '\n' .. (schema.uri or ''), vim.log.levels.INFO, {title = schema.name})
end
mikybars commented 1 month ago

Or you can install yaml-companion