Skip to content

Commit

Permalink
Added Rust LSP server example
Browse files Browse the repository at this point in the history
  • Loading branch information
javierdlg committed Oct 31, 2023
1 parent 32384ba commit 98a541a
Show file tree
Hide file tree
Showing 5 changed files with 344 additions and 211 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"RustLspExtension.RustLanguageServerProvider.DisplayName": "Rust Analyzer LSP server"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace RustLanguageServerProviderExtension
{
using Microsoft.Extensions.DependencyInjection;
using Microsoft.VisualStudio.Extensibility;

/// <summary>
/// Extension entry point for the VisualStudio.Extensibility extension.
/// </summary>
[VisualStudioContribution]
internal class ExtensionEntrypoint : Extension
{
/// <inheritdoc/>
public override ExtensionConfiguration ExtensionConfiguration => new()
{
Metadata = new(
id: "RustLspExtension.003741dc-9931-47c3-ad95-8804705cfbb9",
version: this.ExtensionAssemblyVersion,
publisherName: "Publisher name",
displayName: "RustLspExtension",
description: "Extension description"),
};

/// <inheritdoc />
protected override void InitializeServices(IServiceCollection serviceCollection)
{
base.InitializeServices(serviceCollection);

// You can configure dependency injection here by adding services to the serviceCollection.
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
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;

[VisualStudioContribution]
internal class RustLanguageServerProvider : LanguageServerProvider
{
public RustLanguageServerProvider(ExtensionCore container, VisualStudioExtensibility extensibilityObject)
: base(container, extensibilityObject)
{
}

[VisualStudioContribution]
public static DocumentTypeConfiguration RustDocumentType => new("rust")
{
FileExtensions = new[] { ".rs", ".rust" },
BaseDocumentType = LanguageServerBaseDocumentType,
};

/// <inheritdoc/>
public override LanguageServerProviderConfiguration LanguageServerProviderConfiguration => new(
"%RustLspExtension.RustLanguageServerProvider.DisplayName%",
new[]
{
DocumentFilter.FromDocumentType(RustDocumentType),
});

/// <inheritdoc/>
public override Task<IDuplexPipe?> 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;

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

if (process.Start())
{
return Task.FromResult<IDuplexPipe?>(new DuplexPipe(
PipeReader.Create(process.StandardOutput.BaseStream),
PipeWriter.Create(process.StandardInput.BaseStream)));
}

return Task.FromResult<IDuplexPipe?>(null);
}

/// <inheritdoc/>
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);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0-windows</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<!-- The VisualStudio.Extensibility preview packages are available from the azure-public/vside/msft_consumption feed -->
<RestoreAdditionalProjectSources>https://pkgs.dev.azure.com/azure-public/vside/_packaging/msft_consumption/nuget/v3/index.json;$(RestoreAdditionalProjectSources)</RestoreAdditionalProjectSources>
</PropertyGroup>

<ItemGroup>
<Content Include="rust-analyzer.exe" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.VisualStudio.Extensibility.Sdk" Version="17.9.42-preview-1" />
<PackageReference Include="Microsoft.VisualStudio.Extensibility.Build" Version="17.9.42-preview-1" />
</ItemGroup>
</Project>
Loading

0 comments on commit 98a541a

Please sign in to comment.