Пример #1
0
        protected override void Execute(ExecutionContext context, Snippet snippet)
        {
            LanguageDefinition language = ((LanguageExecutionContext)context).Language;

            snippet.AddTag(KnownTags.NonUniqueShortcut);
            snippet.AddTag(KnownTags.TitleStartsWithShortcut);

            if (Type == null)
            {
                return;
            }

            if (snippet.HasTag(KnownTags.TryParse) && !Tags.Contains(KnownTags.TryParse))
            {
                context.IsCanceled = true;
                return;
            }

            if (Type.Name == "Void" && snippet.Language == Language.VisualBasic)
            {
                snippet.ReplaceSubOrFunctionLiteral("Sub");

                snippet.RemoveLiteral(LiteralIdentifiers.As);
                snippet.ReplacePlaceholders(LiteralIdentifiers.Type, "");

                snippet.CodeText = Regex.Replace(snippet.CodeText, $@"[\s-[\r\n]]*\${LiteralIdentifiers.As}\$[\s-[\r\n]]*", "");
            }
            else
            {
                snippet.ReplaceSubOrFunctionLiteral("Function");
            }

            snippet.Title = snippet.Title
                            .ReplacePlaceholder(Placeholders.Type, Type.Title)
                            .ReplacePlaceholder(Placeholders.OfType, $"of {Type.Title}")
                            .ReplacePlaceholder(Placeholders.GenericType, language.GetTypeParameterList(Type.Keyword));

            snippet.Description = snippet.Description
                                  .ReplacePlaceholder(Placeholders.Type, Type.Title)
                                  .ReplacePlaceholder(Placeholders.OfType, $"of {Type.Title}")
                                  .ReplacePlaceholder(Placeholders.GenericType, language.GetTypeParameterList(Type.Keyword));

            snippet.AddNamespace(Type.Namespace);

            snippet.AddTag(KnownTags.ExcludeFromReadme);

            snippet.RemoveLiteralAndReplacePlaceholders(LiteralIdentifiers.Type, Type.Keyword);

            Literal valueLiteral = snippet.Literals.Find(LiteralIdentifiers.Value);

            if (valueLiteral != null)
            {
                valueLiteral.DefaultValue = Type.DefaultValue;
            }

            if (Type.DefaultIdentifier != null)
            {
                Literal identifierLiteral = snippet.Literals.Find(LiteralIdentifiers.Identifier);

                if (identifierLiteral != null)
                {
                    identifierLiteral.DefaultValue = Type.DefaultIdentifier;
                }
            }

            string fileName = Path.GetFileName(snippet.FilePath);

            if (fileName.IndexOf("OfT", StringComparison.Ordinal) != -1)
            {
                fileName = fileName.Replace("OfT", $"Of{Type.Name}");
            }
            else if (snippet.HasTag(KnownTags.TryParse))
            {
                fileName = Path.GetFileNameWithoutExtension(fileName) + Type.Name + Path.GetExtension(fileName);
            }
            else
            {
                fileName = Type.Name + fileName;
            }

            snippet.SetFileName(fileName);
        }
Пример #2
0
        protected override void Execute(ExecutionContext context, Snippet snippet)
        {
            if (snippet.HasTag(KnownTags.Initializer) &&
                (Type.IsImmutable || Type.IsInterface))
            {
                context.IsCanceled = true;
                return;
            }

            LanguageDefinition language = ((LanguageExecutionContext)context).Language;

            var typeName = "";
            var fileName = "";

            if (Type.IsDictionary)
            {
                typeName = language.GetTypeParameterList("TKey, TValue");
                fileName = "OfTKeyTValue";
            }
            else if (Type.Arity == 1)
            {
                typeName = language.GetTypeParameterList("T");
                fileName = "OfT";
            }

            typeName = Type.Name + typeName;
            fileName = Type.Name + fileName;

            snippet.Title       = snippet.Title.Replace(Placeholders.Type, typeName);
            snippet.Description = snippet.Description.Replace(Placeholders.Type, typeName);

            snippet.AddNamespace(Type.Namespace);

            snippet.AddTags(KnownTags.NonUniqueShortcut);
            snippet.AddTags(KnownTags.TitleStartsWithShortcut);
            snippet.AddTags(KnownTags.ExcludeFromReadme);

            snippet.RemoveLiteralAndReplacePlaceholders(LiteralIdentifiers.Type, Type.Name);

            if (Type.IsDictionary)
            {
                snippet.RemoveLiteralAndReplacePlaceholders(LiteralIdentifiers.TypeParameterList, language.GetTypeParameterList($"${LiteralIdentifiers.KeyType}$, ${LiteralIdentifiers.ValueType}$"));
                snippet.AddLiteral(LiteralIdentifiers.KeyType, null, language.ObjectType.Keyword);
                snippet.AddLiteral(LiteralIdentifiers.ValueType, null, language.ObjectType.Keyword);

                Literal literal = snippet.Literals.Find(LiteralIdentifiers.Identifier);

                if (literal != null)
                {
                    literal.DefaultValue = "dic";
                }
            }
            else if (Type.Arity == 1)
            {
                snippet.RemoveLiteralAndReplacePlaceholders(LiteralIdentifiers.TypeParameterList, language.GetTypeParameterList($"${LiteralIdentifiers.TypeParameter}$"));
                snippet.AddLiteral(LiteralIdentifiers.TypeParameter, null, "T");
            }
            else
            {
                snippet.RemoveLiteralAndPlaceholders(LiteralIdentifiers.TypeParameterList);
            }

            if (!Tags.Contains(KnownTags.Arguments) &&
                !(Type.IsReadOnly && Tags.Contains(KnownTags.Collection)))
            {
                snippet.RemoveLiteralAndPlaceholders(LiteralIdentifiers.Arguments);
            }

            snippet.SetFileName(fileName + Path.GetFileName(snippet.FilePath));

            if (snippet.HasTag(KnownTags.Initializer) &&
                Tags.Contains(KnownTags.Initializer))
            {
                var clone = (Snippet)snippet.Clone();
                InitializerCommand.AddInitializer(context, clone, GetInitializer(language), language.GetDefaultValue());
                context.Snippets.Add(clone);
            }
            else
            {
                snippet.RemoveLiteralAndPlaceholders(LiteralIdentifiers.Initializer);
            }
        }