예제 #1
0
        public void AugmentCompletionSession(ICompletionSession session, IList<CompletionSet> completionSets)
        {
            if (_disposed || _document.IsParsing)
                return;

            ITextSnapshot snapshot = _buffer.CurrentSnapshot;
            SnapshotPoint? triggerPoint = session.GetTriggerPoint(snapshot);

            if (triggerPoint == null || !triggerPoint.HasValue)
                return;

            SnapshotSpan line = triggerPoint.Value.GetContainingLine().Extent;
            var list = new List<Completion4>();
            int position = triggerPoint.Value.Position;
            ITrackingSpan applicableTo = snapshot.CreateTrackingSpan(position, 0, SpanTrackingMode.EdgeInclusive);

            ParseItem prev = _document.ParseItems.LastOrDefault(p => p.Span.Start < position && !p.Span.Contains(position - 1));
            ParseItem parseItem = _document.ItemAtPosition(position);
            string moniker = null;

            // Property
            if (string.IsNullOrWhiteSpace(line.GetText()) || parseItem?.ItemType == ItemType.Keyword)
            {
                bool isInRoot = !_document.ParseItems.Exists(p => p.ItemType == ItemType.Section && p.Span.Start < position);

                if (isInRoot)
                {
                    if (SchemaCatalog.TryGetKeyword(SchemaCatalog.Root, out Keyword root))
                        list.Add(CreateCompletion(root, root.Category));
                }
                else
                {
                    IEnumerable<Keyword> properties = EditorConfigPackage.CompletionOptions.ShowHiddenKeywords ? SchemaCatalog.AllKeywords : SchemaCatalog.VisibleKeywords;
                    IEnumerable<Keyword> items = properties.Where(i => i.Name != SchemaCatalog.Root);
                    IEnumerable<string> usedKeywords = _document.GetAllIncludedRules();

                    ParseItem parseItemSection = _document.ParseItems.LastOrDefault(p => p.ItemType == ItemType.Section && p.Span.Start < position);
                    if (parseItemSection != null)
                    {
                        Section section = _document.Sections.FirstOrDefault(x => x.Item.Text.Equals(parseItemSection.Text, StringComparison.OrdinalIgnoreCase));
                        if (section != null)
                        {
                            // Set keywords scope to current section
                            usedKeywords = section.Properties.Select(x => x.Keyword.Text).ToList();
                        }
                    }

                    foreach (Keyword property in items)
                    {
                        string keyword = property.Name;

                        // Keyword already used and keyword does not qualify as repeatable
                        if (usedKeywords.Contains(keyword) && !keyword.StartsWith("dotnet_naming_", StringComparison.OrdinalIgnoreCase))
                            continue;

                        list.Add(CreateCompletion(property, property.Category));
                    }
                }

                moniker = "keyword";
            }
            // Value
            else if (parseItem?.ItemType == ItemType.Value)
            {
                if (SchemaCatalog.TryGetKeyword(prev.Text, out Keyword item))
                {
                    if (!item.SupportsMultipleValues && parseItem.Text.Contains(","))
                    {
                        return;
                    }

                    foreach (Value value in item.Values)
                        list.Add(CreateCompletion(value, iconAutomation: "value"));
                }

                moniker = "value";
            }
            // Severity
            else if ((position > 0 && snapshot.Length > 1 && snapshot.GetText(position - 1, 1) == ":") || parseItem?.ItemType == ItemType.Severity)
            {
                if (prev?.ItemType == ItemType.Value || parseItem?.ItemType == ItemType.Severity)
                {
                    Property prop = _document.PropertyAtPosition(prev.Span.Start);
                    if (SchemaCatalog.TryGetKeyword(prop?.Keyword?.Text, out Keyword key) && key.RequiresSeverity)
                        AddSeverity(list);

                    moniker = "severity";
                }
            }
            // Suppression
            else if (parseItem?.ItemType == ItemType.Suppression)
            {
                foreach (Error code in ErrorCatalog.All.OrderBy(e => e.Code))
                    list.Add(CreateCompletion(code));

                moniker = "suppression";
            }

            if (!list.Any())
            {
                if (SchemaCatalog.TryGetKeyword(prev?.Text, out Keyword property))
                {
                    int eq = line.GetText().IndexOf("=");

                    if (eq != -1)
                    {
                        int eqPos = eq + line.Start.Position;

                        if (position > eqPos)
                            foreach (Value value in property.Values)
                                list.Add(CreateCompletion(value));
                    }

                    moniker = "value";
                }
            }
            else
            {
                ITrackingSpan trackingSpan = FindTokenSpanAtPosition(session);
                SnapshotSpan span = trackingSpan.GetSpan(snapshot);
                string text = span.GetText();

                if (text == ":" || text == ",")
                    applicableTo = snapshot.CreateTrackingSpan(new Span(span.Start + 1, 0), SpanTrackingMode.EdgeInclusive);
                else if (!string.IsNullOrWhiteSpace(text))
                    applicableTo = trackingSpan;
            }

            CreateCompletionSet(moniker, completionSets, list, applicableTo);
        }
        public void AugmentQuickInfoSession(IQuickInfoSession session, IList <object> qiContent, out ITrackingSpan applicableToSpan)
        {
            applicableToSpan = null;

            SnapshotPoint?point = session.GetTriggerPoint(_buffer.CurrentSnapshot);

            if (session == null || qiContent == null || !point.HasValue || point.Value.Position >= point.Value.Snapshot.Length)
            {
                return;
            }

            ParseItem item = _document.ItemAtPosition(point.Value);

            if (item == null)
            {
                return;
            }

            applicableToSpan = point.Value.Snapshot.CreateTrackingSpan(item.Span, SpanTrackingMode.EdgeNegative);

            if (item.Errors.Any())
            {
                foreach (DisplayError error in item.Errors)
                {
                    qiContent.Add(new Shared.EditorTooltip(error));
                    return;
                }
            }

            Property property = _document.PropertyAtPosition(point.Value);

            SchemaCatalog.TryGetKeyword(property?.Keyword?.Text, out Keyword keyword);

            // Keyword
            if (keyword != null && item.ItemType == ItemType.Keyword)
            {
                qiContent.Add(new Shared.EditorTooltip(keyword));
            }

            // Value
            else if (keyword != null && item.ItemType == ItemType.Value)
            {
                Value value = keyword.Values.FirstOrDefault(v => v.Name.Is(item.Text));

                if (value != null && !string.IsNullOrEmpty(value.Description))
                {
                    qiContent.Add(new Shared.EditorTooltip(value));
                }
            }

            // Severity
            else if (item.ItemType == ItemType.Severity && SchemaCatalog.TryGetSeverity(item.Text, out Severity severity))
            {
                qiContent.Add(new Shared.EditorTooltip(severity));
            }

            // Suppression
            else if (item.ItemType == ItemType.Suppression && ErrorCatalog.TryGetErrorCode(item.Text, out var code))
            {
                qiContent.Add(new Shared.EditorTooltip(code));
            }
        }