-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from jacksonludwig/main
feat: choose schema with vim.ui.select
- Loading branch information
Showing
5 changed files
with
74 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |