Skip to content
This repository has been archived by the owner on Feb 5, 2025. It is now read-only.

Enhanced the text cursor #363

Merged
merged 14 commits into from
Nov 19, 2024
Prev Previous commit
Next Next commit
added diagnostics
  • Loading branch information
victor-pogor committed Nov 19, 2024
commit 745f9c97070ff89a76542484b311675ceb783aad
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ csharp_preserve_single_line_statements = true
dotnet_diagnostic.IDE0060.severity = warning

# Nullable are treated as errors
dotnet_diagnostic.CS8618.severity = error
dotnet_diagnostic.CS8625.severity = error

# SA1500: Braces for multi-line statements should not share line
Expand Down
4 changes: 3 additions & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<Project>
<ItemGroup>
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.1" />
<PackageVersion Include="Microsoft.Extensions.Localization" Version="8.0.7" />
<PackageVersion Include="Microsoft.Extensions.Localization.Abstractions" Version="8.0.7" />
<PackageVersion Include="Microsoft.Extensions.ObjectPool" Version="8.0.7" />
</ItemGroup>
<!-- Analyzers -->
Expand All @@ -9,7 +11,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageVersion>
<PackageVersion Include="SonarAnalyzer.CSharp" Version="9.29.0.95321">
<PackageVersion Include="SonarAnalyzer.CSharp" Version="9.30.0.95878">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageVersion>
Expand Down
11 changes: 11 additions & 0 deletions src/OffDotNet.CodeAnalysis.Pdf/Configurations/DiagnosticOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// <copyright file="DiagnosticOptions.cs" company="Sunt Programator">
// Copyright (c) Sunt Programator. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// </copyright>

namespace OffDotNet.CodeAnalysis.Pdf.Configurations;

public sealed record DiagnosticOptions
{
public required string HelpLink { get; init; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// <copyright file="RootConfigurations.cs" company="Sunt Programator">
// Copyright (c) Sunt Programator. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// </copyright>

namespace OffDotNet.CodeAnalysis.Pdf.Configurations;

public sealed record RootConfigurations
{
public const string SectionName = "OffDotNet";

public required DiagnosticOptions Diagnostic { get; init; }
}
6 changes: 6 additions & 0 deletions src/OffDotNet.CodeAnalysis.Pdf/Dependencies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@

namespace OffDotNet.CodeAnalysis.Pdf;

using Configurations;
using Diagnostics;
using Microsoft.Extensions.DependencyInjection;
using OffDotNet.CodeAnalysis.Diagnostics;
using OffDotNet.CodeAnalysis.Lexer;

/// <summary>
Expand All @@ -20,8 +23,11 @@ public static class Dependencies
/// <returns>The same service collection so that multiple calls can be chained.</returns>
public static IServiceCollection AddPdfCodeAnalysis(this IServiceCollection services)
{
services.AddOptions<RootConfigurations>(RootConfigurations.SectionName).ValidateOnStart();

services.AddCoreCodeAnalysis();
services.AddSingleton<ILexer, Lexer.Lexer>();
services.AddSingleton<IMessageProvider, MessageProvider>();
return services;
}
}
11 changes: 11 additions & 0 deletions src/OffDotNet.CodeAnalysis.Pdf/Diagnostics/DiagnosticCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// <copyright file="DiagnosticCode.cs" company="Sunt Programator">
// Copyright (c) Sunt Programator. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// </copyright>

namespace OffDotNet.CodeAnalysis.Pdf.Diagnostics;

public enum DiagnosticCode
{
Unknown = 0,
}
43 changes: 43 additions & 0 deletions src/OffDotNet.CodeAnalysis.Pdf/Diagnostics/MessageProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// <copyright file="MessageProvider.cs" company="Sunt Programator">
// Copyright (c) Sunt Programator. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// </copyright>

namespace OffDotNet.CodeAnalysis.Pdf.Diagnostics;

using Configurations;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Options;
using OffDotNet.CodeAnalysis.Diagnostics;

internal sealed class MessageProvider : AbstractMessageProvider
{
private const string TitleSuffix = "_Title";
private const string DescriptionSuffix = "_Description";

private readonly IStringLocalizer<MessageProvider> _localizer;
private readonly DiagnosticOptions _options;

public MessageProvider(IStringLocalizer<MessageProvider> localizer, IOptions<DiagnosticOptions> options)
{
_localizer = localizer;
_options = options.Value ?? throw new ArgumentNullException(nameof(options));
}

public override string LanguagePrefix => "PDF";

public override LocalizedString GetTitle(ushort code) => _localizer[$"{(DiagnosticCode)code}{TitleSuffix}"];

public override LocalizedString GetDescription(ushort code) => _localizer[$"{(DiagnosticCode)code}{DescriptionSuffix}"];

public override string GetHelpLink(ushort code) => string.Format(_options.HelpLink, GetIdForDiagnosticCode(code));

public override byte GetSeverity(ushort code)
{
switch (code)
{
default:
return (byte)DiagnosticSeverity.Hidden;
}
}
}
48 changes: 45 additions & 3 deletions src/OffDotNet.CodeAnalysis.Pdf/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
},
"SonarAnalyzer.CSharp": {
"type": "Direct",
"requested": "[9.29.0.95321, )",
"resolved": "9.29.0.95321",
"contentHash": "dAvGL7gavcUYk83p1StVzvMFcbwN4hr08hjthsRr/wR/uPFgjd7LUn7QGN/1iH92aA7P72x06RRNtdUAqmVHdw=="
"requested": "[9.30.0.95878, )",
"resolved": "9.30.0.95878",
"contentHash": "P0DylTJphECGE9HD6yho7rb6qs2WTTW36QUu5ezGRJeZpq8ItlRLmzUpxNHVOaudJywcnKO1DdRyZSaU7LXOcA=="
},
"StyleCop.Analyzers": {
"type": "Direct",
Expand All @@ -35,6 +35,28 @@
"StyleCop.Analyzers.Unstable": "1.2.0.556"
}
},
"Microsoft.Extensions.Logging.Abstractions": {
"type": "Transitive",
"resolved": "8.0.1",
"contentHash": "RIFgaqoaINxkM2KTOw72dmilDmTrYA0ns2KW4lDz4gZ2+o6IQ894CzmdL3StM2oh7QQq44nCWiqKqc4qUI9Jmg==",
"dependencies": {
"Microsoft.Extensions.DependencyInjection.Abstractions": "8.0.1"
}
},
"Microsoft.Extensions.Options": {
"type": "Transitive",
"resolved": "8.0.2",
"contentHash": "dWGKvhFybsaZpGmzkGCbNNwBD1rVlWzrZKANLW/CcbFJpCEceMCGzT7zZwHOGBCbwM0SzBuceMj5HN1LKV1QqA==",
"dependencies": {
"Microsoft.Extensions.DependencyInjection.Abstractions": "8.0.0",
"Microsoft.Extensions.Primitives": "8.0.0"
}
},
"Microsoft.Extensions.Primitives": {
"type": "Transitive",
"resolved": "8.0.0",
"contentHash": "bXJEZrW9ny8vjMF1JV253WeLhpEVzFo1lyaZu1vQ4ZxWUlVvknZ/+ftFgVheLubb4eZPSwwxBeqS1JkCOjxd8g=="
},
"StyleCop.Analyzers.Unstable": {
"type": "Transitive",
"resolved": "1.2.0.556",
Expand All @@ -44,8 +66,28 @@
"type": "Project",
"dependencies": {
"Microsoft.Extensions.DependencyInjection.Abstractions": "[8.0.1, )",
"Microsoft.Extensions.Localization": "[8.0.7, )",
"Microsoft.Extensions.Localization.Abstractions": "[8.0.7, )",
"Microsoft.Extensions.ObjectPool": "[8.0.7, )"
}
},
"Microsoft.Extensions.Localization": {
"type": "CentralTransitive",
"requested": "[8.0.7, )",
"resolved": "8.0.7",
"contentHash": "kdI24IfC1Jk3zfhWvYmuzvkzuKp+5Shjmfq0vk6EKBB+Udt4Gu1SkVSkMWJkYZFag+ndd+sTroUKDC/sud/DYQ==",
"dependencies": {
"Microsoft.Extensions.DependencyInjection.Abstractions": "8.0.1",
"Microsoft.Extensions.Localization.Abstractions": "8.0.7",
"Microsoft.Extensions.Logging.Abstractions": "8.0.1",
"Microsoft.Extensions.Options": "8.0.2"
}
},
"Microsoft.Extensions.Localization.Abstractions": {
"type": "CentralTransitive",
"requested": "[8.0.7, )",
"resolved": "8.0.7",
"contentHash": "uCsxqBBJEsSoV52tX/v+bxxN0e1O8WYIRR7tmcEmVVkRSX+0k4ZJcaL79ddus+0HIm3ssjV/dJk56XufDrGDqg=="
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/OffDotNet.CodeAnalysis/Dependencies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace OffDotNet.CodeAnalysis;

using Lexer;
using Microsoft.Extensions.DependencyInjection;
using OffDotNet.CodeAnalysis.Diagnostics;

/// <summary>
/// Provides extension methods for registering code analysis services.
Expand All @@ -20,7 +21,9 @@ public static class Dependencies
/// <returns>The same service collection so that multiple calls can be chained.</returns>
public static IServiceCollection AddCoreCodeAnalysis(this IServiceCollection services)
{
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddSingleton<ICursorFactory, CursorFactory>();
services.AddSingleton<IMessageProvider, AbstractMessageProvider>();
return services;
}
}
30 changes: 30 additions & 0 deletions src/OffDotNet.CodeAnalysis/Diagnostics/AbstractMessageProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// <copyright file="AbstractMessageProvider.cs" company="Sunt Programator">
// Copyright (c) Sunt Programator. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// </copyright>

namespace OffDotNet.CodeAnalysis.Diagnostics;

using System.Collections.Concurrent;
using Microsoft.Extensions.Localization;
using DiagnosticCacheTuple = (string LanguagePrefix, ushort Code);

internal abstract class AbstractMessageProvider : IMessageProvider
{
private static readonly ConcurrentDictionary<DiagnosticCacheTuple, string> s_cache = new();

public abstract string LanguagePrefix { get; }

public abstract LocalizedString GetTitle(ushort code);

public abstract LocalizedString GetDescription(ushort code);

public abstract string GetHelpLink(ushort code);

public abstract byte GetSeverity(ushort code);

public string GetIdForDiagnosticCode(ushort diagnosticCode)
{
return s_cache.GetOrAdd((LanguagePrefix, diagnosticCode), key => $"{key.LanguagePrefix}{key.Code:0000}");
}
}
44 changes: 44 additions & 0 deletions src/OffDotNet.CodeAnalysis/Diagnostics/DiagnosticDescriptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// <copyright file="DiagnosticDescriptor.cs" company="Sunt Programator">
// Copyright (c) Sunt Programator. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// </copyright>

namespace OffDotNet.CodeAnalysis.Diagnostics;

using System.Collections.Immutable;
using Microsoft.Extensions.Localization;

public sealed record DiagnosticDescriptor
{
internal DiagnosticDescriptor()
{
}

private static ImmutableDictionary<ushort, DiagnosticDescriptor> s_errorCodeToDescriptorMap = ImmutableDictionary<ushort, DiagnosticDescriptor>.Empty;

public required string Id { get; init; }

public required LocalizedString Title { get; init; }

public required LocalizedString Description { get; init; }

public required string HelpLink { get; init; }

public DiagnosticSeverity DefaultSeverity { get; init; }

internal static DiagnosticDescriptor CreateDescriptor(ushort diagnosticCode, AbstractMessageProvider messageProvider)
{
return ImmutableInterlocked.GetOrAdd(
location: ref s_errorCodeToDescriptorMap,
key: diagnosticCode,
valueFactory: static (code, messageProvider) => new DiagnosticDescriptor
{
Id = messageProvider.GetIdForDiagnosticCode(code),
Title = messageProvider.GetTitle(code),
Description = messageProvider.GetDescription(code),
HelpLink = messageProvider.GetHelpLink(code),
DefaultSeverity = (DiagnosticSeverity)messageProvider.GetSeverity(code),
},
factoryArgument: messageProvider);
}
}
14 changes: 14 additions & 0 deletions src/OffDotNet.CodeAnalysis/Diagnostics/DiagnosticSeverity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// <copyright file="DiagnosticSeverity.cs" company="Sunt Programator">
// Copyright (c) Sunt Programator. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// </copyright>

namespace OffDotNet.CodeAnalysis.Diagnostics;

public enum DiagnosticSeverity
{
Hidden = 0,
Info = 1,
Warning = 2,
Error = 3,
}
10 changes: 10 additions & 0 deletions src/OffDotNet.CodeAnalysis/Diagnostics/IMessageProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// <copyright file="IMessageProvider.cs" company="Sunt Programator">
// Copyright (c) Sunt Programator. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// </copyright>

namespace OffDotNet.CodeAnalysis.Diagnostics;

internal interface IMessageProvider
{
}
2 changes: 2 additions & 0 deletions src/OffDotNet.CodeAnalysis/Lexer/CursorFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

namespace OffDotNet.CodeAnalysis.Lexer;

using Microsoft.Extensions.Localization;

/// <summary>
/// Factory class for creating instances of <see cref="ITextCursor"/>.
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions src/OffDotNet.CodeAnalysis/OffDotNet.CodeAnalysis.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" />
<PackageReference Include="Microsoft.Extensions.Localization" />
<PackageReference Include="Microsoft.Extensions.Localization.Abstractions" />
</ItemGroup>
<ItemGroup>
<InternalsVisibleTo Include="OffDotNet.CodeAnalysis.Pdf"/>
Expand Down
Loading