private static void AppendRefactoringSamples(MarkdownBuilder mb, RefactoringDescriptor refactoring) { if (refactoring.Samples.Count > 0) { AppendSamples(mb, refactoring.Samples, Header4("Before"), Header4("After")); } else if (refactoring.Images.Count > 0) { bool isFirst = true; foreach (ImageDescriptor image in refactoring.Images) { if (!isFirst) { mb.AppendLine(); } AppendRefactoringImage(mb, refactoring, image.Name); isFirst = false; } mb.AppendLine(); } else { AppendRefactoringImage(mb, refactoring, refactoring.Identifier); mb.AppendLine(); } }
public static string CreateRefactoringMarkDown(RefactoringDescriptor refactoring) { using (var sw = new StringWriter()) { sw.WriteLine($"## {refactoring.Title}"); sw.WriteLine(""); sw.WriteLine("Property | Value"); sw.WriteLine("--- | --- "); sw.WriteLine($"Id | {refactoring.Id}"); sw.WriteLine($"Title | {refactoring.Title.EscapeMarkdown()}"); sw.WriteLine($"Syntax | {string.Join(", ", refactoring.Syntaxes.Select(f => f.Name.EscapeMarkdown()))}"); if (!string.IsNullOrEmpty(refactoring.Scope)) { sw.WriteLine($"Scope | {refactoring.Scope.EscapeMarkdown()}"); } sw.WriteLine($"Enabled by Default | {((refactoring.IsEnabledByDefault) ? "yes" : "no")}"); sw.WriteLine(""); sw.WriteLine("### Usage"); sw.WriteLine(""); WriteRefactoringImages(sw, refactoring); sw.WriteLine(""); sw.WriteLine("[full list of refactorings](Refactorings.md)"); return(sw.ToString()); } }
private static void Main(string[] args) { if (args == null || args.Length == 0) { #if DEBUG args = new string[] { @"..\..\..\.." }; #else args = new string[] { Environment.CurrentDirectory }; #endif } string dirPath = args[0]; RefactoringDescriptor[] refactorings = RefactoringDescriptor .LoadFromFile(Path.Combine(dirPath, @"Refactorings\Refactorings.xml")) .ToArray(); var writer = new CodeFileWriter(); var refactoringIdentifiersGenerator = new RefactoringIdentifiersGenerator(); writer.SaveCode( Path.Combine(dirPath, @"Refactorings\RefactoringIdentifiers.Generated.cs"), refactoringIdentifiersGenerator.Generate(refactorings)); var optionsPagePropertiesGenerator = new OptionsPagePropertiesGenerator(); writer.SaveCode( Path.Combine(dirPath, @"VisualStudio.Core\RefactoringsOptionsPage.Generated.cs"), optionsPagePropertiesGenerator.Generate(refactorings)); #if DEBUG Console.WriteLine("DONE"); Console.ReadKey(); #endif }
public bool IsAnyRefactoringEnabled(RefactoringDescriptor refactoring1, RefactoringDescriptor refactoring2, RefactoringDescriptor refactoring3, RefactoringDescriptor refactoring4) { return(IsRefactoringEnabled(refactoring1) || IsRefactoringEnabled(refactoring2) || IsRefactoringEnabled(refactoring3) || IsRefactoringEnabled(refactoring4)); }
public static string CreateRefactoringMarkdown(RefactoringDescriptor refactoring) { var sb = new StringBuilder(); sb.AppendHeader2($"{refactoring.Title}"); sb.AppendLine(); sb.AppendTableHeader("Property", "Value"); sb.AppendTableRow("Id", refactoring.Id); sb.AppendTableRow("Title", refactoring.Title); sb.AppendTableRow("Syntax", string.Join(", ", refactoring.Syntaxes.Select(f => f.Name))); if (!string.IsNullOrEmpty(refactoring.Span)) { sb.AppendTableRow("Span", refactoring.Span); } sb.AppendTableRow("Enabled by Default", GetBooleanAsText(refactoring.IsEnabledByDefault)); sb.AppendLine(); sb.AppendHeader3("Usage"); sb.AppendLine(); WriteRefactoringSamples(sb, refactoring); sb.AppendLine(); sb.AppendLink("full list of refactorings", "Refactorings.md"); return(sb.ToString()); }
private static void WriteRefactoringSamples(StringBuilder sb, RefactoringDescriptor refactoring) { if (refactoring.Samples.Count > 0) { WriteSamples(sb, refactoring.Samples, new MarkdownHeader("Before", 4), new MarkdownHeader("After", 4)); } else if (refactoring.Images.Count > 0) { bool isFirst = true; foreach (ImageDescriptor image in refactoring.Images) { if (!isFirst) { sb.AppendLine(); } AppendRefactoringImage(sb, refactoring, image.Name); isFirst = false; } } else { AppendRefactoringImage(sb, refactoring, refactoring.Identifier); } }
public static CompilationUnitSyntax Generate(RefactoringDescriptor refactoring, string className) { string s = _sourceTemplate .Replace("$ClassName$", className) .Replace("$Identifier$", refactoring.Identifier); return(ParseCompilationUnit(s)); }
public override int GetHashCode(RefactoringDescriptor obj) { if (obj.Id == null) { return(0); } return(StringComparer.Ordinal.GetHashCode(obj.Id)); }
private static void WriteRefactoring(XmlWriter writer, RefactoringDescriptor refactoring) { string href = $"http://github.com/JosefPihrt/Roslynator/blob/master/docs/refactorings/{refactoring.Id}.md"; writer.WriteStartElement("li"); writer.WriteStartElement("a"); writer.WriteAttributeString("href", href); writer.WriteString(refactoring.Title); writer.WriteEndElement(); writer.WriteEndElement(); }
private void WriteRefactoring(XmlWriter writer, RefactoringDescriptor refactoring) { string href = "http://github.com/JosefPihrt/Roslynator/blob/master/source/Refactorings/Refactorings.md#" + refactoring.GetGitHubHref(); writer.WriteStartElement("li"); writer.WriteStartElement("a"); writer.WriteAttributeString("href", href); writer.WriteString(refactoring.Title); writer.WriteEndElement(); writer.WriteEndElement(); }
public void RegisterRefactoring( string title, Func <CancellationToken, Task <Solution> > createChangedSolution, RefactoringDescriptor descriptor, string additionalEquivalenceKey1 = null, string additionalEquivalenceKey2 = null) { RegisterRefactoring( title, createChangedSolution, EquivalenceKey.Create(descriptor, additionalEquivalenceKey1, additionalEquivalenceKey2)); }
public void ComputeRefactoring(RefactoringContext context, RefactoringDescriptor descriptor) { int index = FindNode(context.Span); if (index > 0 && !List[index - 1].IsMissing) { context.RegisterRefactoring( GetTitle(), ct => RefactorAsync(context.Document, index, ct), descriptor); } }
private PropertyDeclarationSyntax CreateRefactoringProperty(RefactoringDescriptor refactoring) { return(PropertyDeclaration(BoolType(), refactoring.Identifier) .WithAttributeLists( AttributeList(Attribute("Category", IdentifierName("RefactoringCategory"))), AttributeList(Attribute("DisplayName", StringLiteralExpression(refactoring.Title))), AttributeList(Attribute("Description", StringLiteralExpression(CreateDescription(refactoring)))), AttributeList(Attribute("TypeConverter", TypeOfExpression(IdentifierName("EnabledDisabledConverter"))))) .WithModifiers(ModifierFactory.Public()) .WithAccessorList( AccessorList( AutoImplementedGetter(), AutoImplementedSetter()))); }
public static string CreateRefactoringMarkdown(RefactoringDescriptor refactoring) { var format = new MarkdownFormat(tableOptions: MarkdownFormat.Default.TableOptions | TableOptions.FormatContent); MDocument document = Document( Heading2(refactoring.Title), Table(TableRow("Property", "Value"), TableRow("Id", refactoring.Id), TableRow("Title", refactoring.Title), TableRow("Syntax", string.Join(", ", refactoring.Syntaxes.Select(f => f.Name))), (!string.IsNullOrEmpty(refactoring.Span)) ? TableRow("Span", refactoring.Span) : null, TableRow("Enabled by Default", CheckboxOrHyphen(refactoring.IsEnabledByDefault))), Heading3("Usage"), GetRefactoringSamples(refactoring), Link("full list of refactorings", "Refactorings.md")); return(document.ToString(format)); }
public override bool Equals(RefactoringDescriptor x, RefactoringDescriptor y) { if (object.ReferenceEquals(x, y)) { return(true); } if (x.Id == null) { return(false); } if (y.Id == null) { return(false); } return(string.Equals(x.Id, y.Id, StringComparison.Ordinal)); }
public override int Compare(RefactoringDescriptor x, RefactoringDescriptor y) { if (object.ReferenceEquals(x, y)) { return(0); } if (x.Id == null) { return(-1); } if (y.Id == null) { return(1); } return(string.CompareOrdinal(x.Id, y.Id)); }
public bool IsRefactoringEnabled(RefactoringDescriptor refactoring) { if (_configOptions.TryGetValue(refactoring.OptionKey, out string value) && bool.TryParse(value, out bool enabled)) { return(enabled); } if (_globalIsEnabled != null) { return(_globalIsEnabled.Value); } if (CodeAnalysisConfig.Instance.Refactorings.TryGetValue(refactoring.OptionKey, out enabled)) { return(enabled); } return(CodeAnalysisConfig.Instance.RefactoringsEnabled ?? true); }
private static string CreateDescription(RefactoringDescriptor refactoring) { string s = ""; if (refactoring.Syntaxes.Count > 0) { s = "Syntax: " + string.Join(", ", refactoring.Syntaxes.Select(f => f.Name)); } if (!string.IsNullOrEmpty(refactoring.Scope)) { if (!string.IsNullOrEmpty(s)) { s += "\r\n"; } s += "Scope: " + refactoring.Scope; } return(s); }
private static void WriteRefactoringImages(StringWriter sw, RefactoringDescriptor refactoring) { if (refactoring.Images.Count > 0) { bool isFirst = true; foreach (ImageDescriptor image in refactoring.Images) { if (!isFirst) { sw.WriteLine(); } sw.WriteLine(CreateImageMarkDown(refactoring, image.Name)); isFirst = false; } } else { sw.WriteLine(CreateImageMarkDown(refactoring, refactoring.Identifier)); } }
public static string CreateRefactoringMarkdown(RefactoringDescriptor refactoring) { var mb = new MarkdownBuilder(new MarkdownSettings(tableFormatting: TableFormatting.All)); mb.AppendHeader2(refactoring.Title); Table("Property", "Value") .AddRow("Id", refactoring.Id) .AddRow("Title", refactoring.Title) .AddRow("Syntax", string.Join(", ", refactoring.Syntaxes.Select(f => f.Name))) .AddRowIf(!string.IsNullOrEmpty(refactoring.Span), "Span", refactoring.Span) .AddRow("Enabled by Default", RawText(GetBooleanAsText(refactoring.IsEnabledByDefault))) .AppendTo(mb); mb.AppendHeader3("Usage"); AppendRefactoringSamples(mb, refactoring); mb.AppendLink("full list of refactorings", "Refactorings.md"); return(mb.ToString()); }
private static IEnumerable <object> GetRefactoringSamples(RefactoringDescriptor refactoring) { if (refactoring.Samples.Count > 0) { foreach (MElement element in GetSamples(refactoring.Samples, Heading4("Before"), Heading4("After"))) { yield return(element); } } else if (refactoring.Images.Count > 0) { bool isFirst = true; foreach (ImageDescriptor image in refactoring.Images) { if (!isFirst) { yield return(NewLine); } yield return(RefactoringImage(refactoring, image.Name)); yield return(NewLine); isFirst = false; } yield return(NewLine); } else { yield return(RefactoringImage(refactoring, refactoring.Identifier)); yield return(NewLine); yield return(NewLine); } }
public static void ComputeRefactoring(RefactoringContext context, MemberDeclarationListSelection selectedMembers) { if (selectedMembers.Count <= 1) { return; } TextSpan span = context.Span; RefactoringDescriptor refactoring = GetRefactoringDescriptor(selectedMembers.First()); if (refactoring.Id == null) { return; } if (!RefactoringDescriptorComparer.Id.Equals(refactoring, GetRefactoringDescriptor(selectedMembers.Last()))) { return; } if (RefactoringDescriptorComparer.Id.Equals(refactoring, RefactoringDescriptors.ConvertBlockBodyToExpressionBody) && context.IsRefactoringEnabled(RefactoringDescriptors.ConvertBlockBodyToExpressionBody)) { context.RegisterRefactoring( ConvertBlockBodyToExpressionBodyRefactoring.Title, ct => ConvertBlockBodyToExpressionBodyRefactoring.RefactorAsync(context.Document, selectedMembers, ct), RefactoringDescriptors.ConvertBlockBodyToExpressionBody); } else if (context.IsRefactoringEnabled(RefactoringDescriptors.ConvertExpressionBodyToBlockBody)) { context.RegisterRefactoring( ConvertExpressionBodyToBlockBodyRefactoring.Title, ct => ConvertExpressionBodyToBlockBodyRefactoring.RefactorAsync(context.Document, selectedMembers, ct), RefactoringDescriptors.ConvertExpressionBodyToBlockBody); } }
private static bool ShouldGenerateProperty(RefactoringDescriptor refactoring) { return(int.Parse(refactoring.Id.Substring(2)) <= 177); }
private static MImage RefactoringImage(RefactoringDescriptor refactoring, string fileName) { return(Image(refactoring.Title, $"../../images/refactorings/{fileName}.png")); }
private static string CreateImageMarkDown(RefactoringDescriptor refactoring, string fileName) { return($"![{refactoring.Title.EscapeMarkdown()}](../../images/refactorings/{fileName.EscapeMarkdown()}.png)"); }
private static void AppendRefactoringImage(MarkdownBuilder mb, RefactoringDescriptor refactoring, string fileName) { mb.AppendImage(refactoring.Title, $"../../images/refactorings/{fileName}.png"); mb.AppendLine(); }
public static async Task ComputeRefactoringAsync(RefactoringContext context, StatementListSelection selectedStatements) { if (context.IsRefactoringEnabled(RefactoringDescriptors.WrapStatementsInUsingStatement)) { var refactoring = new WrapStatementsInUsingStatementRefactoring(); await refactoring.ComputeRefactoringAsync(context, selectedStatements).ConfigureAwait(false); } if (context.IsRefactoringEnabled(RefactoringDescriptors.UseObjectInitializer)) { await UseObjectInitializerRefactoring.ComputeRefactoringsAsync(context, selectedStatements).ConfigureAwait(false); } if (context.IsRefactoringEnabled(RefactoringDescriptors.MergeIfStatements)) { MergeIfStatementsRefactoring.ComputeRefactorings(context, selectedStatements); } if (context.IsRefactoringEnabled(RefactoringDescriptors.ConvertStatementsToIfElse)) { ConvertStatementsToIfElseRefactoring.ComputeRefactorings(context, selectedStatements); } if (context.IsAnyRefactoringEnabled( RefactoringDescriptors.UseCoalesceExpressionInsteadOfIf, RefactoringDescriptors.ConvertIfToConditionalExpression, RefactoringDescriptors.SimplifyIf)) { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); IfAnalysisOptions options = IfStatementRefactoring.GetIfAnalysisOptions(context); foreach (IfAnalysis analysis in IfAnalysis.Analyze(selectedStatements, options, semanticModel, context.CancellationToken)) { RefactoringDescriptor refactoring = IfStatementRefactoring.GetRefactoringDescriptor(analysis); if (context.IsRefactoringEnabled(refactoring)) { context.RegisterRefactoring( analysis.Title, ct => IfRefactoring.RefactorAsync(context.Document, analysis, ct), refactoring); } } } if (context.IsRefactoringEnabled(RefactoringDescriptors.MergeLocalDeclarations)) { await MergeLocalDeclarationsRefactoring.ComputeRefactoringsAsync(context, selectedStatements).ConfigureAwait(false); } if (context.IsRefactoringEnabled(RefactoringDescriptors.RemoveUnnecessaryAssignment)) { RemoveUnnecessaryAssignmentRefactoring.ComputeRefactorings(context, selectedStatements); } if (context.IsRefactoringEnabled(RefactoringDescriptors.CheckExpressionForNull)) { await CheckExpressionForNullRefactoring.ComputeRefactoringAsync(context, selectedStatements).ConfigureAwait(false); } if (context.IsRefactoringEnabled(RefactoringDescriptors.ConvertWhileToFor)) { await ConvertWhileToForRefactoring.ComputeRefactoringAsync(context, selectedStatements).ConfigureAwait(false); } if (context.IsRefactoringEnabled(RefactoringDescriptors.WrapStatementsInCondition)) { context.RegisterRefactoring( WrapInIfStatementRefactoring.Title, ct => WrapInIfStatementRefactoring.Instance.RefactorAsync(context.Document, selectedStatements, ct), RefactoringDescriptors.WrapStatementsInCondition); } if (context.IsRefactoringEnabled(RefactoringDescriptors.WrapLinesInTryCatch)) { context.RegisterRefactoring( WrapLinesInTryCatchRefactoring.Title, ct => WrapLinesInTryCatchRefactoring.Instance.RefactorAsync(context.Document, selectedStatements, ct), RefactoringDescriptors.WrapLinesInTryCatch); } }
private static StringBuilder AppendRefactoringImage(StringBuilder sb, RefactoringDescriptor refactoring, string fileName) { return(sb .AppendImage(refactoring.Title, $"../../images/refactorings/{fileName}.png") .AppendLine()); }
private static void Main(string[] args) { if (args == null || args.Length == 0) { #if DEBUG args = new string[] { @"..\..\..\.." }; #else args = new string[] { Environment.CurrentDirectory }; #endif } string dirPath = args[0]; SortRefactoringsInFile(Path.Combine(dirPath, @"Refactorings\Refactorings.xml")); RefactoringDescriptor[] refactorings = RefactoringDescriptor .LoadFromFile(Path.Combine(dirPath, @"Refactorings\Refactorings.xml")) .OrderBy(f => f.Identifier, StringComparer.InvariantCulture) .ToArray(); Console.WriteLine($"number of refactorings: {refactorings.Length}"); AnalyzerDescriptor[] analyzers = AnalyzerDescriptor .LoadFromFile(Path.Combine(dirPath, @"Analyzers\Analyzers.xml")) .OrderBy(f => f.Id, StringComparer.InvariantCulture) .ToArray(); Console.WriteLine($"number of analyzers: {analyzers.Length}"); SaveFile( Path.Combine(dirPath, @"Analyzers\Analyzers.xml"), CreateAnalyzersXml(analyzers)); var htmlGenerator = new HtmlGenerator(); SaveFile( Path.Combine(dirPath, @"VisualStudio\description.txt"), File.ReadAllText(@"..\text\RoslynatorDescription.txt", Encoding.UTF8) + htmlGenerator.CreateRoslynatorDescription(analyzers, refactorings)); SaveFile( Path.Combine(dirPath, @"VisualStudio.Refactorings\description.txt"), File.ReadAllText(@"..\text\RoslynatorRefactoringsDescription.txt", Encoding.UTF8) + htmlGenerator.CreateRoslynatorRefactoringsDescription(refactorings)); var markdownGenerator = new MarkdownGenerator(); SaveFile( Path.Combine(Path.GetDirectoryName(dirPath), @"README.md"), markdownGenerator.CreateReadMeMarkDown(analyzers, refactorings)); foreach (string imagePath in MarkdownGenerator.FindMissingImages(refactorings, Path.Combine(Path.GetDirectoryName(dirPath), @"images\refactorings"))) { Console.WriteLine($"missing image: {imagePath}"); } SaveFile( Path.Combine(dirPath, @"Refactorings\Refactorings.md"), markdownGenerator.CreateRefactoringsMarkDown(refactorings)); SaveFile( Path.Combine(dirPath, @"Refactorings\README.md"), markdownGenerator.CreateRefactoringsReadMe(refactorings)); SaveFile( Path.Combine(dirPath, @"Analyzers\README.md"), markdownGenerator.CreateAnalyzersReadMe(analyzers)); SaveFile( Path.Combine(dirPath, @"Analyzers\AnalyzersByCategory.md"), markdownGenerator.CreateAnalyzersByCategoryMarkDown(analyzers)); #if DEBUG Console.WriteLine("DONE"); Console.ReadKey(); #endif }
private static void Main(string[] args) { string rootPath = args[0]; var metadata = new RoslynatorMetadata(rootPath); ImmutableArray <AnalyzerDescriptor> analyzers = metadata.Analyzers; ImmutableArray <RefactoringDescriptor> refactorings = metadata.Refactorings; ImmutableArray <CompilerDiagnosticDescriptor> compilerDiagnostics = metadata.CompilerDiagnostics; foreach (string id in args.Skip(1)) { if (_analyzerIdRegex.IsMatch(id)) { AnalyzerDescriptor analyzer = analyzers.FirstOrDefault(f => string.Equals(f.Id, id, StringComparison.OrdinalIgnoreCase)); if (analyzer == null) { Console.WriteLine($"Analyzer '{id}' not found"); continue; } string className = $"{analyzer.Id}{analyzer.Identifier}Tests"; WriteCompilationUnit( $@"Tests\Analyzers.Tests\{className}.cs", AnalyzerTestGenerator.Generate(analyzer, className), autoGenerated: false, normalizeWhitespace: false, fileMustExist: false, overwrite: false); } else if (_refactoringIdRegex.IsMatch(id)) { RefactoringDescriptor refactoring = refactorings.FirstOrDefault(f => string.Equals(f.Id, id, StringComparison.OrdinalIgnoreCase)); if (refactoring == null) { Console.WriteLine($"Refactoring '{id}' not found"); continue; } string className = $"{refactoring.Id}{refactoring.Identifier}Tests"; WriteCompilationUnit( $@"Tests\Refactorings.Tests\{className}.cs", RefactoringTestGenerator.Generate(refactoring, className), autoGenerated: false, normalizeWhitespace: false, fileMustExist: false, overwrite: false); } else if (_codeFixIdRegex.IsMatch(id)) { CompilerDiagnosticDescriptor compilerDiagnostic = compilerDiagnostics.FirstOrDefault(f => string.Equals(f.Id, id, StringComparison.OrdinalIgnoreCase)); if (compilerDiagnostic == null) { Console.WriteLine($"Compiler diagnostic '{id}' not found"); continue; } string className = $"{compilerDiagnostic.Id}{compilerDiagnostic.Identifier}Tests"; WriteCompilationUnit( $@"Tests\CodeFixes.Tests\{className}.cs", CodeFixTestGenerator.Generate(compilerDiagnostic, className), autoGenerated: false, normalizeWhitespace: false, fileMustExist: false, overwrite: false); } else { Console.WriteLine($"Id '{id}' not recognized"); } } void WriteCompilationUnit( string path, CompilationUnitSyntax compilationUnit, bool autoGenerated = true, bool normalizeWhitespace = true, bool fileMustExist = true, bool overwrite = true) { CodeGenerationHelpers.WriteCompilationUnit( path: Path.Combine(rootPath, path), compilationUnit: compilationUnit, banner: CodeGenerationHelpers.CopyrightBanner, autoGenerated: autoGenerated, normalizeWhitespace: normalizeWhitespace, fileMustExist: fileMustExist, overwrite: overwrite); } }