-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add PushProperty support for naming analyzer
- Loading branch information
Showing
21 changed files
with
312 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#### Inconsistent log property naming in context (can be configured in the extension settings) | ||
|
||
Noncompliant Code Examples: | ||
```csharp | ||
LogContext.PushProperty("property_name", 1); | ||
``` | ||
|
||
Compliant Solution: | ||
```csharp | ||
LogContext.PushProperty("PropertyName", 1); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/ReSharper.Structured.Logging/Highlighting/InconsistentContextLogPropertyNamingWarning.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using JetBrains.DocumentModel; | ||
using JetBrains.ReSharper.Feature.Services.Daemon; | ||
using JetBrains.ReSharper.Psi.CSharp; | ||
using JetBrains.ReSharper.Psi.CSharp.Tree; | ||
|
||
using ReSharper.Structured.Logging.Settings; | ||
|
||
namespace ReSharper.Structured.Logging.Highlighting | ||
{ | ||
[RegisterConfigurableSeverity( | ||
SeverityId, | ||
null, | ||
StructuredLoggingGroup.Id, | ||
Message, | ||
Message, | ||
Severity.WARNING)] | ||
[ConfigurableSeverityHighlighting( | ||
SeverityId, | ||
CSharpLanguage.Name, | ||
OverlapResolve = OverlapResolveKind.WARNING, | ||
ToolTipFormatString = Message)] | ||
public class InconsistentContextLogPropertyNamingWarning : InconsistentLogPropertyNamingWarningBase, IHighlighting | ||
{ | ||
private readonly string _propertyName; | ||
|
||
public const string SeverityId = "InconsistentContextLogPropertyNaming"; | ||
|
||
public InconsistentContextLogPropertyNamingWarning(ICSharpArgument argument, string propertyName, string suggestedName) | ||
{ | ||
_propertyName = propertyName; | ||
Argument = argument; | ||
SuggestedName = suggestedName; | ||
} | ||
|
||
public string ErrorStripeToolTip => ToolTip; | ||
|
||
public ICSharpArgument Argument { get; } | ||
|
||
|
||
public string SuggestedName { get; } | ||
|
||
public string ToolTip => GetToolTipMessage(_propertyName, SuggestedName); | ||
|
||
public DocumentRange CalculateRange() | ||
{ | ||
return Argument.GetDocumentRange(); | ||
} | ||
|
||
public bool IsValid() | ||
{ | ||
return Argument.GetDocumentRange().IsValid(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/ReSharper.Structured.Logging/Highlighting/InconsistentLogPropertyNamingWarningBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace ReSharper.Structured.Logging.Highlighting | ||
{ | ||
public class InconsistentLogPropertyNamingWarningBase | ||
{ | ||
protected const string Message = "Property name does not match naming rules."; | ||
|
||
protected string GetToolTipMessage(string propertyName, string suggestedName) | ||
{ | ||
return $"Property name '{propertyName}' does not match naming rules'. Suggested name is '{suggestedName}'."; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/ReSharper.Structured.Logging/QuickFixes/RenameContextLogPropertyFix.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using System; | ||
|
||
using JetBrains.Annotations; | ||
using JetBrains.Application.Progress; | ||
using JetBrains.ProjectModel; | ||
using JetBrains.ReSharper.Feature.Services.QuickFixes; | ||
using JetBrains.ReSharper.Psi.CSharp; | ||
using JetBrains.ReSharper.Psi.CSharp.Tree; | ||
using JetBrains.ReSharper.Psi.ExtensionsAPI.Tree; | ||
using JetBrains.ReSharper.Resources.Shell; | ||
using JetBrains.TextControl; | ||
using JetBrains.Util; | ||
|
||
using ReSharper.Structured.Logging.Highlighting; | ||
|
||
namespace ReSharper.Structured.Logging.QuickFixes | ||
{ | ||
[QuickFix] | ||
public class RenameContextLogPropertyFix : QuickFixBase | ||
{ | ||
private readonly ICSharpArgument _argument; | ||
|
||
private readonly string _suggestedName; | ||
|
||
public RenameContextLogPropertyFix([NotNull] InconsistentContextLogPropertyNamingWarning error) | ||
{ | ||
_suggestedName = error.SuggestedName; | ||
_argument = error.Argument; | ||
} | ||
|
||
public override string Text => $"Rename property to '{_suggestedName}'"; | ||
|
||
public override bool IsAvailable(IUserDataHolder cache) | ||
{ | ||
return _argument.IsValid(); | ||
} | ||
|
||
protected override Action<ITextControl> ExecutePsiTransaction(ISolution solution, IProgressIndicator progress) | ||
{ | ||
using (WriteLockCookie.Create()) | ||
{ | ||
// ReSharper disable once AssignNullToNotNullAttribute | ||
var factory = CSharpElementFactory.GetInstance(_argument.Expression, false); | ||
|
||
var expression = factory.CreateExpression($"\"{_suggestedName}\""); | ||
ModificationUtil.ReplaceChild(_argument.Expression, expression); | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
} |
Oops, something went wrong.