Skip to content

Commit

Permalink
fix(lsp): nil check for when no root directory is found (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb committed Dec 6, 2023
1 parent d747f19 commit d7cb051
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
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.9.6] - 2023-12-06

### Fixed

- `nil` checks for when there is no root project.
Fixes the error message encountered in [#90](https://github.com/mrcjkb/rustaceanvim/issues/90).

## [3.9.5] - 2023-12-05

### Fixed
Expand Down
24 changes: 15 additions & 9 deletions lua/rustaceanvim/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ local function override_apply_text_edits()
end
end

local function is_library(fname)
---Checks if there is an active client for file_name and returns its root directory if found.
---@param file_name string
---@return string | nil root_dir The root directory of the active client for file_name (if there is one)
local function get_mb_active_client_root(file_name)
---@diagnostic disable-next-line: missing-parameter
local cargo_home = compat.uv.os_getenv('CARGO_HOME') or joinpath(vim.env.HOME, '.cargo')
local registry = joinpath(cargo_home, 'registry', 'src')
Expand All @@ -26,23 +29,23 @@ local function is_library(fname)
local toolchains = joinpath(rustup_home, 'toolchains')

for _, item in ipairs { toolchains, registry } do
if fname:sub(1, #item) == item then
if file_name:sub(1, #item) == item then
local clients = rust_analyzer.get_active_rustaceanvim_clients()
return clients[#clients].config.root_dir
end
end
end

---@param fname string
---@return string
local function get_root_dir(fname)
local reuse_active = is_library(fname)
---@param file_name string
---@return string | nil root_dir
local function get_root_dir(file_name)
local reuse_active = get_mb_active_client_root(file_name)
if reuse_active then
return reuse_active
end
local cargo_crate_dir = vim.fs.dirname(vim.fs.find({ 'Cargo.toml' }, {
upward = true,
path = vim.fs.dirname(fname),
path = vim.fs.dirname(file_name),
})[1])
local cargo_workspace_dir = nil
if vim.fn.executable('cargo') == 1 then
Expand Down Expand Up @@ -72,10 +75,13 @@ local function get_root_dir(fname)
or cargo_crate_dir
or vim.fs.dirname(vim.fs.find({ 'rust-project.json', '.git' }, {
upward = true,
path = vim.fs.dirname(fname),
path = vim.fs.dirname(file_name),
})[1])
end

---@param client lsp.Client
---@param root_dir string
---@return boolean
local function is_in_workspace(client, root_dir)
if not client.workspace_folders then
return false
Expand Down Expand Up @@ -104,7 +110,7 @@ M.start = function()

-- Check if a client is already running and add the workspace folder if necessary.
for _, client in pairs(rust_analyzer.get_active_rustaceanvim_clients()) do
if not is_in_workspace(client, root_dir) then
if root_dir and not is_in_workspace(client, root_dir) then
local workspace_folder = { uri = vim.uri_from_fname(root_dir), name = root_dir }
local params = {
event = {
Expand Down

0 comments on commit d7cb051

Please sign in to comment.