Skip to content
This repository has been archived by the owner on Jan 3, 2024. It is now read-only.

Commit

Permalink
feat: Standalone file support
Browse files Browse the repository at this point in the history
Closes #25
  • Loading branch information
simrat39 committed Jul 19, 2021
1 parent a22eec8 commit 79e8649
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,16 @@ RustJoinLines
RustHoverActions
RustMoveItemDown
RustMoveItemUp
RustStartStandaloneServerForBuffer
```

#### Standalone File Support
rust-tools supports rust analyzer for standalone files (not in a cargo project).
The language server is automatically started when you start a rust file which is
not in a cargo file (nvim abc.rs). If you want to attach some other buffer to
the standalone client (after opening nvim and switching to a new rust file),
then use the ```RustStartStandaloneServerForBuffer``` command.

#### Inlay Hints
![inlay hints](https://github.com/simrat39/rust-tools-demos/raw/master/inlay_hints.png)
```lua
Expand Down
1 change: 1 addition & 0 deletions ftplugin/rust.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
command RustStartStandaloneServerForBuffer :lua require('rust-tools.standalone').start_standalone_client()
25 changes: 25 additions & 0 deletions lua/rust-tools.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
local vim = vim
local nvim_lsp = require 'lspconfig'
local config = require 'rust-tools.config'
local utils = require('rust-tools.utils.utils')
local lspconfig_utils = require('lspconfig.util')

local M = {}

Expand Down Expand Up @@ -73,6 +75,25 @@ end

local function setup_lsp() nvim_lsp.rust_analyzer.setup(config.options.server) end

local function get_root_dir()
local fname = vim.api.nvim_buf_get_name(0)
local cargo_crate_dir = lspconfig_utils.root_pattern 'Cargo.toml'(fname)
local cmd = 'cargo metadata --no-deps --format-version 1'
if cargo_crate_dir ~= nil then
cmd = cmd .. ' --manifest-path ' ..
lspconfig_utils.path.join(cargo_crate_dir, 'Cargo.toml')
end
local cargo_metadata = vim.fn.system(cmd)
local cargo_workspace_dir = nil
if vim.v.shell_error == 0 then
cargo_workspace_dir =
vim.fn.json_decode(cargo_metadata)['workspace_root']
end
return cargo_workspace_dir or cargo_crate_dir or
lspconfig_utils.root_pattern 'rust-project.json'(fname) or
lspconfig_utils.find_git_ancestor(fname)
end

function M.setup(opts)
config.setup(opts)

Expand All @@ -88,6 +109,10 @@ function M.setup(opts)
if config.options.tools.autoSetHints then
require'rust-tools.inlay_hints'.setup_autocmd()
end

if utils.is_bufnr_rust(0) and (get_root_dir() == nil) then
require('rust-tools.standalone').start_standalone_client(config.options.server.handlers)
end
end

return M
39 changes: 39 additions & 0 deletions lua/rust-tools/standalone.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
local ra_config = require 'rust-tools.config'

local M = {}

function M.start_standalone_client(handlers)
local config = {
root_dir = require('lspconfig.util').path.dirname(
vim.api.nvim_buf_get_name(0)),
capabilities = ra_config.options.server.capabilities,
cmd = {'rust-analyzer'},
init_options = {detachedFiles = {vim.api.nvim_buf_get_name(0)}},
on_init = function(client)
vim.lsp.buf_attach_client(0, client.id)
vim.cmd "command! RustSetInlayHints :lua require('rust-tools.inlay_hints').set_inlay_hints()"
vim.cmd "command! RustDisableInlayHints :lua require('rust-tools.inlay_hints').disable_inlay_hints()"
vim.cmd "command! RustToggleInlayHints :lua require('rust-tools.inlay_hints').toggle_inlay_hints()"
vim.cmd "command! RustExpandMacro :lua require('rust-tools.expand_macro').expand_macro()"
vim.cmd "command! RustJoinLines :lua require('rust-tools.join_lines').join_lines()"
vim.cmd "command! RustHoverActions :lua require('rust-tools.hover_actions').hover_actions()"
vim.cmd "command! RustMoveItemDown :lua require('rust-tools.move_item').move_item()"
vim.cmd "command! RustMoveItemUp :lua require('rust-tools.move_item').move_item(true)"
end,
on_exit = function()
vim.cmd "delcommand RustSetInlayHints"
vim.cmd "delcommand RustDisableInlayHints"
vim.cmd "delcommand RustToggleInlayHints"
vim.cmd "delcommand RustExpandMacro"
vim.cmd "delcommand RustJoinLines"
vim.cmd "delcommand RustHoverActions"
vim.cmd "delcommand RustMoveItemDown"
vim.cmd "delcommand RustMoveItemUp"
end,
handlers = handlers
}

vim.lsp.start_client(config)
end

return M
4 changes: 4 additions & 0 deletions lua/rust-tools/utils/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@ function M.snippet_text_edits_to_text_edits(spe)
end
end

function M.is_bufnr_rust(bufnr)
return vim.api.nvim_buf_get_option(bufnr, 'ft') == 'rust'
end

return M

0 comments on commit 79e8649

Please sign in to comment.