From 1fe9c5a2a316b88290036941dd53c56dce45cf3e Mon Sep 17 00:00:00 2001 From: Javier De la Garza Sanchez Date: Tue, 31 Oct 2023 16:01:22 -0500 Subject: [PATCH] Fixed all warnings and addressed PR comments --- .../RustLanguageServerProvider/README.md | 2 +- .../RustExtension.cs | 52 +++++---- .../RustLanguageServerProvider.cs | 110 ++++++++++-------- .../RustLanguageServerProvider.csproj | 1 + 4 files changed, 88 insertions(+), 77 deletions(-) diff --git a/New_Extensibility_Model/Samples/RustLanguageServerProvider/README.md b/New_Extensibility_Model/Samples/RustLanguageServerProvider/README.md index 0d4e3cd3..90963200 100644 --- a/New_Extensibility_Model/Samples/RustLanguageServerProvider/README.md +++ b/New_Extensibility_Model/Samples/RustLanguageServerProvider/README.md @@ -9,7 +9,7 @@ date: 2023-10-31 This sample creates a Rust Language Server Provider extension that serves intellisense and tooltips when a rust file is opened in Visual Studio. ## Prerequisites -This sample requires installing [Rust](https://www.rust-lang.org/tools/install) and [rust-analyzer.exe](https://github.com/rust-lang/rust-analyzer) found under releases named "rust-analyzer-x86_64-pc-windows-msvc.zip" +This sample requires installing [Rust](https://www.rust-lang.org/tools/install) and copying [rust-analyzer.exe](https://github.com/rust-lang/rust-analyzer) found under releases named "rust-analyzer-x86_64-pc-windows-msvc.zip" into the project folder. ## Language Server Provider definition diff --git a/New_Extensibility_Model/Samples/RustLanguageServerProvider/RustExtension.cs b/New_Extensibility_Model/Samples/RustLanguageServerProvider/RustExtension.cs index 47946d04..695bb2da 100644 --- a/New_Extensibility_Model/Samples/RustLanguageServerProvider/RustExtension.cs +++ b/New_Extensibility_Model/Samples/RustLanguageServerProvider/RustExtension.cs @@ -1,31 +1,33 @@ -namespace RustLanguageServerProviderExtension -{ - using Microsoft.Extensions.DependencyInjection; - using Microsoft.VisualStudio.Extensibility; +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace RustLanguageServerProviderExtension; + +using Microsoft.Extensions.DependencyInjection; +using Microsoft.VisualStudio.Extensibility; - /// - /// Extension entry point for the VisualStudio.Extensibility extension. - /// - [VisualStudioContribution] - internal class RustExtension : Extension +/// +/// Extension entry point for the VisualStudio.Extensibility extension. +/// +[VisualStudioContribution] +internal class RustExtension : Extension +{ + /// + public override ExtensionConfiguration ExtensionConfiguration => new() { - /// - public override ExtensionConfiguration ExtensionConfiguration => new() - { - Metadata = new( - id: "RustLspExtension.003741dc-9931-47c3-ad95-8804705cfbb9", - version: this.ExtensionAssemblyVersion, - publisherName: "Microsoft", - displayName: "RustLspExtension", - description: "Rust Language Server Extension"), - }; + Metadata = new( + id: "RustLspExtension.003741dc-9931-47c3-ad95-8804705cfbb9", + version: this.ExtensionAssemblyVersion, + publisherName: "Microsoft", + displayName: "RustLspExtension", + description: "Rust Language Server Extension"), + }; - /// - protected override void InitializeServices(IServiceCollection serviceCollection) - { - base.InitializeServices(serviceCollection); + /// + protected override void InitializeServices(IServiceCollection serviceCollection) + { + base.InitializeServices(serviceCollection); - // You can configure dependency injection here by adding services to the serviceCollection. - } + // You can configure dependency injection here by adding services to the serviceCollection. } } diff --git a/New_Extensibility_Model/Samples/RustLanguageServerProvider/RustLanguageServerProvider.cs b/New_Extensibility_Model/Samples/RustLanguageServerProvider/RustLanguageServerProvider.cs index 3d4639a4..d354e474 100644 --- a/New_Extensibility_Model/Samples/RustLanguageServerProvider/RustLanguageServerProvider.cs +++ b/New_Extensibility_Model/Samples/RustLanguageServerProvider/RustLanguageServerProvider.cs @@ -1,65 +1,73 @@ -namespace RustLanguageServerProviderExtension -{ - using Microsoft.VisualStudio.Extensibility; - using Microsoft.VisualStudio.Extensibility.Editor; - using Microsoft.VisualStudio.Extensibility.LanguageServer; - using Microsoft.VisualStudio.RpcContracts.LanguageServerProvider; - using Nerdbank.Streams; - using System.Diagnostics; - using System.IO.Pipelines; - using System.Reflection; +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace RustLanguageServerProviderExtension; +using System.Diagnostics; +using System.IO.Pipelines; +using System.Reflection; +using Microsoft.VisualStudio.Extensibility; +using Microsoft.VisualStudio.Extensibility.Editor; +using Microsoft.VisualStudio.Extensibility.LanguageServer; +using Microsoft.VisualStudio.RpcContracts.LanguageServerProvider; +using Nerdbank.Streams; + +/// +[VisualStudioContribution] +internal class RustLanguageServerProvider : LanguageServerProvider +{ + /// + /// Gets the document type for rust code files. + /// [VisualStudioContribution] - internal class RustLanguageServerProvider : LanguageServerProvider + public static DocumentTypeConfiguration RustDocumentType => new("rust") { - [VisualStudioContribution] - public static DocumentTypeConfiguration RustDocumentType => new("rust") - { - FileExtensions = new[] { ".rs", ".rust" }, - BaseDocumentType = LanguageServerBaseDocumentType, - }; - - /// - public override LanguageServerProviderConfiguration LanguageServerProviderConfiguration => new( - "%RustLspExtension.RustLanguageServerProvider.DisplayName%", - new[] - { - DocumentFilter.FromDocumentType(RustDocumentType), - }); + FileExtensions = new[] { ".rs", ".rust" }, + BaseDocumentType = LanguageServerBaseDocumentType, + }; - /// - public override Task CreateServerConnectionAsync(CancellationToken cancellationToken) + /// + public override LanguageServerProviderConfiguration LanguageServerProviderConfiguration => new( + "%RustLspExtension.RustLanguageServerProvider.DisplayName%", + new[] { - 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; + DocumentFilter.FromDocumentType(RustDocumentType), + }); - Process process = new Process(); - process.StartInfo = info; + /// + public override Task CreateServerConnectionAsync(CancellationToken cancellationToken) + { + 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; - if (process.Start()) - { - return Task.FromResult(new DuplexPipe( - PipeReader.Create(process.StandardOutput.BaseStream), - PipeWriter.Create(process.StandardInput.BaseStream))); - } +#pragma warning disable CA2000 // The process is disposed after Visual Studio sends the stop command. + Process process = new Process(); +#pragma warning restore CA2000 // Dispose objects before losing scope. + process.StartInfo = info; - return Task.FromResult(null); + if (process.Start()) + { + return Task.FromResult(new DuplexPipe( + PipeReader.Create(process.StandardOutput.BaseStream), + PipeWriter.Create(process.StandardInput.BaseStream))); } - /// - public override Task OnServerInitializationResultAsync(ServerInitializationResult serverInitializationResult, LanguageServerInitializationFailureInfo? initializationFailureInfo, CancellationToken cancellationToken) - { - if (serverInitializationResult == ServerInitializationResult.Failed) - { - // Log telemetry for failure and disable the server from being activated again. - this.Enabled = false; - } + return Task.FromResult(null); + } - return base.OnServerInitializationResultAsync(serverInitializationResult, initializationFailureInfo, cancellationToken); + /// + public override Task OnServerInitializationResultAsync(ServerInitializationResult serverInitializationResult, LanguageServerInitializationFailureInfo? initializationFailureInfo, CancellationToken cancellationToken) + { + if (serverInitializationResult == ServerInitializationResult.Failed) + { + // Log telemetry for failure and disable the server from being activated again. + this.Enabled = false; } + + return base.OnServerInitializationResultAsync(serverInitializationResult, initializationFailureInfo, cancellationToken); } } diff --git a/New_Extensibility_Model/Samples/RustLanguageServerProvider/RustLanguageServerProvider.csproj b/New_Extensibility_Model/Samples/RustLanguageServerProvider/RustLanguageServerProvider.csproj index 10be935b..a24cd02e 100644 --- a/New_Extensibility_Model/Samples/RustLanguageServerProvider/RustLanguageServerProvider.csproj +++ b/New_Extensibility_Model/Samples/RustLanguageServerProvider/RustLanguageServerProvider.csproj @@ -4,6 +4,7 @@ enable enable 10 + $(NoWarn);VSEXTAPI0001; https://pkgs.dev.azure.com/azure-public/vside/_packaging/msft_consumption/nuget/v3/index.json;$(RestoreAdditionalProjectSources)