EditSession CreateEditorSession(TextEditor editor, DocumentContext ctx, int openingPosition, char openingBrace, CancellationToken cancellationToken)
        {
            switch (openingBrace)
            {
            case CurlyBrace.OpenCharacter:
                return(InterpolationCompletionSession.IsContext(editor, ctx, openingPosition, cancellationToken)
                                        ? (EditSession) new InterpolationCompletionSession()
                                                : new CurlyBraceCompletionSession(ctx));

            case DoubleQuote.OpenCharacter:
                return(InterpolatedStringCompletionSession.IsContext(editor, ctx, openingPosition, cancellationToken)
                                        ? (EditSession) new InterpolatedStringCompletionSession()
                                                : new StringLiteralCompletionSession(ctx));

            case Bracket.OpenCharacter: return(new BracketCompletionSession(ctx));

            case Parenthesis.OpenCharacter: return(new ParenthesisCompletionSession(ctx));

            case SingleQuote.OpenCharacter: return(new CharLiteralCompletionSession(ctx));

            case LessAndGreaterThan.OpenCharacter: return(new LessAndGreaterThanCompletionSession(ctx));
            }

            return(null);
        }
        protected virtual bool CheckCodeContext(TextEditor editor, DocumentContext ctx, int position, char openingBrace, CancellationToken cancellationToken)
        {
            // SPECIAL CASE: Allow in curly braces in string literals to support interpolated strings.
            if (openingBrace == CurlyBrace.OpenCharacter &&
                InterpolationCompletionSession.IsContext(editor, ctx, position, cancellationToken))
            {
                return(true);
            }

            if (openingBrace == DoubleQuote.OpenCharacter &&
                InterpolatedStringCompletionSession.IsContext(editor, ctx, position, cancellationToken))
            {
                return(true);
            }

            var analysisDoc = ctx.AnalysisDocument;

            if (analysisDoc == null)
            {
                return(false);
            }

            // check that the user is not typing in a string literal or comment
            var tree = analysisDoc.GetSyntaxTreeAsync(cancellationToken).Result;

            return(!tree.IsInNonUserCode(position, cancellationToken));
        }