Skip to content

Instantly share code, notes, and snippets.

@h0pes
Last active March 1, 2024 11:41
Show Gist options
  • Save h0pes/e0040e96f60a766d78118f2ede81b356 to your computer and use it in GitHub Desktop.
Save h0pes/e0040e96f60a766d78118f2ede81b356 to your computer and use it in GitHub Desktop.
rustaceanvim_yew_lspconfig.md

--custom/plugins.lua section for rustaceanvim

local myconfig = require "custom.utils.core"
    "mrcjkb/rustaceanvim",
    version = "^4", -- Recommended
    ft = { "rust" },
    dependencies = {
      "lvimuser/lsp-inlayhints.nvim",
      opts = {},
    },
    opts = {
      server = {
        on_attach = function(client, bufnr)
          require("plugins.configs.lspconfig").on_attach(client, bufnr)
        end,
        capabilities = require("plugins.configs.lspconfig").capabilities,
        cmd = { "~/.cargo/bin/rust-analyzer" },
        autostart = true,
        settings = {
          ["rust-analyzer"] = myconfig.user_ra_config,
        },
      },
    },
    config = function(_, opts)
      vim.g.rustaceanvim = vim.tbl_deep_extend("force", {}, opts or {})
      require("core.utils").load_mappings "rustaceanvim"
      require("core.utils").load_mappings "lspconfig"
    end,
  },

-- custom/utils/core.lua section for a function to load json settings

function M.json_file(path)
  local file = io.open(path, "rb")
  if not file then
    return {}
  end
  local content = file:read "*a"
  file:close()
  return vim.json.decode(content) or {}
end

local config_home = os.getenv "XDG_CONFIG_HOME" or (os.getenv "HOME" .. "/.config")
M.user_ra_config = M.json_file(config_home .. "/nvim/lua/custom/rust-analyzer.json")

-- custom/rust-analyzer.json

{
  "rust-analyzer": {
    "rustfmt": {
      "overrideCommand": ["yew-fmt", "--"]
    },
    "assist": {
      "importEnforceGranularity": true,
      "importPrefix": "create"
    },
    "cargo": {
      "allFeatures": true
    },
    "checkOnSave": {
      "command": "clippy",
      "allFeatures": true
    },
    "inlayHints": {
      "lifetimeElisionHints": {
        "enable": true,
        "useParameterNames": true
      }
    }
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment