Skip to content

Commit

Permalink
feat!: remove open_programs and use vim.ui.open() (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
saecki authored Aug 8, 2024
1 parent 1c924d5 commit 69b22bc
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 57 deletions.
10 changes: 3 additions & 7 deletions doc/crates.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ For more information about individual config options see |crates-config|.
notification_title = "crates.nvim",
curl_args = { "-sL", "--retry", "1" },
max_parallel_requests = 80,
open_programs = { "xdg-open", "open" },
expand_crate_moves_cursor = true,
enable_update_available_warning = true,
on_attach = function(bufnr) end,
Expand Down Expand Up @@ -624,12 +623,6 @@ max_parallel_requests *crates-config-max_parallel_requests*
Maximum number of parallel requests.


open_programs *crates-config-open_programs*
Type: `string[]`, Default: `{ "xdg-open", "open" }`

A list of programs that used to open urls.


expand_crate_moves_cursor *crates-config-expand_crate_moves_cursor*
Type: `boolean`, Default: `true`

Expand Down Expand Up @@ -705,6 +698,9 @@ text.error *crates-config-text-error*
Format string used when there was an error loading crate information.


open_programs *crates-config-open_programs*
DEPRECATED

highlight *crates-config-highlight*
Section type: `HighlightConfig`

Expand Down
1 change: 0 additions & 1 deletion docgen/wiki/Documentation-unstable.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ require("crates").setup {
notification_title = "crates.nvim",
curl_args = { "-sL", "--retry", "1" },
max_parallel_requests = 80,
open_programs = { "xdg-open", "open" },
expand_crate_moves_cursor = true,
enable_update_available_warning = true,
on_attach = function(bufnr) end,
Expand Down
57 changes: 31 additions & 26 deletions lua/crates/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ local M = {}
---@class Deprecated
---@field new_field string[]?
---@field hard boolean?
---@field msg string?

---@param schema table<string,SchemaElement>|SchemaElement[]
---@param elem SchemaElement
Expand Down Expand Up @@ -210,14 +211,6 @@ entry(M.schema, {
Maximum number of parallel requests.
]],
})
entry(M.schema, {
name = "open_programs",
type = STRING_ARRAY_TYPE,
default = { "xdg-open", "open" },
description = [[
A list of programs that used to open urls.
]],
})
entry(M.schema, {
name = "expand_crate_moves_cursor",
type = BOOLEAN_TYPE,
Expand Down Expand Up @@ -324,6 +317,15 @@ entry(schema_text, {
Format string used when there was an error loading crate information.
]],
})
-- DEPRECATED
entry(M.schema, {
name = "open_programs",
type = STRING_ARRAY_TYPE,
deprecated = {
msg = ", `vim.ui.open()` is used instead",
hard = true,
},
})


local schema_hl = section_entry(M.schema, {
Expand Down Expand Up @@ -1746,41 +1748,44 @@ local function validate_schema(path, schema, user_config)
local dep = elem.deprecated

if dep then
if dep.new_field then
---@type string
local dep_text
if dep.hard then
dep_text = "deprecated and won't work anymore"
else
dep_text = "deprecated and will stop working soon"
end
---@type string
local msg
if dep.msg then
msg = dep.msg
elseif dep.hard or not dep.new_field then
msg = " and won't work anymore"
else
msg = " and will stop working soon"
end

if dep.new_field then
warn(
"'%s' is now %s, please use '%s'",
"`%s` is now deprecated%s\nPlease use `%s`",
table.concat(p, "."),
dep_text,
msg,
table.concat(dep.new_field, ".")
)
else
warn(
"'%s' is now deprecated, ignoring",
table.concat(p, ".")
"`%s` is now deprecated%s",
table.concat(p, "."),
msg
)
end
elseif elem.type.config_type == "section" then
if value_type == "table" then
validate_schema(p, elem.fields, v)
else
warn(
"Config field '%s' was expected to be of type 'table' but was '%s', using default value.",
"Config field `%s` was expected to be of type `table` but was `%s`, using default value.",
table.concat(p, "."),
value_type
)
end
else
if not matches_type(value_type, elem.type) then
warn(
"Config field '%s' was expected to be of type '%s' but was '%s', using default value.",
"Config field `%s` was expected to be of type `%s` but was `%s`, using default value.",
table.concat(p, "."),
to_user_config_type_string(elem.type),
value_type
Expand All @@ -1789,7 +1794,7 @@ local function validate_schema(path, schema, user_config)
end
else
warn(
"Ignoring invalid config key '%s'",
"Ignoring invalid config key `%s`",
table.concat(p, ".")
)
end
Expand All @@ -1800,15 +1805,15 @@ end
---@return Config
local function setup_neoconf(config)
---@type boolean, table
local ok, neoconf = pcall(require, 'neoconf')
local ok, neoconf = pcall(require, "neoconf")
if not ok then
return config
end

-- enables neodev to autocomplete settings in .neoconf.json
pcall(function()
---@type table
local neoconf_plugins = require('neoconf.plugins')
local neoconf_plugins = require("neoconf.plugins")
neoconf_plugins.register {
on_schema = function(schema)
schema:import("crates", config)
Expand Down Expand Up @@ -1871,7 +1876,7 @@ function M.build(user_config)
user_config = user_config or {}
local user_config_type = type(user_config)
if user_config_type ~= "table" then
warn("Expected config of type 'table' found '%s'", user_config_type)
warn("Expected config of type `table` found `%s`", user_config_type)
user_config = {}
end

Expand Down
2 changes: 0 additions & 2 deletions lua/crates/config/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
---@field notification_title string
---@field curl_args string[]
---@field max_parallel_requests integer
---@field open_programs string[]
---@field expand_crate_moves_cursor boolean
---@field enable_update_available_warning boolean
---@field on_attach fun(bufnr: integer)
Expand Down Expand Up @@ -231,7 +230,6 @@
---@field public notification_title? string
---@field public curl_args? string[]
---@field public max_parallel_requests? integer
---@field public open_programs? string[]
---@field public expand_crate_moves_cursor? boolean
---@field public enable_update_available_warning? boolean
---@field public on_attach? fun(bufnr: integer)
Expand Down
14 changes: 0 additions & 14 deletions lua/crates/health.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,6 @@ function M.check()
else
vim.health.error("curl not found")
end

local num = 0
for _, prg in ipairs(state.cfg.open_programs) do
if util.binary_installed(prg) then
vim.health.ok(string.format("%s installed", prg))
---@type integer
num = num + 1
end
end

if num == 0 then
local programs = table.concat(state.cfg.open_programs, " ")
vim.health.warn("none of the following are installed " .. programs)
end
end

return M
10 changes: 3 additions & 7 deletions lua/crates/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -233,14 +233,10 @@ end

---@param url string
function M.open_url(url)
for _, prg in ipairs(state.cfg.open_programs) do
local ok, result = pcall(vim.cmd, string.format("silent !%s %s", prg, url))
if ok == true then
return
end
local _cmd, err = vim.ui.open(url)
if err then
M.notify(vim.log.levels.ERROR, "Couldn't open url: %s", err)
end

M.notify(vim.log.levels.WARN, "Couldn't open url")
end

return M

0 comments on commit 69b22bc

Please sign in to comment.