public bool Add(string value, AutocompleteItemCategory category)
        {
            AutocompleteItem item = null;

            switch (category)
            {
            case AutocompleteItemCategory.Keyword:
                item = new AutocompleteItem(value);
                break;

            case AutocompleteItemCategory.Method:
                item = new MethodAutocompleteItem(value);    // { ImageIndex = (int)category };
                break;

            default:
                return(false);
            }


            if (item.Text != "")
            {
                if (!_suggestions.Exists(x => x.Text == item.Text))
                {
                    Suggestions.Add(item);
                    return(true);
                }
            }
            return(false);
        }
        public IEnumerator <AutocompleteItem> GetEnumerator()
        {
            var text  = menu.Fragment.Text;
            var parts = text.Split('.');
            List <AutocompleteItem> items = new List <AutocompleteItem>();

            foreach (var item in Autocomplete.UserItems.Keys)
            {
                items.Add(new AutocompleteItem(item));
            }
            if (parts.Length < 2)
            {
                items.AddRange(ProgramData.Project.GetDefaultList());
                foreach (var item in CodeAnalysisEngine.Variables)
                {
                    items.Add(new AutocompleteItem(item));
                }
                foreach (var item in items)
                {
                    yield return(item);
                }
            }
            else
            {
                var className = parts[parts.Length - 2];
                foreach (var methodName in ProgramData.Project.GetListByClassName(className))
                {
                    items.Add(methodName);
                }
                if (Autocomplete.UserItems.ContainsKey(className))
                {
                    foreach (var methodName in Autocomplete.UserItems[className])
                    {
                        items.Add(new MethodAutocompleteItem(methodName));
                    }
                }
                if (CodeAnalysisEngine.Objects.ContainsKey(className))
                {
                    foreach (var methodName in CodeAnalysisEngine.Objects[className])
                    {
                        var item = new MethodAutocompleteItem(methodName);
                        if (!items.Contains(item))
                        {
                            items.Add(item);
                        }
                    }
                }
                foreach (var item in items)
                {
                    yield return(item);
                }
            }
        }
        public Sandbox()
        {
            InitializeComponent();

            fctb = new FastColoredTextBox()
            {
                Dock = DockStyle.Fill, Parent = this
            };
            var menu = new AutocompleteMenu(fctb);
            var item = new MethodAutocompleteItem("ToString");

            menu.Items.SetAutocompleteItems(new AutocompleteItem[] { item });
        }
Beispiel #4
0
        public override List <AutocompleteItem> GetListByClassName(string className)
        {
            List <string>           result;
            List <AutocompleteItem> acList = new List <AutocompleteItem>();

            if (Members.TryGetValue(className, out result))
            {
                foreach (var item in result)
                {
                    MethodAutocompleteItem it = new MethodAutocompleteItem(item);
                    it.ImageIndex = 5;
                    acList.Add(it);
                }
                return(acList);
            }
            else
            {
                return(GetDefaultList());
            }
        }
        public IEnumerator <AutocompleteItem> GetEnumerator()
        {
            var compiler = new CSharpCodeProvider();
            // Just to prove a point...

            //get current fragment of the text
            var text = tb.GetLineText(menu.Fragment.ToLine);

            //extract class name (part before dot)
            var parts = text.Split('.');

            if (parts.Length < 2)
            {
                yield break;
            }
            var  obj  = parts[0];
            Type type = null;

            if (obj == "Selected")
            {
                type = typeof(UITreeSelection);
            }
            else if (obj == "Model")
            {
                type = typeof(Model);
            }

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

            PropertyInfo prop;

            for (var i = 1; i < parts.Length - 1; i++)
            {
                var brackets = parts[i].IndexOf('[');
                if (brackets > 0)
                {
                    if (parts[i].IndexOf(']') > brackets + 1)
                    {
                        prop = type.GetProperty(parts[i].Substring(0, brackets));
                        if (prop != null)
                        {
                            var indexerType = prop.PropertyType.GetDefaultMembers().OfType <PropertyInfo>().FirstOrDefault()?.PropertyType;
                            if (indexerType != null)
                            {
                                type = indexerType;
                                continue;
                            }
                            else
                            {
                                yield break;
                            }
                        }
                        else
                        {
                            yield break;
                        }
                    }
                    else
                    {
                        yield break;
                    }
                }

                var parenthesis = parts[i].IndexOf('(');
                if (parenthesis > 0)
                {
                    if (parts[i].IndexOf(')') > brackets)
                    {
                        var method = type.GetMethod(parts[i].Substring(0, parenthesis));
                        if (method != null)
                        {
                            type = method.ReturnType; continue;
                        }
                        else
                        {
                            yield break;
                        }
                    }
                    else
                    {
                        yield break;
                    }
                }

                prop = type.GetProperty(parts[i]);
                if (prop != null)
                {
                    type = prop.PropertyType; continue;
                }
                else
                {
                    yield break;
                }
            }

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

            MethodAutocompleteItem last = null;

            //return methods
            foreach (var mi in type.GetMethods(BindingFlags.Public | BindingFlags.Instance)
                     .Where(m => !m.IsSpecialName && m.DeclaringType != typeof(object)).AsEnumerable())
            {
                switch (mi.Name)
                {
                case "GetEnumerator":
                    continue;
                }

                // Overloaded methods only show up once, but let's display all overrides in the tooltip.
                var    methodName = mi.Name + "()";
                string returnTypeName;
                if (mi.ReturnType == typeof(void))
                {
                    returnTypeName = "void";
                }
                else
                {
                    var returnType = new CodeTypeReference(mi.ReturnType);
                    returnTypeName = compiler.GetTypeOutput(returnType).Replace("TabularEditor.UI.UISelectionList", "IEnumerable").Replace("TabularEditor.TOMWrapper.", "");
                }

                var methodSyntax = returnTypeName + " " + mi.Name + "(" + string.Join(", ", mi.GetParameters().Select(p => p.Name).ToArray()) + ")";

                if (last?.Text == methodName)
                {
                    last.ToolTipTitle += "\n" + methodSyntax;
                }
                else
                {
                    last = new MethodAutocompleteItem(methodName)
                    {
                        ToolTipTitle = methodSyntax,
                        ToolTipText  = mi.GetCustomAttribute <DescriptionAttribute>()?.Description ?? string.Empty,
                        ImageIndex   = TabularIcons.ICON_METHOD
                    };
                    yield return(last);
                }
            }

            // Type derives from IEnumerable, so let's add a few select LINQ methods:
            if (type.GetInterface("IEnumerable") != null)
            {
                yield return(new MethodAutocompleteItem("Select()")
                {
                    ToolTipTitle = "Select",
                    ToolTipText = "Projects each item of the collection into something else using a lambda expression.\nExample: .Select(Measure => Measure.Table)",
                    ImageIndex = TabularIcons.ICON_EXMETHOD
                });

                yield return(new MethodAutocompleteItem("Any()")
                {
                    ToolTipTitle = "Any",
                    ToolTipText = "Determines if the collection contains any items. Specify a lambda expression to determine if the collection contains any items satisfying the given criteria.\nExample: .Any(Measure => Measure.Description == \"\")",
                    ImageIndex = TabularIcons.ICON_EXMETHOD
                });

                yield return(new MethodAutocompleteItem("All()")
                {
                    ToolTipTitle = "All",
                    ToolTipText = "Determines if all items in the collection satisfies the given criteria.\nExample: .All(Measure => Measure.Description == \"\")",
                    ImageIndex = TabularIcons.ICON_EXMETHOD
                });

                // Below adds all LINQ methods, which may be overkill:

                /*foreach (var method in typeof(System.Linq.Enumerable)
                 *  .GetMethods(BindingFlags.Static | BindingFlags.Public).Select(m => m.Name).Distinct())
                 * {
                 *  yield return new MethodAutocompleteItem(method + "()")
                 *  {
                 *      ToolTipTitle = method,
                 *      ImageIndex = TabularIcons.ICON_METHOD
                 *  };
                 * }*/
            }

            //return static properties of the class
            foreach (var pi in type.GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                var propType     = new CodeTypeReference(pi.PropertyType);
                var propTypeName = compiler.GetTypeOutput(propType).Replace("TabularEditor.UI.UISelectionList", "IEnumerable").Replace("TabularEditor.TOMWrapper.", "");

                yield return(new MethodAutocompleteItem(pi.Name)
                {
                    ToolTipTitle = string.Format("{0} {1} {{ {2}}}", propTypeName, pi.Name, (pi.CanRead ? "get; " : "") + (pi.CanWrite ? "set; " : "")),
                    ToolTipText = pi.GetCustomAttribute <DescriptionAttribute>()?.Description ?? string.Empty,
                    ImageIndex = TabularIcons.ICON_PROPERTY
                });
            }
        }
        public void PopulateFromType(Type type)
        {
            this.Clear();
            if (type == null)
            {
                return;
            }
            var compiler = new CSharpCodeProvider();
            MethodAutocompleteItem last = null;

            if (type.IsEnum)
            {
                AddRange(type.GetEnumNames().Select(s => new MethodAutocompleteItem(s)
                {
                    ImageIndex = TabularIcons.ICON_ENUM
                }));
                Add(new MethodAutocompleteItem("HasFlag")
                {
                    ToolTipTitle = "bool Enum.HasFlag(Enum flag)",
                    ToolTipText  = "Determines whether one or more bits are set in the bit mask.",
                    ImageIndex   = TabularIcons.ICON_METHOD
                });
                // TODO: Add our own extensions to the "Types" enum?
                return;
            }

            //return methods
            foreach (var mi in type.GetMethods(BindingFlags.Public | BindingFlags.Instance)
                     .Where(m => !m.IsSpecialName && m.DeclaringType != typeof(object)).AsEnumerable())
            {
                if (mi.GetCustomAttribute <IntelliSenseAttribute>() == null)
                {
                    continue;
                }
                switch (mi.Name)
                {
                case "GetEnumerator":
                    continue;
                }

                // Overloaded methods only show up once, but let's display all overrides in the tooltip.
                var    methodName = mi.Name;
                string returnTypeName;
                if (mi.ReturnType == typeof(void))
                {
                    returnTypeName = "void";
                }
                else
                {
                    var returnType = new CodeTypeReference(mi.ReturnType);
                    returnTypeName = compiler.GetTypeOutput(returnType).Replace("TabularEditor.UI.UISelectionList", "IEnumerable").Replace("TabularEditor.TOMWrapper.", "");
                }

                var methodSyntax = returnTypeName + " " + mi.Name + "(" + string.Join(", ", mi.GetParameters().Select(p => p.Name).ToArray()) + ")";

                if (last?.Text == methodName)
                {
                    last.ToolTipText = methodSyntax + Environment.NewLine + last.ToolTipText;
                }
                else
                {
                    last = new MethodAutocompleteItemParen(methodName)
                    {
                        ToolTipTitle = methodSyntax,
                        ToolTipText  = mi.GetCustomAttribute <IntelliSenseAttribute>()?.Description ?? string.Empty,
                        ImageIndex   = TabularIcons.ICON_METHOD
                    };
                    Add(last);
                }
            }

            // Return constants:
            foreach (var fi in type.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy))
            {
                if (fi.IsLiteral)
                {
                    Add(new MethodAutocompleteItem(fi.Name)
                    {
                        ToolTipTitle = $"(constant) int DaxToken.{fi.Name} = {fi.GetRawConstantValue()}",
                        ImageIndex   = TabularIcons.ICON_PROPERTY
                    });
                }
            }

            // Type derives from IEnumerable, so let's add a few select LINQ methods:
            if (type.GetInterface("IEnumerable") != null)
            {
                Add(new MethodAutocompleteItemParen("Select")
                {
                    ToolTipTitle = "object Select(selector)",
                    ToolTipText  = "Projects each item of the collection into something else using a lambda expression.\nExample: .Select(Measure => Measure.Table)",
                    ImageIndex   = TabularIcons.ICON_EXMETHOD
                });
                Add(new MethodAutocompleteItemParen("Any")
                {
                    ToolTipTitle = "bool Any(predicate)",
                    ToolTipText  = "Determines if the collection contains any items. Specify a lambda expression to determine if the collection contains any items satisfying the given condition.\nExample: .Any(Measure => Measure.Description == \"\")",
                    ImageIndex   = TabularIcons.ICON_EXMETHOD
                });
                Add(new MethodAutocompleteItemParen("All")
                {
                    ToolTipTitle = "bool All(predicate)",
                    ToolTipText  = "Determines if all items in the collection satisfies the given condition.\nExample: .All(Measure => Measure.Description == \"\")",
                    ImageIndex   = TabularIcons.ICON_EXMETHOD
                });
                Add(new MethodAutocompleteItemParen("First")
                {
                    ToolTipTitle = type.Name + " First(predicate)",
                    ToolTipText  = "Returns the first element of the sequence, satisfying the (optionally) specified condition.",
                    ImageIndex   = TabularIcons.ICON_EXMETHOD
                });
                Add(new MethodAutocompleteItemParen("Last")
                {
                    ToolTipTitle = type.Name + " Last(predicate)",
                    ToolTipText  = "Returns the last element of the sequency, satisfying the (optionally) specified condition.",
                    ImageIndex   = TabularIcons.ICON_EXMETHOD
                });
                Add(new MethodAutocompleteItemParen("Take")
                {
                    ToolTipTitle = "IEnumerable<" + type.Name + "> Take(count)",
                    ToolTipText  = "Returns a specified number of contiguous elements from the start of a sequence.",
                    ImageIndex   = TabularIcons.ICON_EXMETHOD
                });
                Add(new MethodAutocompleteItemParen("Skip")
                {
                    ToolTipTitle = "IEnumerable<" + type.Name + "> Skip(count)",
                    ToolTipText  = "Bypasses a specified number of elements in a sequence and then returns the remaining elements.",
                    ImageIndex   = TabularIcons.ICON_EXMETHOD
                });

                // Below adds all LINQ methods, which may be overkill:

                /*foreach (var method in typeof(System.Linq.Enumerable)
                 *  .GetMethods(BindingFlags.Static | BindingFlags.Public).Select(m => m.Name).Distinct())
                 * {
                 *  yield return new MethodAutocompleteItem(method + "()")
                 *  {
                 *      ToolTipTitle = method,
                 *      ImageIndex = TabularIcons.ICON_METHOD
                 *  };
                 * }*/
            }

            //return static properties of the class
            foreach (var pi in type.GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                if (pi.GetCustomAttribute <IntelliSenseAttribute>() == null &&
                    pi.GetCustomAttribute <DisplayNameAttribute>() == null &&
                    pi.GetCustomAttribute <BrowsableAttribute>() == null)
                {
                    continue;
                }
                var propType     = new CodeTypeReference(pi.PropertyType);
                var propTypeName = compiler.GetTypeOutput(propType).Replace("TabularEditor.UI.UISelectionList", "IEnumerable").Replace("TabularEditor.TOMWrapper.", "");

                var canRead  = pi.CanRead && pi.GetMethod.IsPublic;
                var canWrite = pi.CanWrite && pi.SetMethod.IsPublic;

                Add(new MethodAutocompleteItem(pi.Name)
                {
                    ToolTipTitle = string.Format("{0} {1} {{ {2}}}", propTypeName, pi.Name, (canRead ? "get; " : "") + (canWrite ? "set; " : "")),
                    ToolTipText  = pi.GetCustomAttribute <IntelliSenseAttribute>()?.Description ?? string.Empty,
                    ImageIndex   = TabularIcons.ICON_PROPERTY
                });
            }

            this.Sort((a, b) => a.Text.CompareTo(b.Text));
        }