internal static bool TryCreateAddUsingResolutionParams(string fullyQualifiedName, DocumentUri uri, out string @namespace, out RazorCodeActionResolutionParams resolutionParams)
        {
            @namespace = GetNamespaceFromFQN(fullyQualifiedName);
            if (string.IsNullOrEmpty(@namespace))
            {
                @namespace       = null;
                resolutionParams = null;
                return(false);
            }

            var actionParams = new AddUsingsCodeActionParams
            {
                Uri       = uri,
                Namespace = @namespace
            };

            resolutionParams = new RazorCodeActionResolutionParams
            {
                Action   = LanguageServerConstants.CodeActions.AddUsing,
                Language = LanguageServerConstants.CodeActions.Languages.Razor,
                Data     = actionParams,
            };

            return(true);
        }
        internal static CodeAction CreateAddUsingCodeAction(string fullyQualifiedName, DocumentUri uri)
        {
            var @namespace = GetNamespaceFromFQN(fullyQualifiedName);

            if (string.IsNullOrEmpty(@namespace))
            {
                return(null);
            }

            var actionParams = new AddUsingsCodeActionParams
            {
                Uri       = uri,
                Namespace = @namespace
            };

            var resolutionParams = new RazorCodeActionResolutionParams
            {
                Action   = LanguageServerConstants.CodeActions.AddUsing,
                Language = LanguageServerConstants.CodeActions.Languages.Razor,
                Data     = actionParams,
            };

            return(new CodeAction()
            {
                Title = $"@using {@namespace}",
                Data = JToken.FromObject(resolutionParams)
            });
        }
        public async Task Handle_AddOneUsingToEmpty()
        {
            // Arrange
            var documentPath = "c:/Test.razor";
            var documentUri  = new Uri(documentPath);
            var contents     = string.Empty;
            var codeDocument = CreateCodeDocument(contents);

            var resolver     = new AddUsingsCodeActionResolver(LegacyDispatcher, CreateDocumentResolver(documentPath, codeDocument));
            var actionParams = new AddUsingsCodeActionParams
            {
                Uri       = documentUri,
                Namespace = "System"
            };
            var data = JObject.FromObject(actionParams);

            // Act
            var workspaceEdit = await resolver.ResolveAsync(data, default);

            // Assert
            Assert.NotNull(workspaceEdit);
            Assert.NotNull(workspaceEdit.DocumentChanges);
            Assert.Single(workspaceEdit.DocumentChanges);

            var documentChanges = workspaceEdit.DocumentChanges.ToArray();
            var addUsingsChange = documentChanges[0];

            Assert.True(addUsingsChange.IsTextDocumentEdit);
            Assert.Single(addUsingsChange.TextDocumentEdit.Edits);
            var firstEdit = addUsingsChange.TextDocumentEdit.Edits.First();

            Assert.Equal(0, firstEdit.Range.Start.Line);
            Assert.Equal($"@using System{Environment.NewLine}", firstEdit.NewText);
        }
Пример #4
0
        public async Task Handle_AddOneNonSystemUsingToSystemUsings()
        {
            // Arrange
            var documentPath = "c:/Test.razor";
            var documentUri  = new Uri(documentPath);
            var contents     = $"@using System{Environment.NewLine}@using System.Linq{Environment.NewLine}";
            var codeDocument = CreateCodeDocument(contents);

            var resolver     = new AddUsingsCodeActionResolver(new DefaultForegroundDispatcher(), CreateDocumentResolver(documentPath, codeDocument));
            var actionParams = new AddUsingsCodeActionParams
            {
                Uri       = documentUri,
                Namespace = "Microsoft.AspNetCore.Razor.Language"
            };
            var data = JObject.FromObject(actionParams);

            // Act
            var workspaceEdit = await resolver.ResolveAsync(data, default);

            // Assert
            Assert.NotNull(workspaceEdit);
            Assert.NotNull(workspaceEdit.DocumentChanges);
            Assert.Single(workspaceEdit.DocumentChanges);

            var documentChanges = workspaceEdit.DocumentChanges.ToArray();
            var addUsingsChange = documentChanges[0];

            Assert.True(addUsingsChange.IsTextDocumentEdit);
            var firstEdit = Assert.Single(addUsingsChange.TextDocumentEdit.Edits);

            Assert.Equal(2, firstEdit.Range.Start.Line);
            Assert.Equal($"@using Microsoft.AspNetCore.Razor.Language{Environment.NewLine}", firstEdit.NewText);
        }
Пример #5
0
        private void AddComponentAccessFromTag(RazorCodeActionContext context, MarkupStartTagSyntax startTag, List <CommandOrCodeAction> container)
        {
            var matching = FindMatchingTagHelpers(context, startTag);

            // For all the matches, add options for add @using and fully qualify
            foreach (var tagHelperPair in matching.Values)
            {
                if (tagHelperPair.FullyQualified is null)
                {
                    continue;
                }

                var fullyQualifiedComponentName = tagHelperPair.Short.Name;  // We assume .Name is the fully qualified component name
                DefaultRazorTagHelperBinderPhase.ComponentDirectiveVisitor.TrySplitNamespaceAndType(fullyQualifiedComponentName, out var namespaceSpan, out var _);
                var namespaceName = tagHelperPair.Short.Name.Substring(namespaceSpan.Start, namespaceSpan.Length);
                var actionParams  = new AddUsingsCodeActionParams
                {
                    Uri       = context.Request.TextDocument.Uri,
                    Namespace = namespaceName,
                };
                var data = JObject.FromObject(actionParams);

                var resolutionParams = new RazorCodeActionResolutionParams
                {
                    Action = LanguageServerConstants.CodeActions.AddUsing,
                    Data   = data,
                };
                var serializedParams = JToken.FromObject(resolutionParams);
                var arguments        = new JArray(serializedParams);

                // Insert @using
                container.Add(new CommandOrCodeAction(new Command
                {
                    Title     = $"@using {namespaceName}",
                    Name      = LanguageServerConstants.RazorCodeActionRunnerCommand,
                    Arguments = arguments,
                }));

                // Fully qualify
                container.Add(new CommandOrCodeAction(new CodeAction
                {
                    Title = $"{tagHelperPair.Short.Name}",
                    Edit  = CreateRenameTagEdit(context, startTag, tagHelperPair.Short.Name),
                }));
            }
        }
        public async Task Handle_AddOneUsingToPageDirective()
        {
            // Arrange
            var documentPath = "c:/Test.cshtml";
            var documentUri  = new Uri(documentPath);
            var contents     = $"@page{Environment.NewLine}@model IndexModel";

            var projectItem = new TestRazorProjectItem("c:/Test.cshtml", "c:/Test.cshtml", "Test.cshtml")
            {
                Content = contents
            };
            var projectEngine = RazorProjectEngine.Create(RazorConfiguration.Default, TestRazorProjectFileSystem.Empty, (builder) =>
            {
                PageDirective.Register(builder);
                ModelDirective.Register(builder);
            });
            var codeDocument = projectEngine.Process(projectItem);

            codeDocument.SetFileKind(FileKinds.Legacy);

            var resolver     = new AddUsingsCodeActionResolver(LegacyDispatcher, CreateDocumentResolver(documentPath, codeDocument));
            var actionParams = new AddUsingsCodeActionParams
            {
                Uri       = documentUri,
                Namespace = "System"
            };
            var data = JObject.FromObject(actionParams);

            // Act
            var workspaceEdit = await resolver.ResolveAsync(data, default);

            // Assert
            Assert.NotNull(workspaceEdit);
            Assert.NotNull(workspaceEdit.DocumentChanges);
            Assert.Single(workspaceEdit.DocumentChanges);

            var documentChanges = workspaceEdit.DocumentChanges.ToArray();
            var addUsingsChange = documentChanges[0];

            Assert.True(addUsingsChange.IsTextDocumentEdit);
            var firstEdit = Assert.Single(addUsingsChange.TextDocumentEdit.Edits);

            Assert.Equal(1, firstEdit.Range.Start.Line);
            Assert.Equal($"@using System{Environment.NewLine}", firstEdit.NewText);
        }