From a095a1f9397310d108ead239edc068b2624695b6 Mon Sep 17 00:00:00 2001 From: Marc Jakobi Date: Mon, 22 Jan 2024 16:54:20 +0100 Subject: [PATCH] fix(lsp): spawn rust-analyzer in detached mode if no project root found (#178) --- CHANGELOG.md | 7 +++++++ lua/rustaceanvim/lsp.lua | 6 +++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 24c16ec2..1b2c0d3b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lua/rustaceanvim/lsp.lua b/lua/rustaceanvim/lsp.lua index 93d649a5..ae5b2759 100644 --- a/lua/rustaceanvim/lsp.lua +++ b/lua/rustaceanvim/lsp.lua @@ -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]) @@ -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