private ICSharpArgument FindInvalidExceptionArgument(IInvocationExpression invocationExpression, ICSharpArgument templateArgument, IDeclaredType exceptionType) { var templateArgumentIndex = templateArgument.IndexOf(); foreach (var argument in invocationExpression.ArgumentList.Arguments) { var argumentType = argument.Value?.Type(); if (!(argumentType is IDeclaredType declaredType)) { continue; } if (!declaredType.IsSubtypeOf(exceptionType)) { continue; } if (templateArgumentIndex > argument.IndexOf()) { return(null); } return(argument); } return(null); }
private static void HighlightByArgument( HighlightingsConsumer consumer, ICSharpArgument selectedArgument, ICSharpArgument templateArgument, MessageTemplate messageTemplate) { var argumentIndex = selectedArgument.IndexOf() - templateArgument.IndexOf() - 1; var namedProperties = messageTemplate.NamedProperties; var positionalProperties = messageTemplate.PositionalProperties; if (namedProperties != null && argumentIndex < namedProperties.Length) { var property = namedProperties[argumentIndex]; consumer.ConsumeHighlighting( GeneralHighlightingAttributeIds.USAGE_OF_ELEMENT_UNDER_CURSOR, templateArgument.GetTokenInformation(property).DocumentRange); } else if (positionalProperties != null) { foreach (var property in positionalProperties) { if (!property.TryGetPositionalValue(out int position)) { continue; } if (position != argumentIndex) { continue; } consumer.ConsumeHighlighting( GeneralHighlightingAttributeIds.USAGE_OF_ELEMENT_UNDER_CURSOR, templateArgument.GetTokenInformation(property).DocumentRange); } } consumer.ConsumeHighlighting( GeneralHighlightingAttributeIds.USAGE_OF_ELEMENT_UNDER_CURSOR, selectedArgument.GetDocumentRange()); }
private static void HighlightUnusedArguments( IInvocationExpression element, IHighlightingConsumer consumer, ICSharpArgument templateArgument, MessageTemplate messageTemplate) { var templateArgumentIndex = templateArgument.IndexOf(); foreach (var argument in element.ArgumentList.Arguments) { var argumentIndex = argument.IndexOf(); if (argumentIndex <= templateArgumentIndex) { continue; } var argumentPosition = argumentIndex - templateArgumentIndex; if (messageTemplate.NamedProperties != null) { if (messageTemplate.NamedProperties.Length < argumentPosition) { consumer.AddHighlighting(new TemplateFormatStringArgumentIsNotUsedWarning(argument)); } } else if (messageTemplate.PositionalProperties != null) { if (!messageTemplate.PositionalProperties.Any( p => p.TryGetPositionalValue(out int position) && position == argumentPosition - 1)) { consumer.AddHighlighting(new TemplateFormatStringArgumentIsNotUsedWarning(argument)); } } else { consumer.AddHighlighting(new TemplateFormatStringArgumentIsNotUsedWarning(argument)); } } }
private static void HighlightByNamedPlaceholder( HighlightingsConsumer consumer, IPsiView psiView, ICSharpArgument templateArgument, MessageTemplate messageTemplate, TreeNodeCollection <ICSharpArgument> arguments) { if (messageTemplate.NamedProperties != null) { var(selectedToken, index) = GetSelectedToken(psiView, templateArgument, messageTemplate.NamedProperties); if (selectedToken == null) { return; } var argumentIndex = templateArgument.IndexOf() + index + 1; if (arguments.Count <= argumentIndex) { return; } consumer.ConsumeHighlighting( GeneralHighlightingAttributeIds.USAGE_OF_ELEMENT_UNDER_CURSOR, templateArgument.GetTokenInformation(selectedToken).DocumentRange); consumer.ConsumeHighlighting( GeneralHighlightingAttributeIds.USAGE_OF_ELEMENT_UNDER_CURSOR, arguments[argumentIndex].GetDocumentRange()); } else if (messageTemplate.PositionalProperties != null) { var(selectedToken, _) = GetSelectedToken(psiView, templateArgument, messageTemplate.PositionalProperties); if (selectedToken == null) { return; } if (!selectedToken.TryGetPositionalValue(out int position)) { return; } foreach (var property in messageTemplate.PositionalProperties) { if (!property.TryGetPositionalValue(out int propertyPosition)) { continue; } if (propertyPosition != position) { continue; } consumer.ConsumeHighlighting( GeneralHighlightingAttributeIds.USAGE_OF_ELEMENT_UNDER_CURSOR, templateArgument.GetTokenInformation(property).DocumentRange); } var argumentIndex = templateArgument.IndexOf() + position + 1; if (arguments.Count <= argumentIndex) { return; } consumer.ConsumeHighlighting( GeneralHighlightingAttributeIds.USAGE_OF_ELEMENT_UNDER_CURSOR, arguments[argumentIndex].GetDocumentRange()); } }
protected override void Run( IInvocationExpression element, ElementProblemAnalyzerData data, IHighlightingConsumer consumer) { var templateArgument = element.GetTemplateArgument(); if (templateArgument == null) { return; } var templateArgumentIndex = templateArgument.IndexOf(); var exceptionType = element.PsiModule.GetPredefinedType().TryGetType(PredefinedType.EXCEPTION_FQN, NullableAnnotation.Unknown); if (exceptionType == null) { return; } ICSharpArgument invalidExceptionArgument = null; foreach (var argument in element.ArgumentList.Arguments) { var argumentType = argument.Value?.Type(); if (!(argumentType is IDeclaredType declaredType)) { continue; } if (!declaredType.IsSubtypeOf(exceptionType)) { continue; } if (templateArgumentIndex > argument.IndexOf()) { return; } invalidExceptionArgument = argument; break; } if (invalidExceptionArgument == null) { return; } var overloadAvailable = false; var candidates = element.InvocationExpressionReference.GetCandidates().ToArray(); var invalidArgumentIndex = invalidExceptionArgument.IndexOf(); foreach (var candidate in candidates) { if (!(candidate.GetDeclaredElement() is IMethod declaredElement)) { continue; } foreach (var parameter in declaredElement.Parameters) { if (invalidArgumentIndex <= parameter.IndexOf()) { break; } if (parameter.Type.IsSubtypeOf(exceptionType)) { overloadAvailable = true; break; } } if (overloadAvailable) { break; } } if (!overloadAvailable) { return; } consumer.AddHighlighting(new ExceptionPassedAsTemplateArgumentWarning(invalidExceptionArgument.GetDocumentRange())); }