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); }
/// <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); }
/// <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, ""); } } }
/// <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); }
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); } } }
/// <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); }
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; } } } }
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); } }
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); }
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); } } }
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); }
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; } }
public static void AddTag(this Snippet snippet, string tag) { AddKeyword(snippet, KnownTags.MetaTagPrefix + tag); }
public static void SortCollections(this Snippet snippet) { snippet.Literals.Sort(); snippet.Keywords.Sort(); snippet.Namespaces.Sort(); }
public static string FileNameWithoutExtension(this Snippet snippet) { return(Path.GetFileNameWithoutExtension(snippet.FilePath)); }
public static string FileName(this Snippet snippet) { return(Path.GetFileName(snippet.FilePath)); }
public static void ReplacePlaceholders(this Snippet snippet, string identifier, string replacement) { snippet.CodeText = snippet.Code.ReplacePlaceholders(identifier, replacement); }
public static void RemoveLiteralAndPlaceholders(this Snippet snippet, Literal literal) { RemoveLiteralAndPlaceholders(snippet, literal.Identifier); }
public static void SuffixShortcut(this Snippet snippet, string value) { snippet.Shortcut += value; }
/// <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())); }
public static void SetFileName(this Snippet snippet, string fileName) { snippet.FilePath = Path.Combine(Path.GetDirectoryName(snippet.FilePath), fileName); }
public static void SuffixFileName(this Snippet snippet, string value) { SetFileName(snippet, Path.GetFileNameWithoutExtension(snippet.FilePath) + value + Path.GetExtension(snippet.FilePath)); }
public static void SuffixDescription(this Snippet snippet, string value) { snippet.Description += value; }
public static void PrefixFileName(this Snippet snippet, string value) { SetFileName(snippet, value + Path.GetFileName(snippet.FilePath)); }
public static void RemoveLiteralAndReplacePlaceholders(this Snippet snippet, string identifier, string replacement) { snippet.RemoveLiteral(identifier); snippet.ReplacePlaceholders(identifier, replacement); }
public static bool RemoveKeyword(this Snippet snippet, string keyword) { return(snippet.Keywords.Remove(keyword)); }
public static void SuffixTitle(this Snippet snippet, string value) { snippet.Title += value; }
public static bool ContainsKeyword(this Snippet snippet, string keyword) { return(snippet.Keywords.Contains(keyword)); }
public static bool RemoveLiteral(this Snippet snippet, string identifier) { return(snippet.Literals.Remove(identifier)); }
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(); }