Sublime Forum

How to Enable Import Autocompletion with rust-analyzer in Sublime Text LSP?

#1

Hello, I’m new to using Sublime Text with the LSP (Language Server Protocol) plugin, and I’ve been working on a Rust project with the rust-analyzer language server. I’ve successfully installed the necessary plugins, including LSP and LSP-rust-analyzer, and I can see some autocompletion working (like method suggestions), but I am struggling to get import autocompletion working.

Specifically, I am unable to see suggestions for imports (e.g., auto-imports for structs, functions, or types from external crates or the Rust standard library) in my Rust project.

Here’s what I’ve already done:

  1. Installed LSP and LSP-rust-analyzer via Package Control.
  2. Ensured that rust-analyzer is installed and the latest version is available via cargo and is on my system’s path.
  3. Configured LSP for Rust to use rust-analyzer (through the LSP.sublime-settings).
  4. Checked the LSP logs and found no errors regarding rust-analyzer.

What I’m hoping for:

  • I want to enable import autocompletion for my Rust project. When I start typing a type or function name that isn’t imported yet, I’d like Sublime to automatically suggest the appropriate import.

Question:

  • Is this feature (import autocompletion) supported by the LSP-rust-analyzer package in Sublime Text?
  • If yes, what do I need to configure in the LSP-rust-analyzer.sublime-settings file to make it work properly?
  • Are there any additional settings I should enable in rust-analyzer itself to allow for import completions?

It would be extremely helpful to have this feature working, especially when dealing with large Rust codebases where manual imports are tedious. Any advice on getting this set up would be greatly appreciated!

0 Likes

#2

In the Rust-Analyzer LSP package’s configuration, you can see the following:

// Toggles the additional completions that automatically add imports when completed.
		// Note that your client must specify the `additionalTextEdits` LSP client capability to truly have this feature enabled.
		"rust-analyzer.completion.autoimport.enable": true,

I have’ not found a way to get the LSP package’s client to enable additionalTextEdits.

0 Likes

#3

Exactly, that setting is enabled in the default settings, but imports are still not working. I’m not sure what needs to be done to get them working properly. Right now, I’m testing to see if this setup could work on a real-world project, such as ruff.

It would be extremely helpful to successfully import crates within the existing workspace. However, I’m unsure whether this is currently supported by LSP + rust-analyzer. Is this a known limitation, a configuration issue, or something else? I’m not very familiar with LSP and rust-analyzer, so I’m asking to see if anyone has managed to get this working and, if so, what configuration steps are required.

0 Likes

#4

As the documentation says, the LSP client (the LSP package) needs to support additionalTextEdits. This is a LSP capability the client needs to support. Opening an issue at https://github.com/sublimelsp/LSP/issues is the way to get an answer

0 Likes

#5

The LSP package does support to resolve additionalTextEdits for completions (https://github.com/sublimelsp/LSP/blob/c64ac00a1295fdc8461811033e0974443fd9ea55/plugin/core/sessions.py#L312-L313).

So a next step could be to create a minimum example project and check whether the auto-imports work in other editors like VSCode. This might give a hint whether it’s an issue with rust-analyzer or if there is something else missing in the client (LSP or LSP-rust-analyzer packages).

0 Likes