Skip to content

Code Inspections 2.0

Mathieu Guindon edited this page Feb 9, 2017 · 1 revision

In Rubberduck 1.x and early 2.0 releases, code inspections were pretty much an on-the-side "list of issues", pretty much unrelated to parse tree nodes. Some results had a Declaration target, others had an IdentifierReference, and some had a ParserRuleContext target.

The new design streamlines everything to the very simple InspectionResults extension method:

/// <summary>
/// Allows annotating a context with inspection results, iterating them, and clearing them.
/// </summary>
/// <returns>The context instance, cast to an <see cref="ICollection{IInspectionResult}"/>.</returns>
/// <exception cref="InvalidCastException">Thrown when context does not implement <see cref="ICollection{IInspectionResult}"/>.</exception>
/// <remarks>Backed with a <see cref="ConcurrentBag{IInspectionResult}"/>, the collection does not support <see cref="ICollection{IInspectionResult}.Remove"/>.</remarks>
public static ICollection<IInspectionResult> InspectionResults(this ParserRuleContext context)
{
    return (ICollection<IInspectionResult>)context;
}

The IInspectionResult interface describes each inspection result:

/// <summary>
/// Describes an inspection result.
/// </summary>
public interface IInspectionResult : IComparable<IInspectionResult>, IComparable
{
    /// <summary>
    /// The localized result description.
    /// </summary>
    string Description { get; }
        
    /// <summary>
    /// The inspection that produced this result.
    /// </summary>
    IInspection Inspection { get; }

    /// <summary>
    /// The <see cref="IQuickFix"/> options available.
    /// </summary>
    IEnumerable<IQuickFix> QuickFixes { get; }
}

Before these interfaces were modified, an "inspector" was responsible for collecting and aggregating every inspection's results, and returning them to, say, the Inspection Results toolwindow's ViewModel.

As of this writing, this is the IInspection interface:

/// <summary>
/// An interface that abstracts a runnable code inspection.
/// </summary>
public interface IInspection : IInspectionModel, IComparable<IInspection>, IComparable
{
    /// <summary>
    /// Runs code inspection and returns inspection results.
    /// </summary>
    /// <returns>Returns inspection results, if any.</returns>
    [Obsolete("Use the Execute() method instead.")]
    IEnumerable<IInspectionResult> GetInspectionResults();

    /// <summary>
    /// Runs code inspection and annotates targets.
    /// </summary>
    void Execute();
}

A completely different ball game: now when we need inspection results, we have them in the parse tree itself, which makes them much more accessible.

Clone this wiki locally