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

Any Rust Support? #250

Open
goldcoders opened this issue Mar 1, 2022 · 7 comments
Open

Any Rust Support? #250

goldcoders opened this issue Mar 1, 2022 · 7 comments

Comments

@goldcoders
Copy link

Im looking for a refactoring plugin in neovim,
but when i check the language supported
no rust yet, hopefully it will be added

anyway thanks for creating this nvim plugin

:)

@ThePrimeagen
Copy link
Owner

No!

But its not to hard at this point.

The hard parts are types which is why I am a bit hesitant. LSP Integration is back on the table for me so that I can create type annotations as well.

@ten3roberts
Copy link

Fantastic! Would love rust, especially the extract functionality

@Mathijs-Bakker
Copy link

Rust Analyzer offers Extract to Function.
In Neovim you can set up Code Actions to show the extract function in Visual mode.

Eg:

vim.keymap.set({'v', 'n'}, '<Leader>ca', vim.lsp.buf.code_action)

From the Rust Analyzer manual:
https://rust-analyzer.github.io/manual.html

Before:

fn main() {
    let n = 1;
    ┃let m = n + 2;
    // calculate
    let k = m + n;┃
    let g = 3;
}

After:

fn main() {
    let n = 1;
    fun_name(n);
    let g = 3;
}

fn ┃fun_name(n: i32) {
    let m = n + 2;
    // calculate
    let k = m + n;
}

Note: Cursor position or selection is signified by character.

@emmanueltouzery
Copy link

@Mathijs-Bakker thank you, this looks great! however this did not work for me on that sample, it only offers me refactorings such as change the format of the 2 to hex, or change let to if let. i have the latest version of rust-analyzer. 2022-09-19
I realize i'm a littel off-topic here, but did you set up anything in particular in your:

      lspconfig.rust_analyzer.setup {}

to get this to work? thank you!

@Mathijs-Bakker
Copy link

Mathijs-Bakker commented Sep 21, 2022

@emmanueltouzery

I am using rust-tools.nvim
https://github.com/simrat39/rust-tools.nvim

For the setup options:

require('rust-tools').setup(opts)

I have a default setup.

But for the rust-analyzer settings I have this:

-- all the opts to send to nvim-lspconfig
-- these override the defaults set by rust-tools.nvim
-- see https://github.com/neovim/nvim-lspconfig/blob/master/doc/server_configurations.md#rust_analyzer

server = {
    -- cmd_env = requested_server._default_options.cmd_env,
    settings = {
      ['rust-analyzer'] = {
        assist = {
          importEnforceGranularity = true,
          importPrefix = 'crate',
        },
        cargo = {
          allFeatures = true,
        },
        checkOnSave = {
          -- default: `cargo check`
          command = 'clippy',
        },
      },
      inlayHints = {
        lifetimeElisionHints = {
          enable = true,
          useParameterNames = true,
        },
      },
    },
    -- standalone file support
    -- setting it to false may improve startup time
    standalone = true,
  }, -- rust-analyer options

The whole setup:
https://github.com/Mathijs-Bakker/dotfiles/blob/master/nvim/lua/lsp/rust.lua

Hope you get it to work!

@ThePrimeagen
Copy link
Owner

btw, i am likely to start making rust / ts better. so,... its on my radar.

its just open source is so... its so open source.

@Mathijs-Bakker
Copy link

@emmanueltouzery @ThePrimeagen

How it's working now (without refactor plugin):

Extract into Function
https://www.youtube.com/watch?v=i-ftg90GEEw

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants