/// <summary> /// Gets the items. /// </summary> /// <param name="smartGenerateParameters"> /// The get menu items parameters. /// </param> protected override void GetItems(SmartGenerateParameters smartGenerateParameters) { var element = smartGenerateParameters.Element; smartGenerateParameters.PreviousStatement = StatementUtil.GetPreviousStatement(element); var defaultRange = StatementUtil.GetNewStatementPosition(element); foreach (var liveTemplateInfo in LiveTemplateManager.Instance.LiveTemplateInfos) { var constructor = liveTemplateInfo.Type.GetConstructor(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, new Type[0], null); var handler = constructor != null ? (ILiveTemplate)constructor.Invoke(new object[] { }) : (ILiveTemplate)Activator.CreateInstance(liveTemplateInfo.Type); if (handler == null) { continue; } var liveTemplateItems = handler.GetItems(smartGenerateParameters); if (liveTemplateItems == null) { continue; } foreach (var liveTemplateMenuItem in liveTemplateItems) { var shortcut = "@Do not change: " + liveTemplateMenuItem.Shortcut; foreach (var template in LiveTemplatesManager.Instance.TemplateFamily.UserStorage.Templates) { if (template.Shortcut != shortcut) { continue; } var doc = new XmlDocument(); var templateElement = template.WriteToXml(doc); var xml = templateElement.OuterXml; foreach (var key in liveTemplateMenuItem.Variables.Keys) { xml = xml.Replace("$" + key + "$", liveTemplateMenuItem.Variables[key]); template.Description = template.Description.Replace("$" + key + "$", liveTemplateMenuItem.Variables[key]); } global::JetBrains.Util.TextRange range = liveTemplateMenuItem.Range; if (range == global::JetBrains.Util.TextRange.InvalidRange) { range = defaultRange; } this.AddAction(template.Description, xml, range); } } } }
/// <summary> /// Gets the items. /// </summary> /// <param name="parameters"> /// The parameters. /// </param> /// <returns> /// The items. /// </returns> public IEnumerable<LiveTemplateItem> GetItems(SmartGenerateParameters parameters) { var element = parameters.Element; var classLikeDeclaration = element.GetContainingElement(typeof(IClassLikeDeclaration), true); if (classLikeDeclaration != null) { return null; } var enumDecl = element.GetContainingElement(typeof(IEnumDeclaration), true) as IEnumDeclaration; if (enumDecl != null) { return null; } var namespaceDeclaration = element.GetContainingElement(typeof(INamespaceDeclaration), true); if (namespaceDeclaration == null) { return null; } var liveTemplateItem = new LiveTemplateItem { MenuText = "Type", Description = "Type", Shortcut = "Type" }; return new List<LiveTemplateItem> { liveTemplateItem }; }
/// <summary>Gets the items.</summary> /// <param name="smartGenerateParameters">The get menu items parameters.</param> protected override void GetItems(SmartGenerateParameters smartGenerateParameters) { var element = smartGenerateParameters.Element; var classLikeDeclaration = element.GetContainingElement(typeof(IClassLikeDeclaration), true); if (classLikeDeclaration != null) { return; } var enumDecl = element.GetContainingElement(typeof(IEnumDeclaration), true) as IEnumDeclaration; if (enumDecl != null) { return; } var namespaceDeclaration = element.GetContainingElement(typeof(INamespaceDeclaration), true); if (namespaceDeclaration == null) { return; } this.AddAction("Class", "0BC4B773-20A9-4F12-B486-5C5DB7D39C73"); this.AddAction("Enum", "3B6DA53E-E57F-4A22-ACF6-55F65645AF92"); this.AddAction("Interface", "7D2E8D45-8562-45DD-8415-3E98F0EC24BD"); this.AddAction("Struct", "51BFA78B-7FDA-42CC-85E4-8B29BB1103E5"); }
/// <summary> /// Gets the items. /// </summary> /// <param name="parameters"> /// The parameters. /// </param> /// <returns> /// The items. /// </returns> public IEnumerable<LiveTemplateItem> GetItems(SmartGenerateParameters parameters) { var element = parameters.Element; var interfaceDeclaration = element.GetContainingElement(typeof(IInterfaceDeclaration), true); if (interfaceDeclaration == null) { return null; } var memberDeclaration = element.GetContainingElement(typeof(ITypeMemberDeclaration), true); if (memberDeclaration != null && !(memberDeclaration is IInterfaceDeclaration)) { return null; } var liveTemplateItem = new LiveTemplateItem { MenuText = "Interface member", Description = "Interface member", Shortcut = "Interface member" }; return new List<LiveTemplateItem> { liveTemplateItem }; }
/// <summary> /// Gets the items. /// </summary> /// <param name="smartGenerateParameters"> /// The get menu items parameters. /// </param> protected override void GetItems(SmartGenerateParameters smartGenerateParameters) { var element = smartGenerateParameters.Element; var scope = smartGenerateParameters.Scope; if (scope.Count == 0) { return; } var scopeEntry = scope[smartGenerateParameters.ScopeIndex]; var name = scopeEntry.Name; var type = scopeEntry.Type; var state = GetExpressionNullReferenceState(smartGenerateParameters, element, name); if (state == CSharpControlFlowNullReferenceState.NOT_NULL || state == CSharpControlFlowNullReferenceState.NULL) { return; } global::JetBrains.Util.TextRange range = StatementUtil.GetNewStatementPosition(element); if (type.GetPresentableName(element.Language) == "string") { this.AddAction("Check if '{0}' is null or empty", "514313A0-91F4-4AE5-B4EB-2BB53736A023", range, name); } else { if (type.IsReferenceType()) { this.AddAction("Check if '{0}' is null", "F802DB32-A0B1-4227-BE5C-E7D20670284B", range, name); } } }
/// <summary>Gets the items.</summary> /// <param name="parameters">The parameters.</param> /// <returns>The items.</returns> public IEnumerable<LiveTemplateItem> GetItems(SmartGenerateParameters parameters) { var element = parameters.Element; var structDeclaration = element.GetContainingElement(typeof(IStructDeclaration), true) as IStructDeclaration; if (structDeclaration == null) { return null; } var memberDeclaration = element.GetContainingElement(typeof(IClassMemberDeclaration), true); if (memberDeclaration != null && !(memberDeclaration is IStructDeclaration)) { return null; } var modifier = ModifierUtil.GetModifier(element, structDeclaration); var liveTemplateItem = new LiveTemplateItem { MenuText = "Struct member", Description = "Struct member", Shortcut = "Struct member", Text = string.Format("/* @Modifier */\n") }; liveTemplateItem.Variables["Modifier"] = modifier; return new List<LiveTemplateItem> { liveTemplateItem }; }
/// <summary> /// Gets the state. /// </summary> /// <param name="smartGenerateParameters"> /// The smart generate parameters. /// </param> /// <param name="element"> /// The element. /// </param> /// <param name="name"> /// The variable name. /// </param> /// <returns> /// The null reference state. /// </returns> private static CSharpControlFlowNullReferenceState GetExpressionNullReferenceState(SmartGenerateParameters smartGenerateParameters, IElement element, string name) { var state = CSharpControlFlowNullReferenceState.UNKNOWN; var psiManager = PsiManager.GetInstance(smartGenerateParameters.Solution); using (var cookie = smartGenerateParameters.TextControl.Document.EnsureWritable()) { if (cookie.EnsureWritableResult != global::JetBrains.Util.EnsureWritableResult.SUCCESS) { return state; } using (CommandCookie.Create(string.Format("Context Action {0}", "AssignmentCheck"))) { psiManager.DoTransaction(delegate { state = GetExpressionNullReferenceState(element, name); throw new ProcessCancelledException(); }); } } return state; }
/// <summary> /// Gets the items. /// </summary> /// <param name="smartGenerateParameters"> /// The get menu items parameters. /// </param> protected override void GetItems(SmartGenerateParameters smartGenerateParameters) { var element = smartGenerateParameters.Element; var block = element.GetContainingElement(typeof(IBlock), true) as IBlock; if (block == null) { return; } var statement = element.GetContainingElement(typeof(IStatement), true); if (statement != null && !block.Contains(statement)) { return; } var expressionStatement = StatementUtil.GetPreviousStatement(block, element) as IExpressionStatement; if (expressionStatement == null) { return; } var assignmentExpression = expressionStatement.Expression as IAssignmentExpression; if (assignmentExpression == null) { return; } var referenceExpression = assignmentExpression.Dest as IReferenceExpression; if (referenceExpression == null) { return; } var reference = referenceExpression.Reference; if (reference == null) { return; } var resolve = reference.Resolve(); var propertyDeclaration = resolve.DeclaredElement as IPropertyDeclaration; if (propertyDeclaration == null) { return; } var qualifierExpression = referenceExpression.QualifierExpression; if (qualifierExpression == null) { return; } var qualifier = qualifierExpression.GetText(); this.AddAction("Assign another property on '{0}'", "AA6EFC53-174B-4EFD-A137-8115C22666C7", qualifier); }
/// <summary> /// Gets the items. /// </summary> /// <param name="smartGenerateParameters"> /// The get menu items parameters. /// </param> protected override void GetItems(SmartGenerateParameters smartGenerateParameters) { var namespaceDeclaration = smartGenerateParameters.Element.GetContainingElement(typeof(INamespaceDeclaration), true); if (namespaceDeclaration != null) { return; } this.AddAction("Namespace", "63CBED21-2B8A-4722-B585-6F90C35BC0E5"); }
/// <summary> /// Gets the items. /// </summary> /// <param name="parameters"> /// The parameters. /// </param> /// <returns> /// The items. /// </returns> public IEnumerable<LiveTemplateItem> GetItems(SmartGenerateParameters parameters) { var element = parameters.Element; var methodDeclaration = element.GetContainingElement(typeof(IMethodDeclaration), true) as IMethodDeclaration; if (methodDeclaration == null) { return null; } var body = methodDeclaration.Body; if (body == null || body.Statements.Count > 0) { return null; } var name = methodDeclaration.DeclaredName; if (string.IsNullOrEmpty(name)) { return null; } var method = methodDeclaration.DeclaredElement; if (method == null) { return null; } var signatureBuilder = new StringBuilder(); var first = true; foreach (var parameter in method.Parameters) { if (!first) { signatureBuilder.Append(", "); } first = false; signatureBuilder.Append(parameter.Type.GetLongPresentableName(element.Language)); } var signature = signatureBuilder.ToString(); return new List<LiveTemplateItem> { new LiveTemplateItem { MenuText = string.Format("Body in method with signature ({0})", signature), Description = string.Format("Body in method with signature ({0})", signature), Shortcut = string.Format("Body in method with signature ({0})", signature) } }; }
/// <summary>Gets the name of the template.</summary> /// <param name="parameters">The parameters.</param> /// <returns>The items.</returns> public IEnumerable<LiveTemplateItem> GetItems(SmartGenerateParameters parameters) { var element = parameters.Element; var previousStatement = parameters.PreviousStatement; var declarationStatement = previousStatement as IDeclarationStatement; if (declarationStatement == null) { return null; } var localVariableDeclarations = declarationStatement.VariableDeclarations; if (localVariableDeclarations == null || localVariableDeclarations.Count != 1) { return null; } var localVariableDeclaration = localVariableDeclarations[0]; if (localVariableDeclaration == null) { return null; } var value = localVariableDeclaration.Initial.GetText(); var localVariable = localVariableDeclaration as ILocalVariable; if (localVariable == null) { return null; } var type = localVariable.Type; var presentableName = type.GetPresentableName(element.Language); var longPresentableName = type.GetLongPresentableName(element.Language); var shortName = localVariable.ShortName; var liveTemplateItem = new LiveTemplateItem { MenuText = string.Format("After local variable declaration of type '{0}'", presentableName), Description = string.Format("After local variable of type '{0}'", presentableName), Shortcut = string.Format("After local variable of type {0}", longPresentableName), Text = string.Format("/* @VariableName, @VariableType, @Value */\n") }; liveTemplateItem.Variables["VariableName"] = shortName; liveTemplateItem.Variables["VariableType"] = presentableName; liveTemplateItem.Variables["Value"] = value; return new List<LiveTemplateItem> { liveTemplateItem }; }
/// <summary> /// Gets the items. /// </summary> /// <param name="smartGenerateParameters"> /// The get menu items parameters. /// </param> protected override void GetItems(SmartGenerateParameters smartGenerateParameters) { var element = smartGenerateParameters.Element; var enumDeclaration = element.GetContainingElement(typeof(IEnumDeclaration), false) as IEnumDeclaration; if (enumDeclaration == null) { return; } this.AddAction("Add enum member", "587F88E2-6876-41F2-885C-58AD93BBC8B4"); }
/// <summary> /// Gets the items. /// </summary> /// <param name="smartGenerateParameters"> /// The get menu items parameters. /// </param> protected override void GetItems(SmartGenerateParameters smartGenerateParameters) { var element = smartGenerateParameters.Element; var propertyDeclaration = element.GetContainingElement(typeof(IPropertyDeclaration), true) as IProperty; if (propertyDeclaration == null) { return; } var accessorDeclaration = element.GetContainingElement(typeof(IAccessorDeclaration), true) as IAccessorDeclaration; if (accessorDeclaration == null) { return; } var body = accessorDeclaration.Body; if (body == null || body.Statements.Count > 0) { return; } var name = propertyDeclaration.ShortName; if (string.IsNullOrEmpty(name)) { return; } var charArray = name.ToCharArray(); charArray[0] = char.ToLower(charArray[0]); name = new string(charArray); var namingPolicy = CodeStyleSettingsManager.Instance.CodeStyleSettings.GetNamingSettings2().PredefinedNamingRules[NamedElementKinds.PrivateInstanceFields]; var prefix = namingPolicy.NamingRule.Prefix; var typeName = propertyDeclaration.Type.GetPresentableName(element.Language); if (accessorDeclaration.Kind == AccessorKind.GETTER) { this.AddAction("Return '{0}{1}'", "0E03C1D4-A5DF-4011-86AE-4E561E419CD0", prefix, name); if (typeName == "string") { this.AddAction("Return '{0}{1} ?? string.Empty'", "BDC2EEB0-F626-4D6A-AB06-DD5C7C80BB30", prefix, name); } } if (accessorDeclaration.Kind == AccessorKind.SETTER) { this.AddAction("Assign value to '{0}{1}'", "A97E734B-C24B-44A3-A914-C67BBB3FAC65", prefix, name); } }
/// <summary>Gets the items.</summary> /// <param name="parameters">The parameters.</param> /// <returns>The items.</returns> public IEnumerable<LiveTemplateItem> GetItems(SmartGenerateParameters parameters) { var element = parameters.Element; var methodDeclaration = element.GetContainingElement(typeof(IMethodDeclaration), true) as IMethodDeclaration; if (methodDeclaration == null) { return null; } var body = methodDeclaration.Body; if (body == null || body.Statements.Count > 0) { return null; } var name = methodDeclaration.DeclaredName; if (string.IsNullOrEmpty(name)) { return null; } var method = methodDeclaration.DeclaredElement; if (method == null) { return null; } var returnType = method.ReturnType; var returnTypeName = returnType.GetPresentableName(element.Language); if (returnTypeName == "void" || string.IsNullOrEmpty(returnTypeName)) { return null; } var liveTemplateItem = new LiveTemplateItem { MenuText = string.Format("Body in method that returns '{0}'", returnTypeName), Description = string.Format("Body in method that returns '{0}'", returnTypeName), Shortcut = string.Format("Body in method that returns '{0}'", returnTypeName) }; return new List<LiveTemplateItem> { liveTemplateItem }; }
/// <summary>Gets the name of the template.</summary> /// <param name="smartGenerateParameters">The smart generate parameters.</param> /// <returns>The items.</returns> public IEnumerable<LiveTemplateItem> GetItems(SmartGenerateParameters smartGenerateParameters) { var element = smartGenerateParameters.Element; var block = element.GetContainingElement(typeof(IBlock), true) as IBlock; if (block == null) { return null; } var methodDeclaration = element.GetContainingElement(typeof(IMethodDeclaration), true) as IMethodDeclaration; if (methodDeclaration == null) { return null; } if (block != methodDeclaration.Body) { return null; } if (block.Statements.Count > 0) { var statement = block.Statements[0]; var statementRange = statement.GetDocumentRange(); var statementStart = statementRange.TextRange.StartOffset; if (element.GetTreeTextRange().StartOffset.Offset > statementStart) { return null; } } var liveTemplateItem = new LiveTemplateItem { MenuText = "At the start of a method", Description = "At the start of a method", Shortcut = "At the start of a method" }; return new List<LiveTemplateItem> { liveTemplateItem }; }
/// <summary> /// Gets the items. /// </summary> /// <param name="smartGenerateParameters"> /// The get menu items parameters. /// </param> protected override void GetItems(SmartGenerateParameters smartGenerateParameters) { var element = smartGenerateParameters.Element; var methodDeclaration = element.GetContainingElement(typeof(IMethodDeclaration), true) as IMethodDeclaration; if (methodDeclaration == null) { return; } var body = methodDeclaration.Body; if (body == null || body.Statements.Count > 0) { return; } var name = methodDeclaration.DeclaredName; if (string.IsNullOrEmpty(name)) { return; } var method = methodDeclaration.DeclaredElement; if (method == null) { return; } var returnType = method.ReturnType; var returnTypeName = returnType.GetPresentableName(element.Language); if (returnTypeName == "void" || string.IsNullOrEmpty(returnTypeName)) { return; } if (returnType.IsReferenceType()) { this.AddAction("Declare and return variable", "D00FD7E4-EE48-40F0-A126-D1E8AE8C031E", returnTypeName); } else { this.AddAction("Declare and return variable", "1E9A90C2-2FB9-451F-8566-BA59AAB707DF", returnTypeName); } }
/// <summary> /// Gets the items. /// </summary> /// <param name="smartGenerateParameters"> /// The get menu items parameters. /// </param> protected override void GetItems(SmartGenerateParameters smartGenerateParameters) { var element = smartGenerateParameters.Element; var interfaceDeclaration = element.GetContainingElement(typeof(IInterfaceDeclaration), true); if (interfaceDeclaration == null) { return; } var memberDeclaration = element.GetContainingElement(typeof(ITypeMemberDeclaration), true); if (memberDeclaration != null && !(memberDeclaration is IInterfaceDeclaration)) { return; } this.AddAction("Property", "D6EB42DA-2858-46B3-8CB3-E3DEFB245D11"); this.AddAction("Method", "B3DB6158-D43E-42EE-8E67-F10CF7344106"); }
/// <summary> /// Gets the items. /// </summary> /// <param name="smartGenerateParameters"> /// The get menu items parameters. /// </param> protected override void GetItems(SmartGenerateParameters smartGenerateParameters) { var element = smartGenerateParameters.Element; var classDeclaration = element.GetContainingElement(typeof(IClassDeclaration), true) as IClassDeclaration; if (classDeclaration == null) { return; } var memberDeclaration = element.GetContainingElement(typeof(IClassMemberDeclaration), true); if (memberDeclaration != null && !(memberDeclaration is IClassDeclaration)) { return; } var modifier = ModifierUtil.GetModifier(element, classDeclaration); this.AddAction("Auto property", "166BE49C-D068-476D-BC9C-2B5C3AF21B06", modifier); this.AddAction("Property", "a684b217-f179-431b-a485-e3d76dbe57fd", modifier); this.AddAction("Method", "85BBC654-4EE4-4932-BB0C-E0670FA1BB82", modifier); var items = GeneratorManager.GetInstance(smartGenerateParameters.Solution).GetGeneratorItems(smartGenerateParameters.Context); foreach (var item in items) { if (!item.Available(smartGenerateParameters.Context)) { continue; } var action = new ClassMemberAction { GeneratorItem = item, DataContext = smartGenerateParameters.Context, SelectionRange = global::JetBrains.Util.TextRange.InvalidRange, Text = item.Text.Text }; this.AddAction(action); } }
/// <summary> /// Gets the items. /// </summary> /// <param name="smartGenerateParameters"> /// The get menu items parameters. /// </param> protected override void GetItems(SmartGenerateParameters smartGenerateParameters) { var element = smartGenerateParameters.Element; var expression = element.GetContainingElement(typeof(IExpression), false) as IExpression; while (expression != null) { var type = expression.Type(); var typeName = type.GetPresentableName(element.Language); if (typeName == "bool") { this.AddAction("Surround with 'if'", "FA4B31AF-393D-44DB-93D3-F7E48BF97C53", expression.GetDocumentRange().TextRange); return; } expression = expression.GetContainingElement(typeof(IExpression), false) as IExpression; } }
/// <summary>Gets the items.</summary> /// <param name="smartGenerateParameters">The get menu items parameters.</param> protected override void GetItems(SmartGenerateParameters smartGenerateParameters) { var element = smartGenerateParameters.Element; var expression = element.GetContainingElement(typeof(IExpression), false) as IExpression; while (expression != null) { var type = expression.Type(); var typeName = type.GetPresentableName(element.Language); if (typeName == "string") { this.AddAction("Surround with 'string.IsNullOrEmpty'", "3D13FE3E-7004-42B0-B205-2881C5ADBAD2"); return; } expression = expression.GetContainingElement(typeof(IExpression), false) as IExpression; } }
/// <summary>Gets the name of the template.</summary> /// <param name="smartGenerateParameters">The smart generate parameters.</param> /// <returns>The items.</returns> public IEnumerable<LiveTemplateItem> GetItems(SmartGenerateParameters smartGenerateParameters) { var element = smartGenerateParameters.Element; if (!StatementUtil.IsBeforeFirstStatement(element)) { return null; } var liveTemplateItem = new LiveTemplateItem { MenuText = "At the start of a block", Description = "At the start of a block", Shortcut = "At the start of a block" }; return new List<LiveTemplateItem> { liveTemplateItem }; }
/// <summary> /// Gets the items. /// </summary> /// <param name="smartGenerateParameters"> /// The get menu items parameters. /// </param> protected override void GetItems(SmartGenerateParameters smartGenerateParameters) { var element = smartGenerateParameters.Element; var hasLoop = element.GetContainingElement(typeof(IForeachStatement), false) as IForeachStatement != null; hasLoop |= element.GetContainingElement(typeof(IForStatement), false) as IForStatement != null; hasLoop |= element.GetContainingElement(typeof(IWhileStatement), false) as IWhileStatement != null; hasLoop |= element.GetContainingElement(typeof(IDoStatement), false) as IDoStatement != null; if (!hasLoop) { return; } if (!StatementUtil.IsAfterLastStatement(element)) { return; } this.AddAction("'continue'", "F849A86C-A93E-4805-B8E1-4B02CA8807CC"); this.AddAction("'break'", "42DA21AD-1F9F-4ECE-B5F6-E7AFC5EAAE14"); }
/// <summary> /// Gets the items. /// </summary> /// <param name="smartGenerateParameters"> /// The get menu items parameters. /// </param> protected override void GetItems(SmartGenerateParameters smartGenerateParameters) { var element = smartGenerateParameters.Element; var structDeclaration = element.GetContainingElement(typeof(IStructDeclaration), true) as IStructDeclaration; if (structDeclaration == null) { return; } var memberDeclaration = element.GetContainingElement(typeof(IClassMemberDeclaration), true); if (memberDeclaration != null && !(memberDeclaration is IStructDeclaration)) { return; } var modifier = ModifierUtil.GetModifier(element, structDeclaration); this.AddAction("Auto property", "166BE49C-D068-476D-BC9C-2B5C3AF21B06", modifier); this.AddAction("Property", "a684b217-f179-431b-a485-e3d76dbe57fd", modifier); this.AddAction("Method", "85BBC654-4EE4-4932-BB0C-E0670FA1BB82", modifier); }
/// <summary> /// Gets the items. /// </summary> /// <param name="smartGenerateParameters"> /// The get menu items parameters. /// </param> protected override void GetItems(SmartGenerateParameters smartGenerateParameters) { var element = smartGenerateParameters.Element; var switchStatement = element.GetContainingElement(typeof(ISwitchStatement), false) as ISwitchStatement; if (switchStatement == null) { return; } var block = switchStatement.Block; if (block == null) { return; } if (element.ToTreeNode().Parent != block) { return; } this.AddAction("'case'", "16E39695-5810-4C3E-A3CD-AB0CC0127C60"); }
/// <summary> /// Gets the items. /// </summary> /// <param name="smartGenerateParameters"> /// The get menu items parameters. /// </param> protected override void GetItems(SmartGenerateParameters smartGenerateParameters) { var element = smartGenerateParameters.Element; var block = element.GetContainingElement(typeof(IBlock), true) as IBlock; if (block == null) { return; } var statement = element.GetContainingElement(typeof(IStatement), true); if (statement != null && !block.Contains(statement)) { return; } var ifStatement = StatementUtil.GetPreviousStatement(block, element) as IIfStatement; if (ifStatement == null) { return; } var elseStatement = ifStatement.Else; while (elseStatement != null && elseStatement is IIfStatement) { elseStatement = (elseStatement as IIfStatement).Else; } if (elseStatement != null) { return; } this.AddAction("'else'", "9F134F1B-3F0D-4C9E-B549-A469828D1A7F"); this.AddAction("'else if'", "94F834F9-110D-4608-A780-9BD05FE826A1"); }
/// <summary> /// Gets the name of the template. /// </summary> /// <param name="parameters"> /// The parameters. /// </param> /// <returns> /// The items. /// </returns> IEnumerable<LiveTemplateItem> ILiveTemplate.GetItems(SmartGenerateParameters parameters) { var element = parameters.Element; var result = new List<LiveTemplateItem>(); var hasString = false; var hasInt = false; var hasBool = false; var expression = element.GetContainingElement(typeof(IExpression), false) as IExpression; while (expression != null) { var type = expression.Type(); var typeName = type.GetPresentableName(element.Language); var treeTextRange = expression.GetTreeTextRange(); var textRange = new global::JetBrains.Util.TextRange(treeTextRange.StartOffset.Offset, treeTextRange.EndOffset.Offset); if (typeName == "string" && !hasString) { result.Add(new LiveTemplateItem { MenuText = "Surround string expression", Description = "Surround string expression", Shortcut = "Surround string expression", Range = textRange }); hasString = true; } if (typeName == "int" && !hasInt) { result.Add(new LiveTemplateItem { MenuText = "Surround integer expression", Description = "Surround integer expression", Shortcut = "Surround integer expression", Range = textRange }); hasInt = true; } if (typeName == "bool" && !hasBool) { result.Add(new LiveTemplateItem { MenuText = "Surround boolean expression", Description = "Surround boolean expression", Shortcut = "Surround boolean expression", Range = textRange }); hasBool = true; } expression = expression.GetContainingElement(typeof(IExpression), false) as IExpression; } return result; }
/// <summary>Gets the name of the template.</summary> /// <param name="parameters">The parameters.</param> /// <returns>The items.</returns> public IEnumerable<LiveTemplateItem> GetItems(SmartGenerateParameters parameters) { var previousStatement = parameters.PreviousStatement; var expressionStatement = previousStatement as IExpressionStatement; if (expressionStatement == null) { return null; } var invocationExpression = expressionStatement.Expression as IInvocationExpression; if (invocationExpression == null) { return null; } var invokedExpression = invocationExpression.InvokedExpression as IReferenceExpression; if (invokedExpression == null) { return null; } var resolveResult = invokedExpression.Reference.Resolve(); IMethod method = null; var methodDeclaration = resolveResult.DeclaredElement as IMethodDeclaration; if (methodDeclaration != null) { method = methodDeclaration.DeclaredElement; } if (method == null) { method = resolveResult.DeclaredElement as IMethod; } if (method == null) { return null; } var expression = string.Empty; var qualifierExpression = invokedExpression.QualifierExpression; if (qualifierExpression != null) { expression = qualifierExpression.GetText(); } var shortName = method.ShortName; var type = string.Empty; var text = shortName; var shortcut = shortName; var containingType = method.GetContainingType(); if (containingType != null) { text = containingType.ShortName + "." + text; shortcut = containingType.ShortName + "." + shortcut; type = containingType.ShortName; var ns = containingType.GetContainingNamespace(); if (!string.IsNullOrEmpty(ns.ShortName)) { shortcut = ns.ShortName + "." + shortcut; } } var liveTemplateItem = new LiveTemplateItem { MenuText = string.Format("After call to '{0}'", text), Description = string.Format("After call to '{0}'", text), Shortcut = string.Format("After call to {0}", shortcut), Text = string.Format("/* @Qualifier, @MethodName, @ContainingType, @Arg0... */\n") }; liveTemplateItem.Variables["MethodName"] = shortName; liveTemplateItem.Variables["ContainingType"] = type; liveTemplateItem.Variables["Qualifier"] = expression; var arguments = invocationExpression.Arguments; if (arguments != null) { var count = 0; foreach (var argument in arguments) { liveTemplateItem.Variables["Arg" + count] = argument.GetText(); count++; } } return new List<LiveTemplateItem> { liveTemplateItem }; }
/// <summary> /// Gets the name of the template. /// </summary> /// <param name="parameters"> /// The parameters. /// </param> /// <returns> /// The items. /// </returns> public IEnumerable<LiveTemplateItem> GetItems(SmartGenerateParameters parameters) { var previousStatement = parameters.PreviousStatement; var expressionStatement = previousStatement as IExpressionStatement; if (expressionStatement == null) { return null; } var invocationExpression = expressionStatement.Expression as IInvocationExpression; if (invocationExpression == null) { return null; } var invokedExpression = invocationExpression.InvokedExpression as IReferenceExpression; if (invokedExpression == null) { return null; } var resolveResult = invokedExpression.Reference.Resolve(); IMethod method = null; var methodDeclaration = resolveResult.DeclaredElement as IMethodDeclaration; if (methodDeclaration != null) { method = methodDeclaration.DeclaredElement; } if (method == null) { method = resolveResult.DeclaredElement as IMethod; } if (method == null) { return null; } var text = method.ShortName; var shortcut = method.ShortName; var containingType = method.GetContainingType(); if (containingType != null) { text = containingType.ShortName + "." + text; shortcut = containingType.ShortName + "." + shortcut; var ns = containingType.GetContainingNamespace(); if (!string.IsNullOrEmpty(ns.ShortName)) { shortcut = ns.ShortName + "." + shortcut; } } var liveTemplateItem = new LiveTemplateItem { MenuText = string.Format("After call to '{0}'", text), Description = string.Format("After call to '{0}'", text), Shortcut = string.Format("After call to {0}", shortcut) }; liveTemplateItem.Variables["Name"] = method.ShortName; liveTemplateItem.Variables["Type"] = containingType != null ? containingType.ShortName : string.Empty; return new List<LiveTemplateItem> { liveTemplateItem }; }
/// <summary> /// Gets the items. /// </summary> /// <param name="smartGenerateParameters"> /// The get menu items parameters. /// </param> protected override void GetItems(SmartGenerateParameters smartGenerateParameters) { var element = smartGenerateParameters.Element; var scope = smartGenerateParameters.Scope; if (scope.Count == 0) { return; } var name = scope[smartGenerateParameters.ScopeIndex].Name; var type = scope[smartGenerateParameters.ScopeIndex].Type; var isAssigned = scope[smartGenerateParameters.ScopeIndex].IsAssigned; var range = StatementUtil.GetNewStatementPosition(element); if (!isAssigned) { this.AddAction("Assign value to '{0}'", "9BD23D35-AC2A-46E2-AADA-C81D9B53795A", range, name); if (HasAccessableConstructor(type)) { this.AddAction("Assign new instance to '{0}'", "208A11F8-DEE1-4B8B-838F-17DA883DA7A5", range, name, type.GetPresentableName(element.Language)); } return; } var declaredType = type as IDeclaredType; if (declaredType != null) { var enumerate = declaredType.GetTypeElement() as IEnum; if (enumerate != null) { this.AddAction("Generate 'switch' from '{0}'", "EBAF3559-41C5-471D-8457-A20C9566D397", range, name); } var typeName = type.GetPresentableName(element.Language); var module = declaredType.Module; if (module != null && typeName != "string") { var enumerable = TypeFactory.CreateTypeByCLRName("System.Collections.IEnumerable", module); if (declaredType.IsSubtypeOf(enumerable)) { this.AddAction("Iterate '{0}' via 'foreach'", "9CA009C7-468A-4D3E-ACEC-A12F2FAF4B67", range, name); } } } else { if (type is IArrayType) { this.AddAction("Iterate '{0}' via 'foreach'", "9CA009C7-468A-4D3E-ACEC-A12F2FAF4B67", range, name); } } // AddMenuItem(items, "if ({0}.<Method>)...", "1438A7F2-B12C-4784-BFDE-A803FA8F1279", name); // AddMenuItem(items, "var Var = {0}.Method();", "11BACA25-C561-4FE8-934B-41246B7CFAC9", name); // AddMenuItem(items, "if ({0}.Method() == Value)...", "43E2C069-A3E6-4649-A374-104A16C59305", name); // AddMenuItem("Invoke method on '{0}'", "FE9C6A6B-A068-4182-B301-8002FE05A458", range, name); // if(HasWritableProperty(type)) { // AddMenuItem("Assign property on '{0}'", "DA0860C6-535C-489E-940C-841AA6C54C96", range, name); // } }
/// <summary> /// Gets the items. /// </summary> /// <param name="smartGenerateParameters"> /// The get menu items parameters. /// </param> protected override void GetItems(SmartGenerateParameters smartGenerateParameters) { var element = smartGenerateParameters.Element; if (!StatementUtil.IsAfterLastStatement(element)) { return; } var block = element.GetContainingElement(typeof(IBlock), true) as IBlock; if (block == null) { return; } IBlock body = null; var typeName = string.Empty; IType returnType = null; var functionDeclaration = block.GetContainingTypeMemberDeclaration() as IFunctionDeclaration; if (functionDeclaration != null) { var function = functionDeclaration.DeclaredElement; if (function == null) { return; } returnType = function.ReturnType; typeName = returnType.GetPresentableName(element.Language); var methodDeclaration = functionDeclaration as IMethodDeclaration; if (methodDeclaration != null) { body = methodDeclaration.Body; } } else { var propertyDeclaration = block.GetContainingTypeMemberDeclaration() as IPropertyDeclaration; if (propertyDeclaration != null) { var accessorDeclaration = element.GetContainingElement(typeof(IAccessorDeclaration), true) as IAccessorDeclaration; if (accessorDeclaration != null && accessorDeclaration.Kind == AccessorKind.GETTER) { returnType = propertyDeclaration.Type; typeName = returnType.GetPresentableName(element.Language); body = accessorDeclaration.Body; } } } // return; if (string.IsNullOrEmpty(typeName) || typeName == "void") { if (body != block && body != null) { this.AddAction("return;", "19B0E24A-C3C3-489A-BF20-122C5114D7FF"); } } else if (typeName == "bool") { this.AddAction("Return 'true'", "459C8B38-0048-43DF-9279-3E946A3A65F2"); this.AddAction("Return 'false'", "9F342BE4-4A55-48FF-BECF-A67C7D79BF76"); } else { this.AddAction("Return a value", "39530254-7198-4A3C-B528-6160324E9792"); if (returnType != null && returnType.IsReferenceType()) { this.AddAction("Return 'null'", "D34007F3-C131-46F4-96B7-8D2654727D0B"); } } }