コード例 #1
0
        private static IEnumerable <RazorCodeAction> ProcessCodeActionsVS(
            RazorCodeActionContext context,
            IEnumerable <RazorCodeAction> codeActions)
        {
            var typeAccessibilityCodeActions = new List <RazorCodeAction>(1);

            foreach (var codeAction in codeActions)
            {
                if (codeAction.Name.Equals(RazorPredefinedCodeFixProviderNames.FullyQualify, StringComparison.Ordinal))
                {
                    string action;

                    if (!TryGetOwner(context, out var owner))
                    {
                        // Failed to locate a valid owner for the light bulb
                        continue;
                    }
                    else if (IsSingleLineDirectiveNode(owner))
                    {
                        // Don't support single line directives
                        continue;
                    }
                    else if (IsExplicitExpressionNode(owner))
                    {
                        // Don't support explicit expressions
                        continue;
                    }
                    else if (IsImplicitExpressionNode(owner))
                    {
                        action = LanguageServerConstants.CodeActions.UnformattedRemap;
                    }
                    else
                    {
                        // All other scenarios we support default formatted code action behavior
                        action = LanguageServerConstants.CodeActions.Default;
                    }

                    typeAccessibilityCodeActions.Add(codeAction.WrapResolvableCSharpCodeAction(context, action));
                }
                // For add using suggestions, the code action title is of the form:
                // `using System.Net;`
                else if (codeAction.Name.Equals(RazorPredefinedCodeFixProviderNames.AddImport, StringComparison.Ordinal) &&
                         AddUsingsCodeActionProviderHelper.TryExtractNamespace(codeAction.Title, out var @namespace))
                {
                    var newCodeAction = codeAction with {
                        Title = $"@using {@namespace}"
                    };
                    typeAccessibilityCodeActions.Add(newCodeAction.WrapResolvableCSharpCodeAction(context, LanguageServerConstants.CodeActions.AddUsing));
                }
                // Not a type accessibility code action
                else
                {
                    continue;
                }
            }

            return(typeAccessibilityCodeActions);
コード例 #2
0
        public void TryExtractNamespace_WithStatic_ReturnsTruue()
        {
            // Arrange
            var csharpAddUsing = "using static X.Y.Z;";

            // Act
            var res = AddUsingsCodeActionProviderHelper.TryExtractNamespace(csharpAddUsing, out var @namespace);

            // Assert
            Assert.True(res);
            Assert.Equal("static X.Y.Z", @namespace);
        }
コード例 #3
0
        public void TryExtractNamespace_ReturnsTrue()
        {
            // Arrange
            var csharpAddUsing = "using Abc.Xyz;";

            // Act
            var res = AddUsingsCodeActionProviderHelper.TryExtractNamespace(csharpAddUsing, out var @namespace);

            // Assert
            Assert.True(res);
            Assert.Equal("Abc.Xyz", @namespace);
        }
コード例 #4
0
        public void TryExtractNamespace_Invalid_ReturnsFalse()
        {
            // Arrange
            var csharpAddUsing = "Abc.Xyz;";

            // Act
            var res = AddUsingsCodeActionProviderHelper.TryExtractNamespace(csharpAddUsing, out var @namespace);

            // Assert
            Assert.False(res);
            Assert.Empty(@namespace);
        }
コード例 #5
0
        public async override Task <CodeAction?> ResolveAsync(
            CSharpCodeActionParams csharpParams,
            CodeAction codeAction,
            CancellationToken cancellationToken)
        {
            if (csharpParams is null)
            {
                throw new ArgumentNullException(nameof(csharpParams));
            }

            if (codeAction is null)
            {
                throw new ArgumentNullException(nameof(codeAction));
            }

            cancellationToken.ThrowIfCancellationRequested();

            if (!AddUsingsCodeActionProviderHelper.TryExtractNamespace(codeAction.Title, out var @namespace))
            {
                // Invalid text edit, missing namespace
                return(codeAction);
            }

            var documentSnapshot = await _projectSnapshotManagerDispatcher.RunOnDispatcherThreadAsync(() =>
            {
                _documentResolver.TryResolveDocument(csharpParams.RazorFileUri.GetAbsoluteOrUNCPath(), out var documentSnapshot);
                return(documentSnapshot);
            }, cancellationToken).ConfigureAwait(false);

            if (documentSnapshot is null)
            {
                return(codeAction);
            }

            var text = await documentSnapshot.GetTextAsync().ConfigureAwait(false);

            if (text is null)
            {
                return(null);
            }

            var codeDocument = await documentSnapshot.GetGeneratedOutputAsync().ConfigureAwait(false);

            if (codeDocument.IsUnsupported())
            {
                return(null);
            }

            var documentVersion = await _projectSnapshotManagerDispatcher.RunOnDispatcherThreadAsync(() =>
            {
                _documentVersionCache.TryGetDocumentVersion(documentSnapshot, out var version);
                return(version);
            }, cancellationToken).ConfigureAwait(false);

            var codeDocumentIdentifier = new OptionalVersionedTextDocumentIdentifier()
            {
                Uri     = csharpParams.RazorFileUri,
                Version = documentVersion.Value
            };

            var edit = AddUsingsCodeActionResolver.CreateAddUsingWorkspaceEdit(@namespace, codeDocument, codeDocumentIdentifier);

            codeAction = codeAction with {
                Edit = edit
            };

            return(codeAction);
        }
    }