chrishrb / gx.nvim

Implementation of gx without the need of netrw
Apache License 2.0
186 stars 19 forks source link

Handler for Rust's Cargo #40

Closed augustocdias closed 3 months ago

augustocdias commented 5 months ago

Similar to package.json it would be nice to support Ruts's cargo dependencies. Opening in either crates.io or docs.rs

chrishrb commented 5 months ago

can you test this handler if it works for you? I'm not really using rust so I have little to no knowledge šŸ˜„

augustocdias commented 5 months ago

It works for the most important use cases. You can point a dependency to a git repo, but you have the url there and calling gx on the url goes to the url instead of crates.io, so I think this is not important.

chrishrb commented 5 months ago

i'm not sure if i understand you correctly. So for example you have a dependency like the following:

regex = { git = "https://github.com/rust-lang/regex.git" }

if you press gx and your cursor is on the regex part you go to crates.io/crates/regex and if you press gx on the url part you go to the github repo - isnā€˜t that supposed to happen?

chrishrb commented 5 months ago

btw please provide me with edge cases where gx doesn't work. The more data I have, the better the tool will become šŸ‘

augustocdias commented 5 months ago

i'm not sure if i understand you correctly. So for example you have a dependency like the following:

regex = { git = "https://github.com/rust-lang/regex.git" }

if you press gx and your cursor is on the regex part you go to crates.io/crates/regex and if you press gx on the url part you go to the github repo - isnā€˜t that supposed to happen?

This is what I expected to happen yes. Though someone might have a different opinion. Since the dependency is pointing to a git repo, someone might expect to gx with the cursor on regex and go to the github url instead of crates.io

btw please provide me with edge cases where gx doesn't work. The more data I have, the better the tool will become šŸ‘

There are other ways to declare dependencies:

[patch.crates-io.openmls]
package = "openmls"
git = "https://github.com/wireapp/openmls"
branch = "feat/rfc9420"

If you want to follow the current behavior, calling Browse inside the brackets could got to crates.io too. The regex might be tricky though. You can see more examples here: https://github.com/wireapp/core-crypto/blob/develop/Cargo.toml And here: https://github.com/wireapp/core-crypto/blob/develop/crypto/Cargo.toml#L58-L63

chrishrb commented 3 months ago

Hmm.. I donā€™t think it would be a good idea to merge this hacky handler and I also donā€™t know how to improve this (I already tried something with treesitter, but it didnā€˜t work out). So my suggestion is to abandon this PR until I (or someone else) have the time and resources to properly implement this.

augustocdias commented 3 months ago

I think what you already implemented will be helpful for 99% of the time and users.

Maybe add a disclaimer in the readme about this "limitation" and merge?

chrishrb commented 3 months ago

Hm, as I said I'm not fully satisfied with the current behavior, so I donā€™t like to merge this PR. But you can add a custom rust handler (possible since #49) for this specific use case. E.g.:


require("lazy").setup({
{
  dir = "~/jam-dev/home/gx.nvim",
  keys = { { "gx", "<cmd>Browse<cr>", mode = { "n", "x" }} },
  cmd = { "Browse" },
  init = function ()
    vim.g.netrw_nogx = 1 -- disable netrw gx
  end,
  config = function() require("gx").setup{
    handlers = {
      rust = { -- custom handler to open rust's cargo packages
        filetype = { "toml" },
        filename = "Cargo.toml",
        handle = function(mode, line, _)
          local crate = require("gx.helper").find(line, mode, "(%w+)%s-=%s")

          if crate then
            return "https://crates.io/crates/" .. crate
          end
        end,
      },
    },
  } end,
},
})