コード例 #1
0
        protected override JSONItemValidationResult ValidateJsonItem(JSONParseItem item, IJSONValidationContext context)
        {
            var member = item as JSONMember;

            if (member.UnquotedNameText != "version")
            {
                return(JSONItemValidationResult.Continue);
            }

            var package  = item.Parent?.Parent as JSONMember;
            var packages = package?.Parent?.Parent as JSONMember;

            if (package == null || packages == null || packages.UnquotedNameText != "packages")
            {
                return(JSONItemValidationResult.Continue);
            }

            var versions = VSPackage.Manager.Provider.GetVersionsAsync(package.UnquotedNameText).Result;

            if (versions != null && !versions.Contains(member.UnquotedValueText))
            {
                string message = $"({Vsix.Name}) \"{member.UnquotedValueText}\" is not a valid version for \"{package.UnquotedNameText}\".";
                AddError(context, member.Value, message);
            }

            return(JSONItemValidationResult.Continue);
        }
コード例 #2
0
        public JSONItemValidationResult ValidateItem(JSONParseItem item, IJSONValidationContext context)
        {
            JSONMember member = item as JSONMember;

            if (member != null && member.UnquotedNameText == "icon")
            {
                var icons = IconCompletionProvider.GetIcons(member);

                if (icons != null && !icons.ContainsKey(member.UnquotedValueText))
                {
                    JsonErrorTag error = new JsonErrorTag
                    {
                        Flags    = JSONErrorFlags.ErrorListError | JSONErrorFlags.UnderlineRed,
                        Item     = member.Value,
                        Start    = member.Value.Start,
                        AfterEnd = member.Value.AfterEnd,
                        Length   = member.Value.Length,
                        Text     = $"The icon \"{member.UnquotedValueText}\" has not been declared"
                    };

                    Telemetry.TrackEvent("Icon no declared");
                    context.AddError(error);
                }
            }

            return(JSONItemValidationResult.Continue);
        }
コード例 #3
0
        public static IDictionary <string, string> GetIcons(JSONParseItem item)
        {
            var visitor = new JSONItemCollector <JSONMember>(true);
            var dic     = new Dictionary <string, string>();

            if (!item.JSONDocument.Accept(visitor))
            {
                return(dic);
            }

            var icons = visitor.Items.FirstOrDefault(m => m.UnquotedNameText == "icons");
            var value = icons?.Value as JSONObject;

            if (value == null)
            {
                return(dic);
            }

            string folder = Path.GetDirectoryName(item.JSONDocument.DocumentLocation);

            foreach (JSONMember icon in value.Children.OfType <JSONMember>())
            {
                dic.Add(icon.UnquotedNameText, icon.UnquotedValueText);
            }

            return(dic);
        }
コード例 #4
0
        public JSONItemValidationResult ValidateItem(JSONParseItem item, IJSONValidationContext context)
        {
            JSONMember member = item as JSONMember;

            if (member != null && member.UnquotedNameText == "icon")
            {
                var icons = IconCompletionProvider.GetIcons(member);

                if (icons != null && !icons.ContainsKey(member.UnquotedValueText))
                {
                    JsonErrorTag error = new JsonErrorTag
                    {
                        Flags = JSONErrorFlags.ErrorListError | JSONErrorFlags.UnderlineRed,
                        Item = member.Value,
                        Start = member.Value.Start,
                        AfterEnd = member.Value.AfterEnd,
                        Length = member.Value.Length,
                        Text = $"The icon \"{member.UnquotedValueText}\" has not been declared"
                    };

                    Telemetry.TrackEvent("Icon no declared");
                    context.AddError(error);
                }
            }

            return JSONItemValidationResult.Continue;
        }
コード例 #5
0
ファイル: NameValidator.cs プロジェクト: scottaddie/Packman
        protected override JSONItemValidationResult ValidateJsonItem(JSONParseItem item, IJSONValidationContext context)
        {
            var member   = item as JSONMember;
            var packages = item.Parent?.Parent as JSONMember;

            if (packages == null || packages.UnquotedNameText != "packages")
            {
                return(JSONItemValidationResult.Continue);
            }

            var children = (member.Value as JSONObject)?.BlockItemChildren?.OfType <JSONMember>();

            if (!children.Any(c => c.UnquotedNameText == "version"))
            {
                return(JSONItemValidationResult.Continue);
            }

            var names = VSPackage.Manager.Provider.GetPackageNamesAsync().Result;

            if (names != null && !names.Contains(member.UnquotedNameText))
            {
                string message = $"({Vsix.Name}) The package \"{member.UnquotedNameText}\" does not exist";
                AddError(context, member.Name, message);
            }

            return(JSONItemValidationResult.Continue);
        }
コード例 #6
0
        public JSONItemValidationResult ValidateItem(JSONParseItem item, IJSONValidationContext context)
        {
            JSONMember member = item as JSONMember;

            if (member == null || !member.UnquotedValueText.Contains("{{"))
                return JSONItemValidationResult.Continue;

            var variables = GetVariables(member);
            var regex = new Regex("{{(?<name>[^}{]+)}}", RegexOptions.Compiled);

            foreach (Match match in regex.Matches(member.UnquotedValueText))
            {
                Group group = match.Groups["name"];
                string name = group.Value;

                if (!variables.Contains(name))
                {
                    JsonErrorTag error = new JsonErrorTag
                    {
                        Flags = JSONErrorFlags.ErrorListError | JSONErrorFlags.UnderlineRed,
                        Item = member.Value,
                        Start = member.Value.Start + group.Index + 1, // 1 is for the quotation mark
                        AfterEnd = member.Value.Start + group.Index + group.Length,
                        Length = group.Length,
                        Text = $"The variable \"{name}\" has not been declared"
                    };

                    Telemetry.TrackEvent("Icon no declared");
                    context.AddError(error);
                }
            }

            return JSONItemValidationResult.Continue;
        }
コード例 #7
0
        private JSONParseItem GetItemBeforePosition(int pos, JSONComplexItem parentItem)
        {
            JSONParseItem     item     = null;
            JSONParseItemList children = parentItem.Children;
            int start = 0;

            if (children.Any())
            {
                start = children[0].Start;
            }

            if (start < pos)
            {
                int i = FindInsertIndex(children, pos, beforeExisting: true) - 1;

                if (i >= 0)
                {
                    item = children[i];

                    if (item is JSONComplexItem complexItem)
                    {
                        // Recurse to find the deepest item
                        item = GetItemBeforePosition(pos, complexItem);
                    }
                }
            }

            return(item);
        }
コード例 #8
0
        public JSONItemValidationResult ValidateItem(JSONParseItem item, IJSONValidationContext context)
        {
            JSONMember member = item as JSONMember;

            if (member != null && member.Name != null && member.Name.Text == "\"$ref\"")
            {
                var parent = member.FindType <JSONBlockItem>();

                // Only show error when $ref is not the only property in the object
                if (parent == null || parent.BlockItemChildren.Count() == 1)
                {
                    return(JSONItemValidationResult.Continue);
                }

                JsonErrorTag error = new JsonErrorTag
                {
                    Flags    = JSONErrorFlags.UnderlineBlue | JSONErrorFlags.ErrorListMessage,
                    Item     = member.Name,
                    Start    = member.Name.Start,
                    AfterEnd = member.Name.AfterEnd,
                    Length   = member.Name.Length,
                    Text     = "When $ref is present, all other attributes are ignored"
                };

                context.AddError(error);
            }

            return(JSONItemValidationResult.Continue);
        }
コード例 #9
0
        public JSONItemValidationResult ValidateItem(JSONParseItem item, IJSONValidationContext context)
        {
            JSONMember member = item as JSONMember;
            JSONMember icons  = item?.Parent?.FindType <JSONMember>();

            if (icons != null && icons.UnquotedNameText == "icons")
            {
                string folder = Path.GetDirectoryName(item.JSONDocument.DocumentLocation);
                string file   = Path.Combine(folder, member.UnquotedValueText);

                if (!File.Exists(file))
                {
                    JsonErrorTag error = new JsonErrorTag
                    {
                        Flags    = JSONErrorFlags.ErrorListError | JSONErrorFlags.UnderlineRed,
                        Item     = member.Value,
                        Start    = member.Value.Start,
                        AfterEnd = member.Value.AfterEnd,
                        Length   = member.Value.Length,
                        Text     = $"The file \"{member.UnquotedValueText}\" does not exist"
                    };

                    Telemetry.TrackEvent("Icon file missing");
                    context.AddError(error);
                }
            }

            return(JSONItemValidationResult.Continue);
        }
コード例 #10
0
        private async void RetriggerAsync(bool force)
        {
            _lastTyped = DateTime.Now;
            int delay = force ? 50 : _delay;

            await System.Threading.Tasks.Task.Delay(delay);

            // Prevents retriggering from happening while typing fast
            if (_lastTyped.AddMilliseconds(delay) > DateTime.Now)
            {
                return;
            }

            var           doc       = JSONEditorDocument.FromTextView(_textView);
            JSONParseItem parseItem = doc?.JSONDocument.ItemBeforePosition(_textView.Caret.Position.BufferPosition);

            if (parseItem == null)
            {
                return;
            }

            JSONMember member = parseItem.FindType <JSONMember>();

            if (!member.UnquotedNameText.Equals("id") && !member.UnquotedNameText.Equals("path") && member.UnquotedValueText?.Length <= 1)
            {
                return;
            }

            JSONObject parent = parseItem.FindType <JSONObject>();

            if (JsonHelpers.TryGetInstallationState(parent, out ILibraryInstallationState state))
            {
                VsHelpers.DTE.ExecuteCommand("Edit.ListMembers");
            }
        }
コード例 #11
0
        public virtual IEnumerable <ISmartTagAction> GetSmartTagActions(JSONParseItem item, int caretPosition, ITrackingSpan itemTrackingSpan, ITextView view)
        {
            string fileName = itemTrackingSpan.TextBuffer.GetFileName();

            if (string.IsNullOrEmpty(fileName) || Path.GetFileName(fileName) != SupportedFileName)
            {
                return(Enumerable.Empty <ISmartTagAction>());
            }

            JSONMember member = (JSONMember)item;

            if (!member.Name.ContainsRange(caretPosition, 1))
            {
                return(Enumerable.Empty <ISmartTagAction>());
            }

            JSONMember parent = member.Parent.FindType <JSONMember>();

            if (parent == null || !parent.UnquotedNameText.EndsWith("dependencies", StringComparison.OrdinalIgnoreCase))
            {
                return(Enumerable.Empty <ISmartTagAction>());
            }

            return(GetSmartTagActions(member, itemTrackingSpan.TextBuffer));
        }
コード例 #12
0
        public JSONItemValidationResult ValidateItem(JSONParseItem item, IJSONValidationContext context)
        {
            JSONMember member = item as JSONMember;
            JSONMember icons = item?.Parent?.FindType<JSONMember>();

            if (icons != null && icons.UnquotedNameText == "icons")
            {
                string folder = Path.GetDirectoryName(item.JSONDocument.DocumentLocation);
                string file = Path.Combine(folder, member.UnquotedValueText);

                if (!File.Exists(file))
                {
                    JsonErrorTag error = new JsonErrorTag
                    {
                        Flags = JSONErrorFlags.ErrorListError | JSONErrorFlags.UnderlineRed,
                        Item = member.Value,
                        Start = member.Value.Start,
                        AfterEnd = member.Value.AfterEnd,
                        Length = member.Value.Length,
                        Text = $"The file \"{member.UnquotedValueText}\" does not exist"
                    };

                    Telemetry.TrackEvent("Icon file missing");
                    context.AddError(error);
                }
            }

            return JSONItemValidationResult.Continue;
        }
コード例 #13
0
ファイル: BaseValidator.cs プロジェクト: gep13/Packman
        public JSONItemValidationResult ValidateItem(JSONParseItem item, IJSONValidationContext context)
        {
            if (!VSPackage.Manager.Provider.IsInitialized)
                return JSONItemValidationResult.Continue;

            return ValidateJsonItem(item, context);
        }
コード例 #14
0
ファイル: FileValidator.cs プロジェクト: gep13/Packman
        protected override JSONItemValidationResult ValidateJsonItem(JSONParseItem item, IJSONValidationContext context)
        {
            var member = item.FindType<JSONMember>();

            if (member == null || member.UnquotedNameText != "files")
                return JSONItemValidationResult.Continue;

            var package = member.Parent?.Parent as JSONMember;
            var packages = package?.Parent?.Parent as JSONMember;

            if (package == null || packages == null || packages.UnquotedNameText != "packages")
                return JSONItemValidationResult.Continue;

            var parent = member.Parent as JSONObject;
            var children = parent?.BlockItemChildren.OfType<JSONMember>();
            var version = children?.SingleOrDefault(c => c.UnquotedNameText == "version");

            if (version == null)
                return JSONItemValidationResult.Continue;

            var installable = VSPackage.Manager.Provider.GetInstallablePackageAsync(package.UnquotedNameText, version.UnquotedValueText).Result;

            if (installable == null)
                return JSONItemValidationResult.Continue;

            if (!installable.AllFiles.Contains(item.Text.Trim('"', ',')))
            {
                string message = $"({VSPackage.Name}) {item.Text} is not a valid file name for {package.Name.Text}.";
                AddError(context, item, message);
            }

            return JSONItemValidationResult.Continue;
        }
コード例 #15
0
        public JSONItemValidationResult ValidateItem(JSONParseItem item, IJSONValidationContext context)
        {
            JSONMember member = item as JSONMember;

            if (member != null && member.Name != null && member.Name.Text == "\"$ref\"")
            {
                var parent = member.FindType<JSONBlockItem>();

                // Only show error when $ref is not the only property in the object
                if (parent == null || parent.BlockItemChildren.Count() == 1)
                    return JSONItemValidationResult.Continue;

                JsonErrorTag error = new JsonErrorTag
                {
                    Flags = JSONErrorFlags.UnderlineBlue | JSONErrorFlags.ErrorListMessage,
                    Item = member.Name,
                    Start = member.Name.Start,
                    AfterEnd = member.Name.AfterEnd,
                    Length = member.Name.Length,
                    Text = "When $ref is present, all other attributes are ignored"
                };

                context.AddError(error);
            }

            return JSONItemValidationResult.Continue;
        }
コード例 #16
0
        public static IEnumerable <string> GetVariables(JSONParseItem item)
        {
            var visitor = new JSONItemCollector <JSONMember>(true);

            if (!item.JSONDocument.Accept(visitor))
            {
                return(null);
            }

            var           contents = visitor.Items.Where(m => m.UnquotedNameText == "content" && m.IsValid);
            List <string> list     = new List <string>();

            foreach (JSONMember content in contents)
            {
                var value = content?.Value as JSONObject;

                if (value == null)
                {
                    continue;
                }

                var names = value.Children.OfType <JSONMember>().Select(s => s.UnquotedNameText);

                list.AddRange(names.Where(n => !list.Contains(n)));
            }

            return(list);
        }
コード例 #17
0
        public IEnumerable <ITagSpan <JSONSmartTag> > GetTags(NormalizedSnapshotSpanCollection spans)
        {
            List <ITagSpan <JSONSmartTag> > tags = new List <ITagSpan <JSONSmartTag> >();

            if (!EnsureInitialized())
            {
                return(tags);
            }

            // Map view caret to the CSS buffer
            ProjectionSelectionHelper selectionHelpers = new ProjectionSelectionHelper(_textView, new[] { _textBuffer });
            SnapshotPoint?            bufferPoint      = selectionHelpers.MapFromViewToBuffer(_textView.Caret.Position.BufferPosition);

            if (!bufferPoint.HasValue)
            {
                return(tags);
            }

            JSONParseItem currentItem = GetContextItem(_tree, bufferPoint.Value.Position);

            if (currentItem == null || currentItem.Parent == null)
            {
                return(tags);
            }

            JSONParseItem parent = currentItem.Parent;

            IEnumerable <IJSONSmartTagProvider> providers = _smartTagProviders.GetHandlers(parent.GetType());
            List <ISmartTagAction> actions = new List <ISmartTagAction>();

            if (providers != null && _textBuffer.CurrentSnapshot.Length >= currentItem.AfterEnd)
            {
                ITrackingSpan trackingSpan = _textBuffer.CurrentSnapshot.CreateTrackingSpan(currentItem.Start, currentItem.Length, SpanTrackingMode.EdgeInclusive);

                foreach (IJSONSmartTagProvider provider in providers)
                {
                    IEnumerable <ISmartTagAction> newActions = provider.GetSmartTagActions(parent, bufferPoint.Value.Position, trackingSpan, _textView);

                    if (newActions != null)
                    {
                        actions.AddRange(newActions);
                    }
                }
            }

            if (actions.Count > 0)
            {
                SmartTagActionSet        actionSet  = new SmartTagActionSet(actions.AsReadOnly());
                List <SmartTagActionSet> actionSets = new List <SmartTagActionSet>();
                actionSets.Add(actionSet);

                SnapshotSpan itemSpan = new SnapshotSpan(_textBuffer.CurrentSnapshot, currentItem.Start, currentItem.Length);
                JSONSmartTag smartTag = new JSONSmartTag(actionSets.AsReadOnly());

                tags.Add(new TagSpan <JSONSmartTag>(itemSpan, smartTag));
            }

            return(tags);
        }
コード例 #18
0
        public void JSONHelpers_GetItemBeforePosition_EmptyFile()
        {
            JSONComplexItem complexItem = JSONParser.Parse(string.Empty);

            JSONParseItem item = JsonHelpers.GetItemBeforePosition(0, complexItem);

            Assert.IsNull(item);
        }
コード例 #19
0
        private async void RetriggerAsync(bool force)
        {
            _lastTyped = DateTime.Now;
            int delay = force ? 50 : _delay;

            // Don't leave "stale" completion session up while the user is typing, or else we could
            // get completion from a stale session.
            ICompletionSession completionSession = _broker.GetSessions(_textView).FirstOrDefault();

            if (completionSession != null && completionSession.Properties.TryGetProperty <bool>(RetriggerCompletion, out bool retrigger) && retrigger)
            {
                completionSession.Dismiss();
            }

            await System.Threading.Tasks.Task.Delay(delay);

            // Prevents retriggering from happening while typing fast
            if (_lastTyped.AddMilliseconds(delay) > DateTime.Now)
            {
                return;
            }

            // Completion may have gotten invoked via by Web Editor OnPostTypeChar(). Don't invoke again, or else we get flikering completion list
            // TODO:Review the design here post-preview 4 and make sure this completion controller doesn't clash with Web Editors JSON completion controller
            completionSession = _broker.GetSessions(_textView).FirstOrDefault();
            if (completionSession != null && completionSession.Properties.TryGetProperty <bool>(RetriggerCompletion, out retrigger))
            {
                return;
            }

            var doc = JSONEditorDocument.FromTextBuffer(_textView.TextDataModel.DocumentBuffer);

            if (doc == null)
            {
                return;
            }

            JSONParseItem parseItem = GetItemBeforePosition(_textView.Caret.Position.BufferPosition, doc.JSONDocument);

            if (parseItem == null)
            {
                return;
            }

            JSONMember member = parseItem.FindType <JSONMember>();

            if (member == null || (!member.UnquotedNameText.Equals(ManifestConstants.Library) && !member.UnquotedNameText.Equals(ManifestConstants.Destination) && member.UnquotedValueText?.Length <= 1))
            {
                return;
            }

            JSONObject parent = parseItem.FindType <JSONObject>();

            if (JsonHelpers.TryGetInstallationState(parent, out ILibraryInstallationState state))
            {
                VsHelpers.DTE.ExecuteCommand("Edit.ListMembers");
            }
        }
コード例 #20
0
        public void AugmentQuickInfoSession(IQuickInfoSession session, IList <object> qiContent, out ITrackingSpan applicableToSpan)
        {
            applicableToSpan = null;

            if (session == null || qiContent == null)
            {
                return;
            }

            // Map the trigger point down to our buffer.
            SnapshotPoint?point = session.GetTriggerPoint(_buffer.CurrentSnapshot);

            if (!point.HasValue)
            {
                return;
            }

            JSONEditorDocument doc  = JSONEditorDocument.FromTextBuffer(_buffer);
            JSONParseItem      item = doc.JSONDocument.ItemBeforePosition(point.Value.Position);

            if (item == null || !item.IsValid)
            {
                return;
            }

            JSONMember dependency = item.FindType <JSONMember>();

            if (dependency == null)
            {
                return;
            }

            var parent = dependency.Parent.FindType <JSONMember>();

            if (parent == null || !parent.UnquotedNameText.EndsWith("dependencies", StringComparison.OrdinalIgnoreCase))
            {
                return;
            }

            applicableToSpan = _buffer.CurrentSnapshot.CreateTrackingSpan(item.Start, item.Length, SpanTrackingMode.EdgeNegative);

            if (dependency.Value == item)
            {
                string version = item.Text.Trim('"', '~', '^');
                qiContent.Add("   " + version + "  " + Resource.CompletionVersionLatest + Environment.NewLine +
                              "~" + version + "  " + Resource.CompletionVersionMinor + Environment.NewLine +
                              "^" + version + "  " + Resource.CompletionVersionMajor);
            }
            else
            {
                UIElement element = CreateTooltip(dependency.UnquotedNameText, item);

                if (element != null)
                {
                    qiContent.Add(element);
                }
            }
        }
コード例 #21
0
        public override UIElement CreateTooltip(string name, JSONParseItem item)
        {
            BowerPackage package = GetText(name);

            if (package == null)
                return null;

            return BowerInfoBox.Create(package);
        }
コード例 #22
0
        [DataRow(170, "}")]                // The last token item position
        public void JSONHelpers_GetItemBeforePosition_InvalidJson(int position, string expectedText)
        {
            JSONComplexItem complexItem = JSONParser.Parse(_invalidJsonText);

            JSONParseItem item = JsonHelpers.GetItemBeforePosition(position, complexItem);

            Assert.IsTrue(item is JSONTokenItem);
            Assert.AreEqual(expectedText, item.Text);
        }
コード例 #23
0
        public override UIElement CreateTooltip(string name, JSONParseItem item)
        {
            NpmPackage package = NpmPackage.FromPackageName(name);

            if (package == null)
                return null;

            return NpmInfoBox.Create(package);
        }
コード例 #24
0
        private JSONParseItem GetPreviousChild(JSONParseItem child, JSONParseItemList children)
        {
            int index = (child != null) ? children.IndexOf(child) : -1;

            if (index > 0)
            {
                return(children[index - 1]);
            }

            return(null);
        }
コード例 #25
0
        private JSONParseItem GetNextChild(JSONParseItem child, JSONParseItemList children)
        {
            int index = (child != null) ? children.IndexOf(child) : -1;

            if (index != -1 && index + 1 < children.Count)
            {
                return(children[index + 1]);
            }

            return(null);
        }
コード例 #26
0
        public static string SelectItemText(this JSONBlockItem block, string selector)
        {
            JSONParseItem item = SelectItem(block, selector);

            if (item != null)
            {
                string text = item.Text.Replace("\\\"", "\"");
                return(text.Substring(1, text.Length - 2));
            }
            return(string.Empty);
        }
コード例 #27
0
        public override UIElement CreateTooltip(string name, JSONParseItem item)
        {
            NpmPackage package = NpmPackage.FromPackageName(name);

            if (package == null)
            {
                return(null);
            }

            return(NpmInfoBox.Create(package));
        }
コード例 #28
0
        public JSONItemValidationResult ValidateItem(JSONParseItem item, IJSONValidationContext context)
        {
            if (!VSPackage.Manager.Provider.IsInitialized)
                return JSONItemValidationResult.Continue;

            if (item.JSONDocument.HasSchema(_reportCache))
            {
                return ValidateJsonItem(item, context);
            }

            return JSONItemValidationResult.Continue;
        }
コード例 #29
0
        public VisitItemResult Visit(JSONParseItem parseItem)
        {
            var item = parseItem as T;

            if (item != null)
            {
                Items.Add(item);
                return((_includeChildren) ? VisitItemResult.Continue : VisitItemResult.SkipChildren);
            }

            return(VisitItemResult.Continue);
        }
コード例 #30
0
ファイル: BaseValidator.cs プロジェクト: scottaddie/Packman
        public JSONItemValidationResult ValidateItem(JSONParseItem item, IJSONValidationContext context)
        {
            if (!VSPackage.Manager.Provider.IsInitialized)
            {
                return(JSONItemValidationResult.Continue);
            }

            if (item.JSONDocument.HasSchema(_reportCache))
            {
                return(ValidateJsonItem(item, context));
            }

            return(JSONItemValidationResult.Continue);
        }
コード例 #31
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);
        }
コード例 #32
0
        public static IEnumerable<Tuple<string, string>> GetTasks(JSONParseItem item)
        {
            var visitor = new JSONItemCollector<JSONMember>(true);
            item.JSONDocument.Accept(visitor);

            var scripts = visitor.Items.FirstOrDefault(member => member.UnquotedNameText == "scripts");

            if (scripts == null)
                yield break;

            foreach (JSONObject child in scripts.Children.Where(c => c is JSONObject))
                foreach (JSONMember taskItem in child.Children.Where(c => c is JSONMember))
                {
                    yield return Tuple.Create(taskItem.UnquotedNameText, taskItem.Value.Text);
                }
        }
コード例 #33
0
        protected static void AddError(IJSONValidationContext context, JSONParseItem item, string message)
        {
            if (item == null)
                return;

            var error = new JsonErrorTag
            {
                Flags = JSONErrorFlags.ErrorListError | JSONErrorFlags.UnderlineRed,
                Item = item,
                Start = item.Start,
                AfterEnd = item.AfterEnd,
                Length = item.Length,
                Text = message
            };

            context.AddError(error);
        }
コード例 #34
0
        public void AugmentQuickInfoSession(IQuickInfoSession session, IList <object> qiContent, out ITrackingSpan applicableToSpan)
        {
            applicableToSpan = null;

            if (session == null || qiContent == null || qiContent.Count > 0)
            {
                return;
            }

            // Map the trigger point down to our buffer.
            SnapshotPoint?point = session.GetTriggerPoint(_buffer.CurrentSnapshot);

            if (!point.HasValue)
            {
                return;
            }

            var           doc  = JSONEditorDocument.FromTextBuffer(_buffer);
            JSONParseItem item = doc.JSONDocument.ItemBeforePosition(point.Value.Position);

            if (item == null || !item.IsValid)
            {
                return;
            }

            JSONMember member = item.FindType <JSONMember>();

            if (member == null || member.Name == null)
            {
                return;
            }

            IJSONSchema schema = _schemaResolver.DetermineSchemaForTextBuffer(_buffer);

            if (schema != null)
            {
                IJSONSchemaPropertyNameCompletionInfo info = Foo(schema, member);

                if (info != null && !string.IsNullOrEmpty(info.PropertyDocumentation))
                {
                    applicableToSpan = _buffer.CurrentSnapshot.CreateTrackingSpan(item.Start, item.Length, SpanTrackingMode.EdgeNegative);
                    qiContent.Add(info.DisplayText + Environment.NewLine + info.PropertyDocumentation);
                }
            }
        }
コード例 #35
0
ファイル: NameValidator.cs プロジェクト: gep13/Packman
        protected override JSONItemValidationResult ValidateJsonItem(JSONParseItem item, IJSONValidationContext context)
        {
            var member = item as JSONMember;
            var packages = item.Parent?.Parent as JSONMember;

            if (packages == null || packages.UnquotedNameText != "packages")
                return JSONItemValidationResult.Continue;

            var names = VSPackage.Manager.Provider.GetPackageNamesAsync().Result;

            if (names != null && !names.Contains(member.UnquotedNameText))
            {
                string message = $"({VSPackage.Name}) The package \"{member.UnquotedNameText}\" does not exist";
                AddError(context, member.Name, message);
            }

            return JSONItemValidationResult.Continue;
        }
コード例 #36
0
        private static bool IsBandManifest(JSONParseItem item)
        {
            string fileName = Path.GetFileName(item.JSONDocument?.DocumentLocation);

            if (string.IsNullOrEmpty(fileName) || !fileName.Equals("manifest.json", StringComparison.OrdinalIgnoreCase))
            {
                return(false);
            }

            if (item.JSONDocument.Text.Contains("\"manifestVersion\"") ||
                item.JSONDocument.Text.Contains("\"versionString\"") ||
                item.JSONDocument.Text.Contains("\"tileIcon\""))
            {
                return(true);
            }

            return(false);
        }
コード例 #37
0
        public virtual IEnumerable<ISmartTagAction> GetSmartTagActions(JSONParseItem item, int caretPosition, ITrackingSpan itemTrackingSpan, ITextView view)
        {
            string fileName = itemTrackingSpan.TextBuffer.GetFileName();

            if (string.IsNullOrEmpty(fileName) || Path.GetFileName(fileName) != SupportedFileName)
                return Enumerable.Empty<ISmartTagAction>();

            JSONMember member = (JSONMember)item;

            if (!member.Name.ContainsRange(caretPosition, 1))
                return Enumerable.Empty<ISmartTagAction>();

            JSONMember parent = member.Parent.FindType<JSONMember>();

            if (parent == null || !parent.UnquotedNameText.EndsWith("dependencies", StringComparison.OrdinalIgnoreCase))
                return Enumerable.Empty<ISmartTagAction>();

            return GetSmartTagActions(member, itemTrackingSpan.TextBuffer);
        }
コード例 #38
0
ファイル: BaseValidator.cs プロジェクト: scottaddie/Packman
        protected static void AddError(IJSONValidationContext context, JSONParseItem item, string message)
        {
            if (item == null)
            {
                return;
            }

            var error = new JsonErrorTag
            {
                Flags    = JSONErrorFlags.ErrorListError | JSONErrorFlags.UnderlineRed,
                Item     = item,
                Start    = item.Start,
                AfterEnd = item.AfterEnd,
                Length   = item.Length,
                Text     = message
            };

            context.AddError(error);
        }
コード例 #39
0
        private static IEnumerable<IVocabulary> FindApplicableVocabularies(JSONParseItem member, IEnumerable<IVocabulary> vocabularies)
        {
            var parent = member.FindType<JSONBlockItem>();

            while (parent != null)
            {
                var visitor = new JSONItemCollector<JSONMember>();
                parent.Accept(visitor);

                var context = visitor.Items.FirstOrDefault(c => c.Name != null && c.Name.Text == "\"@context\"");

                foreach (IVocabulary vocab in vocabularies)
                {
                    if (vocab.AppliesToContext(context))
                        yield return vocab;
                }

                parent = parent.Parent.FindType<JSONBlockItem>();
            }
        }
コード例 #40
0
        protected override JSONItemValidationResult ValidateJsonItem(JSONParseItem item, IJSONValidationContext context)
        {
            var member = item.FindType <JSONMember>();

            if (member == null || member.UnquotedNameText != "files")
            {
                return(JSONItemValidationResult.Continue);
            }

            var package  = member.Parent?.Parent as JSONMember;
            var packages = package?.Parent?.Parent as JSONMember;

            if (package == null || packages == null || packages.UnquotedNameText != "packages")
            {
                return(JSONItemValidationResult.Continue);
            }

            var parent   = member.Parent as JSONObject;
            var children = parent?.BlockItemChildren.OfType <JSONMember>();
            var version  = children?.SingleOrDefault(c => c.UnquotedNameText == "version");

            if (version == null)
            {
                return(JSONItemValidationResult.Continue);
            }

            var installable = VSPackage.Manager.Provider.GetInstallablePackageAsync(package.UnquotedNameText, version.UnquotedValueText).Result;

            if (installable == null)
            {
                return(JSONItemValidationResult.Continue);
            }

            if (!installable.AllFiles.Contains(item.Text.Trim('"', ',')))
            {
                string message = $"({Vsix.Name}) {item.Text} is not a valid file name for {installable.Name} {installable.Version}.";
                AddError(context, item, message);
            }

            return(JSONItemValidationResult.Continue);
        }
コード例 #41
0
        public static IEnumerable <Tuple <string, string> > GetTasks(JSONParseItem item)
        {
            var visitor = new JSONItemCollector <JSONMember>(true);

            item.JSONDocument.Accept(visitor);

            var scripts = visitor.Items.FirstOrDefault(member => member.UnquotedNameText == "commands");

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

            foreach (JSONObject child in scripts.Children.Where(c => c is JSONObject))
            {
                foreach (JSONMember taskItem in child.Children.Where(c => c is JSONMember))
                {
                    yield return(Tuple.Create(taskItem.UnquotedNameText, taskItem.Value.Text));
                }
            }
        }
コード例 #42
0
        public static JSONParseItem SelectItem(this JSONBlockItem block, string selector)
        {
            string[] paths = selector.Split('/');
            string   path  = paths[0];

            var items = from b in block.BlockItemChildren
                        let m = b as JSONMember
                                where m != null && m.UnquotedNameText == path
                                select m.Value;

            JSONParseItem item = items.ElementAtOrDefault(0);

            JSONObject obj = item as JSONObject;

            if (obj != null)
            {
                return(SelectItem(obj, string.Join("/", paths.Skip(1))));
            }

            return(item);
        }
コード例 #43
0
        private static IEnumerable <IVocabulary> FindApplicableVocabularies(JSONParseItem member, IEnumerable <IVocabulary> vocabularies)
        {
            var parent = member.FindType <JSONBlockItem>();

            while (parent != null)
            {
                var visitor = new JSONItemCollector <JSONMember>();
                parent.Accept(visitor);

                var context = visitor.Items.FirstOrDefault(c => c.Name != null && c.Name.Text == "\"@context\"");

                foreach (IVocabulary vocab in vocabularies)
                {
                    if (vocab.AppliesToContext(context))
                    {
                        yield return(vocab);
                    }
                }

                parent = parent.Parent.FindType <JSONBlockItem>();
            }
        }
コード例 #44
0
        protected override JSONItemValidationResult ValidateJsonItem(JSONParseItem item, IJSONValidationContext context)
        {
            var member = item as JSONMember;
            var packages = item.Parent?.Parent as JSONMember;

            if (packages == null || packages.UnquotedNameText != "packages")
                return JSONItemValidationResult.Continue;

            var children = (member.Value as JSONObject)?.BlockItemChildren?.OfType<JSONMember>();

            if (!children.Any(c => c.UnquotedNameText == "version"))
                return JSONItemValidationResult.Continue;

            var names = VSPackage.Manager.Provider.GetPackageNamesAsync().Result;

            if (names != null && !names.Contains(member.UnquotedNameText))
            {
                string message = $"({Vsix.Name}) The package \"{member.UnquotedNameText}\" does not exist";
                AddError(context, member.Name, message);
            }

            return JSONItemValidationResult.Continue;
        }
コード例 #45
0
        protected override JSONItemValidationResult ValidateJsonItem(JSONParseItem item, IJSONValidationContext context)
        {
            var member = item as JSONMember;

            if (member.UnquotedNameText != "version")
                return JSONItemValidationResult.Continue;

            var package = item.Parent?.Parent as JSONMember;
            var packages = package?.Parent?.Parent as JSONMember;

            if (package == null ||packages == null || packages.UnquotedNameText != "packages")
                return JSONItemValidationResult.Continue;

            var versions = VSPackage.Manager.Provider.GetVersionsAsync(package.UnquotedNameText).Result;

            if (versions != null && !versions.Contains(member.UnquotedValueText))
            {
                string message = $"({Vsix.Name}) \"{member.UnquotedValueText}\" is not a valid version for \"{package.UnquotedNameText}\".";
                AddError(context, member.Value, message);
            }

            return JSONItemValidationResult.Continue;
        }
コード例 #46
0
        public static IDictionary<string, string> GetIcons(JSONParseItem item)
        {
            var visitor = new JSONItemCollector<JSONMember>(true);
            var dic = new Dictionary<string, string>();

            if (!item.JSONDocument.Accept(visitor))
                return dic;

            var icons = visitor.Items.FirstOrDefault(m => m.UnquotedNameText == "icons");
            var value = icons?.Value as JSONObject;

            if (value == null)
                return dic;

            string folder = Path.GetDirectoryName(item.JSONDocument.DocumentLocation);

            foreach (JSONMember icon in value.Children.OfType<JSONMember>())
            {
                dic.Add(icon.UnquotedNameText, icon.UnquotedValueText);
            }

            return dic;
        }
コード例 #47
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);
        }
コード例 #48
0
        public static IEnumerable<string> GetVariables(JSONParseItem item)
        {
            var visitor = new JSONItemCollector<JSONMember>(true);

            if (!item.JSONDocument.Accept(visitor))
                return null;

            var contents = visitor.Items.Where(m => m.UnquotedNameText == "content" && m.IsValid);
            List<string> list = new List<string>();

            foreach (JSONMember content in contents)
            {
                var value = content?.Value as JSONObject;

                if (value == null)
                    continue;

                var names = value.Children.OfType<JSONMember>().Select(s => s.UnquotedNameText);

                list.AddRange(names.Where(n => !list.Contains(n)));
            }

            return list;
        }
コード例 #49
0
 protected abstract JSONItemValidationResult ValidateJsonItem(JSONParseItem item, IJSONValidationContext context);
コード例 #50
0
 public static IEnumerable<IVocabulary> GetVocabularies(JSONParseItem item)
 {
     var vocabs = GetAllVocabularies();
     return FindApplicableVocabularies(item, vocabs);
 }
コード例 #51
0
 public abstract UIElement CreateTooltip(string name, JSONParseItem item);
コード例 #52
0
        private static bool IsBandManifest(JSONParseItem item)
        {
            string fileName = Path.GetFileName(item.JSONDocument?.DocumentLocation);

            if (string.IsNullOrEmpty(fileName) || !fileName.Equals("manifest.json", StringComparison.OrdinalIgnoreCase))
                return false;

            if (item.JSONDocument.Text.Contains("\"manifestVersion\"") ||
                item.JSONDocument.Text.Contains("\"versionString\"") ||
                item.JSONDocument.Text.Contains("\"tileIcon\""))
                return true;

            return false;
        }