Skip to content

Commit

Permalink
use primary constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
CyrusNajmabadi committed May 22, 2023
1 parent f9e7a97 commit b19d2a6
Show file tree
Hide file tree
Showing 251 changed files with 1,831 additions and 4,013 deletions.
12 changes: 3 additions & 9 deletions src/Workspaces/Core/Portable/Classification/ClassifiedSpan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,16 @@

namespace Microsoft.CodeAnalysis.Classification
{
public readonly struct ClassifiedSpan : IEquatable<ClassifiedSpan>
public readonly struct ClassifiedSpan(TextSpan textSpan, string classificationType) : IEquatable<ClassifiedSpan>
{
public string ClassificationType { get; }
public TextSpan TextSpan { get; }
public string ClassificationType { get; } = classificationType;
public TextSpan TextSpan { get; } = textSpan;

public ClassifiedSpan(string classificationType, TextSpan textSpan)
: this(textSpan, classificationType)
{
}

public ClassifiedSpan(TextSpan textSpan, string classificationType)
{
this.ClassificationType = classificationType;
this.TextSpan = textSpan;
}

public override int GetHashCode()
=> Hash.Combine(this.ClassificationType, this.TextSpan.GetHashCode());

Expand Down
12 changes: 3 additions & 9 deletions src/Workspaces/Core/Portable/Classification/ClassifiedText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,9 @@

namespace Microsoft.CodeAnalysis.Classification
{
internal readonly struct ClassifiedText
internal readonly struct ClassifiedText(string classificationType, string text)
{
public string ClassificationType { get; }
public string Text { get; }

public ClassifiedText(string classificationType, string text)
{
ClassificationType = classificationType;
Text = text;
}
public string ClassificationType { get; } = classificationType;
public string Text { get; } = text;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,13 @@ ValueTask<SerializableClassifiedSpans> GetClassificationsAsync(
/// second and third ints encode the span.
/// </summary>
[DataContract]
internal sealed class SerializableClassifiedSpans
internal sealed class SerializableClassifiedSpans(ImmutableArray<string> classificationTypes, ImmutableArray<int> classificationTriples)
{
[DataMember(Order = 0)]
public readonly ImmutableArray<string> ClassificationTypes;
public readonly ImmutableArray<string> ClassificationTypes = classificationTypes;

[DataMember(Order = 1)]
public readonly ImmutableArray<int> ClassificationTriples;

public SerializableClassifiedSpans(ImmutableArray<string> classificationTypes, ImmutableArray<int> classificationTriples)
{
ClassificationTypes = classificationTypes;
ClassificationTriples = classificationTriples;
}
public readonly ImmutableArray<int> ClassificationTriples = classificationTriples;

internal static SerializableClassifiedSpans Dehydrate(ImmutableArray<ClassifiedSpan> classifiedSpans)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,9 @@ namespace Microsoft.CodeAnalysis.CodeActions
/// </list>
/// </summary>
#pragma warning restore RS0030 // Do not used banned APIs
public sealed class ApplyChangesOperation : CodeActionOperation
public sealed class ApplyChangesOperation(Solution changedSolution) : CodeActionOperation
{
public Solution ChangedSolution { get; }

public ApplyChangesOperation(Solution changedSolution)
=> ChangedSolution = changedSolution ?? throw new ArgumentNullException(nameof(changedSolution));
public Solution ChangedSolution { get; } = changedSolution ?? throw new ArgumentNullException(nameof(changedSolution));

internal override bool ApplyDuringTests => true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,17 @@ namespace Microsoft.CodeAnalysis.CodeActions
/// <summary>
/// A code action operation for requesting a document be opened in the host environment.
/// </summary>
public sealed class OpenDocumentOperation : CodeActionOperation
public sealed class OpenDocumentOperation(DocumentId documentId, bool activateIfAlreadyOpen = false) : CodeActionOperation
{
private readonly DocumentId _documentId;
private readonly bool _activate;

public OpenDocumentOperation(DocumentId documentId, bool activateIfAlreadyOpen = false)
{
_documentId = documentId ?? throw new ArgumentNullException(nameof(documentId));
_activate = activateIfAlreadyOpen;
}
private readonly DocumentId _documentId = documentId ?? throw new ArgumentNullException(nameof(documentId));

public DocumentId DocumentId => _documentId;

public override void Apply(Workspace workspace, CancellationToken cancellationToken)
{
if (workspace.CanOpenDocuments)
{
workspace.OpenDocument(_documentId, _activate);
workspace.OpenDocument(_documentId, activateIfAlreadyOpen);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,15 @@

namespace Microsoft.CodeAnalysis.CodeCleanup.Providers
{
internal sealed class FormatCodeCleanupProvider : ICodeCleanupProvider
internal sealed class FormatCodeCleanupProvider(IEnumerable<AbstractFormattingRule>? rules = null) : ICodeCleanupProvider
{
private readonly IEnumerable<AbstractFormattingRule>? _rules;

public FormatCodeCleanupProvider(IEnumerable<AbstractFormattingRule>? rules = null)
{
_rules = rules;
}

public string Name => PredefinedCodeCleanupProviderNames.Format;

public async Task<Document> CleanupAsync(Document document, ImmutableArray<TextSpan> spans, CodeCleanupOptions options, CancellationToken cancellationToken)
{
var formatter = document.GetRequiredLanguageService<ISyntaxFormattingService>();
var root = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var result = formatter.GetFormattingResult(root, spans, options.FormattingOptions, _rules, cancellationToken);
var result = formatter.GetFormattingResult(root, spans, options.FormattingOptions, rules, cancellationToken);

// apply changes to an old text if it already exists
return document.TryGetText(out var oldText)
Expand All @@ -40,7 +33,7 @@ public async Task<Document> CleanupAsync(Document document, ImmutableArray<TextS
public Task<SyntaxNode> CleanupAsync(SyntaxNode root, ImmutableArray<TextSpan> spans, SyntaxFormattingOptions options, SolutionServices services, CancellationToken cancellationToken)
{
var formatter = services.GetRequiredLanguageService<ISyntaxFormattingService>(root.Language);
var result = formatter.GetFormattingResult(root, spans, options, _rules, cancellationToken);
var result = formatter.GetFormattingResult(root, spans, options, rules, cancellationToken);

// apply changes to an old text if it already exists
return (root.SyntaxTree != null && root.SyntaxTree.TryGetText(out var oldText))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,12 @@ public static FixAllProvider Create(
=> this.GetFixAsync((FixAllContext)fixAllContext);
#endregion

private class CallbackDocumentBasedFixAllProvider : DocumentBasedFixAllProvider
private class CallbackDocumentBasedFixAllProvider(
Func<FixAllContext, Document, ImmutableArray<Diagnostic>, Task<Document?>> fixAllAsync,
ImmutableArray<FixAllScope> supportedFixAllScopes) : DocumentBasedFixAllProvider(supportedFixAllScopes)
{
private readonly Func<FixAllContext, Document, ImmutableArray<Diagnostic>, Task<Document?>> _fixAllAsync;

public CallbackDocumentBasedFixAllProvider(
Func<FixAllContext, Document, ImmutableArray<Diagnostic>, Task<Document?>> fixAllAsync,
ImmutableArray<FixAllScope> supportedFixAllScopes)
: base(supportedFixAllScopes)
{
_fixAllAsync = fixAllAsync;
}

protected override Task<Document?> FixAllAsync(FixAllContext context, Document document, ImmutableArray<Diagnostic> diagnostics)
=> _fixAllAsync(context, document, diagnostics);
=> fixAllAsync(context, document, diagnostics);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,30 @@ namespace Microsoft.CodeAnalysis.CodeFixes
/// <summary>
/// Helper to merge many disparate text changes to a single document together into a total set of changes.
/// </summary>
internal class TextChangeMerger
internal class TextChangeMerger(Document document)
{
private readonly struct IntervalIntrospector : IIntervalIntrospector<TextChange>
{
int IIntervalIntrospector<TextChange>.GetStart(TextChange value) => value.Span.Start;
int IIntervalIntrospector<TextChange>.GetLength(TextChange value) => value.Span.Length;
}

private readonly Document _oldDocument;
private readonly IDocumentTextDifferencingService _differenceService;
private readonly IDocumentTextDifferencingService _differenceService = document.Project.Solution.Services.GetRequiredService<IDocumentTextDifferencingService>();

private readonly SimpleIntervalTree<TextChange, IntervalIntrospector> _totalChangesIntervalTree =
SimpleIntervalTree.Create(new IntervalIntrospector(), Array.Empty<TextChange>());

public TextChangeMerger(Document document)
{
_oldDocument = document;
_differenceService = document.Project.Solution.Services.GetRequiredService<IDocumentTextDifferencingService>();
}

/// <summary>
/// Try to merge the changes made to <paramref name="newDocument"/> into the tracked changes. If there is any
/// conflicting change in <paramref name="newDocument"/> with existing changes, then no changes are added.
/// </summary>
public async Task TryMergeChangesAsync(Document newDocument, CancellationToken cancellationToken)
{
Debug.Assert(newDocument.Id == _oldDocument.Id);
Debug.Assert(newDocument.Id == document.Id);

cancellationToken.ThrowIfCancellationRequested();
var currentChanges = await _differenceService.GetTextChangesAsync(
_oldDocument, newDocument, cancellationToken).ConfigureAwait(false);
document, newDocument, cancellationToken).ConfigureAwait(false);

if (AllChangesCanBeApplied(_totalChangesIntervalTree, currentChanges))
{
Expand All @@ -71,7 +64,7 @@ public async Task<SourceText> GetFinalMergedTextAsync(CancellationToken cancella
// WithChanges requires a ordered list of TextChanges without any overlap.
var changesToApply = _totalChangesIntervalTree.Distinct().OrderBy(tc => tc.Span.Start);

var oldText = await _oldDocument.GetValueTextAsync(cancellationToken).ConfigureAwait(false);
var oldText = await document.GetValueTextAsync(cancellationToken).ConfigureAwait(false);
var newText = oldText.WithChanges(changesToApply);

return newText;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,49 +112,30 @@ private FixAllProviderInfo(

public abstract bool CanBeFixed(Diagnostic diagnostic);

private class CodeFixerFixAllProviderInfo : FixAllProviderInfo
private class CodeFixerFixAllProviderInfo(
IFixAllProvider fixAllProvider,
IEnumerable<string> supportedDiagnosticIds,
ImmutableArray<FixAllScope> supportedScopes) : FixAllProviderInfo(fixAllProvider, supportedScopes)
{
private readonly IEnumerable<string> _supportedDiagnosticIds;

public CodeFixerFixAllProviderInfo(
IFixAllProvider fixAllProvider,
IEnumerable<string> supportedDiagnosticIds,
ImmutableArray<FixAllScope> supportedScopes)
: base(fixAllProvider, supportedScopes)
{
_supportedDiagnosticIds = supportedDiagnosticIds;
}

public override bool CanBeFixed(Diagnostic diagnostic)
=> _supportedDiagnosticIds.Contains(diagnostic.Id);
=> supportedDiagnosticIds.Contains(diagnostic.Id);
}

private class SuppressionFixerFixAllProviderInfo : FixAllProviderInfo
private class SuppressionFixerFixAllProviderInfo(
IFixAllProvider fixAllProvider,
IConfigurationFixProvider suppressionFixer,
ImmutableArray<FixAllScope> supportedScopes) : FixAllProviderInfo(fixAllProvider, supportedScopes)
{
private readonly Func<Diagnostic, bool> _canBeSuppressedOrUnsuppressed;

public SuppressionFixerFixAllProviderInfo(
IFixAllProvider fixAllProvider,
IConfigurationFixProvider suppressionFixer,
ImmutableArray<FixAllScope> supportedScopes)
: base(fixAllProvider, supportedScopes)
{
_canBeSuppressedOrUnsuppressed = suppressionFixer.IsFixableDiagnostic;
}
private readonly Func<Diagnostic, bool> _canBeSuppressedOrUnsuppressed = suppressionFixer.IsFixableDiagnostic;

public override bool CanBeFixed(Diagnostic diagnostic)
=> _canBeSuppressedOrUnsuppressed(diagnostic);
}

private class CodeRefactoringFixAllProviderInfo : FixAllProviderInfo
private class CodeRefactoringFixAllProviderInfo(
IFixAllProvider fixAllProvider,
ImmutableArray<FixAllScope> supportedScopes) : FixAllProviderInfo(fixAllProvider, supportedScopes)
{
public CodeRefactoringFixAllProviderInfo(
IFixAllProvider fixAllProvider,
ImmutableArray<FixAllScope> supportedScopes)
: base(fixAllProvider, supportedScopes)
{
}

public override bool CanBeFixed(Diagnostic diagnostic)
=> throw ExceptionUtilities.Unreachable();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,12 @@ public static FixAllProvider Create(
return new CallbackDocumentBasedFixAllProvider(fixAllAsync, supportedFixAllScopes);
}

private sealed class CallbackDocumentBasedFixAllProvider : DocumentBasedFixAllProvider
private sealed class CallbackDocumentBasedFixAllProvider(
Func<FixAllContext, Document, Optional<ImmutableArray<TextSpan>>, Task<Document?>> fixAllAsync,
ImmutableArray<FixAllScope> supportedFixAllScopes) : DocumentBasedFixAllProvider(supportedFixAllScopes)
{
private readonly Func<FixAllContext, Document, Optional<ImmutableArray<TextSpan>>, Task<Document?>> _fixAllAsync;

public CallbackDocumentBasedFixAllProvider(
Func<FixAllContext, Document, Optional<ImmutableArray<TextSpan>>, Task<Document?>> fixAllAsync,
ImmutableArray<FixAllScope> supportedFixAllScopes)
: base(supportedFixAllScopes)
{
_fixAllAsync = fixAllAsync;
}

protected override Task<Document?> FixAllAsync(FixAllContext context, Document document, Optional<ImmutableArray<TextSpan>> fixAllSpans)
=> _fixAllAsync(context, document, fixAllSpans);
=> fixAllAsync(context, document, fixAllSpans);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,18 @@ namespace Microsoft.CodeAnalysis.Workspaces.Diagnostics
/// We have this builder to avoid creating collections unnecessarily.
/// Expectation is that, most of time, most of analyzers doesn't have any diagnostics. so no need to actually create any objects.
/// </summary>
internal struct DiagnosticAnalysisResultBuilder
internal struct DiagnosticAnalysisResultBuilder(Project project, VersionStamp version)
{
public readonly Project Project;
public readonly VersionStamp Version;
public readonly Project Project = project;
public readonly VersionStamp Version = version;

private HashSet<DocumentId>? _lazyDocumentsWithDiagnostics;
private HashSet<DocumentId>? _lazyDocumentsWithDiagnostics = null;

private Dictionary<DocumentId, List<DiagnosticData>>? _lazySyntaxLocals;
private Dictionary<DocumentId, List<DiagnosticData>>? _lazySemanticLocals;
private Dictionary<DocumentId, List<DiagnosticData>>? _lazyNonLocals;
private Dictionary<DocumentId, List<DiagnosticData>>? _lazySyntaxLocals = null;
private Dictionary<DocumentId, List<DiagnosticData>>? _lazySemanticLocals = null;
private Dictionary<DocumentId, List<DiagnosticData>>? _lazyNonLocals = null;

private List<DiagnosticData>? _lazyOthers;

public DiagnosticAnalysisResultBuilder(Project project, VersionStamp version)
{
Project = project;
Version = version;

_lazyDocumentsWithDiagnostics = null;
_lazySyntaxLocals = null;
_lazySemanticLocals = null;
_lazyNonLocals = null;
_lazyOthers = null;
}
private List<DiagnosticData>? _lazyOthers = null;

public readonly ImmutableHashSet<DocumentId> DocumentIds => _lazyDocumentsWithDiagnostics == null ? ImmutableHashSet<DocumentId>.Empty : _lazyDocumentsWithDiagnostics.ToImmutableHashSet();
public readonly ImmutableDictionary<DocumentId, ImmutableArray<DiagnosticData>> SyntaxLocals => Convert(_lazySyntaxLocals);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,11 @@ internal sealed partial class DiagnosticAnalyzerInfoCache
/// </summary>
private readonly ConcurrentDictionary<string, DiagnosticDescriptor?> _idToDescriptorsMap;

private sealed class DiagnosticDescriptorsInfo
private sealed class DiagnosticDescriptorsInfo(ImmutableArray<DiagnosticDescriptor> supportedDescriptors, bool telemetryAllowed)
{
public readonly ImmutableArray<DiagnosticDescriptor> SupportedDescriptors;
public readonly bool TelemetryAllowed;
public readonly bool HasCompilationEndDescriptor;

public DiagnosticDescriptorsInfo(ImmutableArray<DiagnosticDescriptor> supportedDescriptors, bool telemetryAllowed)
{
SupportedDescriptors = supportedDescriptors;
TelemetryAllowed = telemetryAllowed;
HasCompilationEndDescriptor = supportedDescriptors.Any(DiagnosticDescriptorExtensions.IsCompilationEnd);
}
public readonly ImmutableArray<DiagnosticDescriptor> SupportedDescriptors = supportedDescriptors;
public readonly bool TelemetryAllowed = telemetryAllowed;
public readonly bool HasCompilationEndDescriptor = supportedDescriptors.Any(DiagnosticDescriptorExtensions.IsCompilationEnd);
}

[Export, Shared]
Expand Down
Loading

0 comments on commit b19d2a6

Please sign in to comment.