Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add handler for Cargo.toml #41

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ require("lazy").setup({
handler_options = {
search_engine = "google", -- you can select between google, bing, duckduckgo, and ecosia
search_engine = "https://search.brave.com/search?q=", -- or you can pass in a custom search engine
crate_registry = "crates.io", -- you can select between crates.io and docs.rs
},
} end,
},
Expand Down
2 changes: 2 additions & 0 deletions lua/gx/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ local github_handler = require("gx.handlers.github")
local commit_handler = require("gx.handlers.commit")
local markdown_handler = require("gx.handlers.markdown")
local search_handler = require("gx.handlers.search")
local cargo_toml_handler = require("gx.handlers.cargo_toml")

local M = {}

Expand All @@ -30,6 +31,7 @@ function M.get_url(mode, line, activated_handlers, handler_options)
-- ### add here new handlers
add_handler(handlers, brewfile_handler, activated_handlers.brewfile)
add_handler(handlers, package_json_handler, activated_handlers.package_json)
add_handler(handlers, cargo_toml_handler, activated_handlers.cargo_toml)
add_handler(handlers, plugin_handler, activated_handlers.plugin)
add_handler(handlers, github_handler, activated_handlers.github)
add_handler(handlers, commit_handler, activated_handlers.github)
Expand Down
27 changes: 27 additions & 0 deletions lua/gx/handlers/cargo_toml.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
local helper = require("gx.helper")

local M = {
-- only cargo.toml
filetype = { "toml" },
filename = "Cargo.toml",
}

-- navigate to neovim github plugin url
function M.handle(mode, line, handler_options)
local pattern = "(%w+)%s-=%s"
local crate = helper.find(line, mode, pattern)
if not crate then
return
end

if handler_options.crate_registry == "crates.io" then
return "https://crates.io/crates/" .. crate
elseif handler_options.crate_registry == "docs.rs" then
return "https://docs.rs/" .. crate
else
require("gx.notifier").error(handler_options.crate_registry .. " is not supported")
return
end
end

return M
6 changes: 6 additions & 0 deletions lua/gx/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,15 @@ local function with_defaults(options)
true
),
search = helper.ternary(options.handlers.search ~= nil, options.handlers.search, true),
cargo_toml = helper.ternary(
options.handlers.cargo_toml ~= nil,
options.handlers.cargo_toml,
true
),
},
handler_options = {
search_engine = options.handler_options.search_engine or "google",
crate_registry = options.handler_options.crate_registry or "crates.io",
},
}
end
Expand Down
30 changes: 30 additions & 0 deletions test/spec/gx/handlers/cargo_toml_handler_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
local handler = require("gx.handlers.cargo_toml")

describe("cargo_toml_handler_does_work", function()
it("crates.io", function()
local handler_options = {
crate_registry = "crates.io",
}

assert.equals(
"https://crates.io/crates/clap",
handler.handle("v", 'clap = { version = "4.0.18", features = ["derive"] }', handler_options)
)
assert.equals(
"https://crates.io/crates/regex",
handler.handle("v", 'regex = "1"', handler_options)
)
end)

it("docs.rs", function()
local handler_options = {
crate_registry = "docs.rs",
}

assert.equals(
"https://docs.rs/clap",
handler.handle("v", 'clap = { version = "4.0.18", features = ["derive"] }', handler_options)
)
assert.equals("https://docs.rs/regex", handler.handle("v", 'regex = "1"', handler_options))
end)
end)