/// <summary> /// Reformats a block node. If the block has braces then the formatting will be updated /// appropriately, if forceNewLine /// </summary> /// <param name="block"></param> /// <param name="braceOnNewline"></param> private void WalkBlock(Block block) { Debug.Assert(block == null || block.Braces != BraceState.None); if (block != null && block.Braces != BraceState.None) { bool isMultiLine = ContainsLineFeed(block.GetStartIndex(_tree.LocationResolver), block.GetEndIndex(_tree.LocationResolver)); bool isComment = FollowedBySingleLineComment(block.GetStartIndex(_tree.LocationResolver), false); if (block.Count > 0 && isMultiLine && !isComment) { // multiline block statement, make sure the 1st statement // starts on a new line. If this is a comment it can stay on the same line as the brace EnsureNewLineFollowing(block.GetStartIndex(_tree.LocationResolver) + "{".Length); } var parent = block.Parent; Indent(); WalkStatements(block, block.Statements, isMultiLine); Dedent(); if (block.Braces == BraceState.StartAndEnd) { if (isMultiLine) { EnsureNewLinePreceeding(block.GetEndIndex(_tree.LocationResolver) - 1); } else { ReplacePreceedingWhiteSpaceMaybeMultiline(block.GetEndIndex(_tree.LocationResolver) - 1); } } } }
private void WalkFlowControlBlockWithOptionalParens(Block block, int startIndex, int previousExpressionEnd, bool inParens) { if (block != null) { if (block.Braces == BraceState.None) { // braces are omitted... // if (foo) // blah // vs // if (foo) blah // TODO: https://nodejstools.codeplex.com/workitem/1475 causes a failure as our end < start. Parser is failing and until fixed will cause a failure here. bool multiLine = ContainsLineFeed(previousExpressionEnd, block.GetStartIndex(_tree.LocationResolver)); if (multiLine) { // remove trailing whitespace at the end of this line bool followedBySingleLineComment; int startOfWhiteSpace, whiteSpaceCount; ParseEndOfLine(previousExpressionEnd, inParens, out followedBySingleLineComment, out startOfWhiteSpace, out whiteSpaceCount); if (startOfWhiteSpace != -1) { _edits.Add(new Edit(startOfWhiteSpace, whiteSpaceCount, "")); } Indent(); } WalkStatements(startIndex, block.Statements, false); if (multiLine) { Dedent(); } } else { var blockStart = block.GetStartIndex(_tree.LocationResolver); if (_code[blockStart] == '{') { ReplacePreceedingIncludingNewLines(blockStart, GetFlowControlBraceInsertion(previousExpressionEnd, false, blockStart)); } else { ReplacePreceedingIncludingNewLines(blockStart, GetFlowControlBraceInsertion(previousExpressionEnd, false, -1)); } WalkBlock(block); } } }