private void AddComponentAccessFromTag(RazorCodeActionContext context, MarkupStartTagSyntax startTag, List <RazorCodeAction> 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 fullyQualifiedName = tagHelperPair._short.Name; // Insert @using if (AddUsingsCodeActionProviderHelper.TryCreateAddUsingResolutionParams(fullyQualifiedName, context.Request.TextDocument.Uri, out var @namespace, out var resolutionParams)) { var addUsingCodeAction = RazorCodeActionFactory.CreateAddComponentUsing(@namespace, resolutionParams); container.Add(addUsingCodeAction); } // Fully qualify var renameTagWorkspaceEdit = CreateRenameTagEdit(context, startTag, fullyQualifiedName); var fullyQualifiedCodeAction = RazorCodeActionFactory.CreateFullyQualifyComponent(fullyQualifiedName, renameTagWorkspaceEdit); container.Add(fullyQualifiedCodeAction); } }
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);
public void GetNamespaceFromFQN_Valid_ReturnsNamespace() { // Arrange var fqn = "Abc.Xyz"; // Act var namespaceName = AddUsingsCodeActionProviderHelper.GetNamespaceFromFQN(fqn); // Assert Assert.Equal("Abc", namespaceName); }
public void GetNamespaceFromFQN_Invalid_ReturnsEmpty() { // Arrange var fqn = "Abc"; // Act var namespaceName = AddUsingsCodeActionProviderHelper.GetNamespaceFromFQN(fqn); // Assert Assert.Empty(namespaceName); }
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); }
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); }
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); }
public void TryCreateAddUsingResolutionParams_CreatesResolutionParams() { // Arrange var fqn = "Abc.Xyz"; var docUri = DocumentUri.From("c:/path"); // Act var result = AddUsingsCodeActionProviderHelper.TryCreateAddUsingResolutionParams(fqn, docUri, out var @namespace, out var resolutionParams); // Assert Assert.True(result); Assert.Equal("Abc", @namespace); Assert.NotNull(resolutionParams); }
private static IEnumerable <RazorCodeAction> ProcessCodeActionsVSCode( RazorCodeActionContext context, IEnumerable <RazorCodeAction> codeActions) { var diagnostics = context.Request.Context.Diagnostics.Where(diagnostic => diagnostic.Severity == DiagnosticSeverity.Error && diagnostic.Code?.IsString == true && s_supportedDiagnostics.Any(d => diagnostic.Code.Value.String.Equals(d, StringComparison.OrdinalIgnoreCase))); if (diagnostics is null || !diagnostics.Any()) { return(Array.Empty <RazorCodeAction>()); } var typeAccessibilityCodeActions = new List <RazorCodeAction>(); foreach (var diagnostic in diagnostics) { // Corner case handling for diagnostics which (momentarily) linger after // @code block is cleared out if (diagnostic.Range.End.Line > context.SourceText.Lines.Count || diagnostic.Range.End.Character > context.SourceText.Lines[diagnostic.Range.End.Line].End) { continue; } var diagnosticSpan = diagnostic.Range.AsTextSpan(context.SourceText); // Based on how we compute `Range.AsTextSpan` it's possible to have a span // which goes beyond the end of the source text. Something likely changed // between the capturing of the diagnostic (by the platform) and the retrieval of the // document snapshot / source text. In such a case, we skip processing of the diagnostic. if (diagnosticSpan.End > context.SourceText.Length) { continue; } foreach (var codeAction in codeActions) { if (!codeAction.Name.Equals(LanguageServerConstants.CodeActions.CodeActionFromVSCode, StringComparison.Ordinal)) { continue; } var associatedValue = context.SourceText.GetSubTextString(diagnosticSpan); var fqn = string.Empty; // When there's only one FQN suggestion, code action title is of the form: // `System.Net.Dns` if (!codeAction.Title.Any(c => char.IsWhiteSpace(c)) && codeAction.Title.EndsWith(associatedValue, StringComparison.OrdinalIgnoreCase)) { fqn = codeAction.Title; } // When there are multiple FQN suggestions, the code action title is of the form: // `Fully qualify 'Dns' -> System.Net.Dns` else { var expectedCodeActionPrefix = $"Fully qualify '{associatedValue}' -> "; if (codeAction.Title.StartsWith(expectedCodeActionPrefix, StringComparison.OrdinalIgnoreCase)) { fqn = codeAction.Title.Substring(expectedCodeActionPrefix.Length); } } if (string.IsNullOrEmpty(fqn)) { continue; } var fqnCodeAction = CreateFQNCodeAction(context, diagnostic, codeAction, fqn); typeAccessibilityCodeActions.Add(fqnCodeAction); if (AddUsingsCodeActionProviderHelper.TryCreateAddUsingResolutionParams(fqn, context.Request.TextDocument.Uri, out var @namespace, out var resolutionParams)) { var addUsingCodeAction = RazorCodeActionFactory.CreateAddComponentUsing(@namespace, resolutionParams); typeAccessibilityCodeActions.Add(addUsingCodeAction); } } } return(typeAccessibilityCodeActions); }
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); } }