internal static Snippet AddInitializer(ExecutionContext context, Snippet snippet, string initializer, string defaultValue) { string suffix = (snippet.Language == Language.Cpp) ? " (with initialization)" : " (with initializer)"; snippet.SuffixTitle(suffix); snippet.SuffixShortcut(context.WithInitializerSuffix(snippet)); snippet.SuffixDescription(suffix); snippet.ReplacePlaceholders(LiteralIdentifiers.Initializer, initializer); snippet.AddLiteral(LiteralIdentifiers.Value, null, defaultValue); snippet.RemoveLiteral(LiteralIdentifiers.Initializer); if (snippet.Language == Language.Cpp) { Literal typeLiteral = snippet.Literals.Find(LiteralIdentifiers.Type); typeLiteral.DefaultValue = "auto"; LiteralRenamer.Rename(snippet, LiteralIdentifiers.Type, "type"); } else { snippet.RemoveLiteralAndPlaceholders(LiteralIdentifiers.ArrayLength); } snippet.AddTag(KnownTags.ExcludeFromReadme); snippet.SuffixFileName((snippet.Language == Language.Cpp) ? "WithInitialization" : "WithInitializer"); return(snippet); }
protected override void Execute(ExecutionContext context, Snippet snippet) { LanguageDefinition language = ((LanguageExecutionContext)context).Language; snippet.Title = snippet.Title.ReplacePlaceholder(Placeholders.Collection, Type.Name); snippet.Description = snippet.Description.ReplacePlaceholder(Placeholders.Collection, Type.Name); snippet.AddNamespace(Type.Namespace); snippet.AddTags( KnownTags.NonUniqueShortcut, KnownTags.TitleStartsWithShortcut, KnownTags.ExcludeFromReadme); snippet.RemoveLiteralAndReplacePlaceholders(LiteralIdentifiers.Collection, Type.Name); if (Tags.Contains(KnownTags.Dictionary)) { snippet.RemoveLiteralAndReplacePlaceholders(LiteralIdentifiers.GenericType, $"${LiteralIdentifiers.KeyType}$, ${LiteralIdentifiers.ValueType}$"); snippet.AddLiteral(LiteralIdentifiers.KeyType, null, language.Object.Keyword); snippet.AddLiteral(LiteralIdentifiers.ValueType, null, language.Object.Keyword); LiteralRenamer.Rename(snippet, LiteralIdentifiers.CollectionIdentifier, LiteralIdentifiers.DictionaryIdentifier); Literal literal = snippet.Literals.Find(LiteralIdentifiers.DictionaryIdentifier); if (literal != null) { literal.DefaultValue = "dic"; } } if (!Tags.Contains(KnownTags.ArgumentList)) { snippet.RemoveLiteralAndPlaceholders(LiteralIdentifiers.ArgumentList); } ProcessFilePath(context, snippet); if (snippet.HasTag(KnownTags.Initializer) && Tags.Contains(KnownTags.Initializer)) { var clone = (Snippet)snippet.Clone(); InitializerCommand.AddInitializer(clone, GetInitializer(snippet, language), language.DefaultValue); context.Snippets.Add(clone); } else { snippet.RemoveLiteralAndPlaceholders(LiteralIdentifiers.Initializer); } }