public static IReference FindMethodInvocationByArgument([NotNull] ICSharpExpression expression) { var containingExpression = expression.GetContainingParenthesizedExpressionStrict(); var argument = CSharpArgumentNavigator.GetByValue(containingExpression); var invocationExpression = InvocationExpressionNavigator.GetByArgument(argument); var invokedReference = invocationExpression?.InvokedExpression.GetOperandThroughParenthesis() as IReferenceExpression; if (invokedReference == null) { return(null); } var invocationReference = invocationExpression.InvocationExpressionReference; var(declaredElement, _, resolveErrorType) = invocationReference.Resolve(); if (resolveErrorType != ResolveErrorType.OK) { return(null); } if (!(declaredElement is IMethod)) { return(null); } return(invokedReference.Reference); }
protected override Action <ITextControl> ExecutePsiTransaction(ISolution solution, IProgressIndicator progress) { ReferenceExpression.SetName(NewName); if (!Name.StartsWith(nameof(AbstractValidator <object> .Validate))) { // adapt lambda expression var invocationExpression = InvocationExpressionNavigator.GetByInvokedExpression(ReferenceExpression); if (invocationExpression != null) { foreach (var argument in invocationExpression.Arguments) { var lambdaExpression = argument.Expression as ILambdaExpression; if (lambdaExpression == null) { continue; } lambdaExpression.SetAsync(!IsCurrentlyAsync); // foreach (var awaitExpression in lambdaExpression.BodyBlock.Descendants<IAwaitExpression>()) // { // EcmaDesc.Mod // } } } } return(null); }
private static void CheckReferenceExpression([NotNull] IReferenceExpression referenceExpression, [NotNull] IHighlightingConsumer consumer) { var invocationExpression = InvocationExpressionNavigator.GetByInvokedExpression(referenceExpression); if (invocationExpression != null) { return; } var resolveResult = referenceExpression.Reference.Resolve(); if (!resolveResult.ResolveErrorType.IsAcceptable) { return; } var method = resolveResult.DeclaredElement as IMethod; if (method == null || method.IsStatic || method.IsExtensionMethod) { return; } var qualifierType = GetQualifierExpressionType(referenceExpression.QualifierExpression, referenceExpression); if (qualifierType == null) { return; } var isValueType = qualifierType.IsValueType(); if (!isValueType && !qualifierType.IsUnconstrainedGenericType()) { return; } var targetType = referenceExpression.GetImplicitlyConvertedTo(); // delayed if (!targetType.IsDelegateType()) { return; } var description = BakeDescriptionWithTypes( "conversion of value type '{0}' instance method to '{1}' delegate type", qualifierType, targetType); var nameIdentifier = referenceExpression.NameIdentifier; if (isValueType) { consumer.AddHighlighting( new BoxingAllocationHighlighting(nameIdentifier, description), nameIdentifier.GetDocumentRange()); } else { consumer.AddHighlighting( new BoxingAllocationPossibleHighlighting(nameIdentifier, description), nameIdentifier.GetDocumentRange()); } }
private static void CheckStructMethodConversionToDelegateInstance( [NotNull] IReferenceExpression referenceExpression, [NotNull] IHighlightingConsumer consumer) { var invocationExpression = InvocationExpressionNavigator.GetByInvokedExpression(referenceExpression); if (invocationExpression != null) { return; // also filters out 'nameof(o.M)' } var(declaredElement, _, resolveErrorType) = referenceExpression.Reference.Resolve(); if (!resolveErrorType.IsAcceptable) { return; } var method = declaredElement as IMethod; if (method == null || method.IsStatic) { return; } var qualifierType = TryGetQualifierExpressionType(referenceExpression); if (qualifierType == null) { return; } var targetType = referenceExpression.GetImplicitlyConvertedTo(); if (!targetType.IsDelegateType()) { return; } var qualifierTypeKind = IsQualifierOfValueType(qualifierType, includeStructTypeParameters: true); if (qualifierTypeKind == Classification.Not) { return; } var description = BakeDescriptionWithTypes( "conversion of value type '{0}' instance method to '{1}' delegate type", qualifierType, targetType); if (qualifierTypeKind == Classification.Definitely) { consumer.AddHighlighting( new BoxingAllocationHighlighting(referenceExpression.NameIdentifier, description)); } else { consumer.AddHighlighting( new PossibleBoxingAllocationHighlighting(referenceExpression.NameIdentifier, description)); } }
private bool IsUsageSetTransformParent([NotNull] IReferenceExpression referenceExpression, out bool stayInWorldCoords, [CanBeNull] out ICSharpExpression expression) { stayInWorldCoords = true; expression = null; var declaredElement = referenceExpression.Reference.Resolve().DeclaredElement as IClrDeclaredElement; if (declaredElement == null) { return(false); } if (declaredElement is IProperty property) { expression = AssignmentExpressionNavigator.GetByDest(referenceExpression)?.Source; if (!property.ShortName.Equals("parent")) { return(false); } } if (declaredElement is IMethod setParentMethod) { if (!setParentMethod.ShortName.Equals("SetParent")) { return(false); } var invocation = InvocationExpressionNavigator.GetByInvokedExpression(referenceExpression); if (invocation == null) { return(false); } expression = invocation.Arguments[0].Value; if (setParentMethod.Parameters.Count == 2) { var argument = invocation.Arguments[1].Value; if (argument?.ConstantValue.Value is bool constantValue) { stayInWorldCoords = constantValue; } else { return(false); } } } var containingType = declaredElement.GetContainingType(); if (containingType != null && containingType.GetClrName().Equals(KnownTypes.Transform)) { return(true); } return(false); }
private static int GetExistingArgumentsCount([NotNull] IReferenceExpression referenceExpression) { var invocationExpression = InvocationExpressionNavigator.GetByInvokedExpression(referenceExpression); if (invocationExpression == null) { return(0); // todo: -1 to indicate there is no invocation after? } return(invocationExpression.Arguments.Count); }
private static IColorReference ReferenceFromInvocation(IReferenceExpression qualifier, IReferenceExpression methodReferenceExpression) { var invocationExpression = InvocationExpressionNavigator.GetByInvokedExpression(methodReferenceExpression); if (invocationExpression == null || invocationExpression.Arguments.IsEmpty) { return(null); } var methodReference = methodReferenceExpression.Reference; var name = methodReference.GetName(); if (!string.Equals(name, "HSVToRGB", StringComparison.Ordinal)) { return(null); } var arguments = invocationExpression.Arguments; if (arguments.Count < 3 || arguments.Count > 4) { return(null); } var color = GetColorFromHSV(arguments); if (color == null) { return(null); } var qualifierType = qualifier.Reference.Resolve().DeclaredElement as ITypeElement; if (qualifierType == null) { return(null); } var unityColorTypes = UnityColorTypes.GetInstance(qualifierType.Module); if (!unityColorTypes.IsUnityColorTypeSupportingHSV(qualifierType)) { return(null); } var colorElement = new ColorElement(color.Value); var argumentList = invocationExpression.ArgumentList; return(new UnityColorReference(colorElement, invocationExpression, argumentList, argumentList.GetDocumentRange())); }
public override MethodInvocation ProcessUsage(IReference reference) { var referenceExpression = reference.GetTreeNode() as IReferenceExpression; if (referenceExpression == null) { Driver.AddConflict(ReferenceConflict.CreateError(reference, "{0} can not be updated correctly.", "Usage")); return(null); } bool isExtensionMethod = referenceExpression.IsExtensionMethod(); IInvocationExpression invocation = InvocationExpressionNavigator.GetByInvokedExpression(referenceExpression); if (invocation == null) { Driver.AddConflict(ReferenceConflict.CreateError(reference, "{0} can not be updated correctly.", "Usage")); return(null); } ITreeNode element = GetArgument(invocation, isExtensionMethod); var argument = element as ICSharpArgument; IType type = argument != null?GetTypeOfValue(argument.Value) : GetTypeOfValue(element); if (type == null || !type.CanUseExplicitly(invocation)) { Driver.AddConflict(ReferenceConflict.CreateError( reference, "Argument of {0} is not valid 'typeof' expression.", "usage")); return(null); } // we can rely on resolve result since method declaration is not yet changed. ResolveResultWithInfo resolveResult = reference.Resolve(); ISubstitution substitution = resolveResult.Result.Substitution; var method = resolveResult.DeclaredElement as IMethod; if (method == null) { return(null); } if (argument != null) { invocation.RemoveArgument(argument); return(new MethodInvocation(reference, type, method, substitution)); } CSharpElementFactory factory = CSharpElementFactory.GetInstance(invocation.GetPsiModule()); IReferenceExpression newInvokedExpression = invocation.InvokedExpression.ReplaceBy(factory.CreateReferenceExpression("$0", Executer.Method)); return(new MethodInvocation(newInvokedExpression.Reference, type, method, substitution)); }
public void Accept(ITextControl textControl, TextRange nameRange, LookupItemInsertType insertType, Suffix suffix, ISolution solution, bool keepCaretStill) { textControl.Document.ReplaceText(nameRange, "E()"); var psiServices = solution.GetPsiServices(); psiServices.CommitAllDocuments(); var enumMember = myPointer.Resolve(); if (enumMember == null) { return; } var referenceExpression = FindReferenceExpression(textControl, solution); var invocation = InvocationExpressionNavigator.GetByInvokedExpression(referenceExpression); if (invocation == null) { return; } TipsManager.Instance.FeatureIsUsed( "Plugin.ControlFlow.PostfixTemplates.<enum>", textControl.Document, solution); var factory = CSharpElementFactory.GetInstance(referenceExpression); var template = myIsFlags ? "($0 & $1) != 0" : "$0 == $1"; var enumMemberCheck = factory.CreateExpression( template, referenceExpression.QualifierExpression, enumMember); var commandName = typeof(CSharpEnumCaseItemProvider).FullName; var caretPointer = psiServices.DoTransaction(commandName, () => { using (WriteLockCookie.Create()) { var memberCheck = invocation.ReplaceBy(enumMemberCheck); return(memberCheck.CreatePointer()); } }); var checkExpression = caretPointer.GetTreeNode(); if (checkExpression != null) { var offset = checkExpression.GetDocumentRange().TextRange.EndOffset; textControl.Caret.MoveTo(offset, CaretVisualPlacement.DontScrollIfVisible); } }
private void InsertQualifierAsArgument([NotNull] IReferenceExpression referenceExpression, int existingArgumentsCount, [NotNull] ITextControl textControl) { var qualifierExpression = referenceExpression.QualifierExpression.NotNull("qualifierExpression != null"); var qualifierText = qualifierExpression.GetText(); var enumerationType = FindReferencedEnumerationType(qualifierExpression); if (enumerationType != null) { qualifierText = "typeof(" + qualifierText + ")"; } if (IsFirstArgumentAlwaysPassedByRef()) { qualifierText = "ref " + qualifierText; } if (existingArgumentsCount > 0 || HasOnlyMultipleParameters()) { qualifierText += ", "; } TextRange argPosition; // todo: not reliable? var invokedExpression = InvocationExpressionNavigator.GetByInvokedExpression(referenceExpression); if (invokedExpression == null) { qualifierText = "(" + qualifierText; argPosition = referenceExpression.GetDocumentRange().EndOffsetRange().TextRange; // todo: check pars decoration setting // todo: insert ')' if it's insertion is disabled } else { argPosition = invokedExpression.LPar.GetDocumentRange().EndOffsetRange().TextRange; } var qualifierDocumentRange = qualifierExpression.GetDocumentRange(); var qualifierWithDelimiter = qualifierDocumentRange.JoinRight(referenceExpression.NameIdentifier.GetDocumentStartOffset()); textControl.Document.ReplaceText(argPosition, qualifierText); textControl.Document.DeleteText(qualifierWithDelimiter.TextRange); }
private bool IsSpecificArgumentInSpecificMethod(CSharpCodeCompletionContext context, out ICSharpLiteralExpression stringLiteral, Func <IInvocationExpression, bool> methodChecker, Func <IArgumentList, ICSharpArgument, bool> argumentChecker) { stringLiteral = null; var nodeInFile = context.NodeInFile as ITokenNode; if (nodeInFile == null) { return(false); } var possibleInvocationExpression = nodeInFile.Parent; if (possibleInvocationExpression is ICSharpLiteralExpression literalExpression) { if (!literalExpression.Literal.IsAnyStringLiteral()) { return(false); } var argument = CSharpArgumentNavigator.GetByValue(literalExpression); var argumentList = ArgumentListNavigator.GetByArgument(argument); if (argument == null || argumentList == null) { return(false); } if (argumentChecker(argumentList, argument)) { stringLiteral = literalExpression; possibleInvocationExpression = InvocationExpressionNavigator.GetByArgument(argument); } } if (possibleInvocationExpression is IInvocationExpression invocationExpression) { if (methodChecker(invocationExpression)) { return(true); } } stringLiteral = null; return(false); }
private IDeclaredType[] GetParameterTypes() { var node = Reference.GetTreeNode(); var argument = CSharpArgumentNavigator.GetByValue(node as ICSharpLiteralExpression); var argumentList = ArgumentListNavigator.GetByArgument(argument); var invocation = InvocationExpressionNavigator.GetByArgumentList(argumentList); if (invocation?.Reference?.Resolve().DeclaredElement?.ShortName .Equals("StartCoroutine") == true && argumentList?.Arguments.Count > 1) { var secondArgument = argumentList.NotNull("argumentList != null").Arguments[1]; if (secondArgument.Value?.Type() is IDeclaredType type) { return(new[] { type }); } } return(new IDeclaredType[] { }); }
protected override bool AnalyzeNode(ICSharpArgument navigated, ICSharpExpression from) { var invocationExpression = InvocationExpressionNavigator.GetByArgument(navigated); if (invocationExpression != null) { var callee = invocationExpression.Reference.Resolve().DeclaredElement as IMethod; if (BurstCodeAnalysisUtil.IsBurstPossibleArgumentString(navigated) && callee != null && (BurstCodeAnalysisUtil.IsDebugLog(callee) || BurstCodeAnalysisUtil.IsStringFormat(callee))) { return(false); } } return(true); }
public override ReferenceCollection GetReferences(ITreeNode element, ReferenceCollection oldReferences) { var literal = GetValidStringLiteralExpression(element); if (literal == null) { return(ReferenceCollection.Empty); } var argument = CSharpArgumentNavigator.GetByValue(literal); var invocationExpression = InvocationExpressionNavigator.GetByArgument(argument); if (invocationExpression == null) { return(ReferenceCollection.Empty); } // GameObject.AddComponent, GameObject.GetComponent and ScriptableObject.CreateInstance all have the string // literal as the first argument if (invocationExpression.ArgumentsEnumerable.FirstOrDefault() != argument) { return(ReferenceCollection.Empty); } var invocationReference = invocationExpression.Reference; if (!(invocationReference?.Resolve().DeclaredElement is IMethod invokedMethod)) { return(ReferenceCollection.Empty); } var kind = GetExpectedReferenceKind(invokedMethod); var newReferences = kind == ExpectedObjectTypeReferenceKind.None ? ReferenceCollection.Empty : CreateTypeNameReferences(literal, kind); return(ResolveUtil.ReferenceSetsAreEqual(newReferences, oldReferences) ? oldReferences : newReferences); }
public static ICSharpStatement CanBeStatement([NotNull] ICSharpExpression expression) { var argument = CSharpArgumentNavigator.GetByValue(expression); if (argument == null || argument.Kind != ParameterKind.VALUE) { return(null); } var invocation = InvocationExpressionNavigator.GetByArgument(argument); if (invocation == null) { return(null); } var referenceExpression = invocation.InvokedExpression as IReferenceExpression; if (referenceExpression != null && referenceExpression.QualifierExpression == null) { var services = argument.GetSolution().GetComponent <IProjectFileTypeServices>(); var sourceFile = argument.GetSourceFile(); if (sourceFile == null) { return(null); } var service = services.TryGetService <IRazorPsiServices>(sourceFile.LanguageType); if (service != null && service.IsSpecialMethodInvocation(invocation, RazorMethodType.Write)) { return(ExpressionStatementNavigator.GetByExpression(invocation)); } } return(null); }
private static int GetExistingArgumentsCount([NotNull] IReferenceExpression referenceExpression) { var invocationExpression = InvocationExpressionNavigator.GetByInvokedExpression(referenceExpression); if (invocationExpression == null) { return(0); } return(invocationExpression.Arguments.Count); //var arguments = invocationExpression.Arguments; // //// TODO: may be reference from next line parsed as argument // //// special case: "Foo(" parses as invocation with 1 argument //if (arguments.Count == 1) //{ // var textRange = arguments[0].GetTreeTextRange(); // if (textRange.Length == 0) return 0; //} // //return arguments.Count; }
private void PlaceCaretAfterCompletion( [NotNull] ITextControl textControl, [NotNull] IReferenceExpression referenceExpression, int existingArgumentsCount, LookupItemInsertType insertType) { var referenceRange = referenceExpression.GetDocumentRange(); textControl.Caret.MoveTo(referenceRange.TextRange.EndOffset, CaretVisualPlacement.DontScrollIfVisible); var invocationExpression = InvocationExpressionNavigator.GetByInvokedExpression(referenceExpression); if (invocationExpression == null) { return; } var invocationRange = invocationExpression.GetDocumentRange(); textControl.Caret.MoveTo(invocationRange.TextRange.EndOffset, CaretVisualPlacement.DontScrollIfVisible); var settingsStore = referenceExpression.GetSettingsStore(); var parenthesesInsertType = settingsStore.GetValue(CodeCompletionSettingsAccessor.ParenthesesInsertType); var hasMoreParametersToPass = HasMoreParametersToPass(existingArgumentsCount); switch (parenthesesInsertType) { case ParenthesesInsertType.Both: { if (hasMoreParametersToPass) { var rightPar = invocationExpression.RPar; if (rightPar != null) { var rightParRange = rightPar.GetDocumentRange().TextRange; textControl.Caret.MoveTo(rightParRange.StartOffset, CaretVisualPlacement.DontScrollIfVisible); } } break; } case ParenthesesInsertType.Left: case ParenthesesInsertType.None: { // if in insert mode - drop right par and set caret to it's start offest if (insertType == LookupItemInsertType.Insert) { var rightPar = invocationExpression.RPar; if (rightPar != null) { var rightParRange = rightPar.GetDocumentRange().TextRange; invocationExpression.GetPsiServices().Transactions.Execute( commandName: typeof(StaticMethodBehavior).FullName, handler: () => { using (WriteLockCookie.Create()) LowLevelModificationUtil.DeleteChild(rightPar); }); textControl.Caret.MoveTo(rightParRange.StartOffset, CaretVisualPlacement.DontScrollIfVisible); } } break; } } }
private static bool IsInvocationExpression(ICSharpExpression expression) { return(InvocationExpressionNavigator.GetByInvokedExpression(expression) != null); }