Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
saecki committed Jul 29, 2024
1 parent d1be10c commit bd31be8
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 38 deletions.
55 changes: 48 additions & 7 deletions doc/crates.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,16 @@ 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,
open_commands = {
linux = { "xdg-open" },
bsd = { "xdg-open" },
macos = { "open" },
windows = { "start" },
other = { "open", "xdg-open" },
},
text = {
searching = "  Searching",
loading = "  Loading",
Expand Down Expand Up @@ -624,12 +630,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 All @@ -651,6 +651,47 @@ on_attach *crates-config-on_attach*
NOTE: Ignored if |crates-config-autoload| is disabled.


open_programs *crates-config-open_programs*
DEPRECATED

Please use |crates-config-open_commands| instead.

open_commands *crates-config-open_commands*
Section type: `OpenProgramsConfig`

Programs used to open urls.


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

A list of programs used to open urls on Linux.


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

A list of programs used to open urls on BSD.


open_commands.macos *crates-config-open_commands-macos*
Type: `string[]`, Default: `{ "open" }`

A list of programs used to open urls on macOS.


open_commands.windows *crates-config-open_commands-windows*
Type: `string[]`, Default: `{ "start" }`

A list of programs used to open urls on Windows.


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

A list of programs used to open urls when none of the above is found.


text *crates-config-text*
Section type: `TextConfig`

Expand Down
8 changes: 7 additions & 1 deletion docgen/wiki/Documentation-unstable.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,16 @@ 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,
open_commands = {
linux = { "xdg-open" },
bsd = { "xdg-open" },
macos = { "open" },
windows = { "start" },
other = { "open", "xdg-open" },
},
text = {
searching = "  Searching",
loading = "  Loading",
Expand Down
71 changes: 62 additions & 9 deletions lua/crates/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,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 @@ -248,6 +240,67 @@ entry(M.schema, {
NOTE: Ignored if |crates-config-autoload| is disabled.
]],
})
-- deprecated
entry(M.schema, {
name = "open_programs",
type = STRING_ARRAY_TYPE,
deprecated = {
new_field = { "open_commands" },
hard = true,
},
})

local schema_open_commands = section_entry(M.schema, {
name = "open_commands",
type = {
config_type = "section",
emmylua_annotation = "OpenProgramsConfig",
},
description = [[
Programs used to open urls.
]],
fields = {},
})
entry(schema_open_commands, {
name = "linux",
type = STRING_ARRAY_TYPE,
default = { "xdg-open" },
description = [[
A list of programs used to open urls on Linux.
]],
})
entry(schema_open_commands, {
name = "bsd",
type = STRING_ARRAY_TYPE,
default = { "xdg-open" },
description = [[
A list of programs used to open urls on BSD.
]],
})
entry(schema_open_commands, {
name = "macos",
type = STRING_ARRAY_TYPE,
default = { "open" },
description = [[
A list of programs used to open urls on macOS.
]],
})
entry(schema_open_commands, {
name = "windows",
type = STRING_ARRAY_TYPE,
default = { "start" },
description = [[
A list of programs used to open urls on Windows.
]],
})
entry(schema_open_commands, {
name = "other",
type = STRING_ARRAY_TYPE,
default = { "open", "xdg-open" },
description = [[
A list of programs used to open urls when none of the above is found.
]],
})

local schema_text = section_entry(M.schema, {
name = "text",
Expand Down Expand Up @@ -1532,7 +1585,7 @@ entry(M.schema, {
type = BOOLEAN_TYPE,
deprecated = {
new_field = { "completion" },
}
},
})


Expand Down
18 changes: 16 additions & 2 deletions lua/crates/config/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
---@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)
---@field open_commands OpenProgramsConfig
---@field text TextConfig
---@field highlight HighlightConfig
---@field diagnostic DiagnosticConfig
Expand All @@ -28,6 +28,13 @@
---@field null_ls NullLsConfig
---@field lsp LspConfig

---@class OpenProgramsConfig
---@field linux string[]
---@field bsd string[]
---@field macos string[]
---@field windows string[]
---@field other string[]

---@class TextConfig
---@field searching string
---@field loading string
Expand Down Expand Up @@ -231,17 +238,24 @@
---@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)
---@field public open_commands? crates.UserOpenProgramsConfig
---@field public text? crates.UserTextConfig
---@field public highlight? crates.UserHighlightConfig
---@field public popup? crates.UserPopupConfig
---@field public completion? crates.UserCompletionConfig
---@field public null_ls? crates.UserNullLsConfig
---@field public lsp? crates.UserLspConfig

---@class crates.UserOpenProgramsConfig
---@field public linux? string[]
---@field public bsd? string[]
---@field public macos? string[]
---@field public windows? string[]
---@field public other? string[]

---@class crates.UserTextConfig
---@field public searching? string
---@field public loading? string
Expand Down
24 changes: 17 additions & 7 deletions lua/crates/health.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
local M = {}

local state = require("crates.state")
local util = require("crates.util")

function M.check()
Expand All @@ -10,26 +9,37 @@ function M.check()
else
vim.health.info("null-ls.nvim not found")
end
if util.lualib_installed("neoconf") then
vim.health.ok("neoconf.nvim installed")
else
vim.health.info("neoconf.nvim not found")
end
if util.lualib_installed("coq_nvim") then
vim.health.ok("neoconf.nvim installed")
else
vim.health.info("neoconf.nvim not found")
end

vim.health.start("Checking external dependencies")
if util.binary_installed("curl") then
if util.is_executable("curl") then
vim.health.ok("curl installed")
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))
local open_commands = util.open_commands()
for _, cmd in ipairs(open_commands) do
if util.is_executable(cmd) then
vim.health.ok(string.format("%s installed", cmd))
---@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)
local commands = table.concat(open_commands, " ")
vim.health.warn("none of the following were found " .. commands)
end
end

Expand Down
32 changes: 20 additions & 12 deletions lua/crates/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ local FeatureInfo = {
}
M.FeatureInfo = FeatureInfo

local IS_WIN = vim.api.nvim_call_function("has", { "win32" }) == 1

---@return integer
function M.current_buf()
return vim.api.nvim_get_current_buf()
Expand Down Expand Up @@ -175,18 +173,12 @@ function M.lualib_installed(name)
return ok
end

---comment
---@param name string
---@return boolean
function M.binary_installed(name)
if IS_WIN then
name = name .. ".exe"
end

function M.is_executable(name)
return vim.fn.executable(name) == 1
end

---comment
---@param severity integer
---@param s string
---@param ... any
Expand All @@ -212,11 +204,27 @@ function M.lib_rs_url(name)
return "https://lib.rs/crates/" .. name
end

---@return string[]
function M.open_commands()
if vim.fn.has("linux") then
return state.cfg.open_commands.linux
elseif vim.fn.has("win32") then
return state.cfg.open_commands.windows
elseif vim.fn.has("macos") then
return state.cfg.open_commands.macos
elseif vim.fn.has("bsd") then
return state.cfg.open_commands.bsd
else
return state.cfg.open_commands.other
end
end

---@param url string
function M.open_url(url)
for _, prg in ipairs(state.cfg.open_programs) do
if M.binary_installed(prg) then
vim.cmd(string.format("silent !%s %s", prg, url))
local open_commands = M.open_commands()
for _, cmd in ipairs(open_commands) do
local ok = pcall(vim.cmd, string.format("silent !%s %s", cmd, url))
if ok then
return
end
end
Expand Down

0 comments on commit bd31be8

Please sign in to comment.