ReSharper Devguide Help

Analyze Code on the Fly

What you should know beforehand:

Examples (?):

Background code analysis is one of the most popular plugins tasks. The analysis is performed by daemons created using a special IDaemonStage interface. Nevertheless, the easiest way to perform code analysis is to employ the ElementProblemAnalyzer<T> class: it creates the corresponding daemon and daemon stage by itself.

The analysis could be divided into two steps:

  1. Actually, the analysis of code: performed by ElementProblemAnalyzer<T>, where T is the class of code elements you want to analyze.

  2. Problematic code highlighting: performed by a class that implements the IHighlighting interface.

As an example, let's create a code analyzer that checks all variable declarations on whether they contain the word "Crap" (in Create a Quick-Fix, we implement a quick-fix that suggests to replace the "Crap" occurrence with "BadWord").


Problem Analyzer

[ElementProblemAnalyzer(typeof(IVariableDeclaration), HighlightingTypes = new[] {typeof(BadWordNamingWarning)})] public class BadWordNamingAnalyzer : ElementProblemAnalyzer<IVariableDeclaration> { protected override void Run(IVariableDeclaration element, ElementProblemAnalyzerData data, IHighlightingConsumer consumer) { var nodeText = element.DeclaredName.ToLower(); if (!nodeText.Contains("crap")) return; consumer.AddHighlighting(new BadWordNamingWarning(element, element.NameIdentifier.GetDocumentRange())); } }


[StaticSeverityHighlighting(Severity.WARNING, HighlightingGroupIds.GutterMarksGroup)] public class BadWordNamingWarning : IHighlighting { private readonly DocumentRange _documentRange; public readonly IVariableDeclaration VariableDeclaration; public BadWordNamingWarning(IVariableDeclaration variableDeclaration, DocumentRange documentRange) { VariableDeclaration = variableDeclaration; _documentRange = documentRange; } public bool IsValid() { return VariableDeclaration.IsValid(); } public DocumentRange CalculateRange() { return _documentRange; } public string ToolTip => "The name contains a bad word"; public string ErrorStripeToolTip { get; } }
Last modified: 06 August 2021