Beispiel #1
0
        private void Initialize()
        {
            string               pathPrefix            = "Snippets/Templates/";
            Assembly             assembly              = typeof(SnippetsProvider).Assembly;
            IEnumerable <string> manifestResourceNames = assembly.GetManifestResourceNames().Where(p => p.StartsWith(pathPrefix, StringComparison.Ordinal));

            foreach (var manifestResourceName in manifestResourceNames)
            {
                Stream?      stream       = assembly.GetManifestResourceStream(manifestResourceName);
                StreamReader streamReader = new StreamReader(stream ?? throw new ArgumentNullException("Stream is null"), Encoding.Default);

                (string description, string snippetText) = GetDescriptionAndText(streamReader.ReadToEnd());
                string             prefix             = Path.GetFileNameWithoutExtension(manifestResourceName);
                CompletionPriority completionPriority = CompletionPriority.Medium;

                if (prefix.StartsWith("resource"))
                {
                    completionPriority = CompletionPriority.High;
                }

                Snippet snippet = new Snippet(snippetText, completionPriority, prefix, description);

                topLevelNamedDeclarationSnippets.Add(snippet);
            }
        }
Beispiel #2
0
 public CompletionItem(CompletionKind kind, string displayText, string editText = null, string afterText = null, string matchText = null, CompletionPriority priority = CompletionPriority.Normal)
 {
     this.DisplayText = displayText ?? "";
     this.Kind        = kind;
     this.EditText    = editText ?? this.DisplayText;
     this.AfterText   = afterText;
     this.MatchText   = matchText ?? displayText;
     this.Priority    = priority;
 }
Beispiel #3
0
        public Snippet(string text, CompletionPriority completionPriority = CompletionPriority.Medium, string prefix = "", string detail = "")
        {
            var matches = PlaceholderPattern.Matches(text);

            this.Text               = text;
            this.Prefix             = prefix;
            this.Detail             = detail;
            this.CompletionPriority = completionPriority;
            this.Placeholders       = matches
                                      .Select(CreatePlaceholder)
                                      .OrderBy(p => p.Index)
                                      .ToImmutableArray();
        }
Beispiel #4
0
 /// <summary>
 /// Gets the default <see cref="CompletionItem"/> for a token with the specified text.
 /// </summary>
 private static CompletionItem GetDefaultCompletionItem(string text, CompletionKind?ckind, CompletionPriority priority, string ctext = null)
 {
     return(GetDefaultCompletionItem(text, ckind ?? CompletionKind.Syntax, priority, ctext));
 }
Beispiel #5
0
        /// <summary>
        /// Gets the default <see cref="CompletionItem"/> for a token with the specified text.
        /// </summary>
        private static CompletionItem GetDefaultCompletionItem(string text, CompletionKind ckind, CompletionPriority priority, string ctext = null)
        {
            // hide any syntax that starts with _ from completion
            if (text.StartsWith("_"))
            {
                return(null);
            }

            string afterText = null;
            string editText  = ctext ?? text;

            if (ctext != null)
            {
                var cursor = ctext.IndexOf('|');
                if (cursor >= 0)
                {
                    afterText = ctext.Substring(cursor + 1);
                    editText  = ctext.Substring(0, cursor);
                }
            }

            return(new CompletionItem(ckind, displayText: text, editText: editText, afterText: afterText, priority: priority));
        }
Beispiel #6
0
 /// <summary>
 /// A parser that consumes the next <see cref="LexicalToken"/> (or series of adjacent tokens) if it has one of the specified texts, producing a corresponding <see cref="SyntaxToken"/> or an equivalent missing token otherwise.
 /// </summary>
 public static Parser <LexicalToken, SyntaxToken> RequiredToken(IReadOnlyList <string> texts, CompletionKind?ckind = null, CompletionPriority priority = CompletionPriority.Normal) =>
 Required(Token(texts, ckind, priority), () => CreateMissingToken(texts));
Beispiel #7
0
        /// <summary>
        /// Gets the default <see cref="CompletionItem"/> for a token with the specified <see cref="SyntaxKind"/>.
        /// </summary>
        private static CompletionItem GetDefaultCompletionItem(SyntaxKind kind, CompletionKind?ckind, CompletionPriority priority, string ctext = null)
        {
            var text = SyntaxFacts.GetText(kind);

            if (string.IsNullOrWhiteSpace(text))
            {
                return(null);
            }

            switch (kind.GetCategory())
            {
            case SyntaxCategory.Keyword:
                return(GetDefaultCompletionItem(text, ckind ?? CompletionKind.Keyword, priority, ctext));

            case SyntaxCategory.Operator:
                return(GetDefaultCompletionItem(text, ckind ?? CompletionKind.ScalarInfix, priority, ctext));

            case SyntaxCategory.Punctuation:
                return(GetDefaultCompletionItem(text, ckind ?? CompletionKind.Punctuation, priority, ctext));

            default:
                return(null);
            }
        }
Beispiel #8
0
        /// <summary>
        /// A parser that consumes the next <see cref="LexicalToken"/> (or series of adjacent tokens) if it has one of the specified texts, producing a single <see cref="SyntaxToken"/>.
        /// </summary>
        public static Parser <LexicalToken, SyntaxToken> Token(IReadOnlyList <string> texts, CompletionKind?ckind = null, CompletionPriority priority = CompletionPriority.Normal)
        {
            Ensure.ArgumentNotNull(texts, nameof(texts));
            var set = new HashSet <string>(texts);

            var rule = Match(t => set.Contains(t.Text), lt => SyntaxToken.From(lt)).WithTag(string.Join(" | ", texts.Select(t => GetDefaultTag(t))));

            var items = GetCompletionItems(texts, ckind, priority).ToList();

            if (items.Count > 0)
            {
                rule = rule.WithAnnotations(items);
            }

            return(rule);
        }
Beispiel #9
0
 /// <summary>
 /// A parser that consumes the next <see cref="LexicalToken"/> (or series of adjacent tokens) if it has the specified text, producing a corresponding <see cref="SyntaxToken"/> or an equivalent missing token otherwise.
 /// </summary>
 public static Parser <LexicalToken, SyntaxToken> RequiredToken(string text, CompletionKind?ckind = null, CompletionPriority priority = CompletionPriority.Normal, string ctext = null) =>
 Required(Token(text, ckind, priority, ctext), () => CreateMissingToken(text));
Beispiel #10
0
 public CompletionItem WithPriority(CompletionPriority priority)
 {
     return(new CompletionItem(this.Kind, this.DisplayText, this.EditText, this.AfterText, this.MatchText, priority));
 }
Beispiel #11
0
        /// <summary>
        /// A parser that consumes the next <see cref="LexicalToken"/> (or series of adjacent tokens) if it has the specified text, producing a single <see cref="SyntaxToken"/>.
        /// </summary>
        public static Parser <LexicalToken, SyntaxToken> Token(string text, CompletionKind?ckind = null, CompletionPriority priority = CompletionPriority.Normal, string ctext = null)
        {
            var rule = MatchText(text).WithTag(GetDefaultTag(text));

            var item = GetDefaultCompletionItem(text, ckind, priority, ctext);

            if (item != null)
            {
                rule = rule.WithAnnotations(new[] { item });
            }

            return(rule);
        }
Beispiel #12
0
        /// <summary>
        /// A parser that consumes the next <see cref="LexicalToken"/> (or series of adjacent tokens) if it has the specified text, producing a single <see cref="SyntaxToken"/>.
        /// </summary>
        public static Parser <LexicalToken, SyntaxToken> Token(string text, CompletionKind?ckind = null, CompletionPriority priority = CompletionPriority.Normal, string ctext = null)
        {
            var item = GetDefaultCompletionItem(text, ckind, priority, ctext);

            return(Token(text, item));
        }
Beispiel #13
0
 /// <summary>
 /// Gets the default <see cref="CompletionItem"/> for tokens with any of the specified <see cref="SyntaxKind"/>.
 /// </summary>
 private static IEnumerable <CompletionItem> GetCompletionItems(IEnumerable <SyntaxKind> kinds, CompletionKind?ckind, CompletionPriority priority) =>
 kinds.Select(k => GetDefaultCompletionItem(k, ckind, priority)).Where(i => i != null);
 /// <summary>
 /// Creates a completion with a contextual snippet. This will look like a snippet to the user.
 /// </summary>
 public static CompletionItem CreateContextualSnippetCompletion(string label, string detail, string snippet, bool preselect = false, CompletionPriority priority = CompletionPriority.Medium) =>
 new CompletionItem
 {
     Kind             = CompletionItemKind.Snippet,
     Label            = label,
     InsertTextFormat = InsertTextFormat.Snippet,
     InsertText       = snippet,
     Detail           = detail,
     Documentation    = new StringOrMarkupContent(new MarkupContent
     {
         Kind  = MarkupKind.Markdown,
         Value = $"```bicep\n{new Snippet(snippet).FormatDocumentation()}\n```"
     }),
     Preselect = preselect,
     SortText  = GetSortText(label, priority)
 };
 public static CompletionItem CreateTypeCompletion(TypeSymbol type, bool preselect = false, CompletionPriority priority = CompletionPriority.Medium) => CreatePlaintextCompletion(CompletionItemKind.Class, type.Name, type.Name, preselect, priority);
 public static CompletionItem CreateKeywordCompletion(string keyword, string detail, bool preselect = false, CompletionPriority priority = CompletionPriority.Medium) => CreatePlaintextCompletion(CompletionItemKind.Keyword, keyword, detail, preselect, priority);
 /// <summary>
 /// Creates a completion that inserts a snippet. The user may not necessarily know that a snippet is being inserted.
 /// </summary>
 public static CompletionItem CreateSnippetCompletion(CompletionItemKind kind, string label, string snippet, string detail, bool preselect = false, CompletionPriority priority = CompletionPriority.Medium, Container <string>?commitCharacters = null) =>
 new CompletionItem
 {
     Kind             = kind,
     Label            = label,
     InsertTextFormat = InsertTextFormat.Snippet,
     InsertText       = snippet,
     Detail           = detail,
     Preselect        = preselect,
     SortText         = GetSortText(label, priority),
     CommitCharacters = commitCharacters
 };
 public static CompletionItem CreatePropertyNameCompletion(TypeProperty property, bool preselect = false, CompletionPriority priority = CompletionPriority.Medium) =>
 new CompletionItem
 {
     Kind             = CompletionItemKind.Property,
     Label            = property.Name,
     InsertTextFormat = InsertTextFormat.PlainText,
     // property names containg spaces need to be escaped
     InsertText       = IsPropertyNameEscapingRequired(property) ? StringUtils.EscapeBicepString(property.Name) : property.Name,
     CommitCharacters = PropertyCommitChars,
     Detail           = FormatPropertyDetail(property),
     Documentation    = new StringOrMarkupContent(new MarkupContent
     {
         Kind  = MarkupKind.Markdown,
         Value = FormatPropertyDocumentation(property)
     }),
     Preselect = preselect,
     SortText  = GetSortText(property.Name, priority)
 };
Beispiel #19
0
 /// <summary>
 /// Gets the default <see cref="CompletionItem"/> for tokens with any of the specified texts.
 /// </summary>
 private static IEnumerable <CompletionItem> GetCompletionItems(IEnumerable <string> texts, CompletionKind?ckind, CompletionPriority priority) =>
 texts.Select(t => GetDefaultCompletionItem(t, ckind, priority)).Where(i => i != null);
Beispiel #20
0
        /// <summary>
        /// A parser that consumes the next next <see cref="LexicalToken"/> if it has the specified <see cref="SyntaxKind"/>, producing a corresponding <see cref="SyntaxToken"/>.
        /// </summary>
        public static Parser <LexicalToken, SyntaxToken> Token(SyntaxKind kind, CompletionKind?ckind = null, CompletionPriority priority = CompletionPriority.Normal, string ctext = null)
        {
            var rule = Match(t => t.Kind == kind, lt => SyntaxToken.From(lt)).WithTag(GetDefaultTag(kind));

            var item = GetDefaultCompletionItem(kind, ckind, priority, ctext);

            if (item != null)
            {
                rule = rule.WithAnnotations(new[] { item });
            }

            return(rule);
        }