Beispiel #1
0
        private static Snippet CreateSelfClosingElementSnippet()
        {
            var s = new Snippet()
            {
                Title    = "self-closing element",
                Shortcut = ElementShortcut
            };

            s.Description = s.Title;

            Literal nameLiteral = CreateElementNameLiteral();

            s.AddLiteral(nameLiteral);

            Literal attributeLiteral = CreateAttributeLiteral();

            s.AddLiteral(attributeLiteral);

            var w = new SnippetCodeWriter();

            w.Write("<");
            w.WritePlaceholder(nameLiteral.Identifier);
            w.WritePlaceholder(attributeLiteral.Identifier);
            w.Write(" />");
            w.WriteEndPlaceholder();
            s.CodeText = w.ToString();

            s.FilePath = "SelfClosingElement";

            return(s);
        }
        private static Snippet CreateCDataSnippetSnippet()
        {
            var s = new Snippet()
            {
                Title    = "CDATA section",
                Shortcut = "cdata"
            };

            s.Description = s.Title;

            s.AddLiteral(new Literal(CDataIdentifier)
            {
                DefaultValue = "]]>", IsEditable = false
            });

            s.AddLiteral(CreateContentLiteral());

            var b = new SnippetCodeBuilder();

            b.Append("<![CDATA[");
            b.AppendPlaceholder(ContentIdentifier);
            b.AppendPlaceholder(CDataIdentifier);
            s.CodeText = b.ToString();

            s.FilePath = "CDataSection";

            return(s);
        }
        private static void AddAttributeLiteralsAndPlaceholders(Snippet s)
        {
            s.AddLiteral(CreateAttributeNameLiteral());
            s.AddLiteral(CreateAttributeValueLiteral());

            s.ReplacePlaceholders(AttributeIdentifier, $" ${AttributeNameIdentifier}$=\"${AttributeValueIdentifier}$\"");
        }
        private static Snippet CreateElementSnippet()
        {
            var s = new Snippet()
            {
                Title    = "element",
                Shortcut = ElementShortcut
            };

            s.Description = s.Title;

            s.AddLiteral(CreateElementNameLiteral());

            s.AddLiteral(CreateContentLiteral());

            var b = new SnippetCodeBuilder();

            b.Append("<");
            b.AppendPlaceholder(ElementNameIdentifier);
            b.AppendPlaceholder(AttributeIdentifier);
            b.Append(">");
            b.AppendPlaceholder(ContentIdentifier);
            b.Append("</");
            b.AppendPlaceholder(ElementNameIdentifier);
            b.Append(">");

            s.CodeText = b.ToString();

            s.FilePath = "Element";

            return(s);
        }
Beispiel #5
0
        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);
            }
        }
Beispiel #6
0
        private static Snippet CreateCommentSnippet()
        {
            var s = new Snippet()
            {
                Title    = "comment",
                Shortcut = CommentShortcut
            };

            s.Description = s.Title;

            Literal commentLiteral = CreateCommentLiteral();

            s.AddLiteral(commentLiteral);

            var w = new SnippetCodeWriter();

            w.Write("<!-- ");
            w.WritePlaceholder(commentLiteral.Identifier);
            w.Write(" -->");
            w.WriteEndPlaceholder();
            s.CodeText = w.ToString();

            s.FilePath = "Comment";

            return(s);
        }
Beispiel #7
0
        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);
        }
Beispiel #8
0
        protected override void Execute(ExecutionContext context, Snippet snippet)
        {
            snippet.SuffixTitle(" (with arguments)");
            snippet.SuffixShortcut(ShortcutChars.WithArguments);
            snippet.SuffixDescription(" (with arguments)");

            snippet.RemoveLiteralAndReplacePlaceholders(LiteralIdentifiers.ArgumentList, "($arguments$)");

            snippet.AddLiteral("arguments", "Arguments", "arguments");

            snippet.AddTag(KnownTags.ExcludeFromReadme);

            snippet.SuffixFileName("WithArguments");
        }
Beispiel #9
0
        private static Snippet CreateElementSnippet()
        {
            var s = new Snippet();

            s.Title       = "element";
            s.Shortcut    = ElementShortcut;
            s.Description = s.Title;

            Literal nameLiteral = CreateElementNameLiteral();

            s.AddLiteral(nameLiteral);

            Literal attributeLiteral = CreateAttributeLiteral();

            s.AddLiteral(attributeLiteral);

            Literal contentLiteral = CreateContentLiteral();

            s.AddLiteral(contentLiteral);

            var w = new SnippetCodeWriter();

            w.Write("<");
            w.WritePlaceholder(nameLiteral.Identifier);
            w.WritePlaceholder(attributeLiteral.Identifier);
            w.Write(">");
            w.WritePlaceholder(contentLiteral.Identifier);
            w.Write("</");
            w.WritePlaceholder(NameIdentifier);
            w.Write(">");
            w.WriteEndPlaceholder();
            s.CodeText = w.ToString();

            s.FilePath = "Element";

            return(s);
        }
        protected override void Execute(ExecutionContext context, Snippet snippet)
        {
            LanguageDefinition language = ((LanguageExecutionContext)context).Language;

            snippet.SuffixTitle(" (with parameters)");
            snippet.SuffixShortcut(ShortcutChars.WithParameters);
            snippet.SuffixDescription(" (with parameters)");

            snippet.RemoveLiteralAndReplacePlaceholders(LiteralIdentifiers.ParameterList, "($parameters$)");

            snippet.AddLiteral("parameters", "Parameters", language.GetDefaultParameter());

            snippet.AddTag(KnownTags.ExcludeFromReadme);

            snippet.SuffixFileName("WithParameters");
        }
Beispiel #11
0
        protected override void Execute(ExecutionContext context, Snippet snippet)
        {
            snippet.SuffixTitle(" property");
            snippet.SuffixShortcut("py");
            snippet.SuffixDescription(" property");
            snippet.RemoveTag(KnownTags.GenerateXamlProperty);
            snippet.RemoveTag(KnownTags.NonUniqueShortcut);
            snippet.AddTag(KnownTags.AutoGenerated);

            snippet.Literals.Clear();
            snippet.AddLiteral("property", "Property name", ".");

            string name = Path.GetFileNameWithoutExtension(snippet.FilePath);

            snippet.CodeText = $"<{name}$property$>$end$</{name}$property$>";

            snippet.SuffixFileName("Property");
        }
Beispiel #12
0
        internal static Snippet AddInitializer(Snippet snippet, string initializer, string defaultValue)
        {
            snippet.SuffixTitle(" (with initializer)");
            snippet.SuffixShortcut(ShortcutChars.WithInitializer);
            snippet.SuffixDescription(" (with initializer)");

            snippet.ReplacePlaceholders(LiteralIdentifiers.Initializer, initializer);

            snippet.AddLiteral(LiteralIdentifiers.Value, null, defaultValue);

            snippet.RemoveLiteral(LiteralIdentifiers.Initializer);

            snippet.RemoveLiteralAndPlaceholders(LiteralIdentifiers.ArrayLength);

            snippet.AddTag(KnownTags.ExcludeFromReadme);

            snippet.SuffixFileName("WithInitializer");

            return(snippet);
        }
        private static Snippet CreateCommentSnippet()
        {
            var s = new Snippet()
            {
                Title    = "comment",
                Shortcut = CommentShortcut
            };

            s.Description = s.Title;

            s.AddLiteral(CreateCommentLiteral());

            var b = new SnippetCodeBuilder();

            b.Append("<!-- ");
            b.AppendPlaceholder(Placeholder.SelectedIdentifier);
            b.AppendPlaceholder(CommentIdentifier);
            b.Append(" -->");
            s.CodeText = b.ToString();

            s.FilePath = "Comment";

            return(s);
        }
Beispiel #14
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.Keyword)
                            .ReplacePlaceholder(Placeholders.OfType, $"of {Type.Keyword}")
                            .ReplacePlaceholder(Placeholders.GenericType, language.GetTypeParameterList(Type.Keyword));

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

            snippet.AddNamespace(Type.Namespace);

            snippet.AddTag(KnownTags.ExcludeFromReadme);

            if (Type.Keyword == "this")
            {
                snippet.AddLiteral(Literal.CreateClassNameLiteral("this", "Containing type name", "ThisName"));
                snippet.RemoveLiteralAndReplacePlaceholders(LiteralIdentifiers.Type, "$this$");
            }
            else
            {
                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);
        }
Beispiel #15
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);
            }
        }