public static Snippet BuildSnippet(XElement element) { var snippet = new Snippet(); var decarations = GetDecarations(element); var dictionary = new Dictionary<string, SnippetReplaceableTextElement>(); var text = GetTheCode(element); while (text.ContainsDeclaration(decarations)) { var theNextId = text.GetTheNextId(decarations); var text2 = text.Substring(0, text.IndexOf(theNextId, System.StringComparison.Ordinal)); if (!string.IsNullOrEmpty(text2)) { snippet.Elements.Add(new SnippetTextElement { Text = text2 }); text = text.Remove(0, text2.Length); } text = text.Remove(0, theNextId.Length); if (theNextId == "$end$") { snippet.Elements.Add(new SnippetCaretElement()); } else { if (theNextId == "$selection$") { snippet.Elements.Add(new SnippetSelectionElement()); } else { if (dictionary.ContainsKey(theNextId)) { snippet.Elements.Add(new SnippetBoundElement { TargetElement = dictionary[theNextId] }); } else { var snippetReplaceableTextElement = new SnippetReplaceableTextElement { Text = decarations[theNextId].Default }; snippet.Elements.Add(snippetReplaceableTextElement); dictionary.Add(theNextId, snippetReplaceableTextElement); } } } } if (!string.IsNullOrEmpty(text)) { snippet.Elements.Add(new SnippetTextElement { Text = text }); } return snippet; }
private static Snippet BuildSnippet(string text) { var expanded = text .Trim() .Replace("\\r", "\r") .Replace("\\n", "\n") .Replace("\\t", "\t") .ReplaceDate(); var snippet = new Snippet(); var replaceable = new Regex(@"(\$\w+\$)"); foreach (var token in replaceable.Split(expanded)) { if (token == "$END$") snippet.Elements.Add(new SnippetCaretElement()); else if (replaceable.IsMatch(token)) snippet.Elements.Add(new SnippetReplaceableTextElement { Text = token.Trim('$') }); else snippet.Elements.Add(new SnippetTextElement { Text = token }); } return snippet; }
/// <summary> /// Initializes the code snippets. /// </summary> private void InitializeSnippets() { // "for (|;;)\n{\n}" Snippet forSnippet = new Snippet(); forSnippet.Elements.Add(new SnippetTextElement { Text = "for (" }); forSnippet.Elements.Add(new SnippetCaretElement()); forSnippet.Elements.Add(new SnippetTextElement { Text = ";;)\n{\n}" }); // "if (|)\n{\n}" Snippet ifSnippet = new Snippet(); ifSnippet.Elements.Add(new SnippetTextElement { Text = "if (" }); ifSnippet.Elements.Add(new SnippetCaretElement()); ifSnippet.Elements.Add(new SnippetTextElement { Text = ")\n{\n}" }); // "while (|)\n{\n}" Snippet whileSnippet = new Snippet(); whileSnippet.Elements.Add(new SnippetTextElement { Text = "while (" }); whileSnippet.Elements.Add(new SnippetCaretElement()); whileSnippet.Elements.Add(new SnippetTextElement { Text = ")\n{\n}" }); // "technique |\n{\npass\n{\n}\n}" Snippet techniqueSnippet = new Snippet(); techniqueSnippet.Elements.Add(new SnippetTextElement { Text = "technique " }); techniqueSnippet.Elements.Add(new SnippetCaretElement()); techniqueSnippet.Elements.Add(new SnippetTextElement { Text = "\n{\npass\n{\n}\n}" }); // "pass |\n{\n}" Snippet passSnippet = new Snippet(); passSnippet.Elements.Add(new SnippetTextElement { Text = "pass " }); passSnippet.Elements.Add(new SnippetCaretElement()); passSnippet.Elements.Add(new SnippetTextElement { Text = "\n{\n}" }); SnippetCompletionData[] snippets = { new SnippetCompletionData("for", "for loop", MultiColorGlyphs.Snippet, forSnippet), new SnippetCompletionData("if", "if statement", MultiColorGlyphs.Snippet, ifSnippet), new SnippetCompletionData("while", "while loop", MultiColorGlyphs.Snippet, whileSnippet), new SnippetCompletionData("technique", null, MultiColorGlyphs.Snippet, techniqueSnippet), new SnippetCompletionData("pass", null, MultiColorGlyphs.Snippet, passSnippet), }; foreach (SnippetCompletionData snippet in snippets) Snippets.Add(snippet.Text, snippet); }