Skip to content

Commit

Permalink
fix(lsp): spawn rust-analyzer in detached mode if no project root fou…
Browse files Browse the repository at this point in the history
…nd (#178)
  • Loading branch information
mrcjkb authored Jan 22, 2024
1 parent b0c03f0 commit a095a1f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [3.17.1] - 2024-01-22

### Fixed

- Spawn rust-analyzer in detached mode when no project root is found.
This adds support for standalone files without a Rust project.

## [3.17.0] - 2024-01-20

### Added
Expand Down
6 changes: 5 additions & 1 deletion lua/rustaceanvim/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ local function get_root_dir(file_name)
end
return cargo_workspace_dir
or cargo_crate_dir
or vim.fs.dirname(vim.fs.find({ 'rust-project.json', '.git' }, {
or vim.fs.dirname(vim.fs.find({ 'rust-project.json' }, {
upward = true,
path = vim.fs.dirname(file_name),
})[1])
Expand Down Expand Up @@ -121,6 +121,10 @@ M.start = function(bufnr)
local root_dir = get_root_dir(vim.api.nvim_buf_get_name(bufnr))
root_dir = root_dir and normalize_path(root_dir)
lsp_start_opts.root_dir = root_dir
if not root_dir then
--- No project root found. Run in detached mode.
lsp_start_opts.init_options = { detachedFiles = { vim.api.nvim_buf_get_name(0) } }
end

local settings = client_config.settings
lsp_start_opts.settings = type(settings) == 'function' and settings(root_dir) or settings
Expand Down

0 comments on commit a095a1f

Please sign in to comment.