Skip to content

Commit

Permalink
Merge pull request #75384 from dotnet/dev/jorobich/warn-misc-files
Browse files Browse the repository at this point in the history
Add a WorkspaceKind property to ProjectContext.
  • Loading branch information
JoeRobich authored Oct 8, 2024
2 parents b7b39ca + 32d3c40 commit c625f52
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 deletions.
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
66 changes: 65 additions & 1 deletion src/LanguageServer/ProtocolUnitTests/ProtocolConversionsTests.cs
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;
}
}
}

0 comments on commit c625f52

Please sign in to comment.