Beispiel #1
0
        async Task <ISymbol> GetMemberToDocument(CancellationToken cancellationToken = default(CancellationToken))
        {
            var parsedDocument = DocumentContext.ParsedDocument;

            if (parsedDocument == null)
            {
                return(null);
            }

            var partialDoc = await CSharpCompletionTextEditorExtension.WithFrozenPartialSemanticsAsync(DocumentContext.AnalysisDocument, cancellationToken).ConfigureAwait(false);

            var semanticModel = await partialDoc.GetSemanticModelAsync();

            if (semanticModel == null)
            {
                return(null);
            }
            var caretOffset = Editor.CaretOffset;
            var offset      = caretOffset;
            var root        = semanticModel.SyntaxTree.GetRoot();

            while (offset < Editor.Length)
            {
                var node = root.FindNode(TextSpan.FromBounds(offset, offset));
                if (node == null || node.GetLastToken().SpanStart < caretOffset)
                {
                    offset++;
                    continue;
                }
                var fieldDeclarationSyntax = node as FieldDeclarationSyntax;
                if (fieldDeclarationSyntax != null)
                {
                    node = fieldDeclarationSyntax.Declaration.Variables.First();
                }

                var eventDeclaration = node as EventFieldDeclarationSyntax;
                if (eventDeclaration != null)
                {
                    node = eventDeclaration.Declaration.Variables.First();
                }

                if (node.Span.Contains(caretOffset))
                {
                    return(null);
                }

                var declaredSymbol = semanticModel.GetDeclaredSymbol(node);
                if (declaredSymbol != null)
                {
                    return(declaredSymbol);
                }
                offset = node.FullSpan.End + 1;
            }
            return(null);
        }
        public override async Task <BraceMatchingResult?> GetMatchingBracesAsync(IReadonlyTextDocument editor, DocumentContext context, int offset, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (context.ParsedDocument == null)
            {
                return(await fallback.GetMatchingBracesAsync(editor, context, offset, cancellationToken));
            }

            var analysisDocument = context.AnalysisDocument;

            if (analysisDocument == null)
            {
                return(null);
            }
            var partialDoc = await CSharpCompletionTextEditorExtension.WithFrozenPartialSemanticsAsync(analysisDocument, cancellationToken).ConfigureAwait(false);

            var root = await partialDoc.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            if (offset < 0 || root.Span.End <= offset)
            {
                return(null);
            }
            var token     = root.FindToken(offset);
            var tokenSpan = token.Span;

            if (offset < tokenSpan.Start || offset > tokenSpan.End)
            {
                return(null);
            }
            for (int i = 0; i < tokenPairs.Length / 2; i++)
            {
                var         open  = tokenPairs [i * 2];
                var         close = tokenPairs [i * 2 + 1];
                SyntaxToken match;
                if (token.IsKind(open))
                {
                    if (TryFindMatchingToken(token, out match, open, close))
                    {
                        return(new BraceMatchingResult(new TextSegment(tokenSpan.Start, tokenSpan.Length), new TextSegment(match.Span.Start, match.Span.Length), true));
                    }
                }
                else if (token.IsKind(close))
                {
                    if (TryFindMatchingToken(token, out match, open, close))
                    {
                        return(new BraceMatchingResult(new TextSegment(match.Span.Start, match.Span.Length), new TextSegment(tokenSpan.Start, tokenSpan.Length), false));
                    }
                }
            }

            if (token.IsKind(SyntaxKind.StringLiteralToken))
            {
                if (token.IsVerbatimStringLiteral())
                {
                    if (offset <= tokenSpan.Start)
                    {
                        return(new BraceMatchingResult(new TextSegment(tokenSpan.Start, 2), new TextSegment(tokenSpan.End - 1, 1), true));
                    }
                    if (offset >= tokenSpan.End - 1)
                    {
                        return(new BraceMatchingResult(new TextSegment(tokenSpan.Start, 2), new TextSegment(tokenSpan.End - 1, 1), false));
                    }
                }
                else
                {
                    if (offset <= tokenSpan.Start)
                    {
                        return(new BraceMatchingResult(new TextSegment(tokenSpan.Start, 1), new TextSegment(tokenSpan.End - 1, 1), true));
                    }
                    if (offset >= tokenSpan.End - 1)
                    {
                        return(new BraceMatchingResult(new TextSegment(tokenSpan.Start, 1), new TextSegment(tokenSpan.End - 1, 1), false));
                    }
                }
            }

            if (token.IsKind(SyntaxKind.CharacterLiteralToken))
            {
                if (offset <= tokenSpan.Start)
                {
                    return(new BraceMatchingResult(new TextSegment(tokenSpan.Start, 1), new TextSegment(tokenSpan.End - 1, 1), true));
                }
                if (offset >= tokenSpan.End - 1)
                {
                    return(new BraceMatchingResult(new TextSegment(tokenSpan.Start, 1), new TextSegment(tokenSpan.End - 1, 1), false));
                }
            }

            return(null);
        }
        async Task <ISymbol> GetMemberToDocument(CancellationToken cancellationToken = default(CancellationToken))
        {
            var parsedDocument = DocumentContext.ParsedDocument;

            if (parsedDocument == null)
            {
                return(null);
            }

            try {
                var analysisDoc = DocumentContext.AnalysisDocument;
                if (analysisDoc == null)
                {
                    return(null);
                }
                var partialDoc = await CSharpCompletionTextEditorExtension.WithFrozenPartialSemanticsAsync(analysisDoc, cancellationToken).ConfigureAwait(false);

                var semanticModel = await partialDoc.GetSemanticModelAsync();

                if (semanticModel == null)
                {
                    return(null);
                }
                var caretOffset  = Editor.CaretOffset;
                var offset       = caretOffset;
                var root         = semanticModel.SyntaxTree.GetRoot();
                var tokenAtCaret = root.FindTrivia(offset - 1, true);
                if (!tokenAtCaret.IsKind(SyntaxKind.SingleLineCommentTrivia))
                {
                    return(null);
                }
                while (offset < Editor.Length)
                {
                    var node = root.FindNode(TextSpan.FromBounds(offset, offset));

                    if (node == null || node.GetLastToken().SpanStart < caretOffset)
                    {
                        offset++;
                        continue;
                    }
                    var fieldDeclarationSyntax = node as FieldDeclarationSyntax;
                    if (fieldDeclarationSyntax != null)
                    {
                        node = fieldDeclarationSyntax.Declaration.Variables.First();
                    }

                    var eventDeclaration = node as EventFieldDeclarationSyntax;
                    if (eventDeclaration != null)
                    {
                        node = eventDeclaration.Declaration.Variables.First();
                    }

                    if (node.Span.Contains(caretOffset))
                    {
                        return(null);
                    }

                    var declaredSymbol = semanticModel.GetDeclaredSymbol(node);
                    if (declaredSymbol != null)
                    {
                        return(declaredSymbol);
                    }
                    offset = node.FullSpan.End + 1;
                }
                return(null);
            } catch (Exception e) {
                LoggingService.LogError("Error wihle getting member to document.", e);
                return(null);
            }
        }