Esempio n. 1
0
        private static CodeAction SingleAction(Document document, CodeRefactoringProvider refactoring, int position)
        {
            var context = new RefactoringContext(document, refactoring, position);
            var token   = context.SyntaxRoot.FindToken(position);

            refactoring.ComputeRefactoringsAsync(context.CreateRefactoringContext(token.Span)).GetAwaiter().GetResult();
            return(context.Actions.Count switch
            {
                0 => SingleAction(context, token.Parent),
                1 => context.Actions[0],
                _ => throw new NotSupportedException("More than one action available. Currently not supporting invoking action by index. We should add support for it."),
            });
Esempio n. 2
0
        private static IReadOnlyList <CodeAction> CodeActions(CodeRefactoringProvider refactoring, string testCode, int position, IEnumerable <MetadataReference> metadataReferences)
        {
            var sln = CodeFactory.CreateSolutionWithOneProject(
                testCode,
                CodeFactory.DefaultCompilationOptions(Array.Empty <DiagnosticAnalyzer>()),
                metadataReferences);
            var document = sln.Projects.Single().Documents.Single();
            var context  = new RefactoringContext(document, refactoring, position);
            var token    = context.SyntaxRoot.FindToken(position);

            refactoring.ComputeRefactoringsAsync(context.CreateRefactoringContext(token.Span)).GetAwaiter().GetResult();

            var node = token.Parent;

            while (node != null &&
                   node.SpanStart == position)
            {
                refactoring.ComputeRefactoringsAsync(context.CreateRefactoringContext(node.Span)).GetAwaiter().GetResult();
                node = node.Parent;
            }

            return(context.Actions);
        }
Esempio n. 3
0
        private static CodeAction SingleAction(RefactoringContext context, SyntaxNode node)
        {
            if (node != null &&
                node.SpanStart == context.Position)
            {
                context.Refactoring.ComputeRefactoringsAsync(context.CreateRefactoringContext(node.Span)).GetAwaiter().GetResult();
                switch (context.Actions.Count)
                {
                case 0:
                    return(SingleAction(context, node.Parent));

                case 1:
                    return(context.Actions[0]);

                default:
                    throw new NotSupportedException("More than one action available. Currently not supporting invoking action by index. We should add support for it.");
                }
            }

            throw new InvalidOperationException("The refactoring did not register any refactorings at the position.");
        }