private static bool AnchorOperationCandidate(TokenPairWithOperations pair) { if (pair.LineOperation == null) { return(pair.TokenStream.GetTriviaData(pair.PairIndex).SecondTokenIsFirstTokenOnLine); } if (pair.LineOperation.Option == AdjustNewLinesOption.ForceLinesIfOnSingleLine) { return(!pair.TokenStream.TwoTokensOriginallyOnSameLine(pair.Token1, pair.Token2) && pair.TokenStream.GetTriviaData(pair.PairIndex).SecondTokenIsFirstTokenOnLine); } return(false); }
private static void ApplySpaceAndWrappingOperationsBody( FormattingContext context, TokenStream tokenStream, TokenPairWithOperations operation, OperationApplier applier, CancellationToken cancellationToken) { var token1 = operation.Token1; var token2 = operation.Token2; // check whether one of tokens is missing (which means syntax error exist around two tokens) // in error case, we leave code as user wrote if (token1.IsMissing || token2.IsMissing) { return; } var triviaInfo = tokenStream.GetTriviaData(operation.PairIndex); var spanBetweenTokens = TextSpan.FromBounds(token1.Span.End, token2.SpanStart); if (operation.LineOperation != null) { if (!context.IsWrappingSuppressed(spanBetweenTokens, triviaInfo.TreatAsElastic)) { // TODO : need to revisit later for the case where line and space operations // are conflicting each other by forcing new lines and removing new lines. // // if wrapping operation applied, no need to run any other operation if (applier.Apply(operation.LineOperation, operation.PairIndex, cancellationToken)) { return; } } } if (operation.SpaceOperation != null) { if (!context.IsSpacingSuppressed(spanBetweenTokens, triviaInfo.TreatAsElastic)) { applier.Apply(operation.SpaceOperation, operation.PairIndex); } } }
private TokenPairWithOperations[] CreateTokenOperation( TokenStream tokenStream, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); using (Logger.LogBlock(FunctionId.Formatting_CollectTokenOperation, cancellationToken)) { // pre-allocate list once. this is cheaper than re-adjusting list as items are added. var list = new TokenPairWithOperations[tokenStream.TokenCount - 1]; foreach (var pair in tokenStream.TokenIterator) { cancellationToken.ThrowIfCancellationRequested(); var spaceOperation = _formattingRules.GetAdjustSpacesOperation(pair.Item2, pair.Item3); var lineOperation = _formattingRules.GetAdjustNewLinesOperation(pair.Item2, pair.Item3); list[pair.Item1] = new TokenPairWithOperations(tokenStream, pair.Item1, spaceOperation, lineOperation); } return(list); } }