public static string WriteBlock(BlockSyntax block, bool writeBraces = true, int indent = 0) { var writer = new TempWriter(); writer.Indent = indent; if (writeBraces) { writer.OpenBrace(); } //writer.Indent++; foreach (var statement in block.Statements) { // writer.WriteIndent(); Write(writer, statement); } TriviaProcessor.ProcessTrivias(writer, block.DescendantTrivia()); // writer.Indent--; if (writeBraces) { writer.CloseBrace(); } return(writer.ToString()); }
private static void AnalyzeExpression(SyntaxNodeAnalysisContext context, BlockSyntax block, ExpressionSyntax expression) { if (block.DescendantTrivia().All(f => f.IsWhitespaceOrEndOfLineTrivia()) && expression.IsSingleLine()) { ReportDiagnostic(context, block, expression); } }
/// <summary> /// Extracts the block comments using the specified body /// </summary> /// <param name="body">The body</param> /// <returns>An enumerable of string</returns> public static IEnumerable <string> ExtractBlockComments(BlockSyntax?body) => body? .DescendantTrivia() .Where(trivia => trivia.Kind() == SyntaxKind.SingleLineCommentTrivia) .Select(commentTrivia => commentTrivia .ToFullString() .Replace("//", string.Empty) .Trim()) ?? new List <string>();
public BlockStatement ParseBlockStatement(BlockSyntax syntax, SemanticModel semanticModel) { // ライン取得 var blockLineSpan = syntax.SyntaxTree.GetLineSpan(syntax.Span); List <Statement> statements = new List <Statement>(); List <FileLinePositionSpan> statementSpans = new List <FileLinePositionSpan>(); // 中身 foreach (var statement in syntax.Statements) { var statementLineSpan = statement.SyntaxTree.GetLineSpan(statement.Span); var result = ParseStatement(statement, semanticModel); if (result != null) { result.StartingLine = statementLineSpan.StartLinePosition.Line - blockLineSpan.StartLinePosition.Line; result.EndingLine = statementLineSpan.EndLinePosition.Line - blockLineSpan.StartLinePosition.Line; statementSpans.Add(statementLineSpan); statements.Add(result); } } // コメント foreach (var t in syntax.DescendantTrivia()) { if (t.IsKind(SyntaxKind.SingleLineCommentTrivia)) { var commentLineSpan = t.SyntaxTree.GetLineSpan(t.Span); // ステートメント内は含まない if (statementSpans.Any(_ => _.StartLinePosition.Line <= commentLineSpan.StartLinePosition.Line && commentLineSpan.EndLinePosition.Line <= _.EndLinePosition.Line)) { continue; } var result = new CommentStatement(); result.Text = t.ToString(); result.Text = result.Text.Substring(2).Trim(); result.StartingLine = commentLineSpan.StartLinePosition.Line - blockLineSpan.StartLinePosition.Line; result.EndingLine = commentLineSpan.EndLinePosition.Line - blockLineSpan.StartLinePosition.Line; statements.Add(result); } } statements = statements.OrderBy(_ => _.StartingLine).ToList(); var bs = new BlockStatement(); bs.Statements = statements.ToArray(); return(bs); }
private static bool AnalyzeBlock(SyntaxNodeAnalysisContext context, BlockSyntax block, bool checkTrivia = true) { if (block == null) { return(false); } SyntaxList <StatementSyntax> statements = block.Statements; if (statements.Count == 0) { return(false); } var returnStatement = statements[0] as ReturnStatementSyntax; if (returnStatement == null) { return(false); } if (returnStatement.Expression == null) { return(false); } if (checkTrivia && block .DescendantTrivia(descendIntoTrivia: true) .Any(f => !f.IsWhitespaceOrEndOfLine())) { return(false); } if (!returnStatement.IsSingleline()) { return(false); } context.ReportDiagnostic( DiagnosticDescriptors.UseExpressionBodiedMember, block.GetLocation()); DiagnosticHelper.FadeOutToken(context, returnStatement.ReturnKeyword, DiagnosticDescriptors.UseExpressionBodiedMemberFadeOut); DiagnosticHelper.FadeOutBraces(context, block, DiagnosticDescriptors.UseExpressionBodiedMemberFadeOut); return(true); }
private int GetLinesOfComment(BlockSyntax blockSyntax) { IEnumerable <SyntaxTrivia> result = blockSyntax.DescendantTrivia(null, false); result = result.Where((SyntaxTrivia node) => { return(node.IsKind(Microsoft.CodeAnalysis.CSharp.SyntaxKind.MultiLineCommentTrivia) || node.IsKind(Microsoft.CodeAnalysis.CSharp.SyntaxKind.SingleLineCommentTrivia)); }); int lines = 0; foreach (SyntaxTrivia syntaxTrivia in result) { FileLinePositionSpan span = syntaxTrivia.GetLocation().GetLineSpan(); lines += span.EndLinePosition.Line - span.StartLinePosition.Line + 1; } return(lines); }
private static AccessorListSyntax CreateAccessorList(MethodDeclarationSyntax method) { BlockSyntax body = method.Body; if (body != null) { SyntaxList <StatementSyntax> statements = body.Statements; bool singleline = statements.Count == 1 && body.DescendantTrivia(body.Span).All(f => f.IsWhitespaceOrEndOfLineTrivia()) && statements[0].IsSingleLine(); return(CreateAccessorList(Block(body.Statements), singleline) .WithOpenBraceToken(body.OpenBraceToken) .WithCloseBraceToken(body.CloseBraceToken)); } return(CreateAccessorList(Block(), singleline: true)); }
public static void Analyze(SyntaxNodeAnalysisContext context, BlockSyntax block) { SyntaxList <StatementSyntax> statements = block.Statements; if (!statements.Any() && !(block.Parent is AccessorDeclarationSyntax)) { int startLine = block.OpenBraceToken.GetSpanStartLine(); int endLine = block.CloseBraceToken.GetSpanEndLine(); if ((endLine - startLine) != 1 && block .DescendantTrivia(block.Span) .All(f => f.IsWhitespaceOrEndOfLineTrivia())) { context.ReportDiagnostic(DiagnosticDescriptors.FormatEmptyBlock, block); } } }
public static void WriteBlock(ScalaWriter writer, BlockSyntax block, bool writeBraces = true) { if (writeBraces) { writer.WriteOpenBrace(); } foreach (var statement in block.Statements) { Write(writer, statement); } TriviaProcessor.ProcessTrivias(writer, block.DescendantTrivia()); if (writeBraces) { writer.WriteCloseBrace(); } }
public static void WriteBlock(OutputWriter writer, BlockSyntax block, bool writeBraces = true) { if (writeBraces) { writer.OpenBrace(); } //writer.Indent++; foreach (var statement in block.Statements) { // writer.WriteIndent(); Write(writer, statement); } TriviaProcessor.ProcessTrivias(writer, block.DescendantTrivia()); // writer.Indent--; if (writeBraces) { writer.CloseBrace(); } }
public static void WriteBlock(OutputWriter writer, BlockSyntax block, bool writeBraces = true) { if (writeBraces) writer.OpenBrace(); //writer.Indent++; foreach (var statement in block.Statements) { // writer.WriteIndent(); Write(writer, statement); } TriviaProcessor.ProcessTrivias(writer, block.DescendantTrivia()); // writer.Indent--; if (writeBraces) writer.CloseBrace(); }
public static string WriteBlock(BlockSyntax block, bool writeBraces = true, int indent=0) { var writer = new TempWriter(); writer.Indent = indent; if (writeBraces) writer.OpenBrace(); //writer.Indent++; foreach (var statement in block.Statements) { // writer.WriteIndent(); Write(writer, statement); } TriviaProcessor.ProcessTrivias(writer, block.DescendantTrivia()); // writer.Indent--; if (writeBraces) writer.CloseBrace(); return writer.ToString(); }