Пример #1
0
        public void AnonymouseExpressionTest()
        {
            var output = CompileAndRun(@"new { One = 1, Two = 2.0, Three = 3f }");
            Assert.AreEqual(new { One = 1, Two = 2.0, Three = 3f }.ToString(), output.Value.ToString());

            var details = RexReflectionUtils.ExtractDetails(output.Value).Select(i => i.ToString()).ToList();
            Assert.Contains("int One { get; } = 1", details);
            Assert.Contains("double Two { get; } = 2", details);
            Assert.Contains("float Three { get; } = 3", details);
        }
Пример #2
0
        private IEnumerable <CodeCompletion> SearchWithoutType(Group search, int offset)
        {
            var lowerSearch = search.Value.ToLower();
            var variables   = from i in RexHelper.Variables
                              let lowerItem = i.Key.ToLower()
                                              where lowerItem.Contains(lowerSearch)
                                              select new
            {
                SearchName        = lowerItem,
                IsNested          = false,
                ReplacementString = i.Key,
                Details           = new MemberDetails(i.Value.VarValue,
                                                      RexUtils.GetCSharpRepresentation(i.Value.VarType, false).Concat(new[] {
                    Syntax.Space, Syntax.Name(i.Key),
                    Syntax.Space, Syntax.EqualsOp,
                    Syntax.Space
                }).Concat(RexReflectionUtils.GetSyntaxForValue(i.Value.VarValue))),
                isInScope = true
            };

            var types = from type in RexUtils.AllVisibleTypes
                        let lowerItem = type.Name.ToLower()
                                        where lowerItem.Contains(lowerSearch) && !RexReflectionUtils.IsCompilerGenerated(type)
                                        let isInScope = RexCompileEngine.Instance.NamespaceInfos.Any(i => i.Name == type.Namespace && i.Selected)
                                                        select new
            {
                SearchName = lowerItem,
                type.IsNested,
                ReplacementString = GetNestedName(type),
                Details           = RexUtils.GetCSharpRepresentation(type),
                isInScope
            };

            return(from i in types.Concat(variables)
                   orderby i.SearchName.IndexOf(lowerSearch), i.SearchName.Length, i.isInScope, i.IsNested
                   select new CodeCompletion
            {
                Details = i.Details,
                Start = offset,
                End = offset + search.Length - 1,
                ReplaceString = i.ReplacementString,                           //.Details.Name.String,
                Search = search.Value,
                IsInScope = i.isInScope
            });
        }
Пример #3
0
        /// <summary>
        /// Handles a variable declaration.
        /// </summary>
        /// <param name="varName">Name of the variable</param>
        /// <param name="val">Value of the variable</param>
        /// <param name="showMessages">Should show an warning message or not</param>
        /// <param name="messages">Any errors or warnings are added to this dic.</param>
        private static void DeclaringVariable(string varName, object val, Dictionary <MessageType, List <string> > messages)
        {
            var warning = string.Empty;

            if (val != null)
            {
                var valType = val.GetType();

                if (RexReflectionUtils.ContainsAnonymousType(valType))
                {
                    warning = string.Format("Cannot declare a variable '{0}' with anonymous type", varName);
                }
                else
                {
                    if (!valType.IsVisible)
                    {
                        var interfaces  = valType.GetInterfaces();
                        var iEnumerable = interfaces.FirstOrDefault(t => t.IsGenericType && t.GetInterface("IEnumerable") != null);
                        if (iEnumerable != null)
                        {
                            Variables[varName] = new Variable {
                                VarValue = val, VarType = iEnumerable
                            };
                            return;
                        }
                        warning = string.Format("Expression returned a compiler generated class. Could not declare variable '{0}'", varName);
                    }
                    else
                    {
                        Variables[varName] = new Variable {
                            VarValue = val, VarType = valType
                        };
                        return;
                    }
                }
            }
            else
            {
                warning = string.Format("Expression returned null. Could not declare variable '{0}'", varName);
            }
            messages.Add(MessageType.Warning, warning);
        }
        /// <summary>
        /// Displays a single variable, returns true if the varible was deleted else false.
        /// </summary>
        /// <param name="VarName">Name of the var. (key of the <see cref="RexHelper.Variables"/>)</param>
        /// <param name="var">The varible to display. (Value of the <see cref="RexHelper.Variables"/>)</param>
        private bool DisplayVariable(string VarName, RexHelper.Variable var)
        {
            string highlightedString;

            var type = RexUtils.GetCSharpRepresentation(var.VarType);

            // Format the code for syntax highlighting using richtext.
            highlightedString = RexUIUtils.SyntaxHighlingting(type.Concat(new[] {
                Syntax.Space, Syntax.Name(VarName), Syntax.Space, Syntax.EqualsOp, Syntax.Space
            }).Concat(RexReflectionUtils.GetSyntaxForValue(var.VarValue)));

            var shouldDelete = GUILayout.Button(_texts.GetText("remove_variable", tooltipFormat: VarName), GUILayout.Width(20));

            // Draw the button as a label.
            if (GUILayout.Button(_texts.GetText("inspect_variable", highlightedString, VarName), varLabelStyle, GUILayout.ExpandWidth(true)))
            {
                var ouput = new OutputEntry();
                ouput.LoadObject(var.VarValue);
                RexHelper.AddOutput(ouput);
            }

            return(shouldDelete);
        }
Пример #5
0
        private static Dictionary <string, List <MemberDetails> > GetAllMemberInfos(Type varType, BindingFlags bindings)
        {
            var helpList = new Dictionary <string, List <MemberDetails> >();

            // If it is enum only display it's fields.
            if (varType.IsEnum)
            {
                foreach (var field in varType.GetFields(bindings))
                {
                    helpList.Add(field.Name, new List <MemberDetails> {
                        RexReflectionUtils.GetMemberDetails(field)
                    });
                }
                return(helpList);
            }

            // properties
            foreach (var prop in GetProperties(varType, bindings))
            {
                helpList.Add(prop.Name, new List <MemberDetails> {
                    RexReflectionUtils.GetMemberDetails(prop)
                });
            }

            // fields
            foreach (var field in varType.GetFields(bindings))
            {
                helpList.Add(field.Name, new List <MemberDetails> {
                    RexReflectionUtils.GetMemberDetails(field)
                });
            }

            // methods
            foreach (var metod in from met in varType.GetMethods(bindings)
                     where !Regex.IsMatch(met.Name, propsRegex)
                     select met)
            {
                var infoStr = RexReflectionUtils.GetMemberDetails(metod);
                if (!helpList.ContainsKey(metod.Name))
                {
                    helpList.Add(metod.Name, new List <MemberDetails> {
                        infoStr
                    });
                }
                else
                {
                    helpList[metod.Name].Add(infoStr);
                }
            }

            // extension methods
            //foreach (var metod in from met in Utils.GetExtensionMethods(varType)
            //                      where met.Name.Contains(search.Value)
            //                      select met)
            //{
            //    var infoStr = GetMemberDetails(metod);
            //    if (!helpList.ContainsKey(metod.Name))
            //    {
            //        helpList.Add(metod.Name, new List<MemberDetails> { infoStr });
            //    }
            //    else
            //        helpList[metod.Name].Add(infoStr);
            //}

            return(helpList);
        }
Пример #6
0
 protected override void LoadSingleObject(object value)
 {
     base.LoadSingleObject(value);
     LoadInDetails(value, RexReflectionUtils.ExtractDetails(value));
 }