Example #1
0
        TypeDeclaration GetTypeDeclaration(MDRefactoringContext context)
        {
            var result = context.GetNode <TypeDeclaration> ();

            if (result == null || result.Parent is TypeDeclaration)
            {
                return(null);
            }
            if (result != null && result.NameToken.Contains(context.Location))
            {
                return(result);
            }
            return(null);
        }
Example #2
0
        public override IEnumerable <MonoDevelop.CodeActions.CodeAction> GetActions(MonoDevelop.Ide.Gui.Document document, object refactoringContext, TextLocation loc, CancellationToken cancellationToken)
        {
            MDRefactoringContext context = (MDRefactoringContext)refactoringContext;

            if (context.IsInvalid)
            {
                yield break;
            }

            VariableInitializer currentVariable = context.GetNode <VariableInitializer>();

            if (currentVariable == null)
            {
                yield break;
            }

            FieldDeclaration currentField = currentVariable.Parent as FieldDeclaration;

            if (currentField == null)
            {
                yield break;
            }

            if (!currentField.Modifiers.HasFlag(Modifiers.Const))
            {
                yield break;
            }

            PrimitiveType baseType = TypeToIntegerPrimitive(context, currentField.ReturnType);

            if (baseType == null)
            {
                //Can't make enums of these types
                yield break;
            }

            TypeDeclaration containerType = currentVariable.GetParent <TypeDeclaration>();

            //Get all the fields/variables that the enum can possibly cover
            //Don't check the name just yet. That'll come later.

            var constFields = containerType.Members.OfType <FieldDeclaration>()
                              .Where(field => field.GetParent <TypeDeclaration>() == containerType && field.HasModifier(Modifiers.Const)).ToList();

            var constVariables = constFields.SelectMany(field => field.Variables).ToList();

            //Now, it's time to check the name of the selected variable
            //We'll use this to search for prefixes later

            var    names       = constVariables.Select(variable => variable.Name).ToList();
            string currentName = currentVariable.Name;

            //Now, find the common name prefixes
            //If the variable is called 'A_B_C_D', then 'A', 'A_B' and 'A_B_C' are
            //the potentially available prefixes.
            //Note that the common prefixes are the ones that more than one variable
            //has.
            //Each prefix has an associated action.

            foreach (var prefix in GetCommonPrefixes(currentName, names))
            {
                string title = string.Format(GettextCatalog.GetString("Create enum '{0}'"), prefix);

                yield return(new DefaultCodeAction(title, (ctx, script) => {
                    PrepareToRunAction(prefix, baseType, containerType, constVariables, cancellationToken, ctx, script);
                }));
            }
        }
		TypeDeclaration GetTypeDeclaration (MDRefactoringContext context)
		{
			var result = context.GetNode<TypeDeclaration> ();
			if (result == null || result.Parent is TypeDeclaration)
				return null;
			if (result != null && result.NameToken.Contains (context.Location))
				return result;
			return null;
		}