rust-analyzer icon indicating copy to clipboard operation
rust-analyzer copied to clipboard

Visual Studio integration

Open clemenswasser opened this issue 3 years ago • 16 comments

Description

This issue serves as a meta-issue, where we can discuss and inform each other about the integration of rust-analyzer and Visual Studio. Visual Studio support was brought up by @vsrs and @deeprobin in #5516 and #7369.

Resources

Add a Language Server Protocol extension (Microsoft Docs)


Since @vsrs wrote that he is working on a Visual Studio Extension that integrates rust-analyzer into Visual Studio here, it would be great to hear from you, what the current status is, how we could collaborate on this and what is missing to make this usable.

clemenswasser avatar May 04 '21 18:05 clemenswasser

I have a VS extension that adds support for building and debugging cargo projects (Open Folder Extension), but obviously, this is not enough and we need good editor integration. The main problem with VS is that its implementation of the LSP client is not Language Server Protocol Specification compliant. Initially, I decided to reimplement the client part but turned out I do not have enough time. So now I just check each new VS build and hope sometime VS LSP client will work as it should.

vsrs avatar May 06 '21 12:05 vsrs

@vsrs Could you write more about what's wrong with VS implementation of LSP client? Is there a list of issues somewhere?

crlf0710 avatar Jul 02 '21 07:07 crlf0710

@vsrs could you elaborate on what issues you noticed with the VS implementation of the LSP protocol? VS implementation of LSP is (or at the very least is supposed to be) a compatible superset of the LSP protocol, in the same way a new version of the LSP protocol is a compatible superset of the previous version and should still work with an older language server as long as the server is not too strict in enforcing certain expectations. One example of this is that VS may send values for certain enumerations that are not listed in the LSP specification. But this is also true for a new version of the LSP protocol which may also add new enumeration values. I would love to learn more about what issues you encountered. Thanks Matteo

matteo-prosperi avatar Aug 27 '21 21:08 matteo-prosperi

Some comments from the specification side: to support the evolution of enumerations a consuming side of an enumeration (usually a server) shouldn't fail on unknown values. If a server does a newer client could never use an older server although the server would still work perfectly fine with the client.

I added some clarification around this to the specification.

dbaeumer avatar Sep 06 '21 13:09 dbaeumer

I don't know remember how I noticed it so feel free to ignore me, but I vaguely recall noticing that Visual Studio was using a draft version of the semantic tokens spec, and was incompatible with standard servers.

lnicola avatar Sep 06 '21 13:09 lnicola

Good call, we indeed handle enum in a suboptimal way, filed https://github.com/gluon-lang/lsp-types/issues/213 for that. Would be so much easier to fix with https://github.com/microsoft/language-server-protocol/issues/67#issuecomment-656187138 ;)

matklad avatar Sep 06 '21 13:09 matklad

Agree :-)

dbaeumer avatar Sep 07 '21 07:09 dbaeumer

Hi Everybody,

Visual Studio 2022 RC released today has an LSP implementation that is compatible with rust-analyzer. I tested it and you can see some screenshots at the end of this comment.

In order to make rust-analyzer work, I had to make changes to two enums in the lsp/types repo: Quick and dirty fix to enums that are not compatible with Visual Studio. The features I tested, seem to work fine without requiring any other change to rust-analyzer.

You may want to suppress these notifications though as they are very intrusive in VS: image

You can also consider supporting some of the LSP functionalities specific to Visual Studio for which a specification was recently released.

Feel free to get in touch with me if you need any clarification. Thanks!

Matteo

Screeshots of rust-analyzer in Visual Studio 2022 RC.

Peek: image

Code actions: image

Autocompletion: image

Find all references: image

Signature tooltips: image

More signature tooltips: image

matteo-prosperi avatar Oct 12 '21 20:10 matteo-prosperi

Great news, thanks!

vsrs avatar Oct 12 '21 21:10 vsrs

Looks great! Someone (TM) will take care of https://github.com/gluon-lang/lsp-types/issues/213 soon.

You may want to suppress these notifications though as they are very intrusive in VS:

Fortunately, those are only shown by from-source builds.

lnicola avatar Oct 13 '21 15:10 lnicola

@matteo-prosperi I somehow managed to miss your comment 😅 The integration looks amazing and really usable! Could you maybe share what you did to get rust-analyzer working in Visual Studio? I imagine we would need to write a separate Visual Studio extension for this integration?

clemenswasser avatar Dec 20 '21 00:12 clemenswasser

@clemenswasser, what I did was only a test integration. I basically followed the guide here. I packaged rust-analyzer.exe within my VSIX, so the meaningful part of my code would only be:

    [ContentType("rs")]
    [Export(typeof(ILanguageClient))]
    public class RustLanguageClient : ILanguageClient
    {
        public async Task<Connection> ActivateAsync(CancellationToken token)
        {
            await Task.Yield();

            ProcessStartInfo info = new ProcessStartInfo();
            info.FileName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"rust-analyzer.exe");
            info.RedirectStandardInput = true;
            info.RedirectStandardOutput = true;
            info.UseShellExecute = false;
            info.CreateNoWindow = true;

            Process process = new Process();
            process.StartInfo = info;

            if (process.Start())
            {
                return new Connection(process.StandardOutput.BaseStream, process.StandardInput.BaseStream);
            }

            return null;
        }

which is almost identical to the code from the guide.

I decided to package my own copy of rust-analyzer.exe because the vanilla one was not working with Visual Studio at the time (as described above in this thread). But now it may be better to rely on a rust-analyzer.exe installed together with the rest of the Rust tools.

matteo-prosperi avatar Dec 20 '21 18:12 matteo-prosperi

@matteo-prosperi is it possible that you could share your extension? I tried making my own, but can't get it working 😢 Also would it be okay for you, if I try to make a "official" extension out of yours which would then probably live under editors/...? @matklad is this even a good idea?

clemenswasser avatar Dec 27 '21 11:12 clemenswasser

At some point we will probably move the Code extension out of this repository (although there are some technical reasons that make it non-trivial).

lnicola avatar Dec 27 '21 14:12 lnicola

@matteo-prosperi is it possible that you could share your extension? I tried making my own, but can't get it working 😢 Also would it be okay for you, if I try to make a "official" extension out of yours which would then probably live under editors/...?

@clemenswasser, I copied my proof of concept code here. I just built it and tested it on VS 2022 17.0.4 and it works fine. You, or anybody else in the Rust community, are welcome to take that code and do whatever you want with it without any need to acknowledge my contribution. Let me know if you have any question.

matteo-prosperi avatar Dec 28 '21 19:12 matteo-prosperi

@matteo-prosperi It looks great! However I'm curious - support for tests inside Visual Studio is not a part of that what rust-analyzer would bring?

Hau-Hau avatar Jul 27 '22 09:07 Hau-Hau

There is now a Visual Studio extension for RA (#14012), closing.

lnicola avatar Jan 30 '23 06:01 lnicola

@Hau-Hau @matteo-prosperi @clemenswasser @vsrs @dbaeumer @matklad @crlf0710 here it is https://marketplace.visualstudio.com/items?itemName=kitamstudios.RustAnalyzer&ssr=false#overview

For now the following are functional - please feel free to use it or even better, help me out

  • Build, Clean (errors in Error list with details in output window).
  • Debug & Run without debugging.
  • Workspace support (continuing to get enhanced as I find more examples).
  • Intellisense / Auto-complete / Goto definition / Code actions / Find references etc. all features from Rust language server.
  • Tested above features with top Rust OSS projects like cargo, ruffle, iced, geo, ruff, reqwest, wasmtime.

Coming up shortly: examples integration, clippy/fmt, unit testing etc.

parthopdas avatar Jan 30 '23 12:01 parthopdas