Exemplo n.º 1
0
        public static string GetSubmenuShortcut(this Snippet snippet)
        {
            if (snippet.HasTag(KnownTags.TitleStartsWithShortcut))
            {
                string s = snippet.Title;

                int i = 0;

                while (i < s.Length &&
                       s[i] != ' ')
                {
                    i++;
                }

                return(s.Substring(0, i));
            }

            return(null);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Serializes the specified <see cref="Snippet"/> the specified snippet file, optionally using <see cref="SaveSettings"/> to modify serialization process.
        /// </summary>
        /// <param name="filePath">The absolute or relative path to the file.</param>
        /// <param name="snippet">A <see cref="Snippet"/> to be serialized.</param>
        /// <param name="settings">A <see cref="SaveSettings"/> that modify serialization process.</param>
        /// <exception cref="ArgumentNullException"><paramref name="filePath"/> or <paramref name="snippet"/> or <paramref name="settings"/> is <c>null</c>.</exception>
        public static void Serialize(string filePath, Snippet snippet, SaveSettings settings)
        {
            if (filePath == null)
            {
                throw new ArgumentNullException(nameof(filePath));
            }

            if (snippet == null)
            {
                throw new ArgumentNullException(nameof(snippet));
            }

            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            using (var stream = new FileStream(filePath, FileMode.CreateNew, FileAccess.Write, FileShare.None))
                Serialize(stream, snippet, settings);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Removes all literals that do not have corresponding placeholder (placeholder with same identifier).
        /// </summary>
        /// <param name="snippet"><see cref="Snippet"/> to remove literals from.</param>
        public static void RemoveUnusedLiterals(Snippet snippet)
        {
            List <string> identifiers = null;

            foreach (Literal literal in snippet.Literals)
            {
                if (!snippet.Code.Placeholders.Contains(literal.Identifier))
                {
                    (identifiers ?? (identifiers = new List <string>())).Add(literal.Identifier);
                }
            }

            if (identifiers != null)
            {
                foreach (string identifier in identifiers)
                {
                    snippet.CodeText = snippet.Code.ReplacePlaceholders(identifier, "");
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Serializes the specified <see cref="Snippet"/> the specified <see cref="Stream"/>, optionally using <see cref="SaveSettings"/> to modify serialization process.
        /// </summary>
        /// <param name="stream">The stream to output this <see cref="Snippet"/> to.</param>
        /// <param name="snippet">A <see cref="Snippet"/> to be serialized.</param>
        /// <param name="settings">A <see cref="SaveSettings"/> that modify serialization process.</param>
        /// <exception cref="ArgumentNullException"><paramref name="stream"/> or <paramref name="snippet"/> or <paramref name="settings"/> is <c>null</c>.</exception>
        public static void Serialize(Stream stream, Snippet snippet, SaveSettings settings)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            if (snippet == null)
            {
                throw new ArgumentNullException(nameof(snippet));
            }

            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            using (XmlWriter xmlWriter = XmlWriter.Create(stream, GetXmlWriterSettings(settings)))
                Serialize(xmlWriter, snippet, settings);
        }
Exemplo n.º 5
0
        private static void LoadReferences(ReferenceElement[] references, Snippet snippet)
        {
            foreach (ReferenceElement element in references)
            {
                if (!string.IsNullOrEmpty(element.Assembly))
                {
                    var reference = new AssemblyReference()
                    {
                        AssemblyName = element.Assembly
                    };

                    if (!string.IsNullOrEmpty(element.Url) &&
                        Uri.TryCreate(element.Url, UriKind.RelativeOrAbsolute, out Uri url))
                    {
                        reference.Url = url;
                    }

                    snippet.AssemblyReferences.Add(reference);
                }
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Maps a specified <see cref="Snippet"/> to the newly created <see cref="CodeSnippetElement"/>, optionally modifying serialization process.
        /// </summary>
        /// <param name="snippet">A <see cref="Snippet"/> to be serialized.</param>
        /// <param name="settings">A <see cref="SaveSettings"/> that modify serialization process.</param>
        /// <returns>Newly created <see cref="CodeSnippetElement"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="snippet"/> or <paramref name="settings"/> is <c>null</c>.</exception>
        public static CodeSnippetElement MapToElement(Snippet snippet, SaveSettings settings)
        {
            if (snippet == null)
            {
                throw new ArgumentNullException(nameof(snippet));
            }

            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            var context = new SerializationContext(snippet, settings);

            SerializeVersion(snippet.FormatVersion, context);

            context.Element.Header  = CreateHeaderElement(context);
            context.Element.Snippet = CreateSnippetElement(context);

            return(context.Element);
        }
Exemplo n.º 7
0
        private static void LoadHeaderElement(HeaderElement element, Snippet snippet)
        {
            snippet.Author      = element.Author;
            snippet.Description = element.Description;
            snippet.Shortcut    = element.Shortcut;
            snippet.Title       = element.Title;

            if (element.AlternativeShortcuts != null)
            {
                foreach (string shortcut in element.AlternativeShortcuts)
                {
                    snippet.AlternativeShortcuts.Add(shortcut);
                }
            }

            if (element.HelpUrl != null &&
                Uri.TryCreate(element.HelpUrl, UriKind.RelativeOrAbsolute, out Uri uri))
            {
                snippet.HelpUrl = uri;
            }

            if (element.Keywords != null)
            {
                foreach (string keyword in element.Keywords)
                {
                    snippet.Keywords.Add(keyword);
                }
            }

            if (element.SnippetTypes != null)
            {
                foreach (string value in element.SnippetTypes)
                {
                    if (Enum.TryParse(value, out SnippetTypes snippetTypes))
                    {
                        snippet.SnippetTypes |= snippetTypes;
                    }
                }
            }
        }
Exemplo n.º 8
0
        private static void LoadSnippetElement(SnippetElement element, Snippet snippet)
        {
            if (element.Code != null)
            {
                LoadCodeElement(element.Code, snippet);
            }

            if (element.Declarations != null)
            {
                LoadDeclarationsElement(element.Declarations, snippet);
            }

            if (element.Imports != null)
            {
                LoadImports(element.Imports, snippet);
            }

            if (element.References != null)
            {
                LoadReferences(element.References, snippet);
            }
        }
Exemplo n.º 9
0
        public static bool HasTag(this Snippet snippet, string tag)
        {
            foreach (string keyword in snippet.Keywords)
            {
                if (keyword.StartsWith(KnownTags.MetaTagPrefix))
                {
                    int i = KnownTags.MetaTagPrefix.Length;
                    while (i < keyword.Length &&
                           char.IsWhiteSpace(keyword[i]))
                    {
                        i++;
                    }

                    if (string.Equals(keyword.Substring(i, Math.Min(keyword.Length - i, tag.Length)), tag, StringComparison.Ordinal))
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
Exemplo n.º 10
0
        public static bool RemoveTag(this Snippet snippet, string tag)
        {
            string tagWithPrefix = KnownTags.MetaTagPrefix + tag;

            if (RemoveKeyword(snippet, tagWithPrefix))
            {
                return(true);
            }
            else
            {
                string keyword = snippet.Keywords.FirstOrDefault(f => f.StartsWith(tagWithPrefix + " ", StringComparison.Ordinal));

                if (keyword != null)
                {
                    return(snippet.RemoveKeyword(keyword));
                }
                else
                {
                    return(false);
                }
            }
        }
Exemplo n.º 11
0
        public static string GetTagValueOrDefault(this Snippet snippet, string tag)
        {
            foreach (string keyword in snippet.Keywords)
            {
                if (keyword.StartsWith(KnownTags.MetaTagPrefix))
                {
                    int i = KnownTags.MetaTagPrefix.Length;
                    while (i < keyword.Length &&
                           char.IsWhiteSpace(keyword[i]))
                    {
                        i++;
                    }

                    if (string.Equals(keyword.Substring(i, Math.Min(keyword.Length - i, tag.Length)), tag, StringComparison.Ordinal))
                    {
                        return(keyword.Substring(i + tag.Length).Trim());
                    }
                }
            }

            return(null);
        }
Exemplo n.º 12
0
        private static void LoadCodeElement(CodeElement element, Snippet snippet)
        {
            if (element.Delimiter?.Length == 1)
            {
                snippet.Delimiter = element.Delimiter[0];
            }

            if (element.Kind != null &&
                ContextKinds.TryGetValue(element.Kind, out ContextKind kind))
            {
                snippet.ContextKind = kind;
            }

            if (element.Language != null)
            {
                snippet.Language = LanguageMapper.MapTextToEnum(element.Language);
            }

            if (element.Code != null)
            {
                snippet.CodeText = element.Code;
            }
        }
Exemplo n.º 13
0
 public static void AddTag(this Snippet snippet, string tag)
 {
     AddKeyword(snippet, KnownTags.MetaTagPrefix + tag);
 }
Exemplo n.º 14
0
 public static void SortCollections(this Snippet snippet)
 {
     snippet.Literals.Sort();
     snippet.Keywords.Sort();
     snippet.Namespaces.Sort();
 }
Exemplo n.º 15
0
 public static string FileNameWithoutExtension(this Snippet snippet)
 {
     return(Path.GetFileNameWithoutExtension(snippet.FilePath));
 }
Exemplo n.º 16
0
 public static string FileName(this Snippet snippet)
 {
     return(Path.GetFileName(snippet.FilePath));
 }
Exemplo n.º 17
0
 public static void ReplacePlaceholders(this Snippet snippet, string identifier, string replacement)
 {
     snippet.CodeText = snippet.Code.ReplacePlaceholders(identifier, replacement);
 }
Exemplo n.º 18
0
 public static void RemoveLiteralAndPlaceholders(this Snippet snippet, Literal literal)
 {
     RemoveLiteralAndPlaceholders(snippet, literal.Identifier);
 }
Exemplo n.º 19
0
 public static void SuffixShortcut(this Snippet snippet, string value)
 {
     snippet.Shortcut += value;
 }
Exemplo n.º 20
0
 /// <summary>
 /// Maps a specified <see cref="Snippet"/> to the newly created <see cref="CodeSnippetElement"/>.
 /// </summary>
 /// <param name="snippet">A <see cref="Snippet"/> to be serialized.</param>
 /// <returns>Newly created <see cref="CodeSnippetElement"/>.</returns>
 public static CodeSnippetElement MapToElement(Snippet snippet)
 {
     return(MapToElement(snippet, new SaveSettings()));
 }
Exemplo n.º 21
0
 public static void SetFileName(this Snippet snippet, string fileName)
 {
     snippet.FilePath = Path.Combine(Path.GetDirectoryName(snippet.FilePath), fileName);
 }
Exemplo n.º 22
0
 public static void SuffixFileName(this Snippet snippet, string value)
 {
     SetFileName(snippet, Path.GetFileNameWithoutExtension(snippet.FilePath) + value + Path.GetExtension(snippet.FilePath));
 }
Exemplo n.º 23
0
 public static void SuffixDescription(this Snippet snippet, string value)
 {
     snippet.Description += value;
 }
Exemplo n.º 24
0
 public static void PrefixFileName(this Snippet snippet, string value)
 {
     SetFileName(snippet, value + Path.GetFileName(snippet.FilePath));
 }
Exemplo n.º 25
0
 public static void RemoveLiteralAndReplacePlaceholders(this Snippet snippet, string identifier, string replacement)
 {
     snippet.RemoveLiteral(identifier);
     snippet.ReplacePlaceholders(identifier, replacement);
 }
Exemplo n.º 26
0
 public static bool RemoveKeyword(this Snippet snippet, string keyword)
 {
     return(snippet.Keywords.Remove(keyword));
 }
Exemplo n.º 27
0
 public static void SuffixTitle(this Snippet snippet, string value)
 {
     snippet.Title += value;
 }
Exemplo n.º 28
0
 public static bool ContainsKeyword(this Snippet snippet, string keyword)
 {
     return(snippet.Keywords.Contains(keyword));
 }
Exemplo n.º 29
0
 public static bool RemoveLiteral(this Snippet snippet, string identifier)
 {
     return(snippet.Literals.Remove(identifier));
 }
Exemplo n.º 30
0
 public static void RemoveShortcutFromTitle(this Snippet snippet)
 {
     snippet.Title = snippet.GetTitleWithoutShortcut();
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="SerializationContext"/> class with a specified code snippet and and settings.
 /// </summary>
 /// <param name="snippet">A <see cref="Snippet"/> instance to serialize.</param>
 /// <param name="settings">A <see cref="SaveSettings"/> that enables to modify code snippet serialization process.</param>
 public SerializationContext(Snippet snippet, SaveSettings settings)
 {
     Snippet = snippet;
     Settings = settings;
     Element = new CodeSnippetElement();
 }