Ejemplo n.º 1
0
        public static string GetMemberName(this JSONDocument doc, ICompletionSession session)
        {
            if (session == null || doc == null)
            {
                return(null);
            }

            JSONParseItem member = doc.ItemBeforePosition(session.TextView.Caret.Position.BufferPosition.Position);

            if (member != null)
            {
                return(member.Text.Trim('"'));
            }

            return(null);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// This code was copied from CompletionEngine.cs in the CSS code. If this class gets
        /// copied into the CSS code, reuse that other function (CompletionEngine.GetCompletionContextLeafItem)
        /// </summary>
        private static JSONParseItem GetContextItem(JSONDocument styleSheet, int position)
        {
            // Look on both sides of the cursor for a context item.

            JSONParseItem prevItem = styleSheet.ItemBeforePosition(position) ?? styleSheet;
            JSONParseItem nextItem = styleSheet.ItemAfterPosition(position);

            if (position > prevItem.AfterEnd)
            {
                // Not touching the previous item, check its parents

                for (; prevItem != null; prevItem = prevItem.Parent)
                {
                    if (prevItem.IsUnclosed || prevItem.AfterEnd >= position)
                    {
                        break;
                    }
                }
            }

            // Only use the next item if the cursor is touching it, and it's not a comment
            if (nextItem != null && (position < nextItem.Start))// || nextItem.FindType<Comment>() != null))
            {
                nextItem = null;
            }

            // When two things touch the cursor inside of a selector, always prefer the previous item.
            // If this logic gets larger, consider a better design to choose between two items.
            if (nextItem != null &&
                prevItem != null &&
                prevItem.AfterEnd == position)
            {
                nextItem = null;
            }

            return(nextItem ?? prevItem);
        }
Ejemplo n.º 3
0
        public static JSONCompletionOptionResult GetCompletionOptions(StringWithFileNameTextProvider instanceTextProvider, StringWithFileNameTextProvider schemaTextProvider, IEnumerable <IJSONSchemaFormatHandler> formatHandlers, int cursorPosition)
        {
            JSONDocumentLoader loader      = new JSONDocumentLoader();
            JSONParser         parser      = new JSONParser();
            JSONDocument       schemaDoc   = parser.Parse(schemaTextProvider);
            JSONDocument       instanceDoc = parser.Parse(instanceTextProvider);
            List <JSONOption>  allOptions  = new List <JSONOption>();

            loader.SetCacheItem(new JSONDocumentLoadResult(schemaDoc));
            loader.SetCacheItem(new JSONDocumentLoadResult(instanceDoc));

            JSONSchemaDraft4EvaluationTreeNode tree = JSONSchemaDraft4EvaluationTreeProducer.CreateEvaluationTreeAsync(instanceDoc, (JSONObject)schemaDoc.TopLevelValue, loader, formatHandlers).Result;
            var parseItem = instanceDoc.ItemBeforePosition(cursorPosition);

            var convertToReportMethod = typeof(JSONSchemaDraft4Evaluator).GetMethod("ConvertToReport", BindingFlags.Static | BindingFlags.NonPublic);
            var report = (JSONSchemaDraft4EvaluationReport)convertToReportMethod.Invoke(null, new object[] { DateTime.Now, tree });

            if (parseItem?.PreviousSibling is JSONMember && parseItem.PreviousSibling.IsUnclosed)
            {
                parseItem = parseItem.PreviousSibling;
            }

            var originalItem = parseItem;

            while (parseItem is JSONTokenItem)
            {
                parseItem = parseItem.Parent;
            }

            JSONMember member = parseItem as JSONMember;

            if (member != null)
            {
                //Don't glue completion to the previous member's value if calling up completion after a comma
                if ((member.Comma == null || cursorPosition < member.Comma.AfterEnd) && member.Name != originalItem)
                {
                    var options = report.GetValueOptionsAsync(member, cursorPosition).Result;

                    foreach (var option in options)
                    {
                        allOptions.Add(new JSONOption
                        {
                            InsertionText = option.InsertionText,
                            DisplayText   = option.DisplayText,
                            Type          = option.Format
                        });
                    }

                    return(new JSONCompletionOptionResult
                    {
                        ReplaceStart = member.Value?.Start ?? member.Colon.AfterEnd,
                        ReplaceLength = member.Value?.Length ?? 0,
                        Options = allOptions
                    });
                }

                parseItem = parseItem.Parent;
            }

            JSONArrayElement arrayElement = parseItem as JSONArrayElement;

            if (arrayElement != null)
            {
                JSONArray arr   = (JSONArray)arrayElement.Parent;
                int       index = arr.Elements.IndexOf(arrayElement);

                var options = report.GetValueOptionsAsync(arr, index, cursorPosition).Result;

                foreach (var option in options)
                {
                    allOptions.Add(new JSONOption
                    {
                        InsertionText = option.InsertionText,
                        DisplayText   = option.DisplayText,
                        Type          = option.TypeDescription
                    });
                }

                return(new JSONCompletionOptionResult
                {
                    ReplaceStart = cursorPosition,
                    ReplaceLength = 0,
                    Options = allOptions
                });
            }

            JSONArray array = parseItem as JSONArray;

            if (array != null)
            {
                int index;
                for (index = 0; index < array.Elements.Count && cursorPosition > array.Elements[index].AfterEnd; ++index)
                {
                }

                var options = report.GetValueOptionsAsync(array, index, cursorPosition).Result;

                foreach (var option in options)
                {
                    allOptions.Add(new JSONOption
                    {
                        InsertionText = option.InsertionText,
                        DisplayText   = option.DisplayText,
                        Type          = option.TypeDescription
                    });
                }

                return(new JSONCompletionOptionResult
                {
                    ReplaceStart = cursorPosition,
                    ReplaceLength = 0,
                    Options = allOptions
                });
            }

            JSONObject obj = parseItem as JSONObject;

            if (obj != null)
            {
                var options = report.GetPropertyInfosAsync(obj).Result;

                foreach (var option in options)
                {
                    JSONMember existingMember = obj.Members.FirstOrDefault(x => x.CanonicalizedNameText == option.DisplayText);
                    if (existingMember == null || (member != null && existingMember == member && cursorPosition < existingMember.AfterEnd))
                    {
                        allOptions.Add(new JSONOption
                        {
                            InsertionText = option.InsertionText,
                            DisplayText   = option.DisplayText,
                            Type          = option.TypeDescription
                        });
                    }
                }

                int replaceStart  = member?.AfterEnd >= cursorPosition ? member.Name.Start : cursorPosition;
                int replaceLength = member?.AfterEnd >= cursorPosition ? member.Name.Length : 0;

                return(new JSONCompletionOptionResult
                {
                    ReplaceStart = replaceStart,
                    ReplaceLength = replaceLength,
                    Options = allOptions
                });
            }

            return(new JSONCompletionOptionResult
            {
                Options = new List <JSONOption>()
            });
        }