private void CloseFormattingScope() { Debug.Assert(_tokens.CurrentToken.TokenType == RTokenType.CloseCurlyBrace); _tb.SoftLineBreak(); _tb.CloseIndentLevel(); _tb.SoftIndent(); if (_formattingScopes.Count > 1) { if (_formattingScopes.Peek().CloseBracePosition == _tokens.Position) { FormattingScope scope = _formattingScopes.Pop(); scope.Close(); } } AppendToken(leadingSpace: false, trailingSpace: false); if (SuppressLineBreakCount == 0 && !_tokens.IsEndOfStream()) { // We insert line break after } unless next token is comma // (scope is in the argument list) or a closing brace // (last parameter in a function or indexer) or it is followed by 'else' // so 'else' does not get separated from 'if'. if (!KeepCurlyAndElseTogether()) { if (!IsClosingToken(_tokens.CurrentToken) && !IsInArguments()) { _tb.SoftLineBreak(); } } } }
public void TryCloseScope(int tokenIndex) { if (_formattingScopes.Count > 1) { if (_formattingScopes.Peek().CloseCurlyBraceTokenIndex == tokenIndex) { FormattingScope scope = _formattingScopes.Pop(); scope.Dispose(); } } }
private void CloseFormattingScope() { Debug.Assert(_tokens.CurrentToken.TokenType == RTokenType.CloseCurlyBrace); if (!SingleLineScope) { _tb.SoftLineBreak(); _tb.CloseIndentLevel(); _tb.SoftIndent(); } var leadingSpace = SingleLineScope && _tokens.PreviousToken.TokenType != RTokenType.CloseCurlyBrace; if (_formattingScopes.Count > 1) { if (_formattingScopes.Peek().CloseBracePosition == _tokens.Position) { FormattingScope scope = _formattingScopes.Pop(); scope.Dispose(); } } AppendToken(leadingSpace: leadingSpace, trailingSpace: false); bool singleLineScopeJustClosed = false; if (_tokens.CurrentToken.Start >= _singleLineScopeEnd) { _singleLineScopeEnd = -1; singleLineScopeJustClosed = true; } if (SuppressLineBreakCount == 0 && !_tokens.IsEndOfStream()) { // We insert line break after } unless // a) Next token is comma (scope is in the argument list) or // b) Next token is a closing brace (last parameter in a function or indexer) or // c) Next token is by 'else' (so 'else' does not get separated from 'if') or // d) We are in a single-line scope sequence like if() {{ }} if (!KeepCurlyAndElseTogether()) { if (singleLineScopeJustClosed && !IsClosingToken(_tokens.CurrentToken) && !IsInArguments()) { SoftLineBreak(); } } } }
/// <summary> /// Opens new formatting scope. Scope is opened when either /// code discovers open curly brace or when line break /// suppression is on. /// </summary> private void OpenFormattingScope() { Debug.Assert(_tokens.CurrentToken.TokenType == RTokenType.OpenCurlyBrace); if (IsInArguments()) { // Inside argument lists indentation rules are different // so open new set of options and indentation FormattingScope formattingScope = new FormattingScope(_tb, _tokens, _options); _formattingScopes.Push(formattingScope); } // If scope is empty, make it { } unless there is a line break already in it if (_tokens.NextToken.TokenType == RTokenType.CloseCurlyBrace && _textProvider.IsWhiteSpaceOnlyRange(_tokens.CurrentToken.End, _tokens.NextToken.Start)) { AppendToken(leadingSpace: _tokens.PreviousToken.TokenType == RTokenType.CloseBrace, trailingSpace: true); AppendToken(leadingSpace: false, trailingSpace: false); return; } else { // Determine if the scope is a single line like if(TRUE) { 1 } and keep it // on a single line unless there are line breaks in it. Continue without // breaks until the scope ends. Includes multiple nested scopes such as {{{ 1 }}}. // We continue on the outer scope boundaries. if (_singleLineScopeEnd < 0) { _singleLineScopeEnd = GetSingleLineScopeEnd(); } if (_options.BracesOnNewLine && !SingleLineScope) { SoftLineBreak(); } else if (!IsOpenBraceToken(_tokens.PreviousToken.TokenType)) { _tb.AppendSpace(); } } AppendToken(leadingSpace: false, trailingSpace: false); if (!SingleLineScope) { _tb.SoftLineBreak(); _tb.NewIndentLevel(); } }
/// <summary> /// Opens new formatting scope. Scope is opened when either /// code discovers open curly brace or when line break /// suppression is on. /// </summary> private void OpenFormattingScope() { Debug.Assert(_tokens.CurrentToken.TokenType == RTokenType.OpenCurlyBrace); if (IsInArguments()) { FormattingScope formattingScope = new FormattingScope(_indentBuilder); if (formattingScope.Open(_textProvider, _tokens, _options)) { _formattingScopes.Push(formattingScope); } } // If scope is empty, make it { } unless there is a line break already in it if (_tokens.NextToken.TokenType == RTokenType.CloseCurlyBrace && IsWhiteSpaceOnlyRange(_tokens.CurrentToken.End, _tokens.NextToken.Start)) { AppendToken(leadingSpace: _tokens.PreviousToken.TokenType == RTokenType.CloseBrace, trailingSpace: true); AppendToken(leadingSpace: false, trailingSpace: false); return; } else { if (_options.BracesOnNewLine) { _tb.SoftLineBreak(); } else if (!IsOpenBraceToken(_tokens.PreviousToken.TokenType)) { _tb.AppendSpace(); } } AppendToken(leadingSpace: false, trailingSpace: false); _tb.SoftLineBreak(); _tb.NewIndentLevel(); }
public void OpenScope(FormattingScope scope) => _formattingScopes.Push(scope);
/// <summary> /// Opens new formatting scope. Scope is opened when either /// code discovers open curly brace or when line break /// suppression is on. /// </summary> private void OpenFormattingScope() { Debug.Assert(_tokens.CurrentToken.TokenType == RTokenType.OpenCurlyBrace); if (IsInArguments()) { FormattingScope formattingScope = new FormattingScope(_indentBuilder); if (formattingScope.Open(_textProvider, _tokens, _options)) { _formattingScopes.Push(formattingScope); } } // If scope is empty, make it { } unless there is a line break already in it if (_tokens.NextToken.TokenType == RTokenType.CloseCurlyBrace && IsWhiteSpaceOnlyRange(_tokens.CurrentToken.End, _tokens.NextToken.Start)) { AppendToken(leadingSpace: _tokens.PreviousToken.TokenType == RTokenType.CloseBrace, trailingSpace: true); AppendToken(leadingSpace: false, trailingSpace: false); return; } else { if (_options.BracesOnNewLine) { _tb.SoftLineBreak(); } else if (!IsOpenBraceToken(_tokens.PreviousToken.TokenType)) { _tb.AppendSpace(); } } AppendToken(leadingSpace: false, trailingSpace: false); _tb.SoftLineBreak(); _tb.NewIndentLevel(); }
public void OpenScope(FormattingScope scope) => _formattingScopes.Push(scope);