Skip to content
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

Add a WorkspaceKind property to ProjectContext. #75384

Merged
merged 3 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,8 @@ public static LSP.VSProjectContext ProjectToProjectContext(Project project)
var projectContext = new LSP.VSProjectContext
{
Id = ProjectIdToProjectContextId(project.Id),
Label = project.Name
Label = project.Name,
IsMiscellaneous = project.Solution.WorkspaceKind == WorkspaceKind.MiscellaneousFiles,
};

if (project.Language == LanguageNames.CSharp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ public VSProjectKind Kind
set;
}

/// <summary>
/// Gets or sets this project context represents miscellaneous files.
/// </summary>
[JsonPropertyName("_vs_is_miscellaneous")]
public bool IsMiscellaneous
{
get;
set;
}

public static bool operator ==(VSProjectContext? value1, VSProjectContext? value2)
{
if (ReferenceEquals(value1, value2))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,24 @@
using System;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Text;
using Roslyn.LanguageServer.Protocol;
using Roslyn.Test.Utilities;
using Roslyn.Utilities;
using Xunit;
using Xunit.Abstractions;
using Range = Roslyn.LanguageServer.Protocol.Range;

namespace Microsoft.CodeAnalysis.LanguageServer.UnitTests
{
public class ProtocolConversionsTests
public class ProtocolConversionsTests : AbstractLanguageServerProtocolTests
{
public ProtocolConversionsTests(ITestOutputHelper testOutputHelper) : base(testOutputHelper)
{
}

[Fact]
public void CreateAbsoluteUri_LocalPaths_AllAscii()
{
Expand Down Expand Up @@ -276,5 +283,62 @@ private static string GetTestMarkup()
}";
return markup;
}

[Theory, CombinatorialData]
public async Task ProjectToProjectContext_HostWorkspace(bool mutatingLspWorkspace)
{
var source = """
class {|caret:A|}
{
void M()
{
}
}
""";

// Create a server with an existing file.
await using var testLspServer = await CreateTestLspServerAsync(source, mutatingLspWorkspace, new InitializationOptions { ServerKind = WellKnownLspServerKinds.CSharpVisualBasicLspServer });
var caret = testLspServer.GetLocations("caret").Single();

var document = await GetTextDocumentAsync(testLspServer, caret.Uri);
Assert.NotNull(document);

var projectContext = ProtocolConversions.ProjectToProjectContext(document.Project);

Assert.False(projectContext.IsMiscellaneous);
}

[Theory, CombinatorialData]
public async Task ProjectToProjectContext_MiscellaneousFilesWorkspace(bool mutatingLspWorkspace)
{
var source = """
class A
{
void M()
{
}
}
""";

// Create a server that supports LSP misc files.
await using var testLspServer = await CreateTestLspServerAsync(string.Empty, mutatingLspWorkspace, new InitializationOptions { ServerKind = WellKnownLspServerKinds.CSharpVisualBasicLspServer });

// Open an empty loose file.
var looseFileUri = ProtocolConversions.CreateAbsoluteUri(@"C:\SomeFile.cs");
await testLspServer.OpenDocumentAsync(looseFileUri, source).ConfigureAwait(false);

var document = await GetTextDocumentAsync(testLspServer, looseFileUri);
Assert.NotNull(document);

var projectContext = ProtocolConversions.ProjectToProjectContext(document.Project);

Assert.True(projectContext.IsMiscellaneous);
}

internal static async Task<TextDocument?> GetTextDocumentAsync(TestLspServer testLspServer, Uri uri)
{
var (_, _, textDocument) = await testLspServer.GetManager().GetLspDocumentInfoAsync(new TextDocumentIdentifier { Uri = uri }, CancellationToken.None);
return textDocument;
}
}
}
Loading