Skip to content

Commit

Permalink
Merge pull request #36 from kysect/refactor/fix-naming
Browse files Browse the repository at this point in the history
Fix some naming issues
  • Loading branch information
FrediKats authored Sep 2, 2023
2 parents 8d298f3 + 804655e commit 47b45c6
Show file tree
Hide file tree
Showing 38 changed files with 307 additions and 340 deletions.
8 changes: 4 additions & 4 deletions Sources/Kysect.Configuin.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@ IServiceProvider InitializeServiceProvider()
void GenerateCodeStyle(IServiceProvider serviceProvider)
{
IEditorConfigContentProvider editorConfigContentProvider = serviceProvider.GetRequiredService<IEditorConfigContentProvider>();
IEditorConfigRuleParser editorConfigRuleParser = serviceProvider.GetRequiredService<IEditorConfigRuleParser>();
IEditorConfigSettingsParser editorConfigSettingsParser = serviceProvider.GetRequiredService<IEditorConfigSettingsParser>();
IMsLearnDocumentationInfoProvider msLearnDocumentationInfoProvider = serviceProvider.GetRequiredService<IMsLearnDocumentationInfoProvider>();
IMsLearnDocumentationParser msLearnDocumentationParser = serviceProvider.GetRequiredService<IMsLearnDocumentationParser>();
ICodeStyleGenerator codeStyleGenerator = serviceProvider.GetRequiredService<ICodeStyleGenerator>();
ICodeStyleWriter codeStyleWriter = serviceProvider.GetRequiredService<ICodeStyleWriter>();

string editorConfigContent = editorConfigContentProvider.Provide();
EditorConfigRuleSet editorConfigRuleSet = editorConfigRuleParser.Parse(editorConfigContent);
EditorConfigSettings editorConfigSettings = editorConfigSettingsParser.Parse(editorConfigContent);

MsLearnDocumentationRawInfo msLearnDocumentationRawInfo = msLearnDocumentationInfoProvider.Provide();
RoslynRules roslynRules = msLearnDocumentationParser.Parse(msLearnDocumentationRawInfo);

CodeStyleInfo codeStyleInfo = codeStyleGenerator.Generate(editorConfigRuleSet, roslynRules);
codeStyleWriter.Write(codeStyleInfo);
CodeStyle codeStyle = codeStyleGenerator.Generate(editorConfigSettings, roslynRules);
codeStyleWriter.Write(codeStyle);
}
Original file line number Diff line number Diff line change
@@ -1,106 +1,107 @@
using Kysect.CommonLib.Collections.Extensions;
using Kysect.Configuin.Core.CodeStyleGeneration.Models;
using Kysect.Configuin.Core.EditorConfigParsing;
using Kysect.Configuin.Core.EditorConfigParsing.Rules;
using Kysect.Configuin.Core.EditorConfigParsing.Settings;
using Kysect.Configuin.Core.RoslynRuleModels;

namespace Kysect.Configuin.Core.CodeStyleGeneration;

public class CodeStyleGenerator : ICodeStyleGenerator
{
public CodeStyleInfo Generate(EditorConfigRuleSet editorConfigRuleSet, RoslynRules roslynRules)
public CodeStyle Generate(EditorConfigSettings editorConfigSettings, RoslynRules roslynRules)
{
IReadOnlyCollection<IEditorConfigRule> notProcessedRules = editorConfigRuleSet.Rules;
IReadOnlyCollection<RoslynStyleRuleOption> optionsFromDocs = roslynRules.GetOptions();
IReadOnlyCollection<RoslynStyleRuleOption> roslynRuleOptions = roslynRules.GetOptions();
IReadOnlyCollection<IEditorConfigSetting> notProcessedSettings = editorConfigSettings.Settings;

notProcessedRules = notProcessedRules.Where(IsSupported).ToList();
notProcessedSettings = notProcessedSettings.Where(IsSupported).ToList();

// TODO: support in some way
notProcessedRules = notProcessedRules.Where(r => r is not GeneralEditorConfigRule).ToList();

// TODO: support in some way
notProcessedRules = notProcessedRules.Where(r => r is not CompositeRoslynOptionEditorConfigRule).ToList();

IReadOnlyCollection<RoslynOptionConfiguration> roslynOptionEditorConfigRules = notProcessedRules
.OfType<RoslynOptionEditorConfigRule>()
.Select(o => ParseOption(o, optionsFromDocs))
IReadOnlyCollection<CodeStyleRoslynOptionConfiguration> optionConfigurations = notProcessedSettings
.OfType<RoslynOptionEditorConfigSetting>()
.Select(o => ParseOptionSettings(o, roslynRuleOptions))
.ToList();

notProcessedRules = notProcessedRules.Where(r => r is not RoslynOptionEditorConfigRule).ToList();
notProcessedSettings = notProcessedSettings.Where(r => r is not RoslynOptionEditorConfigSetting).ToList();

IReadOnlyCollection<ICodeStyleElement> elements = notProcessedRules
.OfType<RoslynSeverityEditorConfigRule>()
.Select(r => ParseRule(r, roslynOptionEditorConfigRules, roslynRules))
IReadOnlyCollection<ICodeStyleElement> ruleConfiguration = notProcessedSettings
.OfType<RoslynSeverityEditorConfigSetting>()
.Select(severitySetting => ParseRuleSettings(severitySetting, optionConfigurations, roslynRules))
.ToList();

notProcessedRules = notProcessedRules.Where(r => r is not RoslynSeverityEditorConfigRule).ToList();
notProcessedSettings = notProcessedSettings.Where(r => r is not RoslynSeverityEditorConfigSetting).ToList();

if (notProcessedRules.Any())
if (notProcessedSettings.Any())
{
string unsupportedTypes = notProcessedRules
string unsupportedTypes = notProcessedSettings
.Select(r => r.GetType())
.Distinct()
.ToSingleString();

throw new ConfiguinException($"Rule type is not supported: {unsupportedTypes}");
}

return new CodeStyleInfo(elements);
return new CodeStyle(ruleConfiguration);
}

private bool IsSupported(IEditorConfigRule rule)
private bool IsSupported(IEditorConfigSetting setting)
{
// TODO: support parsing for this rule
if (rule is RoslynSeverityEditorConfigRule severityEditorConfigRule
&& severityEditorConfigRule.RuleId.Equals(new RoslynRuleId(RoslynRuleType.StyleRule, 1006)))
// TODO: #35 support parsing for this rule
if (setting is RoslynSeverityEditorConfigSetting severityEditorConfigRule
&& severityEditorConfigRule.RuleId.Equals(RoslynRuleId.Parse("IDE1006")))
{
return false;
}

// TODO: Probably, most of this rules related to IDE1006
if (setting is CompositeRoslynOptionEditorConfigSetting)
return false;

// TODO: Maybe we need to support it in some way
if (setting is GeneralEditorConfigSetting)
return false;

return true;
}

// TODO: Rework naming
private RoslynOptionConfiguration ParseOption(RoslynOptionEditorConfigRule optionEditorConfigRule, IReadOnlyCollection<RoslynStyleRuleOption> optionsFromDocs)
private CodeStyleRoslynOptionConfiguration ParseOptionSettings(RoslynOptionEditorConfigSetting optionEditorConfigSetting, IReadOnlyCollection<RoslynStyleRuleOption> styleRuleOptions)
{
// TODO: check for duplicate?
RoslynStyleRuleOption? roslynStyleRuleOption = optionsFromDocs.FirstOrDefault(o => o.Name == optionEditorConfigRule.Key);
RoslynStyleRuleOption? roslynStyleRuleOption = styleRuleOptions.SingleOrDefault(o => o.Name == optionEditorConfigSetting.Key);

if (roslynStyleRuleOption is null)
throw new ConfiguinException($"Option {optionEditorConfigRule.Key} was not found in documentation");
throw new ConfiguinException($"Option {optionEditorConfigSetting.Key} was not found in documentation");

return new RoslynOptionConfiguration(roslynStyleRuleOption, optionEditorConfigRule.Value);
return new CodeStyleRoslynOptionConfiguration(roslynStyleRuleOption, optionEditorConfigSetting.Value);
}

private ICodeStyleElement ParseRule(
RoslynSeverityEditorConfigRule severityEditorConfigRule,
IReadOnlyCollection<RoslynOptionConfiguration> roslynOptionEditorConfigRules,
private ICodeStyleElement ParseRuleSettings(
RoslynSeverityEditorConfigSetting severityEditorConfigSetting,
IReadOnlyCollection<CodeStyleRoslynOptionConfiguration> optionConfigurations,
RoslynRules roslynRules)
{
RoslynStyleRule? roslynStyleRule = roslynRules.StyleRules.FirstOrDefault(s => s.RuleId.Equals(severityEditorConfigRule.RuleId));
RoslynStyleRule? roslynStyleRule = roslynRules.StyleRules.FirstOrDefault(s => s.RuleId.Equals(severityEditorConfigSetting.RuleId));
if (roslynStyleRule is not null)
{
var options = roslynStyleRule
.Options
.Select(o => GetOption(roslynOptionEditorConfigRules, o.Name))
.Select(o => GetOptionConfiguration(optionConfigurations, o.Name))
.ToList();

return new RoslynStyleRuleConfiguration(roslynStyleRule, severityEditorConfigRule.Severity, options);
return new CodeStyleRoslynStyleRuleConfiguration(roslynStyleRule, severityEditorConfigSetting.Severity, options);
}

RoslynQualityRule? roslynQualityRule = roslynRules.QualityRules.FirstOrDefault(q => q.RuleId.Equals(severityEditorConfigRule.RuleId));
RoslynQualityRule? roslynQualityRule = roslynRules.QualityRules.FirstOrDefault(q => q.RuleId.Equals(severityEditorConfigSetting.RuleId));
if (roslynQualityRule is not null)
{
return new RoslynQualityRuleConfiguration(roslynQualityRule, severityEditorConfigRule.Severity);
return new CodeStyleRoslynQualityRuleConfiguration(roslynQualityRule, severityEditorConfigSetting.Severity);
}

throw new ConfiguinException($"Rule with id {severityEditorConfigRule.RuleId} was not found");
throw new ConfiguinException($"Rule with id {severityEditorConfigSetting.RuleId} was not found");
}

private RoslynOptionConfiguration GetOption(
IReadOnlyCollection<RoslynOptionConfiguration> roslynOptionEditorConfigRules,
private CodeStyleRoslynOptionConfiguration GetOptionConfiguration(
IReadOnlyCollection<CodeStyleRoslynOptionConfiguration> optionConfigurations,
string name)
{
RoslynOptionConfiguration? option = roslynOptionEditorConfigRules.FirstOrDefault(o => o.Option.Name == name);
CodeStyleRoslynOptionConfiguration? option = optionConfigurations.FirstOrDefault(o => o.Option.Name == name);
if (option is null)
throw new ConfiguinException($"Option with name {name} was not found");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ namespace Kysect.Configuin.Core.CodeStyleGeneration;

public interface ICodeStyleGenerator
{
CodeStyleInfo Generate(EditorConfigRuleSet editorConfigRuleSet, RoslynRules roslynRules);
CodeStyle Generate(EditorConfigSettings editorConfigSettings, RoslynRules roslynRules);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ namespace Kysect.Configuin.Core.CodeStyleGeneration;

public interface ICodeStyleWriter
{
void Write(CodeStyleInfo codeStyleInfo);
void Write(CodeStyle codeStyle);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace Kysect.Configuin.Core.CodeStyleGeneration.Models;

public record CodeStyle(IReadOnlyCollection<ICodeStyleElement> Elements);

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using Kysect.Configuin.Core.RoslynRuleModels;

namespace Kysect.Configuin.Core.CodeStyleGeneration.Models;

public record CodeStyleRoslynOptionConfiguration(RoslynStyleRuleOption Option, string SelectedValue);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Kysect.Configuin.Core.RoslynRuleModels;

namespace Kysect.Configuin.Core.CodeStyleGeneration.Models;

public record CodeStyleRoslynQualityRuleConfiguration(
RoslynQualityRule Rule,
RoslynRuleSeverity Severity)
: ICodeStyleElement;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Kysect.Configuin.Core.RoslynRuleModels;

namespace Kysect.Configuin.Core.CodeStyleGeneration.Models;

public record CodeStyleRoslynStyleRuleConfiguration(
RoslynStyleRule Rule,
RoslynRuleSeverity Severity,
IReadOnlyCollection<CodeStyleRoslynOptionConfiguration> Options)
: ICodeStyleElement;

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 47b45c6

Please sign in to comment.