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

Merge master to master-vs-deps #51385

Merged
merged 2 commits into from
Feb 22, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -7,7 +7,6 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.UnusedReferences
Expand All @@ -26,22 +25,32 @@ internal static class UnusedReferencesRemover
};

public static async Task<ImmutableArray<ReferenceInfo>> GetUnusedReferencesAsync(
Project project,
Solution solution,
string projectFilePath,
ImmutableArray<ReferenceInfo> references,
CancellationToken cancellationToken)
{
// Create a lookup of used assembly paths
var compilation = await project.GetCompilationAsync(cancellationToken).ConfigureAwait(false);
if (compilation is null)
var projects = solution.Projects
.Where(project => projectFilePath.Equals(project.FilePath, System.StringComparison.OrdinalIgnoreCase));

HashSet<string> usedAssemblyFilePaths = new();

foreach (var project in projects)
{
return ImmutableArray<ReferenceInfo>.Empty;
}
// Create a lookup of used assembly paths
var compilation = await project.GetCompilationAsync(cancellationToken).ConfigureAwait(false);
if (compilation is null)
{
continue;
}

var usedAssemblyReferences = compilation.GetUsedAssemblyReferences(cancellationToken);
HashSet<string> usedAssemblyFilePaths = new(usedAssemblyReferences
.OfType<PortableExecutableReference>()
.Select(reference => reference.FilePath)
.WhereNotNull());
var usedAssemblyReferences = compilation.GetUsedAssemblyReferences(cancellationToken);

usedAssemblyFilePaths.AddRange(usedAssemblyReferences
.OfType<PortableExecutableReference>()
.Select(reference => reference.FilePath)
.WhereNotNull());
}

return GetUnusedReferences(usedAssemblyFilePaths, references);
}
Expand Down Expand Up @@ -192,16 +201,17 @@ internal static ImmutableArray<string> GetAllCompilationAssemblies(ReferenceInfo
.ToImmutableArray();
}

public static async Task<Project> UpdateReferencesAsync(
Project project,
public static async Task<Solution> UpdateReferencesAsync(
Solution solution,
string projectFilePath,
ImmutableArray<ReferenceUpdate> referenceUpdates,
CancellationToken cancellationToken)
{
var referenceCleanupService = project.Solution.Workspace.Services.GetRequiredService<IReferenceCleanupService>();
var referenceCleanupService = solution.Workspace.Services.GetRequiredService<IReferenceCleanupService>();

await ApplyReferenceUpdatesAsync(referenceCleanupService, project.FilePath!, referenceUpdates, cancellationToken).ConfigureAwait(true);
await ApplyReferenceUpdatesAsync(referenceCleanupService, projectFilePath, referenceUpdates, cancellationToken).ConfigureAwait(true);

return project.Solution.Workspace.CurrentSolution.GetRequiredProject(project.Id);
return solution.Workspace.CurrentSolution;
}

internal static async Task ApplyReferenceUpdatesAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ public RemoveUnusedReferencesDialog(UnusedReferencesTableProvider tableProvider)
InitializeComponent();
}

public bool? ShowModal(Project project, ImmutableArray<ReferenceUpdate> referenceUpdates)
public bool? ShowModal(Solution solution, string projectFilePath, ImmutableArray<ReferenceUpdate> referenceUpdates)
{
bool? result = null;

try
{
_tableProvider.AddTableData(project, referenceUpdates);
_tableProvider.AddTableData(solution, projectFilePath, referenceUpdates);

using var tableControl = _tableProvider.CreateTableControl();
TablePanel.Child = tableControl.Control;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,8 @@ internal partial class UnusedReferencesTableProvider
{
internal class ReferenceImageMonikers
{
// These GUIDs and IDs are defined in src\Microsoft.VisualStudio.ProjectSystem.Managed.VS\ManagedImages.imagemanifest
private static readonly Guid s_manifestGuid = new("{259567C1-AA6B-46BF-811C-C145DD9F3B48}");

// Change this to use KnownMonikers.NuGetNoColor once we are able to move to Microsoft.VisualStudio.ImageCatalog v16.9
public static ImageMoniker Package => new() { Guid = s_manifestGuid, Id = 9 };
public static ImageMoniker Package => new() { Guid = KnownImageIds.ImageCatalogGuid, Id = 3902 };
public static ImageMoniker Project => KnownMonikers.Application;
public static ImageMoniker Assembly => KnownMonikers.Reference;
}
Expand Down Expand Up @@ -104,6 +101,17 @@ internal static FrameworkElement CreateGridElement(ImageMoniker imageMoniker, st
return stackPanel;
}

private static ImageMoniker GetReferenceTypeImageMoniker(ReferenceType referenceType)
{
return referenceType switch
{
ReferenceType.Package => ReferenceImageMonikers.Package,
ReferenceType.Project => ReferenceImageMonikers.Project,
ReferenceType.Assembly => ReferenceImageMonikers.Assembly,
_ => throw ExceptionUtilities.UnexpectedValue(referenceType)
};
}

[Export(typeof(ITableColumnDefinition))]
[Name(UnusedReferencesColumnDefinitions.SolutionName)]
internal class SolutionNameColumnDefinition : TableColumnDefinitionBase
Expand All @@ -128,6 +136,11 @@ public override bool TryCreateColumnContent(ITableEntryHandle entry, bool single
return false;
}

public override bool TryCreateStringContent(ITableEntryHandle entry, bool truncatedText, bool singleColumnView, out string content)
{
return entry.TryGetValue(UnusedReferencesTableKeyNames.SolutionName, out content);
}

public override IEntryBucket? CreateBucketForEntry(ITableEntryHandle entry)
{
return entry.TryGetValue(UnusedReferencesTableKeyNames.SolutionName, out string name)
Expand Down Expand Up @@ -160,6 +173,11 @@ public override bool TryCreateColumnContent(ITableEntryHandle entry, bool single
return false;
}

public override bool TryCreateStringContent(ITableEntryHandle entry, bool truncatedText, bool singleColumnView, out string content)
{
return entry.TryGetValue(UnusedReferencesTableKeyNames.ProjectName, out content);
}

public override IEntryBucket? CreateBucketForEntry(ITableEntryHandle entry)
{
return entry.TryGetValue(UnusedReferencesTableKeyNames.ProjectName, out string name)
Expand Down Expand Up @@ -191,30 +209,27 @@ public override bool TryCreateColumnContent(ITableEntryHandle entry, bool single
{
if (entry.TryGetValue<ReferenceType>(UnusedReferencesTableKeyNames.ReferenceType, out var referenceType))
{
content = CreateGridElement(GetImageMoniker(referenceType), GetText(referenceType), isBold: false);
content = CreateGridElement(GetReferenceTypeImageMoniker(referenceType), GetText(referenceType), isBold: false);
return true;
}

content = null;
return false;
}

public override IEntryBucket? CreateBucketForEntry(ITableEntryHandle entry)
public override bool TryCreateStringContent(ITableEntryHandle entry, bool truncatedText, bool singleColumnView, out string? content)
{
return entry.TryGetValue<ReferenceType>(UnusedReferencesTableKeyNames.ReferenceType, out var referenceType)
? new ImageEntryBucket(GetImageMoniker(referenceType), GetText(referenceType))
content = entry.TryGetValue<ReferenceType>(UnusedReferencesTableKeyNames.ReferenceType, out var referenceType)
? GetText(referenceType)
: null;
return content != null;
}

private static ImageMoniker GetImageMoniker(ReferenceType referenceType)
public override IEntryBucket? CreateBucketForEntry(ITableEntryHandle entry)
{
return referenceType switch
{
ReferenceType.Package => ReferenceImageMonikers.Package,
ReferenceType.Project => ReferenceImageMonikers.Project,
ReferenceType.Assembly => ReferenceImageMonikers.Assembly,
_ => throw ExceptionUtilities.UnexpectedValue(referenceType)
};
return entry.TryGetValue<ReferenceType>(UnusedReferencesTableKeyNames.ReferenceType, out var referenceType)
? new ImageEntryBucket(GetReferenceTypeImageMoniker(referenceType), GetText(referenceType))
: null;
}

private static string GetText(ReferenceType referenceType)
Expand Down Expand Up @@ -250,16 +265,15 @@ public override bool TryCreateColumnContent(ITableEntryHandle entry, bool single
return true;
}

public override bool TryCreateStringContent(ITableEntryHandle entry, bool truncatedText, bool singleColumnView, out string content)
{
return entry.TryGetValue(UnusedReferencesTableKeyNames.ReferenceName, out content);
}

private static ImageMoniker GetImageMoniker(ITableEntryHandle entry)
{
return entry.TryGetValue(UnusedReferencesTableKeyNames.ReferenceType, out ReferenceType referenceType)
? referenceType switch
{
ReferenceType.Package => KnownMonikers.PackageReference,
ReferenceType.Project => KnownMonikers.Library,
ReferenceType.Assembly => KnownMonikers.Reference,
_ => throw ExceptionUtilities.UnexpectedValue(referenceType)
}
? GetReferenceTypeImageMoniker(referenceType)
: default;
}

Expand All @@ -284,6 +298,7 @@ public UpdateActionColumnDefinition()
public override string Name => UnusedReferencesColumnDefinitions.UpdateAction;
public override string DisplayName => ServicesVSResources.Action;
public override bool IsFilterable => false;
public override bool IsSortable => false;
public override double MinWidth => 100;

public override bool TryCreateColumnContent(ITableEntryHandle entry, bool singleColumnView, out FrameworkElement? content)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ public IDisposable Subscribe(ITableDataSink sink)
return new SinkManager(this, sink);
}

public void AddTableData(Project project, ImmutableArray<ReferenceUpdate> referenceUpdates)
public void AddTableData(Solution solution, string projectFilePath, ImmutableArray<ReferenceUpdate> referenceUpdates)
{
var solutionName = Path.GetFileName(project.Solution.FilePath);
var solutionName = Path.GetFileName(solution.FilePath);
var project = solution.Projects.First(project => projectFilePath.Equals(project.FilePath, StringComparison.OrdinalIgnoreCase));
var entries = referenceUpdates
.Select(update => new UnusedReferencesEntry(solutionName, project.Name, project.Language, update))
.ToImmutableArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,16 @@ static ImmutableArray<ColumnState> BuildColumnStates()
{
return ImmutableArray.Create(
new ColumnState2(UnusedReferencesColumnDefinitions.SolutionName, isVisible: false, width: 200, sortPriority: 0, descendingSort: false, groupingPriority: 1),
new ColumnState2(UnusedReferencesColumnDefinitions.ProjectName, isVisible: false, width: 200, sortPriority: 0, descendingSort: false, groupingPriority: 2),
new ColumnState2(UnusedReferencesColumnDefinitions.ReferenceType, isVisible: false, width: 200, sortPriority: 0, descendingSort: false, groupingPriority: 3),
new ColumnState(UnusedReferencesColumnDefinitions.ReferenceName, isVisible: true, width: 300, sortPriority: 0, descendingSort: false),
new ColumnState(UnusedReferencesColumnDefinitions.UpdateAction, isVisible: true, width: 100, sortPriority: 0, descendingSort: false));
new ColumnState2(UnusedReferencesColumnDefinitions.ProjectName, isVisible: false, width: 200, sortPriority: 1, descendingSort: false, groupingPriority: 2),
new ColumnState2(UnusedReferencesColumnDefinitions.ReferenceType, isVisible: false, width: 200, sortPriority: 2, descendingSort: false, groupingPriority: 3),
new ColumnState(UnusedReferencesColumnDefinitions.ReferenceName, isVisible: true, width: 300, sortPriority: 3, descendingSort: false),
new ColumnState(UnusedReferencesColumnDefinitions.UpdateAction, isVisible: true, width: 100, sortPriority: 4, descendingSort: false));
}
}

public void AddTableData(Project project, ImmutableArray<ReferenceUpdate> referenceUpdates)
public void AddTableData(Solution solution, string projectFilePath, ImmutableArray<ReferenceUpdate> referenceUpdates)
{
_dataSource.AddTableData(project, referenceUpdates);
_dataSource.AddTableData(solution, projectFilePath, referenceUpdates);
}

public void ClearTableData()
Expand Down
Loading