Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lsp): don't auto-attach to buffers that aren't files #272

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ 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).

## [Unreleased]
## [4.11.0] - 2024-03-03

### Added

- LSP: By default, don't auto-attach to buffers that aren't files [[#268](https://github.com/mrcjkb/rustaceanvim/issues/268)].

### Fixed

Expand Down
2 changes: 1 addition & 1 deletion doc/rustaceanvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ RustcOpts *RustcOpts*
RustaceanLspClientOpts *RustaceanLspClientOpts*

Fields: ~
{auto_attach?} (boolean|fun():boolean) Whether to automatically attach the LSP client. Defaults to `true` if the `rust-analyzer` executable is found.
{auto_attach?} (boolean|fun(bufnr:integer):boolean) Whether to automatically attach the LSP client. Defaults to `true` if the `rust-analyzer` executable is found.
{cmd?} (string[]|fun():string[]) Command and arguments for starting rust-analyzer
{settings?} (table|fun(project_root:string|nil):table) Setting passed to rust-analyzer. Defaults to a function that looks for a `rust-analyzer.json` file or returns an empty table. See https://rust-analyzer.github.io/manual.html#configuration.
{standalone?} (boolean) Standalone file support (enabled by default). Disabling it may improve rust-analyzer's startup time.
Expand Down
14 changes: 7 additions & 7 deletions ftplugin/rust.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
---@type RustaceanConfig
local config = require('rustaceanvim.config.internal')
local types = require('rustaceanvim.types.internal')
local lsp = require('rustaceanvim.lsp')

if not vim.g.did_rustaceanvim_initialize then
vim.lsp.commands['rust-analyzer.runSingle'] = function(command)
Expand Down Expand Up @@ -45,10 +43,12 @@ end

vim.g.did_rustaceanvim_initialize = true

local auto_attach = types.evaluate(config.server.auto_attach)

if not auto_attach then
return
local auto_attach = config.server.auto_attach
if type(auto_attach) == 'function' then
local bufnr = vim.api.nvim_get_current_buf()
auto_attach = auto_attach(bufnr)
end

lsp.start()
if auto_attach then
require('rustaceanvim.lsp').start()
end
2 changes: 1 addition & 1 deletion lua/rustaceanvim/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ vim.g.rustaceanvim = vim.g.rustaceanvim
---@field edition string The edition to use. See https://rustc-dev-guide.rust-lang.org/guides/editions.html. Default '2021'.

---@class RustaceanLspClientOpts
---@field auto_attach? boolean | fun():boolean Whether to automatically attach the LSP client. Defaults to `true` if the `rust-analyzer` executable is found.
---@field auto_attach? boolean | fun(bufnr: integer):boolean Whether to automatically attach the LSP client. Defaults to `true` if the `rust-analyzer` executable is found.
---@field cmd? string[] | fun():string[] Command and arguments for starting rust-analyzer
---@field settings? table | fun(project_root:string|nil):table Setting passed to rust-analyzer. Defaults to a function that looks for a `rust-analyzer.json` file or returns an empty table. See https://rust-analyzer.github.io/manual.html#configuration.
---@field standalone? boolean Standalone file support (enabled by default). Disabling it may improve rust-analyzer's startup time.
Expand Down
12 changes: 10 additions & 2 deletions lua/rustaceanvim/config/internal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local types = require('rustaceanvim.types.internal')
local compat = require('rustaceanvim.compat')
local config = require('rustaceanvim.config')
local executors = require('rustaceanvim.executors')
local os = require('rustaceanvim.os')

local RustaceanConfig

Expand Down Expand Up @@ -228,9 +229,16 @@ local RustaceanDefaultConfig = {
---@diagnostic disable-next-line: undefined-doc-class
---@class RustaceanLspClientConfig: vim.lsp.ClientConfig
server = {
---@type boolean | fun():boolean Whether to automatically attach the LSP client.
---@type boolean | fun(bufnr: integer):boolean Whether to automatically attach the LSP client.
---Defaults to `true` if the `rust-analyzer` executable is found.
auto_attach = function()
auto_attach = function(bufnr)
if #vim.bo[bufnr].buftype > 0 then
return false
end
local path = vim.api.nvim_buf_get_name(bufnr)
if not os.is_valid_file_path(path) then
return false
end
local cmd = types.evaluate(RustaceanConfig.server.cmd)
---@cast cmd string[]
local rs_bin = cmd[1]
Expand Down
2 changes: 1 addition & 1 deletion lua/rustaceanvim/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ M.start = function(bufnr)
---@type LspStartConfig
local lsp_start_config = vim.tbl_deep_extend('force', {}, client_config)
local root_dir = cargo.get_root_dir(vim.api.nvim_buf_get_name(bufnr))
root_dir = root_dir and os.normalize_path(root_dir)
root_dir = root_dir and os.normalize_path_on_windows(root_dir)
lsp_start_config.root_dir = root_dir
if not root_dir then
--- No project root found. Start in detached/standalone mode.
Expand Down
18 changes: 15 additions & 3 deletions lua/rustaceanvim/os.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
local os = {}

local compat = require('rustaceanvim.compat')
local shell = require('rustaceanvim.shell')

---@param url string
function os.open_url(url)
Expand Down Expand Up @@ -35,12 +36,23 @@ end

---Normalize path for Windows, which is case insensitive
---@param path string
---@return string normalize_path
function os.normalize_path(path)
if require('rustaceanvim.shell').is_windows() then
---@return string normalized_path
function os.normalize_path_on_windows(path)
if shell.is_windows() then
return path:lower()
end
return path
end

---@param path string
---@return boolean
function os.is_valid_file_path(path)
local normalized_path = vim.fs.normalize(path, { expand_env = false })
if shell.is_windows() then
local starts_with_drive_letter = normalized_path:match('^%a:') ~= nil
return starts_with_drive_letter
end
return vim.startswith(normalized_path, '/')
end

return os
2 changes: 1 addition & 1 deletion lua/rustaceanvim/rust_analyzer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ M.file_request = function(file_path, method, params, handler)
local client_found = false
for _, client in ipairs(M.get_active_rustaceanvim_clients(nil, { method = method })) do
local root_dir = client.config.root_dir
if root_dir and vim.startswith(os.normalize_path(file_path), root_dir) then
if root_dir and vim.startswith(os.normalize_path_on_windows(file_path), root_dir) then
local bufnr = find_buffer_by_name(file_path)
if not params then
params = {
Expand Down
Loading