// Internal for testing internal IReadOnlyList <RazorCompletionItem> GetAttributeCompletions( string selectedAttributeName, string containingTagName, IEnumerable <string> attributes, TagHelperDocumentContext tagHelperDocumentContext) { var descriptorsForTag = _tagHelperFactsService.GetTagHelpersGivenTag(tagHelperDocumentContext, containingTagName, parentTag: null); if (descriptorsForTag.Count == 0) { // If the current tag has no possible descriptors then we can't have any directive attributes. return(Array.Empty <RazorCompletionItem>()); } // Attributes are case sensitive when matching var attributeCompletions = new Dictionary <string, (HashSet <AttributeDescriptionInfo>, HashSet <string>)>(StringComparer.Ordinal); for (var i = 0; i < descriptorsForTag.Count; i++) { var descriptor = descriptorsForTag[i]; foreach (var attributeDescriptor in descriptor.BoundAttributes) { if (!attributeDescriptor.IsDirectiveAttribute()) { // We don't care about non-directive attributes continue; } if (!TryAddCompletion(attributeDescriptor.Name, attributeDescriptor, descriptor) && attributeDescriptor.BoundAttributeParameters.Count > 0) { // This attribute has parameters and the base attribute name (@bind) is already satisfied. We need to check if there are any valid // parameters left to be provided, if so, we need to still represent the base attribute name in the completion list. for (var j = 0; j < attributeDescriptor.BoundAttributeParameters.Count; j++) { var parameterDescriptor = attributeDescriptor.BoundAttributeParameters[j]; if (!attributes.Any(name => TagHelperMatchingConventions.SatisfiesBoundAttributeWithParameter(name, attributeDescriptor, parameterDescriptor))) { // This bound attribute parameter has not had a completion entry added for it, re-represent the base attribute name in the completion list AddCompletion(attributeDescriptor.Name, attributeDescriptor, descriptor); break; } } } if (!string.IsNullOrEmpty(attributeDescriptor.IndexerNamePrefix)) { TryAddCompletion(attributeDescriptor.IndexerNamePrefix + "...", attributeDescriptor, descriptor); } } } var completionItems = new List <RazorCompletionItem>(); foreach (var completion in attributeCompletions) { var insertText = completion.Key; if (insertText.EndsWith("...")) { // Indexer attribute, we don't want to insert with the triple dot. insertText = insertText.Substring(0, insertText.Length - 3); } if (insertText.StartsWith("@")) { // Strip off the @ from the insertion text. This change is here to align the insertion text with the // completion hooks into VS and VSCode. Basically, completion triggers when `@` is typed so we don't // want to insert `@bind` because `@` already exists. insertText = insertText.Substring(1); } (var attributeDescriptionInfos, var commitCharacters) = completion.Value; var razorCompletionItem = new RazorCompletionItem( completion.Key, insertText, RazorCompletionItemKind.DirectiveAttribute, commitCharacters); var completionDescription = new AttributeCompletionDescription(attributeDescriptionInfos.ToArray()); razorCompletionItem.SetAttributeCompletionDescription(completionDescription); completionItems.Add(razorCompletionItem); } return(completionItems); bool TryAddCompletion(string attributeName, BoundAttributeDescriptor boundAttributeDescriptor, TagHelperDescriptor tagHelperDescriptor) { if (attributes.Any(name => string.Equals(name, attributeName, StringComparison.Ordinal)) && !string.Equals(selectedAttributeName, attributeName, StringComparison.Ordinal)) { // Attribute is already present on this element and it is not the selected attribute. // It shouldn't exist in the completion list. return(false); } AddCompletion(attributeName, boundAttributeDescriptor, tagHelperDescriptor); return(true); } void AddCompletion(string attributeName, BoundAttributeDescriptor boundAttributeDescriptor, TagHelperDescriptor tagHelperDescriptor) { if (!attributeCompletions.TryGetValue(attributeName, out var attributeDetails)) { attributeDetails = (new HashSet <AttributeDescriptionInfo>(), new HashSet <string>()); attributeCompletions[attributeName] = attributeDetails; } (var attributeDescriptionInfos, var commitCharacters) = attributeDetails; var descriptionInfo = new AttributeDescriptionInfo( boundAttributeDescriptor.TypeName, tagHelperDescriptor.GetTypeName(), boundAttributeDescriptor.GetPropertyName(), boundAttributeDescriptor.Documentation); attributeDescriptionInfos.Add(descriptionInfo); if (attributeName.EndsWith("...")) { // Indexer attribute, we don't want to commit with standard chars return; } commitCharacters.Add("="); if (tagHelperDescriptor.BoundAttributes.Any(b => b.IsBooleanProperty)) { commitCharacters.Add(" "); } if (tagHelperDescriptor.BoundAttributes.Any(b => b.BoundAttributeParameters.Count > 0)) { commitCharacters.Add(":"); } } }
// Internal for testing internal IReadOnlyList <RazorCompletionItem> GetAttributeParameterCompletions( string attributeName, string parameterName, string containingTagName, IEnumerable <string> attributes, TagHelperDocumentContext tagHelperDocumentContext) { var descriptorsForTag = _tagHelperFactsService.GetTagHelpersGivenTag(tagHelperDocumentContext, containingTagName, parentTag: null); if (descriptorsForTag.Count == 0) { // If the current tag has no possible descriptors then we can't have any additional attributes. return(Array.Empty <RazorCompletionItem>()); } // Attribute parameters are case sensitive when matching var attributeCompletions = new Dictionary <string, HashSet <AttributeDescriptionInfo> >(StringComparer.Ordinal); foreach (var descriptor in descriptorsForTag) { for (var i = 0; i < descriptor.BoundAttributes.Count; i++) { var attributeDescriptor = descriptor.BoundAttributes[i]; var boundAttributeParameters = attributeDescriptor.BoundAttributeParameters; if (boundAttributeParameters.Count == 0) { continue; } if (TagHelperMatchingConventions.CanSatisfyBoundAttribute(attributeName, attributeDescriptor)) { for (var j = 0; j < boundAttributeParameters.Count; j++) { var parameterDescriptor = boundAttributeParameters[j]; if (attributes.Any(name => TagHelperMatchingConventions.SatisfiesBoundAttributeWithParameter(name, attributeDescriptor, parameterDescriptor))) { // There's already an existing attribute that satisfies this parameter, don't show it in the completion list. continue; } if (!attributeCompletions.TryGetValue(parameterDescriptor.Name, out var attributeDescriptionInfos)) { attributeDescriptionInfos = new HashSet <AttributeDescriptionInfo>(); attributeCompletions[parameterDescriptor.Name] = attributeDescriptionInfos; } var descriptionInfo = new AttributeDescriptionInfo( parameterDescriptor.TypeName, descriptor.GetTypeName(), parameterDescriptor.GetPropertyName(), parameterDescriptor.Documentation); attributeDescriptionInfos.Add(descriptionInfo); } } } } var completionItems = new List <RazorCompletionItem>(); foreach (var completion in attributeCompletions) { if (string.Equals(completion.Key, parameterName, StringComparison.Ordinal)) { // This completion is identical to the selected parameter, don't provide for completions for what's already // present in the document. continue; } var razorCompletionItem = new RazorCompletionItem( completion.Key, completion.Key, RazorCompletionItemKind.DirectiveAttributeParameter); var completionDescription = new AttributeCompletionDescription(completion.Value.ToArray()); razorCompletionItem.SetAttributeCompletionDescription(completionDescription); completionItems.Add(razorCompletionItem); } return(completionItems); }