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); } }
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; }
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(); }
/// <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)); }
/// <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)); }
/// <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));
/// <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); } }
/// <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); }
/// <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));
public CompletionItem WithPriority(CompletionPriority priority) { return(new CompletionItem(this.Kind, this.DisplayText, this.EditText, this.AfterText, this.MatchText, priority)); }
/// <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); }
/// <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)); }
/// <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) };
/// <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);
/// <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); }