simrat39 / rust-tools.nvim

Tools for better development in rust using neovim's builtin lsp
MIT License
2.17k stars 160 forks source link

User Snippet Completions #399

Closed adaszko closed 1 year ago

adaszko commented 1 year ago

Hi 👋

Are User Snippet Completions supported? They’re more precise than language-agnostic snippets and thus more useful.

The only information I found on them is this unanswered question: https://neovim.discourse.group/t/how-to-add-custom-snippets-to-rust-analyzer/3806 . I tried this without success:

local rt = require("rust-tools")
rt.setup({
  server = {
    completion = {
        snippets = {
            custom = {
                ["thread spawn"] = {
                    prefix = { "spawn", "tspawn" },
                    body = { "thread::spawn(move || {", "   $0", "});" },
                    description = "Insert a thread::spawn call",
                    requires = "std::thread",
                    scope = "expr"
                }
            },
        },
    },
  }
})

Thanks!

simrat39 commented 1 year ago
    settings = {
      ["rust-analyzer"] = {
        completion = {
          snippets = {
            custom = {
              ["thread spawn"] = {
                prefix = { "spawn", "tspawn" },
                body = { "thread::spawn(move || {", "   $0", "});" },
                description = "Insert a thread::spawn call",
                requires = "std::thread",
                scope = "expr",
              },
            },
          },
        },
        checkOnSave = {
          command = "clippy",
        },
      },
    },

Works for me, Try it this way

adaszko commented 1 year ago

Thank you! For the posteriority, here’s a full example that worked for me: https://github.com/IWANABETHATGUY/nvim-config/blob/ca33c2631e29e50c9eb725567a8b5ca85cbec262/lua/user/rust-tools.lua#L91