Skip to content

Commit a8060cb

Browse files
committed
Fix highlighting with escaping
1 parent 68da5b8 commit a8060cb

11 files changed

+75
-45
lines changed

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ image: Visual Studio 2017
33
install:
44
- dotnet tool install -g Cake.Tool --version 0.30.0
55
build_script:
6-
- cmd: dotnet cake -Target=CI
6+
- cmd: dotnet cake -Target=CI -buildConfig=Release
77
test: off
88
cache:
99
- '%USERPROFILE%\.sonar\cache'

build.cake

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44

55
var target = Argument("target", "Default");
66
var buildConfiguration = Argument("buildConfig", "Debug");
7-
var waveVersion = Argument("wave", "[182.0]");
8-
var extensionsVersion = Argument("Version", "2018.2.3");
7+
var waveVersion = Argument("wave", "[183.0]");
8+
var extensionsVersion = Argument("Version", "2018.3.0");
99

1010
var solutionName = "ReSharper.Structured.Logging";
1111
var projectName = solutionName;
1212

1313
var solutionFile = string.Format("./src/{0}.sln", solutionName);
1414
var solutionFolder = string.Format("./src/{0}/", solutionName);
15-
var testBinaries = string.Format("test/src/**/ReSharper.Structured.Logging.Tests.dll", solutionFolder);
1615
var projectFile = string.Format("{0}{1}.csproj", solutionFolder, projectName);
1716

1817
Task("AppendBuildNumber")
@@ -55,16 +54,12 @@ Task("NugetPack")
5554

5655
var files = new List<NuSpecContent>();
5756
files.Add(new NuSpecContent {Source = string.Format("{0}/{1}.dll", buildPath, projectName), Target = "dotFiles"});
58-
59-
if (buildConfiguration == "Debug")
60-
{
61-
files.Add(new NuSpecContent {Source = string.Format("{0}/{1}.pdb", buildPath, projectName), Target = "dotFiles"});
62-
}
57+
files.Add(new NuSpecContent {Source = string.Format("{0}/{1}.pdb", buildPath, projectName), Target = "dotFiles"});
6358

6459
var nuGetPackSettings = new NuGetPackSettings {
6560
Id = projectName,
6661
Version = extensionsVersion,
67-
Title = "Structure Logging",
62+
Title = "Structured Logging",
6863
Authors = new[] { "Oleg Shevchenko" },
6964
Owners = new[] { "Oleg Shevchenko" },
7065
Description = "Provides support for Serilog",

src/ReSharper.Structured.Logging/Analyzer/DuplicatePropertiesTemplateAnalyzer.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
using JetBrains.ReSharper.Feature.Services.Daemon;
44
using JetBrains.ReSharper.Psi.CSharp.Tree;
5-
using JetBrains.ReSharper.Psi.Tree;
65
using JetBrains.ReSharper.Psi.Util;
76

87
using ReSharper.Structured.Logging.Extensions;
@@ -38,7 +37,7 @@ protected override void Run(
3837
return;
3938
}
4039

41-
var messageTemplate = _messageTemplateParser.Parse(stringLiteral.CompiledValue);
40+
var messageTemplate = _messageTemplateParser.Parse(stringLiteral.Expression.GetUnquotedText());
4241
if (messageTemplate.NamedProperties == null)
4342
{
4443
return;
@@ -50,10 +49,7 @@ protected override void Run(
5049
{
5150
foreach (var token in duplicates)
5251
{
53-
consumer.AddHighlighting(
54-
new DuplicateTemplatePropertyWarning(
55-
stringLiteral.Expression.GetDocumentRange()
56-
.GetTokenDocumentRange(token)));
52+
consumer.AddHighlighting(new DuplicateTemplatePropertyWarning(stringLiteral.GetTokenDocumentRange(token)));
5753
}
5854
}
5955
}

src/ReSharper.Structured.Logging/Analyzer/TemplateFormatAnalyzer.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using JetBrains.ReSharper.Feature.Services.Daemon;
55
using JetBrains.ReSharper.Psi;
66
using JetBrains.ReSharper.Psi.CSharp.Tree;
7-
using JetBrains.ReSharper.Psi.Tree;
87
using JetBrains.ReSharper.Psi.Util;
98

109
using ReSharper.Structured.Logging.Extensions;
@@ -41,7 +40,7 @@ protected override void Run(
4140
return;
4241
}
4342

44-
var messageTemplate = _messageTemplateParser.Parse(stringLiteral.CompiledValue);
43+
var messageTemplate = _messageTemplateParser.Parse(stringLiteral.Expression.GetUnquotedText());
4544

4645
HighlightTemplate(consumer, stringLiteral, messageTemplate);
4746
HighlightUnusedArguments(element, consumer, templateArgument, messageTemplate);
@@ -56,9 +55,8 @@ protected override void Run(
5655
if (argumentsCount < 0)
5756
{
5857
consumer.AddHighlighting(
59-
new TemplateFormatStringInexistingArgumentWarning(
60-
stringLiteral.Expression.GetDocumentRange()
61-
.GetTokenDocumentRange(property)));
58+
new TemplateFormatStringUnexistingArgumentWarning(
59+
stringLiteral.GetTokenDocumentRange(property)));
6260
}
6361
}
6462
}
@@ -74,9 +72,8 @@ protected override void Run(
7472
if (position >= argumentsCount)
7573
{
7674
consumer.AddHighlighting(
77-
new TemplateFormatStringInexistingArgumentWarning(
78-
stringLiteral.Expression.GetDocumentRange()
79-
.GetTokenDocumentRange(property)));
75+
new TemplateFormatStringUnexistingArgumentWarning(
76+
stringLiteral.GetTokenDocumentRange(property)));
8077
}
8178
}
8279
}
@@ -87,7 +84,6 @@ private static void HighlightTemplate(
8784
IStringLiteralAlterer stringLiteral,
8885
MessageTemplate messageTemplate)
8986
{
90-
var documentRange = stringLiteral.Expression.GetDocumentRange();
9187
foreach (var token in messageTemplate.Tokens)
9288
{
9389
if (!(token is PropertyToken))
@@ -97,7 +93,7 @@ private static void HighlightTemplate(
9793

9894
consumer.AddHighlighting(
9995
new StringEscapeCharacterHighlighting(
100-
documentRange.GetTokenDocumentRange(token),
96+
stringLiteral.GetTokenDocumentRange(token),
10197
HighlightingAttributeIds.FORMAT_STRING_ITEM));
10298
}
10399
}

src/ReSharper.Structured.Logging/Extensions/PsiExtensions.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using JetBrains.ReSharper.Feature.Services.CSharp.CodeStyle.Suggestions;
66
using JetBrains.ReSharper.Psi;
77
using JetBrains.ReSharper.Psi.CSharp.Tree;
8+
using JetBrains.ReSharper.Psi.Tree;
89
using JetBrains.ReSharper.Psi.Util;
910
using JetBrains.Util;
1011

@@ -33,19 +34,23 @@ public static ICSharpArgument GetTemplateArgument(this IInvocationExpression inv
3334
a => a.GetMatchingParameterName() == templateParameterName);
3435
}
3536

36-
[CanBeNull]
37-
public static IStringLiteralAlterer GetMessageTemplateStringLiteral(this IInvocationExpression invocation)
37+
public static DocumentRange GetTokenDocumentRange(this ICSharpArgument argument, MessageTemplateToken token)
3838
{
39-
var templateArgument = invocation.GetTemplateArgument();
40-
if (templateArgument == null)
41-
{
42-
return null;
43-
}
39+
var documentRange = argument.GetDocumentRange();
40+
41+
return GetTokenDocumentRange(token, documentRange);
42+
}
43+
44+
public static DocumentRange GetTokenDocumentRange(this IStringLiteralAlterer stringLiteralAlterer, MessageTemplateToken token)
45+
{
46+
var documentRange = stringLiteralAlterer.Expression.GetDocumentRange();
4447

45-
return StringLiteralAltererUtil.TryCreateStringLiteralByExpression(templateArgument.Value);
48+
return GetTokenDocumentRange(token, documentRange);
4649
}
4750

48-
public static DocumentRange GetTokenDocumentRange(this DocumentRange documentRange, MessageTemplateToken token)
51+
private static DocumentRange GetTokenDocumentRange(
52+
MessageTemplateToken token,
53+
DocumentRange documentRange)
4954
{
5055
var startOffset = documentRange.TextRange.StartOffset + token.StartIndex + 1;
5156

src/ReSharper.Structured.Logging/Highlighting/TemplateFormatItemAndMatchingArgumentHighlighter.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ protected override void CollectHighlightings(
8080
}
8181

8282
var templateString = StringLiteralAltererUtil.TryCreateStringLiteralByExpression(templateArgument.Value)
83-
?.CompiledValue;
83+
?.Expression.GetUnquotedText();
8484
if (templateString == null)
8585
{
8686
return;
@@ -141,7 +141,7 @@ private static void HighlightByArgument(
141141
var property = namedProperties[argumentIndex];
142142
consumer.ConsumeHighlighting(
143143
HighlightingAttributeIds.USAGE_OF_ELEMENT_UNDER_CURSOR,
144-
templateArgument.GetDocumentRange().GetTokenDocumentRange(property));
144+
templateArgument.GetTokenDocumentRange(property));
145145
consumer.ConsumeHighlighting(
146146
HighlightingAttributeIds.USAGE_OF_ELEMENT_UNDER_CURSOR,
147147
selectedArgument.GetDocumentRange());
@@ -162,7 +162,7 @@ private static void HighlightByArgument(
162162

163163
consumer.ConsumeHighlighting(
164164
HighlightingAttributeIds.USAGE_OF_ELEMENT_UNDER_CURSOR,
165-
templateArgument.GetDocumentRange().GetTokenDocumentRange(property));
165+
templateArgument.GetTokenDocumentRange(property));
166166
}
167167

168168
consumer.ConsumeHighlighting(
@@ -194,7 +194,7 @@ private static void HighlightByNamedPlaceholder(
194194

195195
consumer.ConsumeHighlighting(
196196
HighlightingAttributeIds.USAGE_OF_ELEMENT_UNDER_CURSOR,
197-
templateArgument.GetDocumentRange().GetTokenDocumentRange(selectedToken));
197+
templateArgument.GetTokenDocumentRange(selectedToken));
198198
consumer.ConsumeHighlighting(
199199
HighlightingAttributeIds.USAGE_OF_ELEMENT_UNDER_CURSOR,
200200
arguments[argumentIndex].GetDocumentRange());
@@ -226,7 +226,7 @@ private static void HighlightByNamedPlaceholder(
226226

227227
consumer.ConsumeHighlighting(
228228
HighlightingAttributeIds.USAGE_OF_ELEMENT_UNDER_CURSOR,
229-
templateArgument.GetDocumentRange().GetTokenDocumentRange(property));
229+
templateArgument.GetTokenDocumentRange(property));
230230
}
231231

232232
var argumentIndex = templateArgument.IndexOf() + position + 1;

src/ReSharper.Structured.Logging/Highlighting/TemplateFormatStringInexistingArgumentWarning.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
using ReSharper.Structured.Logging.Highlighting;
66

77
[assembly:
8-
RegisterConfigurableSeverity(TemplateFormatStringInexistingArgumentWarning.SeverityId, null, HighlightingGroupIds.CompilerWarnings,
9-
TemplateFormatStringInexistingArgumentWarning.Message, TemplateFormatStringInexistingArgumentWarning.Message,
8+
RegisterConfigurableSeverity(TemplateFormatStringUnexistingArgumentWarning.SeverityId, null, HighlightingGroupIds.CompilerWarnings,
9+
TemplateFormatStringUnexistingArgumentWarning.Message, TemplateFormatStringUnexistingArgumentWarning.Message,
1010
Severity.WARNING)]
1111

1212
namespace ReSharper.Structured.Logging.Highlighting
@@ -16,15 +16,15 @@ namespace ReSharper.Structured.Logging.Highlighting
1616
CSharpLanguage.Name,
1717
OverlapResolve = OverlapResolveKind.WARNING,
1818
ToolTipFormatString = Message)]
19-
public class TemplateFormatStringInexistingArgumentWarning : IHighlighting
19+
public class TemplateFormatStringUnexistingArgumentWarning : IHighlighting
2020
{
2121
public const string SeverityId = "TemplateFormatStringProblem";
2222

2323
public const string Message = "Non-existing argument in message template";
2424

2525
private readonly DocumentRange _documentRange;
2626

27-
public TemplateFormatStringInexistingArgumentWarning(DocumentRange documentRange)
27+
public TemplateFormatStringUnexistingArgumentWarning(DocumentRange documentRange)
2828
{
2929
_documentRange = documentRange;
3030
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Serilog;
2+
3+
namespace ConsoleApp
4+
{
5+
public static class Program
6+
{
7+
public static void Main()
8+
{
9+
Log.Logger.Information("\r\n{MyProperty} \r\n {OtherProperty}", 1, 2);
10+
}
11+
}
12+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Serilog;
2+
3+
namespace ConsoleApp
4+
{
5+
public static class Program
6+
{
7+
public static void Main()
8+
{
9+
Log.Logger.Information("|\r|(0)|\n|(1)|{MyProperty}|(2) |\r|(3)|\n|(4) |{OtherProperty}|(5)", 1, 2);
10+
}
11+
}
12+
}
13+
14+
---------------------------------------------------------
15+
(0): ReSharper String Escape Character 1:
16+
(1): ReSharper String Escape Character 2:
17+
(2): ReSharper Format String Item:
18+
(3): ReSharper String Escape Character 1:
19+
(4): ReSharper String Escape Character 2:
20+
(5): ReSharper Format String Item:

test/src/Analyzer/MessageTemplateTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ protected override bool HighlightingPredicate(
2121
IContextBoundSettingsStore settingsStore)
2222
{
2323
return highlighting is TemplateFormatStringArgumentIsNotUsedWarning
24-
|| highlighting is TemplateFormatStringInexistingArgumentWarning
24+
|| highlighting is TemplateFormatStringUnexistingArgumentWarning
2525
|| highlighting is StringEscapeCharacterHighlighting
2626
|| highlighting is DuplicateTemplatePropertyWarning
2727
|| highlighting is ExceptionPassedAsTemplateArgumentWarning;

0 commit comments

Comments
 (0)