/// <summary> /// Matches a <c>VariableList</c> non-terminal. /// </summary> /// <returns><c>true</c> if the <c>VariableList</c> was matched successfully; otherwise, <c>false</c>.</returns> /// <remarks> /// The non-terminal can start with: <c>Identifier</c>. /// </remarks> protected virtual bool MatchVariableList(IAstNodeList variables) { Expression variable; if (!this.MatchVariable(out variable)) return false; variables.Add( variable ); while (this.TokenIs(this.LookAheadToken, LuatTokenId.Comma)) { if (!this.Match(LuatTokenId.Comma)) return false; if (!this.MatchVariable(out variable)) return false; variables.Add( variable ); } return true; }
/// <summary> /// Matches a <c>ExpressionList</c> non-terminal. /// </summary> /// <returns><c>true</c> if the <c>ExpressionList</c> was matched successfully; otherwise, <c>false</c>.</returns> /// <remarks> /// The non-terminal can start with: <c>Function</c>, <c>OpenParenthesis</c>, <c>OpenCurlyBrace</c>, <c>Identifier</c>, <c>TripleDot</c>, <c>Nil</c>, <c>False</c>, <c>True</c>, <c>Number</c>, <c>Subtraction</c>, <c>Not</c>, <c>Hash</c>, <c>String</c>. /// </remarks> protected virtual bool MatchExpressionList(IAstNodeList expressions) { Expression expression = null; if (!this.MatchExpression(out expression)) return false; expressions.Add( expression ); while (this.TokenIs(this.LookAheadToken, LuatTokenId.Comma)) { if (!this.Match(LuatTokenId.Comma)) return false; if (!this.MatchExpression(out expression)) return false; expressions.Add( expression ); } return true; }
/// <summary> /// Matches a <c>FieldList</c> non-terminal. /// </summary> /// <returns><c>true</c> if the <c>FieldList</c> was matched successfully; otherwise, <c>false</c>.</returns> /// <remarks> /// The non-terminal can start with: <c>Function</c>, <c>OpenParenthesis</c>, <c>OpenCurlyBrace</c>, <c>OpenSquareBracket</c>, <c>Identifier</c>, <c>TripleDot</c>, <c>Nil</c>, <c>False</c>, <c>True</c>, <c>Number</c>, <c>Subtraction</c>, <c>Not</c>, <c>Hash</c>, <c>String</c>. /// </remarks> protected virtual bool MatchFieldList(IAstNodeList fields) { Field field; if (!this.MatchField(out field)) return false; fields.Add( field ); if (((this.TokenIs(this.LookAheadToken, LuatTokenId.SemiColon)) || (this.TokenIs(this.LookAheadToken, LuatTokenId.Comma)))) { if (!this.MatchFieldSep()) return false; if (this.IsInMultiMatchSet(4, this.LookAheadToken)) { if (!this.MatchFieldList(fields)) return false; } } return true; }
/// <summary> /// Matches a <c>IdentifierList</c> non-terminal. /// </summary> /// <returns><c>true</c> if the <c>IdentifierList</c> was matched successfully; otherwise, <c>false</c>.</returns> /// <remarks> /// The non-terminal can start with: <c>Identifier</c>. /// </remarks> protected virtual bool MatchIdentifierList(IAstNodeList list) { Identifier identifier = null; if (!this.MatchIdentifier(out identifier)) return false; list.Add( identifier ); while (this.TokenIs(this.LookAheadToken, LuatTokenId.Comma)) { if (!this.Match(LuatTokenId.Comma)) return false; if (!this.MatchIdentifier(out identifier)) return false; list.Add( identifier ); } return true; }
///<summary> /// Formats the Statements in a method, and returns the formatted code as a string. ///</summary> ///<param name="statements">The statements in the method body.</param> ///<param name="comments">The comments in the parent .</param> ///<param name="startOffset">The start offset of the block we are processing.</param> ///<param name="length">The length of the block we are processing.</param> ///<returns>The formatted method body code.</returns> private string ProcessBodyTextInternal(IAstNodeList statements, IAstNodeList comments, int startOffset, int length) { return ProcessBodyTextInternal(statements, comments, startOffset, length, true); }
///<summary> /// Formats the Statements in a method, and returns the formatted code as a string. ///</summary> ///<param name="statements">The statements in the method body.</param> ///<param name="comments">The comments in the parent .</param> ///<param name="startOffset">The start offset of the block we are processing.</param> ///<param name="length">The length of the block we are processing.</param> ///<param name="addBraces">If AddBraces is true, the statement block created will have braces surrounding it. </param> ///<returns>The formatted method body code.</returns> private string ProcessBodyTextInternal(IAstNodeList statements, IAstNodeList comments, int startOffset, int length, bool addBraces) { StringBuilder sb = new StringBuilder(1000); if (statements.ParentNode.ParentNode is AccessorDeclaration && formatSettings.InlineSingleLineGettersAndSetters && comments.Count == 0 && statements.Count < 2) { if (addBraces) sb.Append("{ "); if (statements.Count == 1) { sb.Append(ProcessStatement(statements[0] as Statement)).Append(" "); } if (addBraces) sb.Append("}"); return sb.ToString(); } if (addBraces) sb.AppendLine(Indent + "{"); indentLevel++; SortedList<int, string> statementList = new SortedList<int, string>(); foreach (IAstNode child in statements) { Statement statement = (Statement)child; if (statement is EmptyStatement && formatSettings.OmitEmptyStatements) { continue; } statementList.Add(child.StartOffset, ProcessStatement(statement)); foreach (Comment c in statement.Comments) { if (statement.TextRange.Contains(c.StartOffset)) continue; if (c.StartOffset < statement.ParentNode.StartOffset) continue; // We handle the other comments inside ProcessStatement handledComments[c.StartOffset] = c; string commentText = c.Text; if (formatSettings.CommentLinesAsCommentBlock) { commentText = "/* " + commentText.Replace("/*", "").Replace("*/", "").Replace("//", "").Trim() + " */"; } statementList[c.StartOffset] = commentText; } } foreach (IAstNode child in comments) { if (child.StartOffset >= startOffset && child.StartOffset < startOffset + length) { string commentText = ((Comment)child).Text.Trim(); if (formatSettings.CommentLinesAsCommentBlock) { commentText = "/* " + commentText.Replace("/*", "").Replace("*/", "").Replace("//", "").Trim() + " */"; } statementList.Add(child.StartOffset, commentText); } } foreach (KeyValuePair<int, Region> r in unhandledRegions) { statementList.Add(r.Key, "#region " + r.Value.Name); statementList.Add(r.Value.EndOffset, "#endregion"); } KeyValuePair<int, string>? prevLine = null; foreach (KeyValuePair<int, string> line in statementList) { bool sameLine = false; int lineBreaksBetween = FindPreviousBlankLines(line.Key); if (prevLine.HasValue && lineBreaksBetween == 0) { sameLine = true; } if (prevLine.HasValue && formatSettings.MaintainWhitespace == false) sb.AppendLine().Append(Indent); else if (sameLine) sb.Append(" "); else if (prevLine.HasValue) // If this is not the first item, add a new line. { for (int i = 0; i < lineBreaksBetween; i++) sb.AppendLine(); sb.Append(Indent); } else sb.Append(Indent); sb.Append(line.Value.Trim()); prevLine = line; } if (statementList.Count > 0) sb.AppendLine(); indentLevel--; if (addBraces) sb.AppendLine(Indent + "}"); return sb.ToString(); }
private void Process_Switch_Sections(StringBuilder sb, IAstNodeList sections) { foreach (IAstNode sectionNode in sections) { SwitchSection section = (SwitchSection)sectionNode; foreach (IAstNode labelNode in section.Labels) { SwitchLabel label = (SwitchLabel)labelNode; if (label.Expression == null) { sb.Append(Environment.NewLine).Append(Indent).Append("default:"); } else { sb.Append(Environment.NewLine).Append(Indent).Append("case "); sb.Append(FormatExpression(label.Expression)); sb.Append(":"); } } sb.AppendLine(); string textInternal = ProcessBodyTextInternal(section.Statements, section.Comments, section.StartOffset, section.Length, false); textInternal = textInternal.TrimEnd(Environment.NewLine.ToCharArray()); // ProcessBodyTextInternal puts an extra newline on the end that we don't want. sb.Append(textInternal); } }
///<summary> /// Formats the Statements in a method, and returns the formatted code as a string. ///</summary> ///<param name="statements">The statements in the method body.</param> ///<param name="comments">The comments in the parent .</param> ///<param name="startOffset">The start offset of the block we are processing.</param> ///<param name="length">The length of the block we are processing.</param> ///<returns>The formatted method body code.</returns> public string ProcessBodyText(IAstNodeList statements, IAstNodeList comments, int startOffset, int length) { Reset(); return ProcessBodyTextInternal(statements, comments, startOffset, length); }
/// <summary> /// Processes a list of TypeReferences as generic type arguments. Appends <types> to the given StringBuilder. /// </summary> /// <param name="sb">The StringBuilder to append to.</param> /// <param name="genericArguments">The generic type nodes to append.</param> private void Process_Generic_Type_Arguments(StringBuilder sb, IAstNodeList genericArguments) { if (genericArguments.Count <= 0) return; sb.Append("<"); for (int i = 0; i < genericArguments.Count; i++) { sb.Append(FormatterUtility.FormatDataType(genericArguments[i] as TypeReference, document, controller)); if (genericArguments.Count != 1 && i != genericArguments.Count - 1) sb.Append(", "); } sb.Append(">"); }
private void Process_Catch_Clauses(StringBuilder sb, IAstNodeList clauses) { foreach (IAstNode catchNode in clauses) { CatchClause cat = (CatchClause)catchNode; sb.Append(Indent).Append("catch"); if (cat.VariableDeclarator != null) { sb.Append("("); sb.Append(FormatterUtility.FormatDataType(cat.VariableDeclarator.ReturnType, document, controller)); if (cat.VariableDeclarator.Name != null) sb.Append(" ").Append(cat.VariableDeclarator.Name.Text); sb.Append(")"); } Process_Block_Statement(sb, cat.BlockStatement); } }
private void ProcessBodyText(IBlockAstNode node, IBody function, IAstNodeList statements, IAstNodeList commentsWithinBlock) { formatter.UnhandledRegions.Clear(); foreach (KeyValuePair<int, Region> r in unhandledRegions) { if (r.Key > node.BlockStartOffset && r.Value.EndOffset < node.BlockEndOffset) { formatter.UnhandledRegions.Add(r.Key, r.Value); } } foreach (KeyValuePair<int, Region> r in formatter.UnhandledRegions) unhandledRegions.Remove(r.Key); function.BodyText = formatter.ProcessBodyText(statements, commentsWithinBlock, node.BlockStartOffset, node.BlockEndOffset - node.BlockStartOffset); }
private void SetupBaseConstruct(int startOffset, int endOffset, string documentation, BaseConstruct inter, IAstNodeList attributeSections) { inter.Index = startOffset; inter.TextRange = new TextRange(startOffset, endOffset); SetDocumentation(inter, documentation); AddToParentAndStack(inter); if (attributeSections != null) { SetAttributes(inter, attributeSections); } baseConstructs.Add(startOffset, inter); if (inter is UsingStatement) return; //inter.PreceedingBlankLines = FindPreviousBlankLines(startOffset); }
private void SetAttributes(BaseConstruct inter, IAstNodeList AttributeSections) { foreach (ActiproSoftware.SyntaxEditor.Addons.DotNet.Ast.AttributeSection attrSec in AttributeSections) { inter.AddAttributeSection(GetAttributeSectionFromNode(attrSec)); } }