Skip to content

Commit

Permalink
Merge pull request #9 from jacksonludwig/main
Browse files Browse the repository at this point in the history
feat: choose schema with vim.ui.select
  • Loading branch information
someone-stole-my-name committed May 22, 2022
2 parents 84b5a25 + 0186083 commit 1b6aea2
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 20 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ No mappings included, you need to map it yourself or call it manually:
:Telescope yaml_schema
```

Alternatively, you can use `vim.ui.select` to use the picker of your choice. In that case, you can bind/call the function:
```lua
require("yaml-companion").open_ui_select()
```

### Get the schema name for the current buffer

You can show the current schema in your statusline using a function like:
Expand Down
21 changes: 3 additions & 18 deletions lua/telescope/_extensions/yaml_schema_builtin.lua
Original file line number Diff line number Diff line change
@@ -1,34 +1,19 @@
local M = {}

local lsp = require("yaml-companion.lsp.requests")
local matchers = require("yaml-companion._matchers")._loaded
local lsp = require("yaml-companion.lsp.util")
local pickers = require("telescope.pickers")
local finders = require("telescope.finders")
local conf = require("telescope.config").values
local actions = require("telescope.actions")
local action_state = require("telescope.actions.state")

local yaml_schema = function(opts)
local results = lsp.get_all_jsonschemas(0)
local results = lsp.get_all_yaml_schemas()

if results == nil then
return
end

-- merge with user defined schemas
results = vim.tbl_deep_extend(
"force",
results.result,
require("yaml-companion.config").options.schemas.result or {}
)

-- merge with matchers exposed schemas
for _, matcher in pairs(matchers) do
local handles = matcher.handles() or {}
for _, schema in ipairs(handles) do
table.insert(results, schema)
end
end

opts = opts or {}
pickers.new(opts, {
prompt_title = "Schema",
Expand Down
5 changes: 5 additions & 0 deletions lua/yaml-companion/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,9 @@ M.load_matcher = function(name)
return _matchers.load(name)
end

--- Opens a vim.ui.select menu to choose a schema
M.open_ui_select = function()
require("yaml-companion.select.ui").open_ui_select()
end

return M
33 changes: 31 additions & 2 deletions lua/yaml-companion/lsp/util.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,44 @@
local M = {}

local lsp = vim.lsp
local nvim_lsp = vim.lsp
local yaml_lsp = require("yaml-companion.lsp.requests")
local matchers = require("yaml-companion._matchers")._loaded

-- returns the yamlls client attached to {bufnr} if it has an active yamlls attached
M.client = function(bufnr)
local clients = lsp.buf_get_clients(bufnr)
local clients = nvim_lsp.buf_get_clients(bufnr)
for _, value in pairs(clients) do
if value.name == "yamlls" then
return value
end
end
end

--- Get all of the yaml schemas currently available to the server.
--- @return table schemas: merged list of user-defined and server-provided yaml schemas
M.get_all_yaml_schemas = function()
local schemas = yaml_lsp.get_all_jsonschemas(0)

if schemas == nil then
return
end

-- merge with user defined schemas
schemas = vim.tbl_deep_extend(
"force",
schemas.result,
require("yaml-companion.config").options.schemas.result or {}
)

-- merge with matchers exposed schemas
for _, matcher in pairs(matchers) do
local handles = matcher.handles() or {}
for _, schema in ipairs(handles) do
table.insert(schemas, schema)
end
end

return schemas
end

return M
30 changes: 30 additions & 0 deletions lua/yaml-companion/select/ui.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
local M = {}

local lsp = require("yaml-companion.lsp.util")
local matchers = require("yaml-companion._matchers")._loaded

--- Callback to be passed to vim.ui.select to display a single schema item
--- @param schema table: Schema
local display_schema_item = function(schema)
return schema.name
end

--- Callback to be passed to vim.ui.select that changes the active yaml schema
--- @param schema table: Chosen schema
local select_schema = function(schema)
local selected_schema = { result = { { name = schema.name, uri = schema.uri } } }
require("yaml-companion.context").schema(0, selected_schema)
end

M.open_ui_select = function()
local schemas = lsp.get_all_yaml_schemas()

-- Don't open selection if there are no available schemas
if schemas == nil then
return
end

vim.ui.select(schemas, { format_item = display_schema_item, prompt = "Schema" }, select_schema)
end

return M

0 comments on commit 1b6aea2

Please sign in to comment.