private SyntaxToken AnnotationResolver(
                SyntaxNode node,
                TriviaLocation location,
                SyntaxAnnotation annotation,
                SyntaxNode callsite,
                MethodDeclarationSyntax method)
            {
                var token = node.GetAnnotatedNodesAndTokens(annotation).FirstOrDefault().AsToken();
                if (token.RawKind != 0)
                {
                    return token;
                }

                switch (location)
                {
                    case TriviaLocation.BeforeBeginningOfSpan:
                        return callsite.GetFirstToken(includeZeroWidth: true).GetPreviousToken(includeZeroWidth: true);
                    case TriviaLocation.AfterEndOfSpan:
                        return callsite.GetLastToken(includeZeroWidth: true).GetNextToken(includeZeroWidth: true);
                    case TriviaLocation.AfterBeginningOfSpan:
                        return method.Body.OpenBraceToken.GetNextToken(includeZeroWidth: true);
                    case TriviaLocation.BeforeEndOfSpan:
                        return method.Body.CloseBraceToken.GetPreviousToken(includeZeroWidth: true);
                }

                return Contract.FailWithReturn<SyntaxToken>("can't happen");
            }
Esempio n. 2
0
        public TreeData(SyntaxNode root)
        {
            Contract.ThrowIfNull(root);
            _root = root;

            _firstToken = _root.GetFirstToken(includeZeroWidth: true);
            _lastToken = _root.GetLastToken(includeZeroWidth: true);
        }
        private static Task<Document> GetTransformedDocumentAsync(Document document, SyntaxNode root, SyntaxNode node)
        {
            // The first token is the open parenthesis token. This token has all the inner trivia
            var firstToken = node.GetFirstToken();
            var lastToken = node.GetLastToken();

            var previousToken = firstToken.GetPreviousToken();

            // We want to keep all trivia. The easiest way to do that is by doing it manually
            var newSyntaxRoot = root.RemoveNode(node, SyntaxRemoveOptions.KeepNoTrivia);

            // The removing operation has not changed the location of the previous token
            var newPreviousToken = newSyntaxRoot.FindToken(previousToken.Span.Start);

            var newTrailingTrivia = newPreviousToken.TrailingTrivia.AddRange(firstToken.GetAllTrivia()).AddRange(lastToken.GetAllTrivia());

            newSyntaxRoot = newSyntaxRoot.ReplaceToken(newPreviousToken, newPreviousToken.WithTrailingTrivia(newTrailingTrivia));

            return Task.FromResult(document.WithSyntaxRoot(newSyntaxRoot));
        }
        private static SyntaxToken GetEndToken(SyntaxNode node)
        {
            var lastToken = node.GetLastToken(includeZeroWidth: true, includeSkipped: true);

            if (lastToken.IsMissing)
            {
                var nextToken = lastToken.GetNextToken(includeZeroWidth: true, includeSkipped: true);
                if (nextToken.RawKind != 0)
                {
                    return nextToken;
                }
            }

            return lastToken;
        }
            private static void AddIndentBlockOperations(List<IndentBlockOperation> list, SyntaxNode node)
            {
                // only add indent block operation if the base token is the first token on line
                var text = node.SyntaxTree.GetText();
                var baseToken = node.Parent.GetFirstToken(includeZeroWidth: true);

                list.Add(FormattingOperations.CreateRelativeIndentBlockOperation(
                    baseToken,
                    node.GetFirstToken(includeZeroWidth: true).GetNextToken(includeZeroWidth: true),
                    node.GetLastToken(includeZeroWidth: true).GetPreviousToken(includeZeroWidth: true),
                    indentationDelta: 1,
                    option: IndentBlockOption.RelativeToFirstTokenOnBaseTokenLine));
            }
Esempio n. 6
0
 // TODO What about lambdas?
 private static bool IsLastStatement(SyntaxNode node)
 {
     var nextToken = node.GetLastToken().GetNextToken();
     return nextToken.IsKind(SyntaxKind.CloseBraceToken) &&
         nextToken.Parent.IsKind(SyntaxKind.Block) &&
         IsFunction(nextToken.Parent.Parent);
 }
Esempio n. 7
0
        private void SuppressVariableDeclaration(List<SuppressOperation> list, SyntaxNode node, OptionSet optionSet)
        {
            if (node.IsKind(SyntaxKind.FieldDeclaration) || node.IsKind(SyntaxKind.EventDeclaration) ||
                node.IsKind(SyntaxKind.EventFieldDeclaration) || node.IsKind(SyntaxKind.LocalDeclarationStatement))
            {
                if (optionSet.GetOption(CSharpFormattingOptions.SpacesIgnoreAroundVariableDeclaration))
                {
                    var firstToken = node.GetFirstToken(includeZeroWidth: true);
                    var lastToken = node.GetLastToken(includeZeroWidth: true);

                    list.Add(FormattingOperations.CreateSuppressOperation(firstToken, lastToken, SuppressOption.NoSpacing));
                }
            }
        }
        private static IEnumerable<IndentBlockOperation> GetIndentBlockOperationsFromSmallestSpan(SyntaxNode root, List<IndentBlockOperation> list, int position)
        {
            var lastVisibleToken = default(SyntaxToken);
            var map = new HashSet<TextSpan>();

            // iterate backward
            for (int i = list.Count - 1; i >= 0; i--)
            {
                var operation = list[i];
                if (map.Contains(operation.TextSpan))
                {
                    // no duplicated one
                    continue;
                }

                map.Add(operation.TextSpan);

                // normal case. the operation contains the position
                if (operation.TextSpan.Contains(position))
                {
                    yield return operation;
                    continue;
                }

                // special case for empty span. in case of empty span, consider it
                // contains the position if start == position
                if (operation.TextSpan.IsEmpty && operation.TextSpan.Start == position)
                {
                    yield return operation;
                    continue;
                }

                var nextToken = operation.EndToken.GetNextToken(includeZeroWidth: true);

                // special case where position is same as end position of an operation and
                // its next token is missing token. in this case, we will consider current position 
                // to belong to current operation.
                // this can happen in malformed code where end of indentation is missing
                if (operation.TextSpan.End == position && nextToken.IsMissing)
                {
                    yield return operation;
                    continue;
                }

                // special case where position is same as end position of the operation and
                // its next token is right at the position
                if (operation.TextSpan.End == position && position == nextToken.SpanStart)
                {
                    yield return operation;
                    continue;
                }

                // special case for the end of the span == position
                // if position is at the end of the last token of the tree. consider the position
                // belongs to the operation
                if (root.FullSpan.End == position && operation.TextSpan.End == position)
                {
                    yield return operation;
                    continue;
                }

                // more expensive check
                lastVisibleToken = (lastVisibleToken.RawKind == 0) ? root.GetLastToken() : lastVisibleToken;
                if (lastVisibleToken.Span.End <= position && operation.TextSpan.End == position)
                {
                    yield return operation;
                    continue;
                }
            }
        }
Esempio n. 9
0
        private static bool IsFunctionLikeLastReturnStatement(SyntaxNode node)
        {
            if (!BranchKinds.Contains(node.Kind()))
            {
                return false;
            }

            if (!node.IsKind(SyntaxKind.ReturnStatement))
            {
                return false;
            }

            var blockType = node.Parent.GetType();
            if (!BlockTypes.Any(bType => bType.IsAssignableFrom(blockType)))
            {
                return false;
            }

            if (!IsFunctionLike(node.Parent))
            {
                return false;
            }

            var nextToken = node.GetLastToken().GetNextToken();
            var nextNode = nextToken.Parent;

            return nextToken.IsKind(SyntaxKind.EndKeyword) &&
                node.Parent == nextNode.Parent;
        }