Skip to content

Commit

Permalink
Merge pull request #51384 from dotnet/merges/release/dev16.10-vs-deps…
Browse files Browse the repository at this point in the history
…-to-master-vs-deps

Merge release/dev16.10-vs-deps to master-vs-deps
  • Loading branch information
msftbot[bot] authored Feb 21, 2021
2 parents 0338c43 + a3b21e9 commit 475b525
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 124 deletions.
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

0 comments on commit 475b525

Please sign in to comment.