Example #1
0
        public string[] GetSuggestions(string line, int index)
        {
            if (index > 0)
            {
                line = line.Substring(index, line.Length - index);
            }

            var parts  = line.Split(".");
            var objRef = "this";
            var prefix = line;

            if (parts.Length > 1)
            {
                objRef = string.Join(".", parts.SkipLast(1));
                prefix = parts.Last();
            }

            JSValue obj = null;

            try
            {
                obj = _engine.Evaluate(objRef);
            }
            catch (Exception)
            {
                return(null);
            }

            if (obj.ReferenceValue.ValueType == JavaScriptValueType.Undefined)
            {
                return(null);
            }

            var props   = _engine.GetObjectPropertyNames(obj.ReferenceValue);
            var results = props.Where(x => x.StartsWith(prefix)).Select(x => objRef == "this" ? x : $"{objRef}.{x}")
                          .ToList();

            // Function (append parethesis) or Object (append dot)
            if (results.Count == 1 && results[0] == line)
            {
                var self = _engine.Evaluate(line);
                if (self.ReferenceValue.ValueType == JavaScriptValueType.Function)
                {
                    results[0] = $"{results[0]}(";
                }

                if (self.ReferenceValue.ValueType == JavaScriptValueType.Object)
                {
                    results[0] = $"{results[0]}.";
                }
            }

            return(results.ToArray());
        }
Example #2
0
        private IEnumerable <Span> GetObjectSpans(JavaScriptValue obj, int level, bool inArray)
        {
            var props     = _engine.GetObjectPropertyNames(obj);
            var vals      = new Dictionary <string, JavaScriptValue>();
            var functions = new Dictionary <string, JavaScriptValue>();

            foreach (var p in props.Where(x => !_boringKeys.Contains(x) && !x.StartsWith("_")))
            {
                var pVal = _engine.GetObjectProperty(obj, p);
                switch (pVal.ValueType)
                {
                case JavaScriptValueType.Function:
                    functions[p] = pVal;
                    break;

                default:
                    vals[p] = pVal;
                    break;
                }
            }

            if (level > MaxLevel)
            {
                return(new[] { new Span("{...}") });
            }

            List <Span> spans = new List <Span>();

            spans.Add(new Span("{\n"));
            Action <Dictionary <string, JavaScriptValue>, bool> addToSpans = (dict, shouldContinue) =>
            {
                foreach (var(k, i) in dict.Keys.OrderBy(x => x).Select((k, i) => (k, i)))
                {
                    spans.Add(GetPadding(level + 1));
                    spans.Add(new Span($"{k}: "));
                    spans.AddRange(GetValueSpans(dict[k], level + 1, inArray));
                    if (i < dict.Count - (shouldContinue ? 0 : 1))
                    {
                        spans.Add(new Span(","));
                    }

                    spans.Add(new Span("\n"));
                }
            };

            addToSpans(vals, functions.Count > 0);
            addToSpans(functions, false);
            spans.Add(GetPadding(level - (inArray ? 1 : 0)));
            spans.Add(new Span("}"));
            return(spans);
        }