diff --git a/Sources/Kysect.Configuin.CodeStyleDoc/CodeStyleGenerator.cs b/Sources/Kysect.Configuin.CodeStyleDoc/CodeStyleGenerator.cs index 3082481..8ff8161 100644 --- a/Sources/Kysect.Configuin.CodeStyleDoc/CodeStyleGenerator.cs +++ b/Sources/Kysect.Configuin.CodeStyleDoc/CodeStyleGenerator.cs @@ -2,8 +2,8 @@ using Kysect.CommonLib.Collections.Extensions; using Kysect.Configuin.CodeStyleDoc.Models; using Kysect.Configuin.Common; -using Kysect.Configuin.EditorConfig.DocumentModel; -using Kysect.Configuin.EditorConfig.DocumentModel.Nodes; +using Kysect.Configuin.DotnetConfig.Syntax; +using Kysect.Configuin.DotnetConfig.Syntax.Nodes; using Kysect.Configuin.RoslynModels; using Microsoft.Extensions.Logging; using System.Collections.Immutable; @@ -19,34 +19,34 @@ public CodeStyleGenerator(ILogger logger) _logger = logger; } - public CodeStyle Generate(EditorConfigDocument editorConfigDocument, RoslynRules roslynRules) + public CodeStyle Generate(DotnetConfigDocument dotnetConfigDocument, RoslynRules roslynRules) { - editorConfigDocument.ThrowIfNull(); + dotnetConfigDocument.ThrowIfNull(); roslynRules.ThrowIfNull(); _logger.LogInformation("Start code style generating."); IReadOnlyCollection roslynRuleOptions = roslynRules.GetOptions(); - IReadOnlyCollection notProcessedSettings = editorConfigDocument.DescendantNodes(); + IReadOnlyCollection notProcessedSettings = dotnetConfigDocument.DescendantNodes(); _logger.LogInformation("Try parse {count} settings", notProcessedSettings.Count); notProcessedSettings = notProcessedSettings.Where(IsSupported).ToImmutableList(); IReadOnlyCollection optionConfigurations = notProcessedSettings - .OfType() + .OfType() .Select(o => ParseOptionSettings(o, roslynRuleOptions)) .ToList(); _logger.LogInformation("Parsed {count} option configurations", optionConfigurations.Count); - notProcessedSettings = notProcessedSettings.Where(r => r is not EditorConfigRuleOptionNode).ToImmutableList(); + notProcessedSettings = notProcessedSettings.Where(r => r is not DotnetConfigRuleOptionNode).ToImmutableList(); IReadOnlyCollection ruleConfiguration = notProcessedSettings - .OfType() + .OfType() .Select(severitySetting => ParseRuleSettings(severitySetting, optionConfigurations, roslynRules)) .ToList(); _logger.LogInformation("Parsed {count} rule severity", ruleConfiguration.Count); - notProcessedSettings = notProcessedSettings.Where(r => r is not EditorConfigRuleSeverityNode).ToImmutableList(); + notProcessedSettings = notProcessedSettings.Where(r => r is not DotnetConfigRuleSeverityNode).ToImmutableList(); if (notProcessedSettings.Any()) { @@ -61,15 +61,15 @@ public CodeStyle Generate(EditorConfigDocument editorConfigDocument, RoslynRules return new CodeStyle(ruleConfiguration); } - private bool IsSupported(IEditorConfigNode setting) + private bool IsSupported(IDotnetConfigSyntaxNode setting) { - if (setting is not IEditorConfigPropertyNode) + if (setting is not IDotnetConfigPropertySyntaxNode) { return false; } // TODO: #35 support parsing for this rule - if (setting is EditorConfigRuleSeverityNode severityEditorConfigRule + if (setting is DotnetConfigRuleSeverityNode severityEditorConfigRule && severityEditorConfigRule.RuleId.Equals(RoslynRuleId.Parse("IDE1006"))) { _logger.LogWarning("Rule IDE1006 is not supported and will be skipped."); @@ -77,14 +77,14 @@ private bool IsSupported(IEditorConfigNode setting) } // TODO: #35 Probably, most of this rules related to IDE1006 - if (setting is EditorConfigRuleCompositeOptionNode compositeSetting) + if (setting is DotnetConfigRuleCompositeOptionNode compositeSetting) { _logger.LogWarning("{setting} is not supported and will be skipped.", compositeSetting.ToFullString()); return false; } // TODO: Maybe we need to support it in some way - if (setting is EditorConfigGeneralOptionNode generalSetting) + if (setting is DotnetConfigGeneralOptionNode generalSetting) { _logger.LogWarning("{option} is not supported and will be skipped.", generalSetting.ToFullString()); return false; @@ -93,42 +93,42 @@ private bool IsSupported(IEditorConfigNode setting) return true; } - private CodeStyleRoslynOptionConfiguration ParseOptionSettings(EditorConfigRuleOptionNode optionEditorConfigSetting, IReadOnlyCollection styleRuleOptions) + private CodeStyleRoslynOptionConfiguration ParseOptionSettings(DotnetConfigRuleOptionNode optionDotnetConfigSetting, IReadOnlyCollection styleRuleOptions) { - RoslynStyleRuleOption? roslynStyleRuleOption = styleRuleOptions.SingleOrDefault(o => o.Name == optionEditorConfigSetting.Key); + RoslynStyleRuleOption? roslynStyleRuleOption = styleRuleOptions.SingleOrDefault(o => o.Name == optionDotnetConfigSetting.Key); if (roslynStyleRuleOption is null) - throw new ConfiguinException($"Option {optionEditorConfigSetting.Key} was not found in documentation"); + throw new ConfiguinException($"Option {optionDotnetConfigSetting.Key} was not found in documentation"); - return new CodeStyleRoslynOptionConfiguration(roslynStyleRuleOption, optionEditorConfigSetting.Value); + return new CodeStyleRoslynOptionConfiguration(roslynStyleRuleOption, optionDotnetConfigSetting.Value); } private ICodeStyleElement ParseRuleSettings( - EditorConfigRuleSeverityNode severityEditorConfigSetting, + DotnetConfigRuleSeverityNode severityDotnetConfigSetting, IReadOnlyCollection optionConfigurations, RoslynRules roslynRules) { - RoslynStyleRuleGroup? ruleGroup = roslynRules.StyleRuleGroups.SingleOrDefault(g => g.Rules.Any(r => r.RuleId.Equals(severityEditorConfigSetting.RuleId))); + RoslynStyleRuleGroup? ruleGroup = roslynRules.StyleRuleGroups.SingleOrDefault(g => g.Rules.Any(r => r.RuleId.Equals(severityDotnetConfigSetting.RuleId))); if (ruleGroup is not null) { - RoslynStyleRule rule = ruleGroup.Rules.Single(r => r.RuleId.Equals(severityEditorConfigSetting.RuleId)); + RoslynStyleRule rule = ruleGroup.Rules.Single(r => r.RuleId.Equals(severityDotnetConfigSetting.RuleId)); var options = ruleGroup .Options .Select(o => FindOptionConfiguration(optionConfigurations, o.Name)) .WhereNotNull() .ToList(); - return new CodeStyleRoslynStyleRuleConfiguration(rule, severityEditorConfigSetting.ParseSeverity(), options, ruleGroup.Overview, ruleGroup.Example); + return new CodeStyleRoslynStyleRuleConfiguration(rule, severityDotnetConfigSetting.ParseSeverity(), options, ruleGroup.Overview, ruleGroup.Example); } - RoslynQualityRule? roslynQualityRule = roslynRules.QualityRules.FirstOrDefault(q => q.RuleId.Equals(severityEditorConfigSetting.RuleId)); + RoslynQualityRule? roslynQualityRule = roslynRules.QualityRules.FirstOrDefault(q => q.RuleId.Equals(severityDotnetConfigSetting.RuleId)); if (roslynQualityRule is not null) { - return new CodeStyleRoslynQualityRuleConfiguration(roslynQualityRule, severityEditorConfigSetting.ParseSeverity()); + return new CodeStyleRoslynQualityRuleConfiguration(roslynQualityRule, severityDotnetConfigSetting.ParseSeverity()); } - throw new ConfiguinException($"Rule with id {severityEditorConfigSetting.RuleId} was not found"); + throw new ConfiguinException($"Rule with id {severityDotnetConfigSetting.RuleId} was not found"); } private CodeStyleRoslynOptionConfiguration? FindOptionConfiguration( diff --git a/Sources/Kysect.Configuin.CodeStyleDoc/ICodeStyleGenerator.cs b/Sources/Kysect.Configuin.CodeStyleDoc/ICodeStyleGenerator.cs index fab98aa..de85b7e 100644 --- a/Sources/Kysect.Configuin.CodeStyleDoc/ICodeStyleGenerator.cs +++ b/Sources/Kysect.Configuin.CodeStyleDoc/ICodeStyleGenerator.cs @@ -1,10 +1,10 @@ using Kysect.Configuin.CodeStyleDoc.Models; -using Kysect.Configuin.EditorConfig.DocumentModel.Nodes; +using Kysect.Configuin.DotnetConfig.Syntax.Nodes; using Kysect.Configuin.RoslynModels; namespace Kysect.Configuin.CodeStyleDoc; public interface ICodeStyleGenerator { - CodeStyle Generate(EditorConfigDocument editorConfigDocument, RoslynRules roslynRules); + CodeStyle Generate(DotnetConfigDocument dotnetConfigDocument, RoslynRules roslynRules); } \ No newline at end of file diff --git a/Sources/Kysect.Configuin.CodeStyleDoc/Kysect.Configuin.CodeStyleDoc.csproj b/Sources/Kysect.Configuin.CodeStyleDoc/Kysect.Configuin.CodeStyleDoc.csproj index 590644d..8c6696e 100644 --- a/Sources/Kysect.Configuin.CodeStyleDoc/Kysect.Configuin.CodeStyleDoc.csproj +++ b/Sources/Kysect.Configuin.CodeStyleDoc/Kysect.Configuin.CodeStyleDoc.csproj @@ -1,6 +1,6 @@ - + diff --git a/Sources/Kysect.Configuin.ConfigurationRoot/DependencyBuilder.cs b/Sources/Kysect.Configuin.ConfigurationRoot/DependencyBuilder.cs index a6a9681..1a3b99b 100644 --- a/Sources/Kysect.Configuin.ConfigurationRoot/DependencyBuilder.cs +++ b/Sources/Kysect.Configuin.ConfigurationRoot/DependencyBuilder.cs @@ -1,12 +1,12 @@ using Kysect.Configuin.CodeStyleDoc; using Kysect.Configuin.CodeStyleDoc.Markdown; +using Kysect.Configuin.DotnetConfig.Formatter; +using Kysect.Configuin.DotnetConfig.Syntax; +using Kysect.Configuin.DotnetConfig.Template; using Kysect.Configuin.DotnetFormatIntegration; using Kysect.Configuin.DotnetFormatIntegration.Abstractions; using Kysect.Configuin.DotnetFormatIntegration.Cli; using Kysect.Configuin.DotnetFormatIntegration.FileSystem; -using Kysect.Configuin.EditorConfig.DocumentModel; -using Kysect.Configuin.EditorConfig.Formatter; -using Kysect.Configuin.EditorConfig.Template; using Kysect.Configuin.Learn; using Kysect.Configuin.Learn.Abstraction; using Kysect.Configuin.Markdown.TextExtractor; @@ -42,9 +42,9 @@ public static IServiceCollection InitializeServiceProvider() serviceCollection.AddSingleton(); serviceCollection.AddSingleton(); serviceCollection.AddSingleton(); - serviceCollection.AddSingleton(); - serviceCollection.AddSingleton(); - serviceCollection.AddSingleton(); + serviceCollection.AddSingleton(); + serviceCollection.AddSingleton(); + serviceCollection.AddSingleton(); return serviceCollection; } diff --git a/Sources/Kysect.Configuin.ConfigurationRoot/Kysect.Configuin.ConfigurationRoot.csproj b/Sources/Kysect.Configuin.ConfigurationRoot/Kysect.Configuin.ConfigurationRoot.csproj index 613a2c2..6b1847b 100644 --- a/Sources/Kysect.Configuin.ConfigurationRoot/Kysect.Configuin.ConfigurationRoot.csproj +++ b/Sources/Kysect.Configuin.ConfigurationRoot/Kysect.Configuin.ConfigurationRoot.csproj @@ -16,7 +16,6 @@ - diff --git a/Sources/Kysect.Configuin.Console/CommandAppInitializer.cs b/Sources/Kysect.Configuin.Console/CommandAppInitializer.cs index b4da229..b63e724 100644 --- a/Sources/Kysect.Configuin.Console/CommandAppInitializer.cs +++ b/Sources/Kysect.Configuin.Console/CommandAppInitializer.cs @@ -14,10 +14,10 @@ public static CommandApp Initialize(IServiceCollection services) app.Configure(config => { config.AddCommand("generate-styledoc"); - config.AddCommand("preview"); - config.AddCommand("analyze"); - config.AddCommand("template"); - config.AddCommand("format"); + config.AddCommand("preview"); + config.AddCommand("analyze"); + config.AddCommand("template"); + config.AddCommand("format"); config.AddCommand("generate-roslyn-documentation"); }); diff --git a/Sources/Kysect.Configuin.Console/Commands/AnalyzeDotnetConfigCommand.cs b/Sources/Kysect.Configuin.Console/Commands/AnalyzeDotnetConfigCommand.cs new file mode 100644 index 0000000..b94592d --- /dev/null +++ b/Sources/Kysect.Configuin.Console/Commands/AnalyzeDotnetConfigCommand.cs @@ -0,0 +1,55 @@ +using Kysect.CommonLib.BaseTypes.Extensions; +using Kysect.Configuin.DotnetConfig.Analyzing; +using Kysect.Configuin.DotnetConfig.Syntax; +using Kysect.Configuin.DotnetConfig.Syntax.Nodes; +using Kysect.Configuin.Learn.Abstraction; +using Kysect.Configuin.RoslynModels; +using Microsoft.Extensions.Logging; +using Spectre.Console.Cli; +using System.ComponentModel; +using System.Diagnostics.CodeAnalysis; + +namespace Kysect.Configuin.Console.Commands; + +internal sealed class AnalyzeDotnetConfigCommand( + IRoslynRuleDocumentationParser roslynRuleDocumentationParser, + DotnetConfigDocumentParser dotnetConfigDocumentParser, + ILogger logger + ) : Command +{ + public sealed class Settings : CommandSettings + { + [Description("Path to dotnet config file.")] + [CommandArgument(0, "[dotnet config path]")] + public string? DotnetConfigPath { get; init; } + + [Description("Path to cloned MS Learn repository.")] + [CommandOption("-d|--documentation")] + public string? MsLearnRepositoryPath { get; init; } + } + + public override int Execute([NotNull] CommandContext context, [NotNull] Settings settings) + { + settings.DotnetConfigPath.ThrowIfNull(); + + DotnetConfigDocumentAnalyzer dotnetConfigDocumentAnalyzer = new DotnetConfigDocumentAnalyzer(); + IDotnetConfigAnalyzeReporter reporter = new DotnetConfigAnalyzeLogReporter(logger); + + RoslynRules roslynRules = settings.MsLearnRepositoryPath is null + ? RoslynRuleDocumentationCache.ReadFromCache() + : roslynRuleDocumentationParser.Parse(settings.MsLearnRepositoryPath); + + string editorConfigContent = File.ReadAllText(settings.DotnetConfigPath); + DotnetConfigDocument dotnetConfigDocument = dotnetConfigDocumentParser.Parse(editorConfigContent); + + DotnetConfigMissedConfiguration dotnetConfigMissedConfiguration = dotnetConfigDocumentAnalyzer.GetMissedConfigurations(dotnetConfigDocument, roslynRules); + IReadOnlyCollection incorrectOptionValues = dotnetConfigDocumentAnalyzer.GetIncorrectOptionValues(dotnetConfigDocument, roslynRules); + IReadOnlyCollection incorrectOptionSeverity = dotnetConfigDocumentAnalyzer.GetIncorrectOptionSeverity(dotnetConfigDocument, roslynRules); + + reporter.ReportMissedConfigurations(dotnetConfigMissedConfiguration); + reporter.ReportIncorrectOptionValues(incorrectOptionValues); + reporter.ReportIncorrectOptionSeverity(incorrectOptionSeverity); + + return 0; + } +} \ No newline at end of file diff --git a/Sources/Kysect.Configuin.Console/Commands/AnalyzeEditorConfigCommand.cs b/Sources/Kysect.Configuin.Console/Commands/AnalyzeEditorConfigCommand.cs deleted file mode 100644 index 26e06cb..0000000 --- a/Sources/Kysect.Configuin.Console/Commands/AnalyzeEditorConfigCommand.cs +++ /dev/null @@ -1,55 +0,0 @@ -using Kysect.CommonLib.BaseTypes.Extensions; -using Kysect.Configuin.EditorConfig.Analyzing; -using Kysect.Configuin.EditorConfig.DocumentModel; -using Kysect.Configuin.EditorConfig.DocumentModel.Nodes; -using Kysect.Configuin.Learn.Abstraction; -using Kysect.Configuin.RoslynModels; -using Microsoft.Extensions.Logging; -using Spectre.Console.Cli; -using System.ComponentModel; -using System.Diagnostics.CodeAnalysis; - -namespace Kysect.Configuin.Console.Commands; - -internal sealed class AnalyzeEditorConfigCommand( - IRoslynRuleDocumentationParser roslynRuleDocumentationParser, - EditorConfigDocumentParser editorConfigDocumentParser, - ILogger logger - ) : Command -{ - public sealed class Settings : CommandSettings - { - [Description("Path to editorconfig.")] - [CommandArgument(0, "[editor config path]")] - public string? EditorConfigPath { get; init; } - - [Description("Path to cloned MS Learn repository.")] - [CommandOption("-d|--documentation")] - public string? MsLearnRepositoryPath { get; init; } - } - - public override int Execute([NotNull] CommandContext context, [NotNull] Settings settings) - { - settings.EditorConfigPath.ThrowIfNull(); - - EditorConfigAnalyzer editorConfigAnalyzer = new EditorConfigAnalyzer(); - IEditorConfigAnalyzeReporter reporter = new EditorConfigAnalyzeLogReporter(logger); - - RoslynRules roslynRules = settings.MsLearnRepositoryPath is null - ? RoslynRuleDocumentationCache.ReadFromCache() - : roslynRuleDocumentationParser.Parse(settings.MsLearnRepositoryPath); - - string editorConfigContent = File.ReadAllText(settings.EditorConfigPath); - EditorConfigDocument editorConfigDocument = editorConfigDocumentParser.Parse(editorConfigContent); - - EditorConfigMissedConfiguration editorConfigMissedConfiguration = editorConfigAnalyzer.GetMissedConfigurations(editorConfigDocument, roslynRules); - IReadOnlyCollection incorrectOptionValues = editorConfigAnalyzer.GetIncorrectOptionValues(editorConfigDocument, roslynRules); - IReadOnlyCollection incorrectOptionSeverity = editorConfigAnalyzer.GetIncorrectOptionSeverity(editorConfigDocument, roslynRules); - - reporter.ReportMissedConfigurations(editorConfigMissedConfiguration); - reporter.ReportIncorrectOptionValues(incorrectOptionValues); - reporter.ReportIncorrectOptionSeverity(incorrectOptionSeverity); - - return 0; - } -} \ No newline at end of file diff --git a/Sources/Kysect.Configuin.Console/Commands/FormatEditorconfigCommand.cs b/Sources/Kysect.Configuin.Console/Commands/FormatDotnetConfigCommand.cs similarity index 56% rename from Sources/Kysect.Configuin.Console/Commands/FormatEditorconfigCommand.cs rename to Sources/Kysect.Configuin.Console/Commands/FormatDotnetConfigCommand.cs index 7cb5021..69beae1 100644 --- a/Sources/Kysect.Configuin.Console/Commands/FormatEditorconfigCommand.cs +++ b/Sources/Kysect.Configuin.Console/Commands/FormatDotnetConfigCommand.cs @@ -1,7 +1,7 @@ using Kysect.CommonLib.BaseTypes.Extensions; -using Kysect.Configuin.EditorConfig.DocumentModel; -using Kysect.Configuin.EditorConfig.DocumentModel.Nodes; -using Kysect.Configuin.EditorConfig.Formatter; +using Kysect.Configuin.DotnetConfig.Formatter; +using Kysect.Configuin.DotnetConfig.Syntax; +using Kysect.Configuin.DotnetConfig.Syntax.Nodes; using Kysect.Configuin.Learn.Abstraction; using Kysect.Configuin.RoslynModels; using Spectre.Console.Cli; @@ -9,17 +9,17 @@ namespace Kysect.Configuin.Console.Commands; -internal sealed class FormatEditorconfigCommand( +internal sealed class FormatDotnetConfigCommand( IRoslynRuleDocumentationParser roslynRuleDocumentationParser, - EditorConfigDocumentParser editorConfigDocumentParser, - EditorConfigFormatter editorConfigFormatter - ) : Command + DotnetConfigDocumentParser dotnetConfigDocumentParser, + DotnetConfigDocumentFormatter dotnetConfigDocumentFormatter + ) : Command { public sealed class Settings : CommandSettings { - [Description("Path to editorconfig.")] - [CommandArgument(0, "[editor config path]")] - public string? EditorConfigPath { get; init; } + [Description("Path to dotnet config file.")] + [CommandArgument(0, "[dotnet config path]")] + public string? DotnetConfigPath { get; init; } [Description("Path to cloned MS Learn repository.")] [CommandOption("-d|--documentation")] @@ -33,16 +33,16 @@ public sealed class Settings : CommandSettings public override int Execute(CommandContext context, Settings settings) { - settings.EditorConfigPath.ThrowIfNull(); + settings.DotnetConfigPath.ThrowIfNull(); - string[] editorConfigContentLines = File.ReadAllLines(settings.EditorConfigPath); + string[] editorConfigContentLines = File.ReadAllLines(settings.DotnetConfigPath); RoslynRules roslynRules = settings.MsLearnRepositoryPath is null ? RoslynRuleDocumentationCache.ReadFromCache() : roslynRuleDocumentationParser.Parse(settings.MsLearnRepositoryPath); - EditorConfigDocument editorConfigDocument = editorConfigDocumentParser.Parse(editorConfigContentLines); - EditorConfigDocument formattedDocument = editorConfigFormatter.FormatAccordingToRuleDefinitions(editorConfigDocument, roslynRules, settings.GroupQualityRulesByCategory); - File.WriteAllText(settings.EditorConfigPath, formattedDocument.ToFullString()); + DotnetConfigDocument dotnetConfigDocument = dotnetConfigDocumentParser.Parse(editorConfigContentLines); + DotnetConfigDocument formattedDocument = dotnetConfigDocumentFormatter.FormatAccordingToRuleDefinitions(dotnetConfigDocument, roslynRules, settings.GroupQualityRulesByCategory); + File.WriteAllText(settings.DotnetConfigPath, formattedDocument.ToFullString()); return 0; } diff --git a/Sources/Kysect.Configuin.Console/Commands/GenerateCodeStyleDocCommand.cs b/Sources/Kysect.Configuin.Console/Commands/GenerateCodeStyleDocCommand.cs index 73b3515..beb8390 100644 --- a/Sources/Kysect.Configuin.Console/Commands/GenerateCodeStyleDocCommand.cs +++ b/Sources/Kysect.Configuin.Console/Commands/GenerateCodeStyleDocCommand.cs @@ -1,8 +1,8 @@ using Kysect.CommonLib.BaseTypes.Extensions; using Kysect.Configuin.CodeStyleDoc; using Kysect.Configuin.CodeStyleDoc.Models; -using Kysect.Configuin.EditorConfig.DocumentModel; -using Kysect.Configuin.EditorConfig.DocumentModel.Nodes; +using Kysect.Configuin.DotnetConfig.Syntax; +using Kysect.Configuin.DotnetConfig.Syntax.Nodes; using Kysect.Configuin.Learn.Abstraction; using Kysect.Configuin.RoslynModels; using Spectre.Console.Cli; @@ -15,14 +15,14 @@ internal sealed class GenerateCodeStyleDocCommand( IRoslynRuleDocumentationParser roslynRuleDocumentationParser, ICodeStyleGenerator codeStyleGenerator, ICodeStyleWriter codeStyleWriter, - EditorConfigDocumentParser documentParser + DotnetConfigDocumentParser documentParser ) : Command { public sealed class Settings : CommandSettings { - [Description("Path to editorconfig.")] - [CommandArgument(0, "[editor config path]")] - public string? EditorConfigPath { get; init; } + [Description("Path to dotnet config file.")] + [CommandArgument(0, "[dotnet-config-path]")] + public string? DotnetConfigPath { get; init; } [Description("Path to generated document.")] [CommandOption("-o|--output")] @@ -35,17 +35,17 @@ public sealed class Settings : CommandSettings public override int Execute([NotNull] CommandContext context, [NotNull] Settings settings) { - settings.EditorConfigPath.ThrowIfNull(); + settings.DotnetConfigPath.ThrowIfNull(); settings.OutputPath.ThrowIfNull(); - string editorConfigContent = File.ReadAllText(settings.EditorConfigPath); - EditorConfigDocument editorConfigDocument = documentParser.Parse(editorConfigContent); + string editorConfigContent = File.ReadAllText(settings.DotnetConfigPath); + DotnetConfigDocument dotnetConfigDocument = documentParser.Parse(editorConfigContent); RoslynRules roslynRules = settings.MsLearnRepositoryPath is null ? RoslynRuleDocumentationCache.ReadFromCache() : roslynRuleDocumentationParser.Parse(settings.MsLearnRepositoryPath); - CodeStyle codeStyle = codeStyleGenerator.Generate(editorConfigDocument, roslynRules); + CodeStyle codeStyle = codeStyleGenerator.Generate(dotnetConfigDocument, roslynRules); codeStyleWriter.Write(settings.OutputPath, codeStyle); return 0; diff --git a/Sources/Kysect.Configuin.Console/Commands/GenerateEditorConfigTemplateTemplate.cs b/Sources/Kysect.Configuin.Console/Commands/GenerateDotnetConfigTemplateCommand.cs similarity index 62% rename from Sources/Kysect.Configuin.Console/Commands/GenerateEditorConfigTemplateTemplate.cs rename to Sources/Kysect.Configuin.Console/Commands/GenerateDotnetConfigTemplateCommand.cs index cf45a0a..6226cbd 100644 --- a/Sources/Kysect.Configuin.Console/Commands/GenerateEditorConfigTemplateTemplate.cs +++ b/Sources/Kysect.Configuin.Console/Commands/GenerateDotnetConfigTemplateCommand.cs @@ -1,5 +1,5 @@ using Kysect.CommonLib.BaseTypes.Extensions; -using Kysect.Configuin.EditorConfig.Template; +using Kysect.Configuin.DotnetConfig.Template; using Kysect.Configuin.Learn.Abstraction; using Kysect.Configuin.RoslynModels; using Microsoft.Extensions.Logging; @@ -9,17 +9,17 @@ namespace Kysect.Configuin.Console.Commands; -public class GenerateEditorConfigTemplateTemplate( +public class GenerateDotnetConfigTemplateCommand( IRoslynRuleDocumentationParser roslynRuleDocumentationParser, - EditorConfigTemplateGenerator editorConfigTemplateGenerator, + DotnetConfigDocumentTemplateGenerator dotnetConfigDocumentTemplateGenerator, ILogger logger - ) : Command + ) : Command { public sealed class Settings : CommandSettings { - [Description("Path to editorconfig.")] - [CommandArgument(0, "[editor config path]")] - public string? EditorConfigPath { get; init; } + [Description("Path to dotnet config file.")] + [CommandArgument(0, "[dotnet config path]")] + public string? DotnetConfigPath { get; init; } [Description("Path to cloned MS Learn repository.")] [CommandOption("-d|--documentation")] @@ -28,16 +28,16 @@ public sealed class Settings : CommandSettings public override int Execute([NotNull] CommandContext context, [NotNull] Settings settings) { - settings.EditorConfigPath.ThrowIfNull(); + settings.DotnetConfigPath.ThrowIfNull(); RoslynRules roslynRules = settings.MsLearnRepositoryPath is null ? RoslynRuleDocumentationCache.ReadFromCache() : roslynRuleDocumentationParser.Parse(settings.MsLearnRepositoryPath); - string editorconfigContent = editorConfigTemplateGenerator.GenerateTemplate(roslynRules); + string editorconfigContent = dotnetConfigDocumentTemplateGenerator.GenerateTemplate(roslynRules); // TODO: move to interface? - logger.LogInformation("Writing .editorconfig template to {path}", settings.EditorConfigPath); - File.WriteAllText(settings.EditorConfigPath, editorconfigContent); + logger.LogInformation("Writing .editorconfig template to {path}", settings.DotnetConfigPath); + File.WriteAllText(settings.DotnetConfigPath, editorconfigContent); return 0; } } \ No newline at end of file diff --git a/Sources/Kysect.Configuin.Console/Commands/EditorConfigApplyPreviewCommand.cs b/Sources/Kysect.Configuin.Console/Commands/PreviewDotnetConfigChangesCommand.cs similarity index 53% rename from Sources/Kysect.Configuin.Console/Commands/EditorConfigApplyPreviewCommand.cs rename to Sources/Kysect.Configuin.Console/Commands/PreviewDotnetConfigChangesCommand.cs index 74aaa54..d374946 100644 --- a/Sources/Kysect.Configuin.Console/Commands/EditorConfigApplyPreviewCommand.cs +++ b/Sources/Kysect.Configuin.Console/Commands/PreviewDotnetConfigChangesCommand.cs @@ -4,32 +4,32 @@ namespace Kysect.Configuin.Console.Commands; -internal sealed class EditorConfigApplyPreviewCommand( +internal sealed class PreviewDotnetConfigChangesCommand( IDotnetFormatPreviewGenerator dotnetFormatPreviewGenerator - ) : Command + ) : Command { public sealed class Settings : CommandSettings { [CommandOption("-s|--solution")] public string SolutionPath { get; init; } = null!; // TODO: remove this argument, take solution directory path - [CommandOption("-t|--target")] - public string SourceEditorConfig { get; init; } = null!; - [CommandOption("-e|--editorconfig")] - public string NewEditorConfig { get; init; } = null!; + [CommandOption("--current")] + public string CurrentDotnetConfig { get; init; } = null!; + [CommandOption("--new")] + public string NewDotnetConfig { get; init; } = null!; } public override int Execute(CommandContext context, Settings settings) { settings.SolutionPath.ThrowIfNull(); - settings.SourceEditorConfig.ThrowIfNull(); - settings.NewEditorConfig.ThrowIfNull(); + settings.CurrentDotnetConfig.ThrowIfNull(); + settings.NewDotnetConfig.ThrowIfNull(); - dotnetFormatPreviewGenerator.GetEditorConfigWarningUpdates( + dotnetFormatPreviewGenerator.GetWarningsAfterChangingDotnetConfig( settings.SolutionPath, - settings.NewEditorConfig, - settings.SourceEditorConfig); + settings.NewDotnetConfig, + settings.CurrentDotnetConfig); return 0; } diff --git a/Sources/Kysect.Configuin.Console/Kysect.Configuin.Console.csproj b/Sources/Kysect.Configuin.Console/Kysect.Configuin.Console.csproj index 297636c..833c7b4 100644 --- a/Sources/Kysect.Configuin.Console/Kysect.Configuin.Console.csproj +++ b/Sources/Kysect.Configuin.Console/Kysect.Configuin.Console.csproj @@ -20,7 +20,6 @@ - diff --git a/Sources/Kysect.Configuin.EditorConfig/Analyzing/EditorConfigAnalyzeLogReporter.cs b/Sources/Kysect.Configuin.DotnetConfig/Analyzing/DotnetConfigAnalyzeLogReporter.cs similarity index 69% rename from Sources/Kysect.Configuin.EditorConfig/Analyzing/EditorConfigAnalyzeLogReporter.cs rename to Sources/Kysect.Configuin.DotnetConfig/Analyzing/DotnetConfigAnalyzeLogReporter.cs index 854ea71..f663a7b 100644 --- a/Sources/Kysect.Configuin.EditorConfig/Analyzing/EditorConfigAnalyzeLogReporter.cs +++ b/Sources/Kysect.Configuin.DotnetConfig/Analyzing/DotnetConfigAnalyzeLogReporter.cs @@ -4,51 +4,51 @@ using Kysect.Configuin.RoslynModels; using Microsoft.Extensions.Logging; -namespace Kysect.Configuin.EditorConfig.Analyzing; +namespace Kysect.Configuin.DotnetConfig.Analyzing; -public class EditorConfigAnalyzeLogReporter : IEditorConfigAnalyzeReporter +public class DotnetConfigAnalyzeLogReporter : IDotnetConfigAnalyzeReporter { private readonly ILogger _logger; - public EditorConfigAnalyzeLogReporter(ILogger logger) + public DotnetConfigAnalyzeLogReporter(ILogger logger) { _logger = logger; } - public void ReportMissedConfigurations(EditorConfigMissedConfiguration editorConfigMissedConfiguration) + public void ReportMissedConfigurations(DotnetConfigMissedConfiguration dotnetConfigMissedConfiguration) { - editorConfigMissedConfiguration.ThrowIfNull(); + dotnetConfigMissedConfiguration.ThrowIfNull(); - if (editorConfigMissedConfiguration.StyleRuleSeverity.Any()) + if (dotnetConfigMissedConfiguration.StyleRuleSeverity.Any()) { _logger.LogInformation("Missed style rules:"); - foreach (RoslynRuleId roslynRuleId in editorConfigMissedConfiguration.StyleRuleSeverity) + foreach (RoslynRuleId roslynRuleId in dotnetConfigMissedConfiguration.StyleRuleSeverity) _logger.LogTabInformation(1, roslynRuleId.ToString()); } - if (editorConfigMissedConfiguration.QualityRuleSeverity.Any()) + if (dotnetConfigMissedConfiguration.QualityRuleSeverity.Any()) { _logger.LogInformation("Missed quality rules:"); - foreach (RoslynRuleId roslynRuleId in editorConfigMissedConfiguration.QualityRuleSeverity) + foreach (RoslynRuleId roslynRuleId in dotnetConfigMissedConfiguration.QualityRuleSeverity) _logger.LogTabInformation(1, roslynRuleId.ToString()); } - if (editorConfigMissedConfiguration.StyleRuleOptions.Any()) + if (dotnetConfigMissedConfiguration.StyleRuleOptions.Any()) { _logger.LogInformation("Missed options:"); - foreach (string styleRuleOption in editorConfigMissedConfiguration.StyleRuleOptions) + foreach (string styleRuleOption in dotnetConfigMissedConfiguration.StyleRuleOptions) _logger.LogTabInformation(1, styleRuleOption); } } - public void ReportIncorrectOptionValues(IReadOnlyCollection incorrectOptionValues) + public void ReportIncorrectOptionValues(IReadOnlyCollection incorrectOptionValues) { ArgumentNullException.ThrowIfNull(incorrectOptionValues); if (incorrectOptionValues.Any()) _logger.LogInformation("Incorrect option value:"); - foreach (EditorConfigInvalidOptionValue editorConfigInvalidOptionValue in incorrectOptionValues) + foreach (DotnetConfigInvalidOptionValue editorConfigInvalidOptionValue in incorrectOptionValues) { string availableOptions = editorConfigInvalidOptionValue.AvailableOptions.ToSingleString(o => o.Value); _logger.LogTabInformation(1, $"Option {editorConfigInvalidOptionValue.Key} has value {editorConfigInvalidOptionValue.Value} but available values: [{availableOptions}]"); diff --git a/Sources/Kysect.Configuin.EditorConfig/Analyzing/EditorConfigAnalyzer.cs b/Sources/Kysect.Configuin.DotnetConfig/Analyzing/DotnetConfigDocumentAnalyzer.cs similarity index 62% rename from Sources/Kysect.Configuin.EditorConfig/Analyzing/EditorConfigAnalyzer.cs rename to Sources/Kysect.Configuin.DotnetConfig/Analyzing/DotnetConfigDocumentAnalyzer.cs index 8441d15..8700617 100644 --- a/Sources/Kysect.Configuin.EditorConfig/Analyzing/EditorConfigAnalyzer.cs +++ b/Sources/Kysect.Configuin.DotnetConfig/Analyzing/DotnetConfigDocumentAnalyzer.cs @@ -1,27 +1,27 @@ using Kysect.CommonLib.BaseTypes.Extensions; using Kysect.CommonLib.Collections.Extensions; -using Kysect.Configuin.EditorConfig.DocumentModel; -using Kysect.Configuin.EditorConfig.DocumentModel.Nodes; +using Kysect.Configuin.DotnetConfig.Syntax; +using Kysect.Configuin.DotnetConfig.Syntax.Nodes; using Kysect.Configuin.RoslynModels; -namespace Kysect.Configuin.EditorConfig.Analyzing; +namespace Kysect.Configuin.DotnetConfig.Analyzing; -public class EditorConfigAnalyzer +public class DotnetConfigDocumentAnalyzer { - public EditorConfigMissedConfiguration GetMissedConfigurations(EditorConfigDocument editorConfigDocument, RoslynRules roslynRules) + public DotnetConfigMissedConfiguration GetMissedConfigurations(DotnetConfigDocument dotnetConfigDocument, RoslynRules roslynRules) { - editorConfigDocument.ThrowIfNull(); + dotnetConfigDocument.ThrowIfNull(); roslynRules.ThrowIfNull(); - var selectedSeverity = editorConfigDocument + var selectedSeverity = dotnetConfigDocument .DescendantNodes() - .OfType() + .OfType() .Select(c => c.RuleId) .ToHashSet(); - var selectedOptions = editorConfigDocument + var selectedOptions = dotnetConfigDocument .DescendantNodes() - .OfType() + .OfType() .Select(c => c.Key) .ToHashSet(); @@ -45,19 +45,19 @@ public EditorConfigMissedConfiguration GetMissedConfigurations(EditorConfigDocum .Select(o => o.Name) .ToList(); - return new EditorConfigMissedConfiguration(missedStyleRules, missedQualityRules, missedOptions); + return new DotnetConfigMissedConfiguration(missedStyleRules, missedQualityRules, missedOptions); } - public IReadOnlyCollection GetIncorrectOptionValues(EditorConfigDocument editorConfigDocument, RoslynRules roslynRules) + public IReadOnlyCollection GetIncorrectOptionValues(DotnetConfigDocument dotnetConfigDocument, RoslynRules roslynRules) { - editorConfigDocument.ThrowIfNull(); + dotnetConfigDocument.ThrowIfNull(); roslynRules.ThrowIfNull(); - var result = new List(); + var result = new List(); var optionAvailableValues = roslynRules.GetOptions().ToDictionary(o => o.Name, o => o.Values); - foreach (var node in editorConfigDocument.DescendantNodes().OfType()) + foreach (var node in dotnetConfigDocument.DescendantNodes().OfType()) { string key = node.Key; string value = node.Value; @@ -65,15 +65,15 @@ public IReadOnlyCollection GetIncorrectOptionVal values = Array.Empty(); if (!values.Any(v => v.Value.Equals(value, StringComparison.InvariantCultureIgnoreCase))) - result.Add(new EditorConfigInvalidOptionValue(key, value, values)); + result.Add(new DotnetConfigInvalidOptionValue(key, value, values)); } return result; } - public IReadOnlyCollection GetIncorrectOptionSeverity(EditorConfigDocument editorConfigDocument, RoslynRules roslynRules) + public IReadOnlyCollection GetIncorrectOptionSeverity(DotnetConfigDocument dotnetConfigDocument, RoslynRules roslynRules) { - editorConfigDocument.ThrowIfNull(); + dotnetConfigDocument.ThrowIfNull(); roslynRules.ThrowIfNull(); var ruleIds = new HashSet(); @@ -81,7 +81,7 @@ public IReadOnlyCollection GetIncorrectOptionSeverity(EditorConfig ruleIds.AddEach(roslynRules.QualityRules.Select(r => r.RuleId)); var result = new List(); - foreach (EditorConfigRuleSeverityNode value in editorConfigDocument.DescendantNodes().OfType()) + foreach (DotnetConfigRuleSeverityNode value in dotnetConfigDocument.DescendantNodes().OfType()) { if (!ruleIds.Contains(value.RuleId)) result.Add(value.RuleId); diff --git a/Sources/Kysect.Configuin.EditorConfig/Analyzing/EditorConfigMissedConfiguration.cs b/Sources/Kysect.Configuin.DotnetConfig/Analyzing/DotnetConfigMissedConfiguration.cs similarity index 69% rename from Sources/Kysect.Configuin.EditorConfig/Analyzing/EditorConfigMissedConfiguration.cs rename to Sources/Kysect.Configuin.DotnetConfig/Analyzing/DotnetConfigMissedConfiguration.cs index 4c8d0a2..1984274 100644 --- a/Sources/Kysect.Configuin.EditorConfig/Analyzing/EditorConfigMissedConfiguration.cs +++ b/Sources/Kysect.Configuin.DotnetConfig/Analyzing/DotnetConfigMissedConfiguration.cs @@ -1,15 +1,15 @@ using Kysect.Configuin.RoslynModels; -namespace Kysect.Configuin.EditorConfig.Analyzing; +namespace Kysect.Configuin.DotnetConfig.Analyzing; -public record EditorConfigMissedConfiguration( +public record DotnetConfigMissedConfiguration( IReadOnlyCollection StyleRuleSeverity, IReadOnlyCollection QualityRuleSeverity, IReadOnlyCollection StyleRuleOptions ); -public record EditorConfigInvalidOptionValue( +public record DotnetConfigInvalidOptionValue( string Key, string Value, IReadOnlyCollection AvailableOptions diff --git a/Sources/Kysect.Configuin.DotnetConfig/Analyzing/IDotnetConfigAnalyzeReporter.cs b/Sources/Kysect.Configuin.DotnetConfig/Analyzing/IDotnetConfigAnalyzeReporter.cs new file mode 100644 index 0000000..b8d340d --- /dev/null +++ b/Sources/Kysect.Configuin.DotnetConfig/Analyzing/IDotnetConfigAnalyzeReporter.cs @@ -0,0 +1,10 @@ +using Kysect.Configuin.RoslynModels; + +namespace Kysect.Configuin.DotnetConfig.Analyzing; + +public interface IDotnetConfigAnalyzeReporter +{ + void ReportMissedConfigurations(DotnetConfigMissedConfiguration dotnetConfigMissedConfiguration); + void ReportIncorrectOptionValues(IReadOnlyCollection incorrectOptionValues); + void ReportIncorrectOptionSeverity(IReadOnlyCollection incorrectOptionSeverity); +} \ No newline at end of file diff --git a/Sources/Kysect.Configuin.DotnetConfig/Diff/DotnetConfigDocumentComparator.cs b/Sources/Kysect.Configuin.DotnetConfig/Diff/DotnetConfigDocumentComparator.cs new file mode 100644 index 0000000..1720e5f --- /dev/null +++ b/Sources/Kysect.Configuin.DotnetConfig/Diff/DotnetConfigDocumentComparator.cs @@ -0,0 +1,90 @@ +using Kysect.Configuin.DotnetConfig.Syntax; +using Kysect.Configuin.DotnetConfig.Syntax.Nodes; + +namespace Kysect.Configuin.DotnetConfig.Diff; + +public class DotnetConfigDocumentComparator +{ + public DotnetConfigDocumentDiff Compare(DotnetConfigDocument left, DotnetConfigDocument right) + { + ArgumentNullException.ThrowIfNull(left); + ArgumentNullException.ThrowIfNull(right); + + IReadOnlyCollection severityDiffs = GetSeverityDiff( + left.DescendantNodes().OfType().ToList(), + right.DescendantNodes().OfType().ToList()); + + IReadOnlyCollection optionDiffs = GetOptionDiff( + left.DescendantNodes().OfType().ToList(), + right.DescendantNodes().OfType().ToList()); + + return new DotnetConfigDocumentDiff(severityDiffs, optionDiffs); + } + + private IReadOnlyCollection GetSeverityDiff( + IReadOnlyCollection leftRules, + IReadOnlyCollection rightRules) + { + var diff = new List(); + + foreach (DotnetConfigRuleSeverityNode leftRule in leftRules) + { + // TODO: enhance message in case when >1 rule + DotnetConfigRuleSeverityNode? rightRule = rightRules.SingleOrDefault(r => r.RuleId.Equals(leftRule.RuleId)); + if (rightRule == null) + { + diff.Add(new DotnetConfigRuleSeverityDiff(leftRule.RuleId, leftRule.ParseSeverity(), null)); + } + else if (leftRule.Severity != rightRule.Severity) + { + diff.Add(new DotnetConfigRuleSeverityDiff(leftRule.RuleId, leftRule.ParseSeverity(), rightRule.ParseSeverity())); + } + } + + foreach (DotnetConfigRuleSeverityNode rightRule in rightRules) + { + // TODO: enhance message in case when >1 rule + DotnetConfigRuleSeverityNode? leftRule = leftRules.SingleOrDefault(r => r.RuleId.Equals(rightRule.RuleId)); + if (leftRule is null) + { + diff.Add(new DotnetConfigRuleSeverityDiff(rightRule.RuleId, null, rightRule.ParseSeverity())); + } + } + + return diff; + } + + // TODO: reduce copy paste? + private IReadOnlyCollection GetOptionDiff( + IReadOnlyCollection leftRules, + IReadOnlyCollection rightRules) + { + var diff = new List(); + + foreach (DotnetConfigRuleOptionNode leftRule in leftRules) + { + // TODO: enhance message in case when >1 rule + DotnetConfigRuleOptionNode? rightRule = rightRules.SingleOrDefault(r => r.Key.Equals(leftRule.Key)); + if (rightRule == null) + { + diff.Add(new DotnetConfigRuleOptionDiff(leftRule.Key, leftRule.Value, null)); + } + else if (leftRule.Value != rightRule.Value) + { + diff.Add(new DotnetConfigRuleOptionDiff(leftRule.Key, leftRule.Value, rightRule.Value)); + } + } + + foreach (DotnetConfigRuleOptionNode rightRule in rightRules) + { + // TODO: enhance message in case when >1 rule + DotnetConfigRuleOptionNode? leftRule = leftRules.SingleOrDefault(r => r.Key.Equals(rightRule.Key)); + if (leftRule is null) + { + diff.Add(new DotnetConfigRuleOptionDiff(rightRule.Key, null, rightRule.Value)); + } + } + + return diff; + } +} \ No newline at end of file diff --git a/Sources/Kysect.Configuin.DotnetConfig/Diff/DotnetConfigDocumentDiff.cs b/Sources/Kysect.Configuin.DotnetConfig/Diff/DotnetConfigDocumentDiff.cs new file mode 100644 index 0000000..c52f4f7 --- /dev/null +++ b/Sources/Kysect.Configuin.DotnetConfig/Diff/DotnetConfigDocumentDiff.cs @@ -0,0 +1,7 @@ +using Kysect.Configuin.RoslynModels; + +namespace Kysect.Configuin.DotnetConfig.Diff; + +public record DotnetConfigRuleSeverityDiff(RoslynRuleId Id, RoslynRuleSeverity? Left, RoslynRuleSeverity? Right); +public record DotnetConfigRuleOptionDiff(string Key, string? Left, string? Right); +public record DotnetConfigDocumentDiff(IReadOnlyCollection SeverityDiffs, IReadOnlyCollection OptionDiffs); \ No newline at end of file diff --git a/Sources/Kysect.Configuin.EditorConfig/Formatter/EditorConfigFormatter.cs b/Sources/Kysect.Configuin.DotnetConfig/Formatter/DotnetConfigDocumentFormatter.cs similarity index 54% rename from Sources/Kysect.Configuin.EditorConfig/Formatter/EditorConfigFormatter.cs rename to Sources/Kysect.Configuin.DotnetConfig/Formatter/DotnetConfigDocumentFormatter.cs index 0315d48..6acdc44 100644 --- a/Sources/Kysect.Configuin.EditorConfig/Formatter/EditorConfigFormatter.cs +++ b/Sources/Kysect.Configuin.DotnetConfig/Formatter/DotnetConfigDocumentFormatter.cs @@ -1,48 +1,48 @@ using Kysect.CommonLib.BaseTypes.Extensions; using Kysect.CommonLib.Collections.Extensions; -using Kysect.Configuin.EditorConfig.DocumentModel; -using Kysect.Configuin.EditorConfig.DocumentModel.Nodes; +using Kysect.Configuin.DotnetConfig.Syntax; +using Kysect.Configuin.DotnetConfig.Syntax.Nodes; using Kysect.Configuin.RoslynModels; -namespace Kysect.Configuin.EditorConfig.Formatter; +namespace Kysect.Configuin.DotnetConfig.Formatter; -public class EditorConfigFormatter +public class DotnetConfigDocumentFormatter { - public EditorConfigDocument Format(EditorConfigDocument value) + public DotnetConfigDocument Format(DotnetConfigDocument value) { - List nodesForRemoving = new List(); - IReadOnlyCollection styleRuleNodesForMoving = SelectIdeNodes(value, RoslynRuleTypes.StyleRule).OrderBy(r => r.Key).ToList(); - IReadOnlyCollection qualityRuleNodesForMoving = SelectIdeNodes(value, RoslynRuleTypes.QualityRule).OrderBy(r => r.Key).ToList(); + List nodesForRemoving = new List(); + IReadOnlyCollection styleRuleNodesForMoving = SelectIdeNodes(value, RoslynRuleTypes.StyleRule).OrderBy(r => r.Key).ToList(); + IReadOnlyCollection qualityRuleNodesForMoving = SelectIdeNodes(value, RoslynRuleTypes.QualityRule).OrderBy(r => r.Key).ToList(); nodesForRemoving.AddRange(styleRuleNodesForMoving); nodesForRemoving.AddRange(qualityRuleNodesForMoving); if (nodesForRemoving.IsEmpty()) return value; - EditorConfigCategoryNode autoGeneratedSection = CreateAutoGeneratedCategory(styleRuleNodesForMoving, [new RoslynQualityRuleFormattedSection("CA", qualityRuleNodesForMoving)]); + DotnetConfigCategoryNode autoGeneratedSection = CreateAutoGeneratedCategory(styleRuleNodesForMoving, [new RoslynQualityRuleFormattedSection("CA", qualityRuleNodesForMoving)]); return value .RemoveNodes(nodesForRemoving) .AddChild(autoGeneratedSection); } - public EditorConfigDocument FormatAccordingToRuleDefinitions(EditorConfigDocument document, RoslynRules rules, bool groupQualityRulesByCategory = false) + public DotnetConfigDocument FormatAccordingToRuleDefinitions(DotnetConfigDocument document, RoslynRules rules, bool groupQualityRulesByCategory = false) { rules.ThrowIfNull(); - List nodesForRemoving = new List(); + List nodesForRemoving = new List(); - List propertyNodes = document + List propertyNodes = document .DescendantNodes() - .OfType() + .OfType() .ToList(); - List selectedStyleRuleNodes = new List(); + List selectedStyleRuleNodes = new List(); foreach (RoslynStyleRuleGroup roslynStyleRuleGroup in rules.StyleRuleGroups) { foreach (RoslynStyleRule roslynStyleRule in roslynStyleRuleGroup.Rules) { - IEditorConfigPropertyNode? editorConfigPropertyNode = TryFindSeverityNode(propertyNodes, roslynStyleRule.RuleId); + IDotnetConfigPropertySyntaxNode? editorConfigPropertyNode = TryFindSeverityNode(propertyNodes, roslynStyleRule.RuleId); if (editorConfigPropertyNode is null) continue; @@ -51,7 +51,7 @@ public EditorConfigDocument FormatAccordingToRuleDefinitions(EditorConfigDocumen foreach (RoslynStyleRuleOption roslynStyleRuleOption in roslynStyleRuleGroup.Options) { - IEditorConfigPropertyNode? editorConfigPropertyNode = TryFindOptionNode(propertyNodes, roslynStyleRuleOption); + IDotnetConfigPropertySyntaxNode? editorConfigPropertyNode = TryFindOptionNode(propertyNodes, roslynStyleRuleOption); if (editorConfigPropertyNode is null) continue; @@ -60,9 +60,9 @@ public EditorConfigDocument FormatAccordingToRuleDefinitions(EditorConfigDocumen if (roslynStyleRuleGroup.Rules.Any(r => r.RuleId.Equals(RoslynNameRuleInfo.RuleId))) { - foreach (IEditorConfigPropertyNode editorConfigPropertyNode in propertyNodes) + foreach (IDotnetConfigPropertySyntaxNode editorConfigPropertyNode in propertyNodes) { - if (editorConfigPropertyNode is not EditorConfigRuleCompositeOptionNode option) + if (editorConfigPropertyNode is not DotnetConfigRuleCompositeOptionNode option) continue; if (RoslynNameRuleInfo.IsNameRuleOption(option.ToFullString())) @@ -76,10 +76,10 @@ public EditorConfigDocument FormatAccordingToRuleDefinitions(EditorConfigDocumen { foreach (IGrouping categoryRules in rules.QualityRules.GroupBy(r => r.Category).OrderBy(c => c.Key)) { - List selectedQualityRuleNodes = new List(); + List selectedQualityRuleNodes = new List(); foreach (RoslynQualityRule qualityRule in categoryRules.OrderBy(r => r.RuleId)) { - IEditorConfigPropertyNode? editorConfigPropertyNode = TryFindSeverityNode(propertyNodes, qualityRule.RuleId); + IDotnetConfigPropertySyntaxNode? editorConfigPropertyNode = TryFindSeverityNode(propertyNodes, qualityRule.RuleId); if (editorConfigPropertyNode is null) continue; @@ -92,10 +92,10 @@ public EditorConfigDocument FormatAccordingToRuleDefinitions(EditorConfigDocumen } else { - List selectedQualityRuleNodes = new List(); + List selectedQualityRuleNodes = new List(); foreach (RoslynQualityRule qualityRule in rules.QualityRules) { - IEditorConfigPropertyNode? editorConfigPropertyNode = TryFindSeverityNode(propertyNodes, qualityRule.RuleId); + IDotnetConfigPropertySyntaxNode? editorConfigPropertyNode = TryFindSeverityNode(propertyNodes, qualityRule.RuleId); if (editorConfigPropertyNode is null) continue; @@ -107,26 +107,26 @@ public EditorConfigDocument FormatAccordingToRuleDefinitions(EditorConfigDocumen nodesForRemoving.AddRange(selectedStyleRuleNodes); nodesForRemoving.AddRange(formattedSections.SelectMany(s => s.SelectedQualityRuleNodes)); - EditorConfigCategoryNode autoGeneratedSection = CreateAutoGeneratedCategory(selectedStyleRuleNodes, formattedSections); + DotnetConfigCategoryNode autoGeneratedSection = CreateAutoGeneratedCategory(selectedStyleRuleNodes, formattedSections); return document .RemoveNodes(nodesForRemoving) .AddChild(autoGeneratedSection); } - public record RoslynQualityRuleFormattedSection(string Title, IReadOnlyCollection SelectedQualityRuleNodes); + public record RoslynQualityRuleFormattedSection(string Title, IReadOnlyCollection SelectedQualityRuleNodes); - private EditorConfigCategoryNode CreateAutoGeneratedCategory( - IReadOnlyCollection styleRuleNodesForMoving, + private DotnetConfigCategoryNode CreateAutoGeneratedCategory( + IReadOnlyCollection styleRuleNodesForMoving, IReadOnlyCollection qualityRuleNodesForMoving) { - var autoGeneratedSection = new EditorConfigCategoryNode("*.cs", [], ["# Autogenerated values"], null); + var autoGeneratedSection = new DotnetConfigCategoryNode("*.cs", [], ["# Autogenerated values"], null); if (styleRuleNodesForMoving.Any()) { - var styleRuleSection = new EditorConfigDocumentSectionNode("### IDE ###"); + var styleRuleSection = new DotnetConfigSectionNode("### IDE ###"); - foreach (IEditorConfigPropertyNode styleRule in styleRuleNodesForMoving) + foreach (IDotnetConfigPropertySyntaxNode styleRule in styleRuleNodesForMoving) styleRuleSection = styleRuleSection.AddChild(styleRule); autoGeneratedSection = autoGeneratedSection.AddChild(styleRuleSection); @@ -137,8 +137,8 @@ private EditorConfigCategoryNode CreateAutoGeneratedCategory( if (roslynQualityRuleFormattedSection.SelectedQualityRuleNodes.IsEmpty()) continue; - var qualitySection = new EditorConfigDocumentSectionNode($"### {roslynQualityRuleFormattedSection.Title} ###"); - foreach (IEditorConfigPropertyNode qualityRule in roslynQualityRuleFormattedSection.SelectedQualityRuleNodes) + var qualitySection = new DotnetConfigSectionNode($"### {roslynQualityRuleFormattedSection.Title} ###"); + foreach (IDotnetConfigPropertySyntaxNode qualityRule in roslynQualityRuleFormattedSection.SelectedQualityRuleNodes) qualitySection = qualitySection.AddChild(qualityRule); autoGeneratedSection = autoGeneratedSection.AddChild(qualitySection); } @@ -146,17 +146,17 @@ private EditorConfigCategoryNode CreateAutoGeneratedCategory( return autoGeneratedSection; } - private IReadOnlyCollection SelectIdeNodes(EditorConfigDocument document, string ruleType) + private IReadOnlyCollection SelectIdeNodes(DotnetConfigDocument document, string ruleType) { - List propertyNodes = document + List propertyNodes = document .DescendantNodes() - .OfType() + .OfType() .ToList(); - List styleRuleNodes = new List(); - foreach (IEditorConfigPropertyNode editorConfigPropertyNode in propertyNodes) + List styleRuleNodes = new List(); + foreach (IDotnetConfigPropertySyntaxNode editorConfigPropertyNode in propertyNodes) { - if (editorConfigPropertyNode is not EditorConfigRuleSeverityNode severityConfigSetting) + if (editorConfigPropertyNode is not DotnetConfigRuleSeverityNode severityConfigSetting) continue; if (severityConfigSetting.RuleId.RuleType == ruleType) @@ -166,11 +166,11 @@ private IReadOnlyCollection SelectIdeNodes(EditorConf return styleRuleNodes; } - private IEditorConfigPropertyNode? TryFindSeverityNode(IReadOnlyCollection propertyNodes, RoslynRuleId id) + private IDotnetConfigPropertySyntaxNode? TryFindSeverityNode(IReadOnlyCollection propertyNodes, RoslynRuleId id) { - foreach (IEditorConfigPropertyNode editorConfigPropertyNode in propertyNodes) + foreach (IDotnetConfigPropertySyntaxNode editorConfigPropertyNode in propertyNodes) { - if (editorConfigPropertyNode is not EditorConfigRuleSeverityNode severitySettings) + if (editorConfigPropertyNode is not DotnetConfigRuleSeverityNode severitySettings) continue; if (severitySettings.RuleId == id) @@ -180,11 +180,11 @@ private IReadOnlyCollection SelectIdeNodes(EditorConf return null; } - private IEditorConfigPropertyNode? TryFindOptionNode(IReadOnlyCollection propertyNodes, RoslynStyleRuleOption roslynStyleRuleOption) + private IDotnetConfigPropertySyntaxNode? TryFindOptionNode(IReadOnlyCollection propertyNodes, RoslynStyleRuleOption roslynStyleRuleOption) { - foreach (IEditorConfigPropertyNode editorConfigPropertyNode in propertyNodes) + foreach (IDotnetConfigPropertySyntaxNode editorConfigPropertyNode in propertyNodes) { - if (editorConfigPropertyNode is not EditorConfigRuleOptionNode option) + if (editorConfigPropertyNode is not DotnetConfigRuleOptionNode option) continue; if (option.Key == roslynStyleRuleOption.Name) diff --git a/Sources/Kysect.Configuin.EditorConfig/Kysect.Configuin.EditorConfig.csproj b/Sources/Kysect.Configuin.DotnetConfig/Kysect.Configuin.DotnetConfig.csproj similarity index 79% rename from Sources/Kysect.Configuin.EditorConfig/Kysect.Configuin.EditorConfig.csproj rename to Sources/Kysect.Configuin.DotnetConfig/Kysect.Configuin.DotnetConfig.csproj index fe94ecd..6a45b35 100644 --- a/Sources/Kysect.Configuin.EditorConfig/Kysect.Configuin.EditorConfig.csproj +++ b/Sources/Kysect.Configuin.DotnetConfig/Kysect.Configuin.DotnetConfig.csproj @@ -1,4 +1,4 @@ - + diff --git a/Sources/Kysect.Configuin.EditorConfig/DocumentModel/EditorConfigDocumentParser.cs b/Sources/Kysect.Configuin.DotnetConfig/Syntax/DotnetConfigDocumentParser.cs similarity index 66% rename from Sources/Kysect.Configuin.EditorConfig/DocumentModel/EditorConfigDocumentParser.cs rename to Sources/Kysect.Configuin.DotnetConfig/Syntax/DotnetConfigDocumentParser.cs index b9be294..5af5e02 100644 --- a/Sources/Kysect.Configuin.EditorConfig/DocumentModel/EditorConfigDocumentParser.cs +++ b/Sources/Kysect.Configuin.DotnetConfig/Syntax/DotnetConfigDocumentParser.cs @@ -1,24 +1,14 @@ using Kysect.CommonLib.BaseTypes.Extensions; -using Kysect.Configuin.EditorConfig.DocumentModel.Nodes; +using Kysect.Configuin.DotnetConfig.Syntax.Nodes; using Kysect.Configuin.RoslynModels; -namespace Kysect.Configuin.EditorConfig.DocumentModel; +namespace Kysect.Configuin.DotnetConfig.Syntax; -public record EqualSymbol(string LeadingTrivia, string TrailingTrivia) -{ - public static EqualSymbol Empty { get; } = new EqualSymbol(string.Empty, string.Empty); - - public string ToFullString() - { - return $"{LeadingTrivia}={TrailingTrivia}"; - } -} - -public class EditorConfigDocumentParser +public class DotnetConfigDocumentParser { private readonly HashSet _generalRuleKeys; - public EditorConfigDocumentParser() + public DotnetConfigDocumentParser() { // TODO: Investigate other rules _generalRuleKeys = new HashSet @@ -29,7 +19,7 @@ public EditorConfigDocumentParser() }; } - public EditorConfigDocument Parse(string content) + public DotnetConfigDocument Parse(string content) { content.ThrowIfNull(); @@ -38,11 +28,11 @@ public EditorConfigDocument Parse(string content) return Parse(lines); } - public EditorConfigDocument Parse(string[] lines) + public DotnetConfigDocument Parse(string[] lines) { lines.ThrowIfNull(); - var context = new EditorConfigDocumentParsingContext(); + var context = new DotnetConfigDocumentParsingContext(); foreach (string line in lines) { @@ -57,15 +47,15 @@ public EditorConfigDocument Parse(string[] lines) string categoryName = lineWithoutComment.Substring(1, lineWithoutComment.Length - 2); - EditorConfigCategoryNode categoryNode = comment is not null - ? new EditorConfigCategoryNode(categoryName) { TrailingTrivia = comment } - : new EditorConfigCategoryNode(categoryName); + DotnetConfigCategoryNode categoryNode = comment is not null + ? new DotnetConfigCategoryNode(categoryName) { TrailingTrivia = comment } + : new DotnetConfigCategoryNode(categoryName); context.AddCategory(categoryNode); continue; } - bool isSection = trimmedLine.StartsWith(EditorConfigDocumentSectionNode.NodeIndicator); + bool isSection = trimmedLine.StartsWith(DotnetConfigSectionNode.NodeIndicator); if (isSection) { context.AddSection(trimmedLine); @@ -90,16 +80,16 @@ public EditorConfigDocument Parse(string[] lines) if (parts.Length != 2) throw new ArgumentException($"Line {line} contains unexpected count of '='"); - var keyNode = EditorConfigStringNode.Create(parts[0]); - var valueNode = EditorConfigStringNode.Create(parts[1]); - EqualSymbol equalSymbol = new EqualSymbol(keyNode.TrailingTrivia, valueNode.LeadingTrivia); + var keyNode = DotnetConfigStringNode.Create(parts[0]); + var valueNode = DotnetConfigStringNode.Create(parts[1]); + EqualSymbolSyntaxNode equalSymbol = new EqualSymbolSyntaxNode(keyNode.TrailingTrivia, valueNode.LeadingTrivia); - IEditorConfigPropertyNode editorConfigSetting = ParseSetting(keyNode.Value, equalSymbol, valueNode.Value); + IDotnetConfigPropertySyntaxNode dotnetConfigSyntaxSetting = ParseSetting(keyNode.Value, equalSymbol, valueNode.Value); if (comment is not null) - editorConfigSetting = editorConfigSetting.WithTrailingTrivia(comment); + dotnetConfigSyntaxSetting = dotnetConfigSyntaxSetting.WithTrailingTrivia(comment); - context.AddProperty(editorConfigSetting); + context.AddProperty(dotnetConfigSyntaxSetting); continue; } @@ -118,10 +108,10 @@ public EditorConfigDocument Parse(string[] lines) return (parts[0], parts[1]); } - private IEditorConfigPropertyNode ParseSetting(string key, EqualSymbol equalSymbol, string value) + private IDotnetConfigPropertySyntaxNode ParseSetting(string key, EqualSymbolSyntaxNode equalSymbol, string value) { if (_generalRuleKeys.Contains(key)) - return new EditorConfigGeneralOptionNode(key, equalSymbol, value); + return new DotnetConfigGeneralOptionNode(key, equalSymbol, value); bool isSeveritySetting = key.StartsWith("dotnet_diagnostic."); if (isSeveritySetting) @@ -135,16 +125,16 @@ private IEditorConfigPropertyNode ParseSetting(string key, EqualSymbol equalSymb throw new ArgumentException($"Expect postfix .severity for diagnostic rule but was {keyParts[2]}"); var ruleId = RoslynRuleId.Parse(keyParts[1]); - return new EditorConfigRuleSeverityNode(ruleId, equalSymbol, value); + return new DotnetConfigRuleSeverityNode(ruleId, equalSymbol, value); } bool isCompositeKeyRule = key.Contains('.'); if (isCompositeKeyRule) { string[] keyParts = key.Split('.'); - return new EditorConfigRuleCompositeOptionNode(keyParts, equalSymbol, value); + return new DotnetConfigRuleCompositeOptionNode(keyParts, equalSymbol, value); } - return new EditorConfigRuleOptionNode(key, equalSymbol, value); + return new DotnetConfigRuleOptionNode(key, equalSymbol, value); } } \ No newline at end of file diff --git a/Sources/Kysect.Configuin.EditorConfig/DocumentModel/EditorConfigDocumentParsingContext.cs b/Sources/Kysect.Configuin.DotnetConfig/Syntax/DotnetConfigDocumentParsingContext.cs similarity index 59% rename from Sources/Kysect.Configuin.EditorConfig/DocumentModel/EditorConfigDocumentParsingContext.cs rename to Sources/Kysect.Configuin.DotnetConfig/Syntax/DotnetConfigDocumentParsingContext.cs index 3ecd679..86b5f2c 100644 --- a/Sources/Kysect.Configuin.EditorConfig/DocumentModel/EditorConfigDocumentParsingContext.cs +++ b/Sources/Kysect.Configuin.DotnetConfig/Syntax/DotnetConfigDocumentParsingContext.cs @@ -1,26 +1,26 @@ using Kysect.CommonLib.BaseTypes.Extensions; -using Kysect.Configuin.EditorConfig.DocumentModel.Nodes; +using Kysect.Configuin.DotnetConfig.Syntax.Nodes; using System.Collections.Immutable; -namespace Kysect.Configuin.EditorConfig.DocumentModel; +namespace Kysect.Configuin.DotnetConfig.Syntax; -public class EditorConfigDocumentParsingContext +public class DotnetConfigDocumentParsingContext { - private EditorConfigDocument _currentDocument; - private EditorConfigCategoryNode? _currentCategory; - private EditorConfigDocumentSectionNode? _currentSection; + private DotnetConfigDocument _currentDocument; + private DotnetConfigCategoryNode? _currentCategory; + private DotnetConfigSectionNode? _currentSection; private List _currentTrivia; - public EditorConfigDocumentParsingContext() + public DotnetConfigDocumentParsingContext() { - _currentDocument = new EditorConfigDocument(ImmutableList.Empty); + _currentDocument = new DotnetConfigDocument(ImmutableList.Empty); _currentCategory = null; _currentSection = null; _currentTrivia = new List(); } - public void AddCategory(EditorConfigCategoryNode categoryNode) + public void AddCategory(DotnetConfigCategoryNode categoryNode) { categoryNode.ThrowIfNull(); @@ -33,30 +33,30 @@ public void AddCategory(EditorConfigCategoryNode categoryNode) public void AddSection(string sectionName) { DumpSection(); - _currentSection = new EditorConfigDocumentSectionNode(sectionName) { LeadingTrivia = _currentTrivia.ToImmutableList() }; + _currentSection = new DotnetConfigSectionNode(sectionName) { LeadingTrivia = _currentTrivia.ToImmutableList() }; _currentTrivia = new List(); } - public void AddProperty(IEditorConfigPropertyNode propertyNode) + public void AddProperty(IDotnetConfigPropertySyntaxNode syntaxPropertyNode) { - propertyNode.ThrowIfNull(); + syntaxPropertyNode.ThrowIfNull(); - propertyNode = propertyNode.WithLeadingTrivia(_currentTrivia.ToImmutableList()); + syntaxPropertyNode = syntaxPropertyNode.WithLeadingTrivia(_currentTrivia.ToImmutableList()); _currentTrivia = new List(); if (_currentSection is not null) { - _currentSection = _currentSection.AddChild(propertyNode); + _currentSection = _currentSection.AddChild(syntaxPropertyNode); return; } if (_currentCategory is not null) { - _currentCategory = _currentCategory.AddChild(propertyNode); + _currentCategory = _currentCategory.AddChild(syntaxPropertyNode); return; } - _currentDocument = _currentDocument.AddChild(propertyNode); + _currentDocument = _currentDocument.AddChild(syntaxPropertyNode); } private void DumpSection() @@ -85,7 +85,7 @@ private void DumpCategory() _currentCategory = null; } - public EditorConfigDocument Build() + public DotnetConfigDocument Build() { DumpSection(); DumpCategory(); diff --git a/Sources/Kysect.Configuin.DotnetConfig/Syntax/DotnetConfigDocumentRewriter.cs b/Sources/Kysect.Configuin.DotnetConfig/Syntax/DotnetConfigDocumentRewriter.cs new file mode 100644 index 0000000..183bced --- /dev/null +++ b/Sources/Kysect.Configuin.DotnetConfig/Syntax/DotnetConfigDocumentRewriter.cs @@ -0,0 +1,73 @@ +using Kysect.CommonLib.BaseTypes.Extensions; +using Kysect.Configuin.DotnetConfig.Syntax.Nodes; +using System.Collections.Immutable; + +namespace Kysect.Configuin.DotnetConfig.Syntax; + +public static class DotnetConfigDocumentRewriter +{ + public static DotnetConfigDocument RemoveNodes(this DotnetConfigDocument document, IReadOnlyCollection nodes) + { + return (DotnetConfigDocument) document.FilterChildren(nodes); + } + + public static DotnetConfigDocument ReplaceNodes(this DotnetConfigDocument document, IDotnetConfigSyntaxNode oldSyntaxNode, IDotnetConfigSyntaxNode newSyntaxNode) + { + return (DotnetConfigDocument) document.ReplaceChildren(oldSyntaxNode, newSyntaxNode); + } + + public static DotnetConfigDocument UpdateNodes(this DotnetConfigDocument document, Func morphimsm) + { + document.ThrowIfNull(); + morphimsm.ThrowIfNull(); + + return (DotnetConfigDocument) document.UpdateChildren(morphimsm); + } + + + private static IDotnetConfigSyntaxNode FilterChildren(this IDotnetConfigSyntaxNode syntaxNode, IReadOnlyCollection nodes) + { + if (syntaxNode is not IDotnetConfigContainerSyntaxNode containerNode) + return syntaxNode; + + ImmutableList newChildren = []; + foreach (IDotnetConfigSyntaxNode editorConfigNode in containerNode.Children) + { + if (nodes.Contains(editorConfigNode)) + continue; + + newChildren = newChildren.Add(editorConfigNode.FilterChildren(nodes)); + } + + return containerNode.WithChildren(newChildren); + } + + private static IDotnetConfigSyntaxNode ReplaceChildren(this IDotnetConfigSyntaxNode currentSyntaxNode, IDotnetConfigSyntaxNode oldSyntaxNode, IDotnetConfigSyntaxNode newSyntaxNode) + { + if (currentSyntaxNode == oldSyntaxNode) + return newSyntaxNode; + + if (currentSyntaxNode is not IDotnetConfigContainerSyntaxNode containerNode) + return currentSyntaxNode; + + ImmutableList newChildren = []; + foreach (IDotnetConfigSyntaxNode editorConfigNode in containerNode.Children) + newChildren = newChildren.Add(editorConfigNode.ReplaceChildren(oldSyntaxNode, newSyntaxNode)); + + return containerNode.WithChildren(newChildren); + } + + private static IDotnetConfigSyntaxNode UpdateChildren(this IDotnetConfigSyntaxNode currentSyntaxNode, Func morphimsm) + { + currentSyntaxNode = morphimsm(currentSyntaxNode); + + if (currentSyntaxNode is not IDotnetConfigContainerSyntaxNode containerNode) + return currentSyntaxNode; + + ImmutableList newChildren = containerNode + .Children + .Select(morphimsm) + .ToImmutableList(); + return containerNode.WithChildren(newChildren); + } +} \ No newline at end of file diff --git a/Sources/Kysect.Configuin.DotnetConfig/Syntax/DotnetConfigSyntaxNodeExtensions.cs b/Sources/Kysect.Configuin.DotnetConfig/Syntax/DotnetConfigSyntaxNodeExtensions.cs new file mode 100644 index 0000000..317b199 --- /dev/null +++ b/Sources/Kysect.Configuin.DotnetConfig/Syntax/DotnetConfigSyntaxNodeExtensions.cs @@ -0,0 +1,27 @@ +using Kysect.CommonLib.BaseTypes.Extensions; +using Kysect.Configuin.DotnetConfig.Syntax.Nodes; + +namespace Kysect.Configuin.DotnetConfig.Syntax; + +public static class DotnetConfigSyntaxNodeExtensions +{ + public static IReadOnlyCollection DescendantNodes(this IDotnetConfigSyntaxNode syntaxNode) + { + syntaxNode.ThrowIfNull(); + + List result = + [ + syntaxNode + ]; + + if (syntaxNode is IDotnetConfigContainerSyntaxNode configContainer) + { + foreach (IDotnetConfigSyntaxNode child in configContainer.Children) + { + result.AddRange(child.DescendantNodes()); + } + } + + return result; + } +} \ No newline at end of file diff --git a/Sources/Kysect.Configuin.DotnetConfig/Syntax/Nodes/DotnetConfigCategoryNode.cs b/Sources/Kysect.Configuin.DotnetConfig/Syntax/Nodes/DotnetConfigCategoryNode.cs new file mode 100644 index 0000000..e3ca6dc --- /dev/null +++ b/Sources/Kysect.Configuin.DotnetConfig/Syntax/Nodes/DotnetConfigCategoryNode.cs @@ -0,0 +1,55 @@ +using System.Collections.Immutable; +using System.Text; + +namespace Kysect.Configuin.DotnetConfig.Syntax.Nodes; + +public record DotnetConfigCategoryNode( + string Value, + ImmutableList Children, + ImmutableList LeadingTrivia, + string? TrailingTrivia + ) : IDotnetConfigContainerSyntaxNode +{ + public DotnetConfigCategoryNode(string value) : this(value, ImmutableList.Empty, ImmutableList.Empty, null) + { + } + + IDotnetConfigContainerSyntaxNode IDotnetConfigContainerSyntaxNode.AddChild(IDotnetConfigSyntaxNode child) + { + return AddChild(child); + } + + public IDotnetConfigContainerSyntaxNode WithChildren(ImmutableList children) + { + return this with { Children = children }; + } + + public DotnetConfigCategoryNode AddChild(IDotnetConfigSyntaxNode child) + { + return this with { Children = Children.Add(child) }; + } + + public IDotnetConfigSyntaxNode WithLeadingTrivia(ImmutableList leadingTrivia) + { + return this with { LeadingTrivia = leadingTrivia }; + } + + public IDotnetConfigSyntaxNode WithTrailingTrivia(string? trailingTrivia) + { + return this with { TrailingTrivia = trailingTrivia }; + } + + public string ToFullString() + { + var stringBuilder = new StringBuilder(); + LeadingTrivia.ForEach(s => stringBuilder.AppendLine(s)); + + string line = $"[{Value}]"; + if (TrailingTrivia is not null) + line += $"{line} {TrailingTrivia}"; + stringBuilder.Append(line); + + Children.ForEach(c => stringBuilder.Append($"{Environment.NewLine}{c.ToFullString()}")); + return stringBuilder.ToString(); + } +} \ No newline at end of file diff --git a/Sources/Kysect.Configuin.DotnetConfig/Syntax/Nodes/DotnetConfigDocument.cs b/Sources/Kysect.Configuin.DotnetConfig/Syntax/Nodes/DotnetConfigDocument.cs new file mode 100644 index 0000000..22eb0c1 --- /dev/null +++ b/Sources/Kysect.Configuin.DotnetConfig/Syntax/Nodes/DotnetConfigDocument.cs @@ -0,0 +1,41 @@ +using System.Collections.Immutable; +using System.Text; + +namespace Kysect.Configuin.DotnetConfig.Syntax.Nodes; + +public record DotnetConfigDocument(ImmutableList Children, ImmutableList TrailingTrivia) : IDotnetConfigContainerSyntaxNode +{ + public DotnetConfigDocument() : this([]) + { + } + + public DotnetConfigDocument(ImmutableList children) : this(children, ImmutableList.Empty) + { + } + + IDotnetConfigContainerSyntaxNode IDotnetConfigContainerSyntaxNode.AddChild(IDotnetConfigSyntaxNode child) + { + return AddChild(child); + } + + public IDotnetConfigContainerSyntaxNode WithChildren(ImmutableList children) + { + return this with { Children = children }; + } + + public DotnetConfigDocument AddChild(IDotnetConfigSyntaxNode child) + { + return this with { Children = Children.Add(child) }; + } + + public string ToFullString() + { + var stringBuilder = new StringBuilder(); + List lines = new(); + lines.AddRange(Children.Select(c => c.ToFullString())); + lines.AddRange(TrailingTrivia); + + stringBuilder.AppendJoin(Environment.NewLine, lines); + return stringBuilder.ToString(); + } +} \ No newline at end of file diff --git a/Sources/Kysect.Configuin.EditorConfig/DocumentModel/Nodes/EditorConfigGeneralOptionNode.cs b/Sources/Kysect.Configuin.DotnetConfig/Syntax/Nodes/DotnetConfigGeneralOptionNode.cs similarity index 53% rename from Sources/Kysect.Configuin.EditorConfig/DocumentModel/Nodes/EditorConfigGeneralOptionNode.cs rename to Sources/Kysect.Configuin.DotnetConfig/Syntax/Nodes/DotnetConfigGeneralOptionNode.cs index e685100..c91b4b8 100644 --- a/Sources/Kysect.Configuin.EditorConfig/DocumentModel/Nodes/EditorConfigGeneralOptionNode.cs +++ b/Sources/Kysect.Configuin.DotnetConfig/Syntax/Nodes/DotnetConfigGeneralOptionNode.cs @@ -1,29 +1,29 @@ using System.Collections.Immutable; using System.Text; -namespace Kysect.Configuin.EditorConfig.DocumentModel.Nodes; +namespace Kysect.Configuin.DotnetConfig.Syntax.Nodes; -public record EditorConfigGeneralOptionNode( +public record DotnetConfigGeneralOptionNode( string Key, - EqualSymbol EqualSymbol, + EqualSymbolSyntaxNode EqualSymbol, string Value, ImmutableList LeadingTrivia, - string? TrailingTrivia) : IEditorConfigPropertyNode + string? TrailingTrivia) : IDotnetConfigPropertySyntaxNode { - public EditorConfigGeneralOptionNode(string key, EqualSymbol equalSymbol, string value) : this(key, equalSymbol, value, [], null) + public DotnetConfigGeneralOptionNode(string key, EqualSymbolSyntaxNode equalSymbol, string value) : this(key, equalSymbol, value, [], null) { } - public EditorConfigGeneralOptionNode(string key, string value) : this(key, EqualSymbol.Empty, value, [], null) + public DotnetConfigGeneralOptionNode(string key, string value) : this(key, EqualSymbolSyntaxNode.Empty, value, [], null) { } - public IEditorConfigPropertyNode WithLeadingTrivia(ImmutableList leadingTrivia) + public IDotnetConfigPropertySyntaxNode WithLeadingTrivia(ImmutableList leadingTrivia) { return this with { LeadingTrivia = leadingTrivia }; } - public IEditorConfigPropertyNode WithTrailingTrivia(string? trailingTrivia) + public IDotnetConfigPropertySyntaxNode WithTrailingTrivia(string? trailingTrivia) { return this with { TrailingTrivia = trailingTrivia }; } diff --git a/Sources/Kysect.Configuin.EditorConfig/DocumentModel/Nodes/EditorConfigRuleCompositeOptionNode.cs b/Sources/Kysect.Configuin.DotnetConfig/Syntax/Nodes/DotnetConfigRuleCompositeOptionNode.cs similarity index 51% rename from Sources/Kysect.Configuin.EditorConfig/DocumentModel/Nodes/EditorConfigRuleCompositeOptionNode.cs rename to Sources/Kysect.Configuin.DotnetConfig/Syntax/Nodes/DotnetConfigRuleCompositeOptionNode.cs index bbbab7c..5403f86 100644 --- a/Sources/Kysect.Configuin.EditorConfig/DocumentModel/Nodes/EditorConfigRuleCompositeOptionNode.cs +++ b/Sources/Kysect.Configuin.DotnetConfig/Syntax/Nodes/DotnetConfigRuleCompositeOptionNode.cs @@ -1,32 +1,32 @@ using System.Collections.Immutable; using System.Text; -namespace Kysect.Configuin.EditorConfig.DocumentModel.Nodes; +namespace Kysect.Configuin.DotnetConfig.Syntax.Nodes; -public record EditorConfigRuleCompositeOptionNode( +public record DotnetConfigRuleCompositeOptionNode( IReadOnlyCollection KeyParts, - EqualSymbol EqualSymbol, + EqualSymbolSyntaxNode EqualSymbol, string Value, ImmutableList LeadingTrivia, string? TrailingTrivia - ) : IEditorConfigPropertyNode + ) : IDotnetConfigPropertySyntaxNode { - string IEditorConfigPropertyNode.Key => string.Join('.', KeyParts); + string IDotnetConfigPropertySyntaxNode.Key => string.Join('.', KeyParts); - public EditorConfigRuleCompositeOptionNode(IReadOnlyCollection keyParts, EqualSymbol equalSymbol, string value) : this(keyParts, equalSymbol, value, [], null) + public DotnetConfigRuleCompositeOptionNode(IReadOnlyCollection keyParts, EqualSymbolSyntaxNode equalSymbol, string value) : this(keyParts, equalSymbol, value, [], null) { } - public EditorConfigRuleCompositeOptionNode(IReadOnlyCollection keyParts, string value) : this(keyParts, EqualSymbol.Empty, value, [], null) + public DotnetConfigRuleCompositeOptionNode(IReadOnlyCollection keyParts, string value) : this(keyParts, EqualSymbolSyntaxNode.Empty, value, [], null) { } - public IEditorConfigPropertyNode WithLeadingTrivia(ImmutableList leadingTrivia) + public IDotnetConfigPropertySyntaxNode WithLeadingTrivia(ImmutableList leadingTrivia) { return this with { LeadingTrivia = leadingTrivia }; } - public IEditorConfigPropertyNode WithTrailingTrivia(string? trailingTrivia) + public IDotnetConfigPropertySyntaxNode WithTrailingTrivia(string? trailingTrivia) { return this with { TrailingTrivia = trailingTrivia }; } diff --git a/Sources/Kysect.Configuin.EditorConfig/DocumentModel/Nodes/EditorConfigRuleOptionNode.cs b/Sources/Kysect.Configuin.DotnetConfig/Syntax/Nodes/DotnetConfigRuleOptionNode.cs similarity index 55% rename from Sources/Kysect.Configuin.EditorConfig/DocumentModel/Nodes/EditorConfigRuleOptionNode.cs rename to Sources/Kysect.Configuin.DotnetConfig/Syntax/Nodes/DotnetConfigRuleOptionNode.cs index eaef635..ab604ea 100644 --- a/Sources/Kysect.Configuin.EditorConfig/DocumentModel/Nodes/EditorConfigRuleOptionNode.cs +++ b/Sources/Kysect.Configuin.DotnetConfig/Syntax/Nodes/DotnetConfigRuleOptionNode.cs @@ -1,30 +1,30 @@ using System.Collections.Immutable; using System.Text; -namespace Kysect.Configuin.EditorConfig.DocumentModel.Nodes; +namespace Kysect.Configuin.DotnetConfig.Syntax.Nodes; -public record EditorConfigRuleOptionNode( +public record DotnetConfigRuleOptionNode( string Key, - EqualSymbol EqualSymbol, + EqualSymbolSyntaxNode EqualSymbol, string Value, ImmutableList LeadingTrivia, string? TrailingTrivia -) : IEditorConfigPropertyNode +) : IDotnetConfigPropertySyntaxNode { - public EditorConfigRuleOptionNode(string key, EqualSymbol equalSymbol, string value) : this(key, equalSymbol, value, [], null) + public DotnetConfigRuleOptionNode(string key, EqualSymbolSyntaxNode equalSymbol, string value) : this(key, equalSymbol, value, [], null) { } - public EditorConfigRuleOptionNode(string key, string value) : this(key, EqualSymbol.Empty, value, [], null) + public DotnetConfigRuleOptionNode(string key, string value) : this(key, EqualSymbolSyntaxNode.Empty, value, [], null) { } - public IEditorConfigPropertyNode WithLeadingTrivia(ImmutableList leadingTrivia) + public IDotnetConfigPropertySyntaxNode WithLeadingTrivia(ImmutableList leadingTrivia) { return this with { LeadingTrivia = leadingTrivia }; } - public IEditorConfigPropertyNode WithTrailingTrivia(string? trailingTrivia) + public IDotnetConfigPropertySyntaxNode WithTrailingTrivia(string? trailingTrivia) { return this with { TrailingTrivia = trailingTrivia }; } diff --git a/Sources/Kysect.Configuin.EditorConfig/DocumentModel/Nodes/EditorConfigRuleSeverityNode.cs b/Sources/Kysect.Configuin.DotnetConfig/Syntax/Nodes/DotnetConfigRuleSeverityNode.cs similarity index 53% rename from Sources/Kysect.Configuin.EditorConfig/DocumentModel/Nodes/EditorConfigRuleSeverityNode.cs rename to Sources/Kysect.Configuin.DotnetConfig/Syntax/Nodes/DotnetConfigRuleSeverityNode.cs index cfaf037..e431511 100644 --- a/Sources/Kysect.Configuin.EditorConfig/DocumentModel/Nodes/EditorConfigRuleSeverityNode.cs +++ b/Sources/Kysect.Configuin.DotnetConfig/Syntax/Nodes/DotnetConfigRuleSeverityNode.cs @@ -2,23 +2,23 @@ using System.Collections.Immutable; using System.Text; -namespace Kysect.Configuin.EditorConfig.DocumentModel.Nodes; +namespace Kysect.Configuin.DotnetConfig.Syntax.Nodes; -public record EditorConfigRuleSeverityNode( +public record DotnetConfigRuleSeverityNode( RoslynRuleId RuleId, - EqualSymbol EqualSymbol, + EqualSymbolSyntaxNode EqualSymbol, string Severity, ImmutableList LeadingTrivia, - string? TrailingTrivia) : IEditorConfigPropertyNode + string? TrailingTrivia) : IDotnetConfigPropertySyntaxNode { - string IEditorConfigPropertyNode.Key => $"dotnet_diagnostic.{RuleId}.severity"; - string IEditorConfigPropertyNode.Value => Severity.ToString(); + string IDotnetConfigPropertySyntaxNode.Key => $"dotnet_diagnostic.{RuleId}.severity"; + string IDotnetConfigPropertySyntaxNode.Value => Severity.ToString(); - public EditorConfigRuleSeverityNode(RoslynRuleId ruleId, EqualSymbol equalSymbol, string severity) : this(ruleId, equalSymbol, severity, [], null) + public DotnetConfigRuleSeverityNode(RoslynRuleId ruleId, EqualSymbolSyntaxNode equalSymbol, string severity) : this(ruleId, equalSymbol, severity, [], null) { } - public EditorConfigRuleSeverityNode(RoslynRuleId ruleId, string severity) : this(ruleId, EqualSymbol.Empty, severity, [], null) + public DotnetConfigRuleSeverityNode(RoslynRuleId ruleId, string severity) : this(ruleId, EqualSymbolSyntaxNode.Empty, severity, [], null) { } @@ -27,12 +27,12 @@ public RoslynRuleSeverity ParseSeverity() return Enum.Parse(Severity, ignoreCase: true); } - public IEditorConfigPropertyNode WithLeadingTrivia(ImmutableList leadingTrivia) + public IDotnetConfigPropertySyntaxNode WithLeadingTrivia(ImmutableList leadingTrivia) { return this with { LeadingTrivia = leadingTrivia }; } - public IEditorConfigPropertyNode WithTrailingTrivia(string? trailingTrivia) + public IDotnetConfigPropertySyntaxNode WithTrailingTrivia(string? trailingTrivia) { return this with { TrailingTrivia = trailingTrivia }; } diff --git a/Sources/Kysect.Configuin.EditorConfig/DocumentModel/Nodes/EditorConfigDocumentSectionNode.cs b/Sources/Kysect.Configuin.DotnetConfig/Syntax/Nodes/DotnetConfigSectionNode.cs similarity index 53% rename from Sources/Kysect.Configuin.EditorConfig/DocumentModel/Nodes/EditorConfigDocumentSectionNode.cs rename to Sources/Kysect.Configuin.DotnetConfig/Syntax/Nodes/DotnetConfigSectionNode.cs index 1485380..4d58d02 100644 --- a/Sources/Kysect.Configuin.EditorConfig/DocumentModel/Nodes/EditorConfigDocumentSectionNode.cs +++ b/Sources/Kysect.Configuin.DotnetConfig/Syntax/Nodes/DotnetConfigSectionNode.cs @@ -1,27 +1,27 @@ using System.Collections.Immutable; using System.Text; -namespace Kysect.Configuin.EditorConfig.DocumentModel.Nodes; +namespace Kysect.Configuin.DotnetConfig.Syntax.Nodes; -public record EditorConfigDocumentSectionNode(string Value, ImmutableList Children, ImmutableList LeadingTrivia, string? TrailingTrivia) : IEditorConfigContainerNode +public record DotnetConfigSectionNode(string Value, ImmutableList Children, ImmutableList LeadingTrivia, string? TrailingTrivia) : IDotnetConfigContainerSyntaxNode { public const string NodeIndicator = "###"; - public EditorConfigDocumentSectionNode(string value) : this(value, ImmutableList.Empty, ImmutableList.Empty, null) + public DotnetConfigSectionNode(string value) : this(value, ImmutableList.Empty, ImmutableList.Empty, null) { } - IEditorConfigContainerNode IEditorConfigContainerNode.AddChild(IEditorConfigNode child) + IDotnetConfigContainerSyntaxNode IDotnetConfigContainerSyntaxNode.AddChild(IDotnetConfigSyntaxNode child) { return this with { Children = Children.Add(child) }; } - public IEditorConfigContainerNode WithChildren(ImmutableList children) + public IDotnetConfigContainerSyntaxNode WithChildren(ImmutableList children) { return this with { Children = children }; } - public EditorConfigDocumentSectionNode AddChild(IEditorConfigNode child) + public DotnetConfigSectionNode AddChild(IDotnetConfigSyntaxNode child) { return this with { Children = Children.Add(child) }; } diff --git a/Sources/Kysect.Configuin.EditorConfig/DocumentModel/Nodes/EditorConfigStringNode.cs b/Sources/Kysect.Configuin.DotnetConfig/Syntax/Nodes/DotnetConfigStringNode.cs similarity index 64% rename from Sources/Kysect.Configuin.EditorConfig/DocumentModel/Nodes/EditorConfigStringNode.cs rename to Sources/Kysect.Configuin.DotnetConfig/Syntax/Nodes/DotnetConfigStringNode.cs index be40c79..4e7e59a 100644 --- a/Sources/Kysect.Configuin.EditorConfig/DocumentModel/Nodes/EditorConfigStringNode.cs +++ b/Sources/Kysect.Configuin.DotnetConfig/Syntax/Nodes/DotnetConfigStringNode.cs @@ -1,17 +1,17 @@ using Kysect.CommonLib.BaseTypes.Extensions; -namespace Kysect.Configuin.EditorConfig.DocumentModel.Nodes; +namespace Kysect.Configuin.DotnetConfig.Syntax.Nodes; -public record EditorConfigStringNode(string Value, string LeadingTrivia, string TrailingTrivia) : IEditorConfigNode +public record DotnetConfigStringNode(string Value, string LeadingTrivia, string TrailingTrivia) { - public static EditorConfigStringNode Create(string value) + public static DotnetConfigStringNode Create(string value) { value.ThrowIfNull(); string leadingTriviaLength = value.Substring(0, value.Length - value.TrimStart().Length); string trailingTriviaLength = value.Substring(value.TrimEnd().Length, value.Length - value.TrimEnd().Length); - return new EditorConfigStringNode( + return new DotnetConfigStringNode( value.Trim(), leadingTriviaLength, trailingTriviaLength); diff --git a/Sources/Kysect.Configuin.DotnetConfig/Syntax/Nodes/EqualSymbolSyntaxNode.cs b/Sources/Kysect.Configuin.DotnetConfig/Syntax/Nodes/EqualSymbolSyntaxNode.cs new file mode 100644 index 0000000..48e83e5 --- /dev/null +++ b/Sources/Kysect.Configuin.DotnetConfig/Syntax/Nodes/EqualSymbolSyntaxNode.cs @@ -0,0 +1,11 @@ +namespace Kysect.Configuin.DotnetConfig.Syntax.Nodes; + +public record EqualSymbolSyntaxNode(string LeadingTrivia, string TrailingTrivia) +{ + public static EqualSymbolSyntaxNode Empty { get; } = new EqualSymbolSyntaxNode(string.Empty, string.Empty); + + public string ToFullString() + { + return $"{LeadingTrivia}={TrailingTrivia}"; + } +} \ No newline at end of file diff --git a/Sources/Kysect.Configuin.DotnetConfig/Syntax/Nodes/IDotnetConfigContainerSyntaxNode.cs b/Sources/Kysect.Configuin.DotnetConfig/Syntax/Nodes/IDotnetConfigContainerSyntaxNode.cs new file mode 100644 index 0000000..01cc5e3 --- /dev/null +++ b/Sources/Kysect.Configuin.DotnetConfig/Syntax/Nodes/IDotnetConfigContainerSyntaxNode.cs @@ -0,0 +1,10 @@ +using System.Collections.Immutable; + +namespace Kysect.Configuin.DotnetConfig.Syntax.Nodes; + +public interface IDotnetConfigContainerSyntaxNode : IDotnetConfigSyntaxNode +{ + ImmutableList Children { get; } + IDotnetConfigContainerSyntaxNode AddChild(IDotnetConfigSyntaxNode child); + IDotnetConfigContainerSyntaxNode WithChildren(ImmutableList children); +} \ No newline at end of file diff --git a/Sources/Kysect.Configuin.DotnetConfig/Syntax/Nodes/IDotnetConfigPropertySyntaxNode.cs b/Sources/Kysect.Configuin.DotnetConfig/Syntax/Nodes/IDotnetConfigPropertySyntaxNode.cs new file mode 100644 index 0000000..66e9c72 --- /dev/null +++ b/Sources/Kysect.Configuin.DotnetConfig/Syntax/Nodes/IDotnetConfigPropertySyntaxNode.cs @@ -0,0 +1,14 @@ +using System.Collections.Immutable; + +namespace Kysect.Configuin.DotnetConfig.Syntax.Nodes; + +public interface IDotnetConfigPropertySyntaxNode : IDotnetConfigSyntaxNode +{ + string Key { get; } + string Value { get; } + ImmutableList LeadingTrivia { get; } + string? TrailingTrivia { get; } + + IDotnetConfigPropertySyntaxNode WithLeadingTrivia(ImmutableList leadingTrivia); + IDotnetConfigPropertySyntaxNode WithTrailingTrivia(string? trailingTrivia); +} \ No newline at end of file diff --git a/Sources/Kysect.Configuin.DotnetConfig/Syntax/Nodes/IDotnetConfigSyntaxNode.cs b/Sources/Kysect.Configuin.DotnetConfig/Syntax/Nodes/IDotnetConfigSyntaxNode.cs new file mode 100644 index 0000000..8302827 --- /dev/null +++ b/Sources/Kysect.Configuin.DotnetConfig/Syntax/Nodes/IDotnetConfigSyntaxNode.cs @@ -0,0 +1,6 @@ +namespace Kysect.Configuin.DotnetConfig.Syntax.Nodes; + +public interface IDotnetConfigSyntaxNode +{ + string ToFullString(); +} \ No newline at end of file diff --git a/Sources/Kysect.Configuin.EditorConfig/Template/EditorConfigTemplateBuilder.cs b/Sources/Kysect.Configuin.DotnetConfig/Template/DotnetConfigDocumentTemplateBuilder.cs similarity index 84% rename from Sources/Kysect.Configuin.EditorConfig/Template/EditorConfigTemplateBuilder.cs rename to Sources/Kysect.Configuin.DotnetConfig/Template/DotnetConfigDocumentTemplateBuilder.cs index 5ddc2b1..b9264a4 100644 --- a/Sources/Kysect.Configuin.EditorConfig/Template/EditorConfigTemplateBuilder.cs +++ b/Sources/Kysect.Configuin.DotnetConfig/Template/DotnetConfigDocumentTemplateBuilder.cs @@ -1,9 +1,8 @@ using System.Text; -using ArgumentNullException = System.ArgumentNullException; -namespace Kysect.Configuin.EditorConfig.Template; +namespace Kysect.Configuin.DotnetConfig.Template; -public class EditorConfigTemplateBuilder +public class DotnetConfigDocumentTemplateBuilder { private readonly StringBuilder _templateBuilder = new StringBuilder(); diff --git a/Sources/Kysect.Configuin.EditorConfig/Template/EditorConfigTemplateGenerator.cs b/Sources/Kysect.Configuin.DotnetConfig/Template/DotnetConfigDocumentTemplateGenerator.cs similarity index 86% rename from Sources/Kysect.Configuin.EditorConfig/Template/EditorConfigTemplateGenerator.cs rename to Sources/Kysect.Configuin.DotnetConfig/Template/DotnetConfigDocumentTemplateGenerator.cs index 2752fd9..193f8b0 100644 --- a/Sources/Kysect.Configuin.EditorConfig/Template/EditorConfigTemplateGenerator.cs +++ b/Sources/Kysect.Configuin.DotnetConfig/Template/DotnetConfigDocumentTemplateGenerator.cs @@ -1,24 +1,17 @@ using Kysect.Configuin.RoslynModels; using Microsoft.Extensions.Logging; -namespace Kysect.Configuin.EditorConfig.Template; +namespace Kysect.Configuin.DotnetConfig.Template; -public class EditorConfigTemplateGenerator +public class DotnetConfigDocumentTemplateGenerator(ILogger logger) { - private readonly ILogger _logger; - - public EditorConfigTemplateGenerator(ILogger logger) - { - _logger = logger; - } - public string GenerateTemplate(RoslynRules rules) { ArgumentNullException.ThrowIfNull(rules); - _logger.LogInformation("Generating .editorconfig template."); + logger.LogInformation("Generating .editorconfig template."); - var builder = new EditorConfigTemplateBuilder(); + var builder = new DotnetConfigDocumentTemplateBuilder(); foreach (RoslynStyleRuleGroup roslynStyleRuleGroup in rules.StyleRuleGroups) { diff --git a/Sources/Kysect.Configuin.DotnetFormatIntegration.Abstractions/IDotnetFormatPreviewGenerator.cs b/Sources/Kysect.Configuin.DotnetFormatIntegration.Abstractions/IDotnetFormatPreviewGenerator.cs index 7ee4e6b..a447848 100644 --- a/Sources/Kysect.Configuin.DotnetFormatIntegration.Abstractions/IDotnetFormatPreviewGenerator.cs +++ b/Sources/Kysect.Configuin.DotnetFormatIntegration.Abstractions/IDotnetFormatPreviewGenerator.cs @@ -2,5 +2,5 @@ public interface IDotnetFormatPreviewGenerator { - void GetEditorConfigWarningUpdates(string solutionPath, string newEditorConfig, string sourceEditorConfig); + void GetWarningsAfterChangingDotnetConfig(string solutionPath, string newDotnetConfig, string sourceDotnetConfig); } \ No newline at end of file diff --git a/Sources/Kysect.Configuin.DotnetFormatIntegration/DotnetFormatPreviewGenerator.cs b/Sources/Kysect.Configuin.DotnetFormatIntegration/DotnetFormatPreviewGenerator.cs index b5b5ea9..0c07d25 100644 --- a/Sources/Kysect.Configuin.DotnetFormatIntegration/DotnetFormatPreviewGenerator.cs +++ b/Sources/Kysect.Configuin.DotnetFormatIntegration/DotnetFormatPreviewGenerator.cs @@ -26,11 +26,11 @@ public DotnetFormatPreviewGenerator( _logger = logger; } - public void GetEditorConfigWarningUpdates(string solutionPath, string newEditorConfig, string sourceEditorConfig) + public void GetWarningsAfterChangingDotnetConfig(string solutionPath, string newDotnetConfig, string sourceDotnetConfig) { IReadOnlyCollection originalWarnings = _dotnetFormatWarningGenerator.GenerateWarnings(solutionPath); - IFileMoveUndoOperation undoOperation = _temporaryFileMover.MoveFile(newEditorConfig, sourceEditorConfig); + IFileMoveUndoOperation undoOperation = _temporaryFileMover.MoveFile(newDotnetConfig, sourceDotnetConfig); IReadOnlyCollection newWarnings = _dotnetFormatWarningGenerator.GenerateWarnings(solutionPath); undoOperation.Undo(); diff --git a/Sources/Kysect.Configuin.EditorConfig/Analyzing/IEditorConfigAnalyzeReporter.cs b/Sources/Kysect.Configuin.EditorConfig/Analyzing/IEditorConfigAnalyzeReporter.cs deleted file mode 100644 index 417bddc..0000000 --- a/Sources/Kysect.Configuin.EditorConfig/Analyzing/IEditorConfigAnalyzeReporter.cs +++ /dev/null @@ -1,10 +0,0 @@ -using Kysect.Configuin.RoslynModels; - -namespace Kysect.Configuin.EditorConfig.Analyzing; - -public interface IEditorConfigAnalyzeReporter -{ - void ReportMissedConfigurations(EditorConfigMissedConfiguration editorConfigMissedConfiguration); - void ReportIncorrectOptionValues(IReadOnlyCollection incorrectOptionValues); - void ReportIncorrectOptionSeverity(IReadOnlyCollection incorrectOptionSeverity); -} \ No newline at end of file diff --git a/Sources/Kysect.Configuin.EditorConfig/Diff/EditorConfigSettingsComparator.cs b/Sources/Kysect.Configuin.EditorConfig/Diff/EditorConfigSettingsComparator.cs deleted file mode 100644 index f41e170..0000000 --- a/Sources/Kysect.Configuin.EditorConfig/Diff/EditorConfigSettingsComparator.cs +++ /dev/null @@ -1,90 +0,0 @@ -using Kysect.Configuin.EditorConfig.DocumentModel; -using Kysect.Configuin.EditorConfig.DocumentModel.Nodes; - -namespace Kysect.Configuin.EditorConfig.Diff; - -public class EditorConfigSettingsComparator -{ - public EditorConfigSettingsDiff Compare(EditorConfigDocument left, EditorConfigDocument right) - { - ArgumentNullException.ThrowIfNull(left); - ArgumentNullException.ThrowIfNull(right); - - IReadOnlyCollection severityDiffs = GetSeverityDiff( - left.DescendantNodes().OfType().ToList(), - right.DescendantNodes().OfType().ToList()); - - IReadOnlyCollection optionDiffs = GetOptionDiff( - left.DescendantNodes().OfType().ToList(), - right.DescendantNodes().OfType().ToList()); - - return new EditorConfigSettingsDiff(severityDiffs, optionDiffs); - } - - private IReadOnlyCollection GetSeverityDiff( - IReadOnlyCollection leftRules, - IReadOnlyCollection rightRules) - { - var diff = new List(); - - foreach (EditorConfigRuleSeverityNode leftRule in leftRules) - { - // TODO: enhance message in case when >1 rule - EditorConfigRuleSeverityNode? rightRule = rightRules.SingleOrDefault(r => r.RuleId.Equals(leftRule.RuleId)); - if (rightRule == null) - { - diff.Add(new EditorConfigSettingsRuleSeverityDiff(leftRule.RuleId, leftRule.ParseSeverity(), null)); - } - else if (leftRule.Severity != rightRule.Severity) - { - diff.Add(new EditorConfigSettingsRuleSeverityDiff(leftRule.RuleId, leftRule.ParseSeverity(), rightRule.ParseSeverity())); - } - } - - foreach (EditorConfigRuleSeverityNode rightRule in rightRules) - { - // TODO: enhance message in case when >1 rule - EditorConfigRuleSeverityNode? leftRule = leftRules.SingleOrDefault(r => r.RuleId.Equals(rightRule.RuleId)); - if (leftRule is null) - { - diff.Add(new EditorConfigSettingsRuleSeverityDiff(rightRule.RuleId, null, rightRule.ParseSeverity())); - } - } - - return diff; - } - - // TODO: reduce copy paste? - private IReadOnlyCollection GetOptionDiff( - IReadOnlyCollection leftRules, - IReadOnlyCollection rightRules) - { - var diff = new List(); - - foreach (EditorConfigRuleOptionNode leftRule in leftRules) - { - // TODO: enhance message in case when >1 rule - EditorConfigRuleOptionNode? rightRule = rightRules.SingleOrDefault(r => r.Key.Equals(leftRule.Key)); - if (rightRule == null) - { - diff.Add(new EditorConfigSettingsRuleOptionDiff(leftRule.Key, leftRule.Value, null)); - } - else if (leftRule.Value != rightRule.Value) - { - diff.Add(new EditorConfigSettingsRuleOptionDiff(leftRule.Key, leftRule.Value, rightRule.Value)); - } - } - - foreach (EditorConfigRuleOptionNode rightRule in rightRules) - { - // TODO: enhance message in case when >1 rule - EditorConfigRuleOptionNode? leftRule = leftRules.SingleOrDefault(r => r.Key.Equals(rightRule.Key)); - if (leftRule is null) - { - diff.Add(new EditorConfigSettingsRuleOptionDiff(rightRule.Key, null, rightRule.Value)); - } - } - - return diff; - } -} \ No newline at end of file diff --git a/Sources/Kysect.Configuin.EditorConfig/Diff/EditorConfigSettingsDiff.cs b/Sources/Kysect.Configuin.EditorConfig/Diff/EditorConfigSettingsDiff.cs deleted file mode 100644 index 4c3181d..0000000 --- a/Sources/Kysect.Configuin.EditorConfig/Diff/EditorConfigSettingsDiff.cs +++ /dev/null @@ -1,7 +0,0 @@ -using Kysect.Configuin.RoslynModels; - -namespace Kysect.Configuin.EditorConfig.Diff; - -public record EditorConfigSettingsRuleSeverityDiff(RoslynRuleId Id, RoslynRuleSeverity? Left, RoslynRuleSeverity? Right); -public record EditorConfigSettingsRuleOptionDiff(string Key, string? Left, string? Right); -public record EditorConfigSettingsDiff(IReadOnlyCollection SeverityDiffs, IReadOnlyCollection OptionDiffs); \ No newline at end of file diff --git a/Sources/Kysect.Configuin.EditorConfig/DocumentModel/EditorConfigDocumentExtensions.cs b/Sources/Kysect.Configuin.EditorConfig/DocumentModel/EditorConfigDocumentExtensions.cs deleted file mode 100644 index e3c5c7e..0000000 --- a/Sources/Kysect.Configuin.EditorConfig/DocumentModel/EditorConfigDocumentExtensions.cs +++ /dev/null @@ -1,27 +0,0 @@ -using Kysect.CommonLib.BaseTypes.Extensions; -using Kysect.Configuin.EditorConfig.DocumentModel.Nodes; - -namespace Kysect.Configuin.EditorConfig.DocumentModel; - -public static class EditorConfigDocumentExtensions -{ - public static IReadOnlyCollection DescendantNodes(this IEditorConfigNode node) - { - node.ThrowIfNull(); - - List result = - [ - node - ]; - - if (node is IEditorConfigContainerNode configContainer) - { - foreach (IEditorConfigNode child in configContainer.Children) - { - result.AddRange(child.DescendantNodes()); - } - } - - return result; - } -} \ No newline at end of file diff --git a/Sources/Kysect.Configuin.EditorConfig/DocumentModel/EditorConfigDocumentNodeRewriter.cs b/Sources/Kysect.Configuin.EditorConfig/DocumentModel/EditorConfigDocumentNodeRewriter.cs deleted file mode 100644 index b12c90e..0000000 --- a/Sources/Kysect.Configuin.EditorConfig/DocumentModel/EditorConfigDocumentNodeRewriter.cs +++ /dev/null @@ -1,73 +0,0 @@ -using Kysect.CommonLib.BaseTypes.Extensions; -using Kysect.Configuin.EditorConfig.DocumentModel.Nodes; -using System.Collections.Immutable; - -namespace Kysect.Configuin.EditorConfig.DocumentModel; - -public static class EditorConfigDocumentNodeRewriter -{ - public static EditorConfigDocument RemoveNodes(this EditorConfigDocument document, IReadOnlyCollection nodes) - { - return (EditorConfigDocument) document.FilterChildren(nodes); - } - - public static EditorConfigDocument ReplaceNodes(this EditorConfigDocument document, IEditorConfigNode oldNode, IEditorConfigNode newNode) - { - return (EditorConfigDocument) document.ReplaceChildren(oldNode, newNode); - } - - public static EditorConfigDocument UpdateNodes(this EditorConfigDocument document, Func morphimsm) - { - document.ThrowIfNull(); - morphimsm.ThrowIfNull(); - - return (EditorConfigDocument) document.UpdateChildren(morphimsm); - } - - - private static IEditorConfigNode FilterChildren(this IEditorConfigNode node, IReadOnlyCollection nodes) - { - if (node is not IEditorConfigContainerNode containerNode) - return node; - - ImmutableList newChildren = []; - foreach (IEditorConfigNode editorConfigNode in containerNode.Children) - { - if (nodes.Contains(editorConfigNode)) - continue; - - newChildren = newChildren.Add(editorConfigNode.FilterChildren(nodes)); - } - - return containerNode.WithChildren(newChildren); - } - - private static IEditorConfigNode ReplaceChildren(this IEditorConfigNode currentNode, IEditorConfigNode oldNode, IEditorConfigNode newNode) - { - if (currentNode == oldNode) - return newNode; - - if (currentNode is not IEditorConfigContainerNode containerNode) - return currentNode; - - ImmutableList newChildren = []; - foreach (IEditorConfigNode editorConfigNode in containerNode.Children) - newChildren = newChildren.Add(editorConfigNode.ReplaceChildren(oldNode, newNode)); - - return containerNode.WithChildren(newChildren); - } - - private static IEditorConfigNode UpdateChildren(this IEditorConfigNode currentNode, Func morphimsm) - { - currentNode = morphimsm(currentNode); - - if (currentNode is not IEditorConfigContainerNode containerNode) - return currentNode; - - ImmutableList newChildren = containerNode - .Children - .Select(morphimsm) - .ToImmutableList(); - return containerNode.WithChildren(newChildren); - } -} \ No newline at end of file diff --git a/Sources/Kysect.Configuin.EditorConfig/DocumentModel/IEditorConfigNode.cs b/Sources/Kysect.Configuin.EditorConfig/DocumentModel/IEditorConfigNode.cs deleted file mode 100644 index dabb8e8..0000000 --- a/Sources/Kysect.Configuin.EditorConfig/DocumentModel/IEditorConfigNode.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Kysect.Configuin.EditorConfig.DocumentModel; - -public interface IEditorConfigNode -{ - string ToFullString(); -} \ No newline at end of file diff --git a/Sources/Kysect.Configuin.EditorConfig/DocumentModel/IEditorConfigPropertyNode.cs b/Sources/Kysect.Configuin.EditorConfig/DocumentModel/IEditorConfigPropertyNode.cs deleted file mode 100644 index b4c54d0..0000000 --- a/Sources/Kysect.Configuin.EditorConfig/DocumentModel/IEditorConfigPropertyNode.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System.Collections.Immutable; - -namespace Kysect.Configuin.EditorConfig.DocumentModel; - -public interface IEditorConfigPropertyNode : IEditorConfigNode -{ - string Key { get; } - string Value { get; } - ImmutableList LeadingTrivia { get; } - string? TrailingTrivia { get; } - - IEditorConfigPropertyNode WithLeadingTrivia(ImmutableList leadingTrivia); - IEditorConfigPropertyNode WithTrailingTrivia(string? trailingTrivia); -} \ No newline at end of file diff --git a/Sources/Kysect.Configuin.EditorConfig/DocumentModel/Nodes/EditorConfigCategoryNode.cs b/Sources/Kysect.Configuin.EditorConfig/DocumentModel/Nodes/EditorConfigCategoryNode.cs deleted file mode 100644 index ba7951a..0000000 --- a/Sources/Kysect.Configuin.EditorConfig/DocumentModel/Nodes/EditorConfigCategoryNode.cs +++ /dev/null @@ -1,50 +0,0 @@ -using System.Collections.Immutable; -using System.Text; - -namespace Kysect.Configuin.EditorConfig.DocumentModel.Nodes; - -public record EditorConfigCategoryNode(string Value, ImmutableList Children, ImmutableList LeadingTrivia, string? TrailingTrivia) : IEditorConfigContainerNode -{ - public EditorConfigCategoryNode(string value) : this(value, ImmutableList.Empty, ImmutableList.Empty, null) - { - } - - IEditorConfigContainerNode IEditorConfigContainerNode.AddChild(IEditorConfigNode child) - { - return AddChild(child); - } - - public IEditorConfigContainerNode WithChildren(ImmutableList children) - { - return this with { Children = children }; - } - - public EditorConfigCategoryNode AddChild(IEditorConfigNode child) - { - return this with { Children = Children.Add(child) }; - } - - public IEditorConfigNode WithLeadingTrivia(ImmutableList leadingTrivia) - { - return this with { LeadingTrivia = leadingTrivia }; - } - - public IEditorConfigNode WithTrailingTrivia(string? trailingTrivia) - { - return this with { TrailingTrivia = trailingTrivia }; - } - - public string ToFullString() - { - var stringBuilder = new StringBuilder(); - LeadingTrivia.ForEach(s => stringBuilder.AppendLine(s)); - - string line = $"[{Value}]"; - if (TrailingTrivia is not null) - line += $"{line} {TrailingTrivia}"; - stringBuilder.Append(line); - - Children.ForEach(c => stringBuilder.Append($"{Environment.NewLine}{c.ToFullString()}")); - return stringBuilder.ToString(); - } -} \ No newline at end of file diff --git a/Sources/Kysect.Configuin.EditorConfig/DocumentModel/Nodes/EditorConfigDocument.cs b/Sources/Kysect.Configuin.EditorConfig/DocumentModel/Nodes/EditorConfigDocument.cs deleted file mode 100644 index 1dcff20..0000000 --- a/Sources/Kysect.Configuin.EditorConfig/DocumentModel/Nodes/EditorConfigDocument.cs +++ /dev/null @@ -1,41 +0,0 @@ -using System.Collections.Immutable; -using System.Text; - -namespace Kysect.Configuin.EditorConfig.DocumentModel.Nodes; - -public record EditorConfigDocument(ImmutableList Children, ImmutableList TrailingTrivia) : IEditorConfigContainerNode -{ - public EditorConfigDocument() : this([]) - { - } - - public EditorConfigDocument(ImmutableList children) : this(children, ImmutableList.Empty) - { - } - - IEditorConfigContainerNode IEditorConfigContainerNode.AddChild(IEditorConfigNode child) - { - return AddChild(child); - } - - public IEditorConfigContainerNode WithChildren(ImmutableList children) - { - return this with { Children = children }; - } - - public EditorConfigDocument AddChild(IEditorConfigNode child) - { - return this with { Children = Children.Add(child) }; - } - - public string ToFullString() - { - var stringBuilder = new StringBuilder(); - List lines = new(); - lines.AddRange(Children.Select(c => c.ToFullString())); - lines.AddRange(TrailingTrivia); - - stringBuilder.AppendJoin(Environment.NewLine, lines); - return stringBuilder.ToString(); - } -} \ No newline at end of file diff --git a/Sources/Kysect.Configuin.EditorConfig/DocumentModel/Nodes/IEditorConfigContainerNode.cs b/Sources/Kysect.Configuin.EditorConfig/DocumentModel/Nodes/IEditorConfigContainerNode.cs deleted file mode 100644 index 4ea2f27..0000000 --- a/Sources/Kysect.Configuin.EditorConfig/DocumentModel/Nodes/IEditorConfigContainerNode.cs +++ /dev/null @@ -1,10 +0,0 @@ -using System.Collections.Immutable; - -namespace Kysect.Configuin.EditorConfig.DocumentModel.Nodes; - -public interface IEditorConfigContainerNode : IEditorConfigNode -{ - ImmutableList Children { get; } - IEditorConfigContainerNode AddChild(IEditorConfigNode child); - IEditorConfigContainerNode WithChildren(ImmutableList children); -} \ No newline at end of file diff --git a/Sources/Kysect.Configuin.Tests/CodeStyleGeneration/CodeStyleGeneratorTests.cs b/Sources/Kysect.Configuin.Tests/CodeStyleGeneration/CodeStyleGeneratorTests.cs index b5cafab..948b484 100644 --- a/Sources/Kysect.Configuin.Tests/CodeStyleGeneration/CodeStyleGeneratorTests.cs +++ b/Sources/Kysect.Configuin.Tests/CodeStyleGeneration/CodeStyleGeneratorTests.cs @@ -1,8 +1,8 @@ using Kysect.CommonLib.BaseTypes.Extensions; using Kysect.Configuin.CodeStyleDoc; using Kysect.Configuin.CodeStyleDoc.Models; -using Kysect.Configuin.EditorConfig.DocumentModel; -using Kysect.Configuin.EditorConfig.DocumentModel.Nodes; +using Kysect.Configuin.DotnetConfig.Syntax; +using Kysect.Configuin.DotnetConfig.Syntax.Nodes; using Kysect.Configuin.Learn; using Kysect.Configuin.RoslynModels; using Kysect.Configuin.Tests.Tools; @@ -11,14 +11,14 @@ namespace Kysect.Configuin.Tests.CodeStyleGeneration; public class CodeStyleGeneratorTests { - private readonly EditorConfigDocumentParser _documentParser; + private readonly DotnetConfigDocumentParser _documentParser; private readonly LearnDocumentationParser _learnDocumentationParser; private readonly CodeStyleGenerator _sut; public CodeStyleGeneratorTests() { _sut = new CodeStyleGenerator(TestLogger.ProviderForTests()); - _documentParser = new EditorConfigDocumentParser(); + _documentParser = new DotnetConfigDocumentParser(); _learnDocumentationParser = new LearnDocumentationParser(TestImplementations.GetTextExtractor(), TestLogger.ProviderForTests()); } @@ -30,8 +30,8 @@ public void Generate_ForAllMsLearnDocumentation_FinishWithoutErrors() RoslynRules roslynRules = _learnDocumentationParser.Parse(Constants.GetPathToMsDocsRoot()); string fileText = File.ReadAllText(pathToIniFile); - EditorConfigDocument editorConfigDocument = _documentParser.Parse(fileText); - CodeStyle codeStyle = _sut.Generate(editorConfigDocument, roslynRules); + DotnetConfigDocument dotnetConfigDocument = _documentParser.Parse(fileText); + CodeStyle codeStyle = _sut.Generate(dotnetConfigDocument, roslynRules); ICodeStyleElement codeStyleElement = codeStyle.Elements.ElementAt(2); codeStyleElement.Should().BeOfType(); diff --git a/Sources/Kysect.Configuin.Tests/EditorConfig/EditorConfigAnalyzerTests.cs b/Sources/Kysect.Configuin.Tests/DotnetConfig/DotnetConfigDocumentAnalyzerTests.cs similarity index 57% rename from Sources/Kysect.Configuin.Tests/EditorConfig/EditorConfigAnalyzerTests.cs rename to Sources/Kysect.Configuin.Tests/DotnetConfig/DotnetConfigDocumentAnalyzerTests.cs index 09a2418..f420589 100644 --- a/Sources/Kysect.Configuin.Tests/EditorConfig/EditorConfigAnalyzerTests.cs +++ b/Sources/Kysect.Configuin.Tests/DotnetConfig/DotnetConfigDocumentAnalyzerTests.cs @@ -1,39 +1,39 @@ -using Kysect.Configuin.EditorConfig.Analyzing; -using Kysect.Configuin.EditorConfig.DocumentModel.Nodes; +using Kysect.Configuin.DotnetConfig.Analyzing; +using Kysect.Configuin.DotnetConfig.Syntax.Nodes; using Kysect.Configuin.RoslynModels; using Kysect.Configuin.Tests.Resources; -namespace Kysect.Configuin.Tests.EditorConfig; +namespace Kysect.Configuin.Tests.DotnetConfig; -public class EditorConfigAnalyzerTests +public class DotnetConfigDocumentAnalyzerTests { - private readonly EditorConfigAnalyzer _editorConfigAnalyzer; + private readonly DotnetConfigDocumentAnalyzer _dotnetConfigDocumentAnalyzer; - public EditorConfigAnalyzerTests() + public DotnetConfigDocumentAnalyzerTests() { - _editorConfigAnalyzer = new EditorConfigAnalyzer(); + _dotnetConfigDocumentAnalyzer = new DotnetConfigDocumentAnalyzer(); } [Fact] public void GetMissedConfigurations_AllOptionAreMissed_ReturnAllOptions() { - var editorConfigSettings = new EditorConfigDocument(); + var editorConfigSettings = new DotnetConfigDocument(); RoslynRules roslynRules = RoslynRulesBuilder.New() .AddQuality(WellKnownRoslynRuleDefinitions.CA1064()) .AddStyle(WellKnownRoslynRuleDefinitions.IDE0040()) .Build(); - EditorConfigMissedConfiguration editorConfigMissedConfiguration = _editorConfigAnalyzer.GetMissedConfigurations(editorConfigSettings, roslynRules); + DotnetConfigMissedConfiguration dotnetConfigMissedConfiguration = _dotnetConfigDocumentAnalyzer.GetMissedConfigurations(editorConfigSettings, roslynRules); - editorConfigMissedConfiguration.QualityRuleSeverity + dotnetConfigMissedConfiguration.QualityRuleSeverity .Should().HaveCount(1) .And.Contain(WellKnownRoslynRuleDefinitions.CA1064().RuleId); - editorConfigMissedConfiguration.StyleRuleSeverity + dotnetConfigMissedConfiguration.StyleRuleSeverity .Should().HaveCount(1) .And.Contain(WellKnownRoslynRuleDefinitions.IDE0040().Rules.Single().RuleId); - editorConfigMissedConfiguration.StyleRuleOptions + dotnetConfigMissedConfiguration.StyleRuleOptions .Should().HaveCount(1) .And.Contain(WellKnownRoslynRuleDefinitions.IDE0040().Options.Single().Name); } @@ -41,34 +41,34 @@ public void GetMissedConfigurations_AllOptionAreMissed_ReturnAllOptions() [Fact] public void GetMissedConfigurations_AllOptionExists_ReturnAllOptions() { - var editorConfigSettings = new EditorConfigDocument() - .AddChild(new EditorConfigRuleSeverityNode(WellKnownRoslynRuleDefinitions.IDE0040().Rules.Single().RuleId, RoslynRuleSeverity.Warning.ToString())) - .AddChild(new EditorConfigRuleSeverityNode(WellKnownRoslynRuleDefinitions.CA1064().RuleId, RoslynRuleSeverity.Warning.ToString())) - .AddChild(new EditorConfigRuleOptionNode(WellKnownRoslynRuleDefinitions.IDE0040().Options.Single().Name, "always")); + var editorConfigSettings = new DotnetConfigDocument() + .AddChild(new DotnetConfigRuleSeverityNode(WellKnownRoslynRuleDefinitions.IDE0040().Rules.Single().RuleId, RoslynRuleSeverity.Warning.ToString())) + .AddChild(new DotnetConfigRuleSeverityNode(WellKnownRoslynRuleDefinitions.CA1064().RuleId, RoslynRuleSeverity.Warning.ToString())) + .AddChild(new DotnetConfigRuleOptionNode(WellKnownRoslynRuleDefinitions.IDE0040().Options.Single().Name, "always")); RoslynRules roslynRules = RoslynRulesBuilder.New() .AddQuality(WellKnownRoslynRuleDefinitions.CA1064()) .AddStyle(WellKnownRoslynRuleDefinitions.IDE0040()) .Build(); - EditorConfigMissedConfiguration editorConfigMissedConfiguration = _editorConfigAnalyzer.GetMissedConfigurations(editorConfigSettings, roslynRules); + DotnetConfigMissedConfiguration dotnetConfigMissedConfiguration = _dotnetConfigDocumentAnalyzer.GetMissedConfigurations(editorConfigSettings, roslynRules); - editorConfigMissedConfiguration.QualityRuleSeverity.Should().BeEmpty(); - editorConfigMissedConfiguration.StyleRuleSeverity.Should().BeEmpty(); - editorConfigMissedConfiguration.StyleRuleOptions.Should().BeEmpty(); + dotnetConfigMissedConfiguration.QualityRuleSeverity.Should().BeEmpty(); + dotnetConfigMissedConfiguration.StyleRuleSeverity.Should().BeEmpty(); + dotnetConfigMissedConfiguration.StyleRuleOptions.Should().BeEmpty(); } [Fact] public void GetIncorrectOptionValues_AllOptionsValid_NoElementReturn() { - var editorConfigSettings = new EditorConfigDocument() - .AddChild(new EditorConfigRuleOptionNode(WellKnownRoslynRuleDefinitions.IDE0040().Options.Single().Name, "always")); + var editorConfigSettings = new DotnetConfigDocument() + .AddChild(new DotnetConfigRuleOptionNode(WellKnownRoslynRuleDefinitions.IDE0040().Options.Single().Name, "always")); RoslynRules roslynRules = RoslynRulesBuilder.New() .AddStyle(WellKnownRoslynRuleDefinitions.IDE0040()) .Build(); - IReadOnlyCollection invalidOptionValues = _editorConfigAnalyzer.GetIncorrectOptionValues(editorConfigSettings, roslynRules); + IReadOnlyCollection invalidOptionValues = _dotnetConfigDocumentAnalyzer.GetIncorrectOptionValues(editorConfigSettings, roslynRules); invalidOptionValues.Should().BeEmpty(); } @@ -78,19 +78,19 @@ public void GetIncorrectOptionValues_ForInvalidOptionValue_ReturnInvalidOption() { string incorrectOptionValue = "null"; RoslynStyleRuleOption selectedOption = WellKnownRoslynRuleDefinitions.IDE0040().Options.Single(); - var editorConfigSettings = new EditorConfigDocument() - .AddChild(new EditorConfigRuleOptionNode(selectedOption.Name, incorrectOptionValue)); + var editorConfigSettings = new DotnetConfigDocument() + .AddChild(new DotnetConfigRuleOptionNode(selectedOption.Name, incorrectOptionValue)); RoslynRules roslynRules = RoslynRulesBuilder.New() .AddStyle(WellKnownRoslynRuleDefinitions.IDE0040()) .Build(); - var expected = new EditorConfigInvalidOptionValue( + var expected = new DotnetConfigInvalidOptionValue( selectedOption.Name, incorrectOptionValue, selectedOption.Values); - IReadOnlyCollection invalidOptionValues = _editorConfigAnalyzer.GetIncorrectOptionValues(editorConfigSettings, roslynRules); + IReadOnlyCollection invalidOptionValues = _dotnetConfigDocumentAnalyzer.GetIncorrectOptionValues(editorConfigSettings, roslynRules); invalidOptionValues.Should().HaveCount(1) .And.Subject.ElementAt(0).Should().BeEquivalentTo(expected); @@ -102,19 +102,19 @@ public void GetIncorrectOptionValues_ForInvalidOptionKey_ReturnInvalidOption() string incorrectOptionKey = "null"; string incorrectOptionValue = "null"; - var editorConfigSettings = new EditorConfigDocument() - .AddChild(new EditorConfigRuleOptionNode(incorrectOptionKey, incorrectOptionValue)); + var editorConfigSettings = new DotnetConfigDocument() + .AddChild(new DotnetConfigRuleOptionNode(incorrectOptionKey, incorrectOptionValue)); RoslynRules roslynRules = RoslynRulesBuilder.New() .AddStyle(WellKnownRoslynRuleDefinitions.IDE0040()) .Build(); - var expected = new EditorConfigInvalidOptionValue( + var expected = new DotnetConfigInvalidOptionValue( incorrectOptionKey, incorrectOptionValue, Array.Empty()); - IReadOnlyCollection invalidOptionValues = _editorConfigAnalyzer.GetIncorrectOptionValues(editorConfigSettings, roslynRules); + IReadOnlyCollection invalidOptionValues = _dotnetConfigDocumentAnalyzer.GetIncorrectOptionValues(editorConfigSettings, roslynRules); invalidOptionValues.Should().HaveCount(1) .And.Subject.ElementAt(0).Should().BeEquivalentTo(expected); @@ -123,15 +123,15 @@ public void GetIncorrectOptionValues_ForInvalidOptionKey_ReturnInvalidOption() [Fact] public void GetIncorrectOptionSeverity_ForInvalidSeverityConfiguration_ReturnInvalidRuleIds() { - var editorConfigSettings = new EditorConfigDocument() - .AddChild(new EditorConfigRuleSeverityNode(WellKnownRoslynRuleDefinitions.IDE0040().Rules.Single().RuleId, RoslynRuleSeverity.Warning.ToString())) - .AddChild(new EditorConfigRuleSeverityNode(WellKnownRoslynRuleDefinitions.CA1064().RuleId, RoslynRuleSeverity.Warning.ToString())); + var editorConfigSettings = new DotnetConfigDocument() + .AddChild(new DotnetConfigRuleSeverityNode(WellKnownRoslynRuleDefinitions.IDE0040().Rules.Single().RuleId, RoslynRuleSeverity.Warning.ToString())) + .AddChild(new DotnetConfigRuleSeverityNode(WellKnownRoslynRuleDefinitions.CA1064().RuleId, RoslynRuleSeverity.Warning.ToString())); RoslynRules roslynRules = RoslynRulesBuilder.New() .AddQuality(WellKnownRoslynRuleDefinitions.CA1064()) .Build(); - IReadOnlyCollection incorrectOptionSeverity = _editorConfigAnalyzer.GetIncorrectOptionSeverity(editorConfigSettings, roslynRules); + IReadOnlyCollection incorrectOptionSeverity = _dotnetConfigDocumentAnalyzer.GetIncorrectOptionSeverity(editorConfigSettings, roslynRules); incorrectOptionSeverity.Should().HaveCount(1) .And.Subject.ElementAt(0).Should().BeEquivalentTo(WellKnownRoslynRuleDefinitions.IDE0040().Rules.Single().RuleId); diff --git a/Sources/Kysect.Configuin.Tests/DotnetConfig/DotnetConfigDocumentComparatorTests.cs b/Sources/Kysect.Configuin.Tests/DotnetConfig/DotnetConfigDocumentComparatorTests.cs new file mode 100644 index 0000000..a2bbc09 --- /dev/null +++ b/Sources/Kysect.Configuin.Tests/DotnetConfig/DotnetConfigDocumentComparatorTests.cs @@ -0,0 +1,31 @@ +using Kysect.Configuin.DotnetConfig.Diff; +using Kysect.Configuin.DotnetConfig.Syntax.Nodes; +using Kysect.Configuin.RoslynModels; + +namespace Kysect.Configuin.Tests.DotnetConfig; + +public class DotnetConfigDocumentComparatorTests +{ + [Fact] + public void Compare_WithEmptyCollection_ReturnExpectedResult() + { + var empty = new DotnetConfigDocument(); + var left = new DotnetConfigDocument() + .AddChild(new DotnetConfigRuleSeverityNode(RoslynRuleId.Parse("IDE0001"), RoslynRuleSeverity.Warning.ToString())) + .AddChild(new DotnetConfigRuleOptionNode("OptionKey", "OptionValue")) + .AddChild(new DotnetConfigGeneralOptionNode("Name", "Value")) + .AddChild(new DotnetConfigRuleCompositeOptionNode(new string[] { "Key", "Parts" }, "Value")); + + var editorConfigSettingsComparator = new DotnetConfigDocumentComparator(); + + DotnetConfigDocumentDiff dotnetConfigDocumentDiff = editorConfigSettingsComparator.Compare(left, empty); + + dotnetConfigDocumentDiff.SeverityDiffs + .Should().HaveCount(1) + .And.Contain(new DotnetConfigRuleSeverityDiff(RoslynRuleId.Parse("IDE0001"), RoslynRuleSeverity.Warning, null)); + + dotnetConfigDocumentDiff.OptionDiffs + .Should().HaveCount(1) + .And.Contain(new DotnetConfigRuleOptionDiff("OptionKey", "OptionValue", null)); + } +} \ No newline at end of file diff --git a/Sources/Kysect.Configuin.Tests/EditorConfig/EditorConfigFormatterTests.cs b/Sources/Kysect.Configuin.Tests/DotnetConfig/DotnetConfigDocumentFormatterTests.cs similarity index 81% rename from Sources/Kysect.Configuin.Tests/EditorConfig/EditorConfigFormatterTests.cs rename to Sources/Kysect.Configuin.Tests/DotnetConfig/DotnetConfigDocumentFormatterTests.cs index 5e63f5c..e7ad872 100644 --- a/Sources/Kysect.Configuin.Tests/EditorConfig/EditorConfigFormatterTests.cs +++ b/Sources/Kysect.Configuin.Tests/DotnetConfig/DotnetConfigDocumentFormatterTests.cs @@ -1,22 +1,22 @@ -using Kysect.Configuin.EditorConfig.DocumentModel; -using Kysect.Configuin.EditorConfig.DocumentModel.Nodes; -using Kysect.Configuin.EditorConfig.Formatter; +using Kysect.Configuin.DotnetConfig.Formatter; +using Kysect.Configuin.DotnetConfig.Syntax; +using Kysect.Configuin.DotnetConfig.Syntax.Nodes; using Kysect.Configuin.Learn; using Kysect.Configuin.RoslynModels; using Kysect.Configuin.Tests.Tools; -namespace Kysect.Configuin.Tests.EditorConfig; +namespace Kysect.Configuin.Tests.DotnetConfig; -public class EditorConfigFormatterTests +public class DotnetConfigDocumentFormatterTests { - private readonly EditorConfigFormatter _formatter; - private readonly EditorConfigDocumentParser _parser; + private readonly DotnetConfigDocumentFormatter _documentFormatter; + private readonly DotnetConfigDocumentParser _parser; private readonly LearnDocumentationParser _learnDocumentationParser; - public EditorConfigFormatterTests() + public DotnetConfigDocumentFormatterTests() { - _formatter = new EditorConfigFormatter(); - _parser = new EditorConfigDocumentParser(); + _documentFormatter = new DotnetConfigDocumentFormatter(); + _parser = new DotnetConfigDocumentParser(); _learnDocumentationParser = new LearnDocumentationParser(TestImplementations.GetTextExtractor(), TestLogger.ProviderForTests()); } @@ -97,8 +97,8 @@ public void Format_NamingRule_ReturnOrderedLinesWithHeader() RoslynRules roslynRules = _learnDocumentationParser.Parse(Constants.GetPathToMsDocsRoot()); - EditorConfigDocument editorConfigDocument = _parser.Parse(input); - EditorConfigDocument formattedDocument = _formatter.FormatAccordingToRuleDefinitions(editorConfigDocument, roslynRules, false); + DotnetConfigDocument dotnetConfigDocument = _parser.Parse(input); + DotnetConfigDocument formattedDocument = _documentFormatter.FormatAccordingToRuleDefinitions(dotnetConfigDocument, roslynRules, false); formattedDocument.ToFullString().Should().Be(expected); } @@ -127,8 +127,8 @@ public void Format_QualityRuleByCategory_ReturnGroupedByCategory() RoslynRules roslynRules = _learnDocumentationParser.Parse(Constants.GetPathToMsDocsRoot()); - EditorConfigDocument editorConfigDocument = _parser.Parse(input); - EditorConfigDocument formattedDocument = _formatter.FormatAccordingToRuleDefinitions(editorConfigDocument, roslynRules, true); + DotnetConfigDocument dotnetConfigDocument = _parser.Parse(input); + DotnetConfigDocument formattedDocument = _documentFormatter.FormatAccordingToRuleDefinitions(dotnetConfigDocument, roslynRules, true); formattedDocument.ToFullString().Should().Be(expected); } @@ -144,8 +144,8 @@ public void FormatAccordingToRuleDefinitions_Sample_ReturnExpectedFormatterDocum RoslynRules roslynRules = _learnDocumentationParser.Parse(Constants.GetPathToMsDocsRoot()); - EditorConfigDocument editorConfigDocument = _parser.Parse(input); - EditorConfigDocument formattedDocument = _formatter.FormatAccordingToRuleDefinitions(editorConfigDocument, roslynRules, false); + DotnetConfigDocument dotnetConfigDocument = _parser.Parse(input); + DotnetConfigDocument formattedDocument = _documentFormatter.FormatAccordingToRuleDefinitions(dotnetConfigDocument, roslynRules, false); // TODO: Do smth with this =_= formattedDocument.ToFullString() @@ -156,8 +156,8 @@ public void FormatAccordingToRuleDefinitions_Sample_ReturnExpectedFormatterDocum private void FormatAndCompare(string input, string expected) { - EditorConfigDocument editorConfigDocument = _parser.Parse(input); - EditorConfigDocument formattedDocument = _formatter.Format(editorConfigDocument); + DotnetConfigDocument dotnetConfigDocument = _parser.Parse(input); + DotnetConfigDocument formattedDocument = _documentFormatter.Format(dotnetConfigDocument); formattedDocument.ToFullString().Should().Be(expected); } } \ No newline at end of file diff --git a/Sources/Kysect.Configuin.Tests/EditorConfig/EditorConfigDocumentParserTests.cs b/Sources/Kysect.Configuin.Tests/DotnetConfig/DotnetConfigDocumentParserTests.cs similarity index 60% rename from Sources/Kysect.Configuin.Tests/EditorConfig/EditorConfigDocumentParserTests.cs rename to Sources/Kysect.Configuin.Tests/DotnetConfig/DotnetConfigDocumentParserTests.cs index ebd433f..0b51859 100644 --- a/Sources/Kysect.Configuin.Tests/EditorConfig/EditorConfigDocumentParserTests.cs +++ b/Sources/Kysect.Configuin.Tests/DotnetConfig/DotnetConfigDocumentParserTests.cs @@ -1,20 +1,20 @@ -using Kysect.Configuin.EditorConfig.DocumentModel; -using Kysect.Configuin.EditorConfig.DocumentModel.Nodes; -using Kysect.Configuin.Tests.EditorConfig.Tools; +using Kysect.Configuin.DotnetConfig.Syntax; +using Kysect.Configuin.DotnetConfig.Syntax.Nodes; +using Kysect.Configuin.Tests.DotnetConfig.Tools; using System.Collections.Immutable; -namespace Kysect.Configuin.Tests.EditorConfig; +namespace Kysect.Configuin.Tests.DotnetConfig; -public class EditorConfigDocumentParserTests() +public class DotnetConfigDocumentParserTests() { - private readonly EditorConfigDocumentParser _parser = new EditorConfigDocumentParser(); - private readonly EditorConfigDocumentComparator _comparator = new EditorConfigDocumentComparator(); + private readonly DotnetConfigDocumentParser _parser = new DotnetConfigDocumentParser(); + private readonly DotnetConfigDocumentComparator _comparator = new DotnetConfigDocumentComparator(); [Fact] public void Parse_EmptyFile_ReturnEmptyDocumentNode() { const string content = ""; - EditorConfigDocument expected = new EditorConfigDocument(ImmutableList.Empty, [""]); + DotnetConfigDocument expected = new DotnetConfigDocument(ImmutableList.Empty, [""]); ParseAndCompare(content, expected); } @@ -23,7 +23,7 @@ public void Parse_EmptyFile_ReturnEmptyDocumentNode() public void Parse_TriviaOnly_ReturnEmptyDocumentNode() { const string content = "# comment"; - EditorConfigDocument expected = new EditorConfigDocument(ImmutableList.Empty, ["# comment"]); + DotnetConfigDocument expected = new DotnetConfigDocument(ImmutableList.Empty, ["# comment"]); ParseAndCompare(content, expected); } @@ -34,7 +34,7 @@ public void Parse_Category_ReturnDocumentWithCategoryNode() const string content = """ [*.cs] """; - EditorConfigDocument expected = new EditorConfigDocument([new EditorConfigCategoryNode("*.cs")]); + DotnetConfigDocument expected = new DotnetConfigDocument([new DotnetConfigCategoryNode("*.cs")]); ParseAndCompare(content, expected); } @@ -45,7 +45,7 @@ public void Parse_CategoryWithComment_ReturnDocumentWithCategoryNode() const string content = """ [*.cs] # comment """; - EditorConfigDocument expected = new EditorConfigDocument([new EditorConfigCategoryNode("*.cs") { TrailingTrivia = " comment" }]); + DotnetConfigDocument expected = new DotnetConfigDocument([new DotnetConfigCategoryNode("*.cs") { TrailingTrivia = " comment" }]); ParseAndCompare(content, expected); } @@ -56,9 +56,9 @@ public void Parse_Group_ReturnDocumentWithGroupNode() const string content = """ ### Custom section ### """; - EditorConfigDocument expected = new EditorConfigDocument([new EditorConfigDocumentSectionNode("### Custom section ###")]); + DotnetConfigDocument expected = new DotnetConfigDocument([new DotnetConfigSectionNode("### Custom section ###")]); - EditorConfigDocument actual = _parser.Parse(content); + DotnetConfigDocument actual = _parser.Parse(content); _comparator.Compare(actual, expected); } @@ -69,7 +69,7 @@ public void Parse_Property_ReturnDocumentWithPropertyNode() const string content = """ key=value """; - EditorConfigDocument expected = new EditorConfigDocument([new EditorConfigRuleOptionNode("key", "value")]); + DotnetConfigDocument expected = new DotnetConfigDocument([new DotnetConfigRuleOptionNode("key", "value")]); ParseAndCompare(content, expected); } @@ -80,7 +80,7 @@ public void Parse_PropertyWithComment_ReturnDocumentWithPropertyNode() const string content = """ key=value # comment """; - EditorConfigDocument expected = new EditorConfigDocument([new EditorConfigRuleOptionNode("key", "value") { TrailingTrivia = " comment" }]); + DotnetConfigDocument expected = new DotnetConfigDocument([new DotnetConfigRuleOptionNode("key", "value") { TrailingTrivia = " comment" }]); ParseAndCompare(content, expected); } @@ -95,11 +95,11 @@ public void Parse_CategoryWithProperty_ReturnCorrectDocument() end_of_line=crlf """; - EditorConfigDocument expected = new EditorConfigDocument([ - new EditorConfigCategoryNode("*.cs") - .AddChild(new EditorConfigGeneralOptionNode("tab_width", "4")) - .AddChild(new EditorConfigGeneralOptionNode("indent_size", "4")) - .AddChild(new EditorConfigGeneralOptionNode("end_of_line", "crlf"))]); + DotnetConfigDocument expected = new DotnetConfigDocument([ + new DotnetConfigCategoryNode("*.cs") + .AddChild(new DotnetConfigGeneralOptionNode("tab_width", "4")) + .AddChild(new DotnetConfigGeneralOptionNode("indent_size", "4")) + .AddChild(new DotnetConfigGeneralOptionNode("end_of_line", "crlf"))]); ParseAndCompare(content, expected); } @@ -115,12 +115,12 @@ public void Parse_CategoryWithSectionWithProperty_ReturnCorrectDocument() end_of_line=crlf """; - EditorConfigDocument expected = new EditorConfigDocument([ - new EditorConfigCategoryNode("*.cs") - .AddChild(new EditorConfigDocumentSectionNode("### Custom section ###") - .AddChild(new EditorConfigGeneralOptionNode("tab_width", "4")) - .AddChild(new EditorConfigGeneralOptionNode("indent_size", "4")) - .AddChild(new EditorConfigGeneralOptionNode("end_of_line", "crlf"))) + DotnetConfigDocument expected = new DotnetConfigDocument([ + new DotnetConfigCategoryNode("*.cs") + .AddChild(new DotnetConfigSectionNode("### Custom section ###") + .AddChild(new DotnetConfigGeneralOptionNode("tab_width", "4")) + .AddChild(new DotnetConfigGeneralOptionNode("indent_size", "4")) + .AddChild(new DotnetConfigGeneralOptionNode("end_of_line", "crlf"))) ]); ParseAndCompare(content, expected); @@ -136,9 +136,9 @@ public void Parse_CategoryWithPropertyWithLeadingTrivia_ReturnCorrectDocument() tab_width=4 """; - EditorConfigDocument expected = new EditorConfigDocument([ - new EditorConfigCategoryNode("*.cs") - .AddChild(new EditorConfigGeneralOptionNode("tab_width", "4") { LeadingTrivia = [string.Empty, string.Empty] }) + DotnetConfigDocument expected = new DotnetConfigDocument([ + new DotnetConfigCategoryNode("*.cs") + .AddChild(new DotnetConfigGeneralOptionNode("tab_width", "4") { LeadingTrivia = [string.Empty, string.Empty] }) ]); ParseAndCompare(content, expected); @@ -148,7 +148,7 @@ public void Parse_CategoryWithPropertyWithLeadingTrivia_ReturnCorrectDocument() public void Parse_SampleDocumentAndSerialize_ReturnSameValue() { string expected = File.ReadAllText(Path.Combine("Resources", "Editor-config-sample.ini")); - EditorConfigDocument document = _parser.Parse(expected); + DotnetConfigDocument document = _parser.Parse(expected); string actual = document.ToFullString(); actual.Should().Be(expected); @@ -164,15 +164,15 @@ public void Parse_DocumentWithAllElements_ReturnSameValue() key = value """; - EditorConfigDocument document = _parser.Parse(expected); + DotnetConfigDocument document = _parser.Parse(expected); string actual = document.ToFullString(); actual.Should().Be(expected); } - private void ParseAndCompare(string content, EditorConfigDocument expected) + private void ParseAndCompare(string content, DotnetConfigDocument expected) { - EditorConfigDocument actual = _parser.Parse(content); + DotnetConfigDocument actual = _parser.Parse(content); _comparator.Compare(actual, expected); } diff --git a/Sources/Kysect.Configuin.Tests/DotnetConfig/DotnetConfigDocumentRewriterTests.cs b/Sources/Kysect.Configuin.Tests/DotnetConfig/DotnetConfigDocumentRewriterTests.cs new file mode 100644 index 0000000..8896e4b --- /dev/null +++ b/Sources/Kysect.Configuin.Tests/DotnetConfig/DotnetConfigDocumentRewriterTests.cs @@ -0,0 +1,82 @@ +using Kysect.CommonLib.BaseTypes.Extensions; +using Kysect.Configuin.DotnetConfig.Syntax; +using Kysect.Configuin.DotnetConfig.Syntax.Nodes; +using Kysect.Configuin.Tests.DotnetConfig.Tools; + +namespace Kysect.Configuin.Tests.DotnetConfig; + +public class DotnetConfigDocumentRewriterTests +{ + private readonly DotnetConfigDocumentComparator _comparator = new DotnetConfigDocumentComparator(); + + [Fact] + public void Remove_Child_ReturnDocumentWithoutChild() + { + DotnetConfigDocument input = new DotnetConfigDocument() + .AddChild(new DotnetConfigRuleOptionNode("first", "value")) + .AddChild(new DotnetConfigRuleOptionNode("second", "value")); + + DotnetConfigDocument expected = new DotnetConfigDocument() + .AddChild(new DotnetConfigRuleOptionNode("second", "value")); + + var nodeForRemoving = input.Children.First(); + DotnetConfigDocument actual = input.RemoveNodes([nodeForRemoving]); + + _comparator.Compare(actual, expected); + } + + [Fact] + public void Remove_MultipleChild_ReturnDocumentWithoutChild() + { + DotnetConfigDocument input = new DotnetConfigDocument() + .AddChild(new DotnetConfigRuleOptionNode("first", "value")) + .AddChild(new DotnetConfigRuleOptionNode("second", "value")) + .AddChild(new DotnetConfigRuleOptionNode("third", "value")); + + DotnetConfigDocument expected = new DotnetConfigDocument() + .AddChild(new DotnetConfigRuleOptionNode("third", "value")); + + var nodesForRemoving = input.Children.Take(2).ToList(); + DotnetConfigDocument actual = input.RemoveNodes(nodesForRemoving); + + _comparator.Compare(actual, expected); + } + + [Fact] + public void ReplaceNode_NodeWithChild_ReplaceAll() + { + DotnetConfigDocument input = new DotnetConfigDocument() + .AddChild(new DotnetConfigSectionNode("### Section ###") + .AddChild(new DotnetConfigRuleOptionNode("first", "value"))); + + DotnetConfigDocument expected = new DotnetConfigDocument() + .AddChild(new DotnetConfigSectionNode("### Section ###") + .AddChild(new DotnetConfigRuleOptionNode("second", "value2"))); + + var nodesForRemoving = input.Children.First().To().Children.First(); + DotnetConfigDocument actual = input.ReplaceNodes(nodesForRemoving, new DotnetConfigRuleOptionNode("second", "value2")); + + _comparator.Compare(actual, expected); + } + + [Fact] + public void UpdateNodes_NodeWithChild_OnlyParentChanged() + { + DotnetConfigDocument input = new DotnetConfigDocument() + .AddChild(new DotnetConfigSectionNode("### Section ###") + .AddChild(new DotnetConfigRuleOptionNode("first", "value"))); + + DotnetConfigDocument expected = new DotnetConfigDocument() + .AddChild(new DotnetConfigSectionNode("### Other section ###") + .AddChild(new DotnetConfigRuleOptionNode("first", "value"))); + + DotnetConfigDocument actual = input.UpdateNodes(n => + { + if (n is DotnetConfigSectionNode { Value: "### Section ###" } oldSection) + return oldSection with { Value = "### Other section ###" }; + return n; + }); + + _comparator.Compare(actual, expected); + } +} \ No newline at end of file diff --git a/Sources/Kysect.Configuin.Tests/EditorConfig/EditorConfigTemplateGeneratorTests.cs b/Sources/Kysect.Configuin.Tests/DotnetConfig/DotnetConfigDocumentTemplateGeneratorTests.cs similarity index 86% rename from Sources/Kysect.Configuin.Tests/EditorConfig/EditorConfigTemplateGeneratorTests.cs rename to Sources/Kysect.Configuin.Tests/DotnetConfig/DotnetConfigDocumentTemplateGeneratorTests.cs index d369f31..4c639f9 100644 --- a/Sources/Kysect.Configuin.Tests/EditorConfig/EditorConfigTemplateGeneratorTests.cs +++ b/Sources/Kysect.Configuin.Tests/DotnetConfig/DotnetConfigDocumentTemplateGeneratorTests.cs @@ -1,17 +1,17 @@ -using Kysect.Configuin.EditorConfig.Template; +using Kysect.Configuin.DotnetConfig.Template; using Kysect.Configuin.RoslynModels; using Kysect.Configuin.Tests.Resources; using Kysect.Configuin.Tests.Tools; -namespace Kysect.Configuin.Tests.EditorConfig; +namespace Kysect.Configuin.Tests.DotnetConfig; -public class EditorConfigTemplateGeneratorTests +public class DotnetConfigDocumentTemplateGeneratorTests { - private readonly EditorConfigTemplateGenerator _editorConfigTemplateGenerator; + private readonly DotnetConfigDocumentTemplateGenerator _dotnetConfigDocumentTemplateGenerator; - public EditorConfigTemplateGeneratorTests() + public DotnetConfigDocumentTemplateGeneratorTests() { - _editorConfigTemplateGenerator = new EditorConfigTemplateGenerator(TestLogger.ProviderForTests()); + _dotnetConfigDocumentTemplateGenerator = new DotnetConfigDocumentTemplateGenerator(TestLogger.ProviderForTests()); } [Fact] @@ -38,7 +38,7 @@ public void GenerateTemplate_ForIDE0001_ReturnExpectedString() """; - string generateTemplate = _editorConfigTemplateGenerator.GenerateTemplate(roslynRules); + string generateTemplate = _dotnetConfigDocumentTemplateGenerator.GenerateTemplate(roslynRules); generateTemplate.Should().Be(expected); } @@ -60,7 +60,7 @@ public void GenerateTemplate_ForIDE0007_0008_ReturnExpectedString() """; - string generateTemplate = _editorConfigTemplateGenerator.GenerateTemplate(roslynRules); + string generateTemplate = _dotnetConfigDocumentTemplateGenerator.GenerateTemplate(roslynRules); generateTemplate.Should().Be(expected); } @@ -100,7 +100,7 @@ public void GenerateTemplate_ForIDE0040_ReturnExpectedString() """; - string generateTemplate = _editorConfigTemplateGenerator.GenerateTemplate(roslynRules); + string generateTemplate = _dotnetConfigDocumentTemplateGenerator.GenerateTemplate(roslynRules); generateTemplate.Should().Be(expected); } @@ -121,7 +121,7 @@ public void GenerateTemplate_ForCA1064_ReturnExpectedString() """; - string generateTemplate = _editorConfigTemplateGenerator.GenerateTemplate(roslynRules); + string generateTemplate = _dotnetConfigDocumentTemplateGenerator.GenerateTemplate(roslynRules); generateTemplate.Should().Be(expected); } diff --git a/Sources/Kysect.Configuin.Tests/EditorConfig/Tools/EditorConfigDocumentComparator.cs b/Sources/Kysect.Configuin.Tests/DotnetConfig/Tools/DotnetConfigDocumentComparator.cs similarity index 60% rename from Sources/Kysect.Configuin.Tests/EditorConfig/Tools/EditorConfigDocumentComparator.cs rename to Sources/Kysect.Configuin.Tests/DotnetConfig/Tools/DotnetConfigDocumentComparator.cs index 90d8b41..cf678fa 100644 --- a/Sources/Kysect.Configuin.Tests/EditorConfig/Tools/EditorConfigDocumentComparator.cs +++ b/Sources/Kysect.Configuin.Tests/DotnetConfig/Tools/DotnetConfigDocumentComparator.cs @@ -1,13 +1,12 @@ using Kysect.CommonLib.BaseTypes.Extensions; -using Kysect.Configuin.EditorConfig.DocumentModel; -using Kysect.Configuin.EditorConfig.DocumentModel.Nodes; +using Kysect.Configuin.DotnetConfig.Syntax.Nodes; using System.Collections.Immutable; -namespace Kysect.Configuin.Tests.EditorConfig.Tools; +namespace Kysect.Configuin.Tests.DotnetConfig.Tools; -public class EditorConfigDocumentComparator +public class DotnetConfigDocumentComparator { - public void Compare(EditorConfigDocument actual, EditorConfigDocument expected) + public void Compare(DotnetConfigDocument actual, DotnetConfigDocument expected) { actual.ThrowIfNull(); expected.ThrowIfNull(); @@ -16,7 +15,7 @@ public void Compare(EditorConfigDocument actual, EditorConfigDocument expected) actual.TrailingTrivia.Should().BeEquivalentTo(expected.TrailingTrivia); } - private void CompareChildren(ImmutableList actual, ImmutableList expected) + private void CompareChildren(ImmutableList actual, ImmutableList expected) { actual.Should().HaveCount(expected.Count); for (int i = 0; i < actual.Count; i++) @@ -25,7 +24,7 @@ private void CompareChildren(ImmutableList actual, ImmutableL } } - private void CompareCategory(EditorConfigCategoryNode actual, EditorConfigCategoryNode expected) + private void CompareCategory(DotnetConfigCategoryNode actual, DotnetConfigCategoryNode expected) { actual.ThrowIfNull(); expected.ThrowIfNull(); @@ -36,7 +35,7 @@ private void CompareCategory(EditorConfigCategoryNode actual, EditorConfigCatego CompareChildren(actual.Children, expected.Children); } - private void CompareSection(EditorConfigDocumentSectionNode actual, EditorConfigDocumentSectionNode expected) + private void CompareSection(DotnetConfigSectionNode actual, DotnetConfigSectionNode expected) { actual.ThrowIfNull(); expected.ThrowIfNull(); @@ -47,7 +46,7 @@ private void CompareSection(EditorConfigDocumentSectionNode actual, EditorConfig CompareChildren(actual.Children, expected.Children); } - private void CompareProperty(IEditorConfigPropertyNode actual, IEditorConfigPropertyNode expected) + private void CompareProperty(IDotnetConfigPropertySyntaxNode actual, IDotnetConfigPropertySyntaxNode expected) { actual.ThrowIfNull(); expected.ThrowIfNull(); @@ -59,25 +58,25 @@ private void CompareProperty(IEditorConfigPropertyNode actual, IEditorConfigProp actual.Value.Should().Be(expected.Value); } - private void Compare(IEditorConfigNode actual, IEditorConfigNode expected) + private void Compare(IDotnetConfigSyntaxNode actual, IDotnetConfigSyntaxNode expected) { actual.GetType().Should().Be(expected.GetType()); - if (actual is EditorConfigCategoryNode actualCategory) + if (actual is DotnetConfigCategoryNode actualCategory) { - CompareCategory(actualCategory, (EditorConfigCategoryNode) expected); + CompareCategory(actualCategory, (DotnetConfigCategoryNode) expected); return; } - if (actual is EditorConfigDocumentSectionNode sectionNode) + if (actual is DotnetConfigSectionNode sectionNode) { - CompareSection(sectionNode, (EditorConfigDocumentSectionNode) expected); + CompareSection(sectionNode, (DotnetConfigSectionNode) expected); return; } - if (actual is IEditorConfigPropertyNode propertyNode) + if (actual is IDotnetConfigPropertySyntaxNode propertyNode) { - CompareProperty(propertyNode, (IEditorConfigPropertyNode) expected); + CompareProperty(propertyNode, (IDotnetConfigPropertySyntaxNode) expected); return; } diff --git a/Sources/Kysect.Configuin.Tests/EditorConfig/EditorConfigDocumentNodeRewriterTests.cs b/Sources/Kysect.Configuin.Tests/EditorConfig/EditorConfigDocumentNodeRewriterTests.cs deleted file mode 100644 index 77761fd..0000000 --- a/Sources/Kysect.Configuin.Tests/EditorConfig/EditorConfigDocumentNodeRewriterTests.cs +++ /dev/null @@ -1,82 +0,0 @@ -using Kysect.CommonLib.BaseTypes.Extensions; -using Kysect.Configuin.EditorConfig.DocumentModel; -using Kysect.Configuin.EditorConfig.DocumentModel.Nodes; -using Kysect.Configuin.Tests.EditorConfig.Tools; - -namespace Kysect.Configuin.Tests.EditorConfig; - -public class EditorConfigDocumentNodeRewriterTests -{ - private readonly EditorConfigDocumentComparator _comparator = new EditorConfigDocumentComparator(); - - [Fact] - public void Remove_Child_ReturnDocumentWithoutChild() - { - EditorConfigDocument input = new EditorConfigDocument() - .AddChild(new EditorConfigRuleOptionNode("first", "value")) - .AddChild(new EditorConfigRuleOptionNode("second", "value")); - - EditorConfigDocument expected = new EditorConfigDocument() - .AddChild(new EditorConfigRuleOptionNode("second", "value")); - - var nodeForRemoving = input.Children.First(); - EditorConfigDocument actual = input.RemoveNodes([nodeForRemoving]); - - _comparator.Compare(actual, expected); - } - - [Fact] - public void Remove_MultipleChild_ReturnDocumentWithoutChild() - { - EditorConfigDocument input = new EditorConfigDocument() - .AddChild(new EditorConfigRuleOptionNode("first", "value")) - .AddChild(new EditorConfigRuleOptionNode("second", "value")) - .AddChild(new EditorConfigRuleOptionNode("third", "value")); - - EditorConfigDocument expected = new EditorConfigDocument() - .AddChild(new EditorConfigRuleOptionNode("third", "value")); - - var nodesForRemoving = input.Children.Take(2).ToList(); - EditorConfigDocument actual = input.RemoveNodes(nodesForRemoving); - - _comparator.Compare(actual, expected); - } - - [Fact] - public void ReplaceNode_NodeWithChild_ReplaceAll() - { - EditorConfigDocument input = new EditorConfigDocument() - .AddChild(new EditorConfigDocumentSectionNode("### Section ###") - .AddChild(new EditorConfigRuleOptionNode("first", "value"))); - - EditorConfigDocument expected = new EditorConfigDocument() - .AddChild(new EditorConfigDocumentSectionNode("### Section ###") - .AddChild(new EditorConfigRuleOptionNode("second", "value2"))); - - var nodesForRemoving = input.Children.First().To().Children.First(); - EditorConfigDocument actual = input.ReplaceNodes(nodesForRemoving, new EditorConfigRuleOptionNode("second", "value2")); - - _comparator.Compare(actual, expected); - } - - [Fact] - public void UpdateNodes_NodeWithChild_OnlyParentChanged() - { - EditorConfigDocument input = new EditorConfigDocument() - .AddChild(new EditorConfigDocumentSectionNode("### Section ###") - .AddChild(new EditorConfigRuleOptionNode("first", "value"))); - - EditorConfigDocument expected = new EditorConfigDocument() - .AddChild(new EditorConfigDocumentSectionNode("### Other section ###") - .AddChild(new EditorConfigRuleOptionNode("first", "value"))); - - EditorConfigDocument actual = input.UpdateNodes(n => - { - if (n is EditorConfigDocumentSectionNode { Value: "### Section ###" } oldSection) - return oldSection with { Value = "### Other section ###" }; - return n; - }); - - _comparator.Compare(actual, expected); - } -} \ No newline at end of file diff --git a/Sources/Kysect.Configuin.Tests/EditorConfig/EditorConfigSettingsComparatorTests.cs b/Sources/Kysect.Configuin.Tests/EditorConfig/EditorConfigSettingsComparatorTests.cs deleted file mode 100644 index 43a3a8d..0000000 --- a/Sources/Kysect.Configuin.Tests/EditorConfig/EditorConfigSettingsComparatorTests.cs +++ /dev/null @@ -1,31 +0,0 @@ -using Kysect.Configuin.EditorConfig.Diff; -using Kysect.Configuin.EditorConfig.DocumentModel.Nodes; -using Kysect.Configuin.RoslynModels; - -namespace Kysect.Configuin.Tests.EditorConfig; - -public class EditorConfigSettingsComparatorTests -{ - [Fact] - public void Compare_WithEmptyCollection_ReturnExpectedResult() - { - var empty = new EditorConfigDocument(); - var left = new EditorConfigDocument() - .AddChild(new EditorConfigRuleSeverityNode(RoslynRuleId.Parse("IDE0001"), RoslynRuleSeverity.Warning.ToString())) - .AddChild(new EditorConfigRuleOptionNode("OptionKey", "OptionValue")) - .AddChild(new EditorConfigGeneralOptionNode("Name", "Value")) - .AddChild(new EditorConfigRuleCompositeOptionNode(new string[] { "Key", "Parts" }, "Value")); - - var editorConfigSettingsComparator = new EditorConfigSettingsComparator(); - - EditorConfigSettingsDiff editorConfigSettingsDiff = editorConfigSettingsComparator.Compare(left, empty); - - editorConfigSettingsDiff.SeverityDiffs - .Should().HaveCount(1) - .And.Contain(new EditorConfigSettingsRuleSeverityDiff(RoslynRuleId.Parse("IDE0001"), RoslynRuleSeverity.Warning, null)); - - editorConfigSettingsDiff.OptionDiffs - .Should().HaveCount(1) - .And.Contain(new EditorConfigSettingsRuleOptionDiff("OptionKey", "OptionValue", null)); - } -} \ No newline at end of file diff --git a/Sources/Kysect.Configuin.Tests/Kysect.Configuin.Tests.csproj b/Sources/Kysect.Configuin.Tests/Kysect.Configuin.Tests.csproj index 19a6d6f..ee1054a 100644 --- a/Sources/Kysect.Configuin.Tests/Kysect.Configuin.Tests.csproj +++ b/Sources/Kysect.Configuin.Tests/Kysect.Configuin.Tests.csproj @@ -25,7 +25,6 @@ - diff --git a/Sources/Kysect.Configuin.sln b/Sources/Kysect.Configuin.sln index 48c8852..82f8f8d 100644 --- a/Sources/Kysect.Configuin.sln +++ b/Sources/Kysect.Configuin.sln @@ -14,8 +14,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Kysect.Configuin.RoslynMode EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Kysect.Configuin.Markdown", "Kysect.Configuin.Markdown\Kysect.Configuin.Markdown.csproj", "{BA63EE03-27FE-4502-B2D6-42A45D4B10AE}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Kysect.Configuin.EditorConfig", "Kysect.Configuin.EditorConfig\Kysect.Configuin.EditorConfig.csproj", "{AF0452EC-1E00-4490-9B1A-C3FA9810B2C9}" -EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Kysect.Configuin.ConfigurationRoot", "Kysect.Configuin.ConfigurationRoot\Kysect.Configuin.ConfigurationRoot.csproj", "{71590EE8-BD16-4E45-8299-4D1CA3B0EC42}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Kysect.Configuin.CodeStyleDoc", "Kysect.Configuin.CodeStyleDoc\Kysect.Configuin.CodeStyleDoc.csproj", "{372CD01F-754F-4D95-8F59-F08F9BC645A6}" @@ -32,7 +30,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Kysect.Configuin.Learn", "K EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Kysect.Configuin", "Kysect.Configuin\Kysect.Configuin.csproj", "{DC52757E-B97D-433D-BCC7-EB27364D8FC8}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Kysect.Configuin.DotnetFormatIntegration.Abstractions", "Kysect.Configuin.DotnetFormatIntegration.Abstractions\Kysect.Configuin.DotnetFormatIntegration.Abstractions.csproj", "{38439A63-7A20-40E1-AC7F-0143050CE124}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Kysect.Configuin.DotnetFormatIntegration.Abstractions", "Kysect.Configuin.DotnetFormatIntegration.Abstractions\Kysect.Configuin.DotnetFormatIntegration.Abstractions.csproj", "{38439A63-7A20-40E1-AC7F-0143050CE124}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Kysect.Configuin.DotnetConfig", "Kysect.Configuin.DotnetConfig\Kysect.Configuin.DotnetConfig.csproj", "{ADC86105-58F8-4460-91A3-849A5A5BE266}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -48,6 +48,10 @@ Global {4428CEC8-DB24-42BC-9689-BA8DF769F603}.Debug|Any CPU.Build.0 = Debug|Any CPU {4428CEC8-DB24-42BC-9689-BA8DF769F603}.Release|Any CPU.ActiveCfg = Release|Any CPU {4428CEC8-DB24-42BC-9689-BA8DF769F603}.Release|Any CPU.Build.0 = Release|Any CPU + {ADC86105-58F8-4460-91A3-849A5A5BE266}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {ADC86105-58F8-4460-91A3-849A5A5BE266}.Debug|Any CPU.Build.0 = Debug|Any CPU + {ADC86105-58F8-4460-91A3-849A5A5BE266}.Release|Any CPU.ActiveCfg = Release|Any CPU + {ADC86105-58F8-4460-91A3-849A5A5BE266}.Release|Any CPU.Build.0 = Release|Any CPU {B0B65BDF-5E19-4AC0-A5A7-B69DCC0BDCE2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {B0B65BDF-5E19-4AC0-A5A7-B69DCC0BDCE2}.Debug|Any CPU.Build.0 = Debug|Any CPU {B0B65BDF-5E19-4AC0-A5A7-B69DCC0BDCE2}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -76,10 +80,6 @@ Global {C16DFC44-C11B-4B7D-A1EF-242FAAC55DD6}.Debug|Any CPU.Build.0 = Debug|Any CPU {C16DFC44-C11B-4B7D-A1EF-242FAAC55DD6}.Release|Any CPU.ActiveCfg = Release|Any CPU {C16DFC44-C11B-4B7D-A1EF-242FAAC55DD6}.Release|Any CPU.Build.0 = Release|Any CPU - {AF0452EC-1E00-4490-9B1A-C3FA9810B2C9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {AF0452EC-1E00-4490-9B1A-C3FA9810B2C9}.Debug|Any CPU.Build.0 = Debug|Any CPU - {AF0452EC-1E00-4490-9B1A-C3FA9810B2C9}.Release|Any CPU.ActiveCfg = Release|Any CPU - {AF0452EC-1E00-4490-9B1A-C3FA9810B2C9}.Release|Any CPU.Build.0 = Release|Any CPU {71590EE8-BD16-4E45-8299-4D1CA3B0EC42}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {71590EE8-BD16-4E45-8299-4D1CA3B0EC42}.Debug|Any CPU.Build.0 = Debug|Any CPU {71590EE8-BD16-4E45-8299-4D1CA3B0EC42}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -100,3 +100,15 @@ Global SolutionGuid = {01578FC6-10E0-4D67-A696-986C508E2723} EndGlobalSection EndGlobal +Any CPU.ActiveCfg = Debug|Any CPU + {DC52757E-B97D-433D-BCC7-EB27364D8FC8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DC52757E-B97D-433D-BCC7-EB27364D8FC8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DC52757E-B97D-433D-BCC7-EB27364D8FC8}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {01578FC6-10E0-4D67-A696-986C508E2723} + EndGlobalSection +EndGlobal