Skip to content

Visual Studio integration #8729

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

Closed
clemenswasser opened this issue May 4, 2021 · 18 comments
Closed

Visual Studio integration #8729

clemenswasser opened this issue May 4, 2021 · 18 comments
Labels
E-hard good first issue S-actionable Someone could pick this issue up and work on it right now

Comments

@clemenswasser
Copy link
Contributor

clemenswasser commented May 4, 2021

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.

@Veykril Veykril added the S-actionable Someone could pick this issue up and work on it right now label May 4, 2021
@vsrs
Copy link
Contributor

vsrs commented May 6, 2021

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.

@crlf0710
Copy link
Member

crlf0710 commented Jul 2, 2021

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

@matteo-prosperi
Copy link

@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

@dbaeumer
Copy link

dbaeumer commented Sep 6, 2021

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.

@lnicola
Copy link
Member

lnicola commented Sep 6, 2021

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.

@matklad
Copy link
Member

matklad commented Sep 6, 2021

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

@dbaeumer
Copy link

dbaeumer commented Sep 7, 2021

Agree :-)

@matteo-prosperi
Copy link

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

@vsrs
Copy link
Contributor

vsrs commented Oct 12, 2021

Great news, thanks!

@lnicola
Copy link
Member

lnicola commented Oct 13, 2021

Looks great! Someone (TM) will take care of gluon-lang/lsp-types#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.

bors bot added a commit that referenced this issue Oct 18, 2021
10551: Pull in new lsp-types for VS compat r=lnicola a=lnicola

CC #8729

Depends on gluon-lang/lsp-types#218

Co-authored-by: Laurențiu Nicola <[email protected]>
@clemenswasser
Copy link
Contributor Author

@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?

@matteo-prosperi
Copy link

@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.

@clemenswasser
Copy link
Contributor Author

@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?

@lnicola
Copy link
Member

lnicola commented Dec 27, 2021

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).

@matteo-prosperi
Copy link

@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.

@Hau-Hau
Copy link

Hau-Hau commented Jul 27, 2022

@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?

@lnicola
Copy link
Member

lnicola commented Jan 30, 2023

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

@lnicola lnicola closed this as completed Jan 30, 2023
@parthopdas
Copy link
Contributor

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E-hard good first issue S-actionable Someone could pick this issue up and work on it right now
Projects
None yet
Development

No branches or pull requests

10 participants