public virtual CodeSnippetGroup All(string viewEngine) { var physicalPath = Path.Combine(BasePath, viewEngine); CodeSnippetGroup root = new CodeSnippetGroup() { Name = "root", Parent = null, ViewEngine = viewEngine }; if (Directory.Exists(physicalPath)) { root.ChildGroups = AllGroups(viewEngine, root, physicalPath); root.CodeSnippets = AllCodeSnippet(root, physicalPath); } return root; }
private IEnumerable<CodeSnippet> AllCodeSnippet(CodeSnippetGroup group, string physicalPath) { return Directory.EnumerateFiles(physicalPath, "*" + FileExtension).Select(it => new CodeSnippet() { Group = group, ViewEngine = group.ViewEngine, Name = Path.GetFileNameWithoutExtension(it), Code = IOUtility.ReadAsString(it) }); }
private IEnumerable <CodeSnippet> AllCodeSnippet(CodeSnippetGroup group, string physicalPath) { return(Directory.EnumerateFiles(physicalPath, "*" + FileExtension).Select(it => new CodeSnippet() { Group = group, ViewEngine = group.ViewEngine, Name = Path.GetFileNameWithoutExtension(it), Code = IO.IOUtility.ReadAsString(it) })); }
public override IEnumerable <ISnippetCompletionItem> GetSnippets() { CodeSnippetGroup g = SnippetManager.Instance.FindGroup(Path.GetExtension(this.FileName)); if (g != null) { return(g.Snippets.Select(s => s.CreateCompletionItem(this))); } else { return(base.GetSnippets()); } }
public virtual CodeSnippetGroup All(string viewEngine) { var physicalPath = Path.Combine(BasePath, viewEngine); CodeSnippetGroup root = new CodeSnippetGroup() { Name = "root", Parent = null, ViewEngine = viewEngine }; if (Directory.Exists(physicalPath)) { root.ChildGroups = AllGroups(viewEngine, root, physicalPath); root.CodeSnippets = AllCodeSnippet(root, physicalPath); } return(root); }
private IEnumerable<CodeSnippetGroup> AllGroups(string viewEngine, CodeSnippetGroup parent, string physicalPath) { var dir = Directory.GetDirectories(physicalPath); if (dir.Length != 0) { foreach (var item in dir.ExcludeSvn()) { var group = new CodeSnippetGroup() { ViewEngine = viewEngine, Name = Path.GetFileNameWithoutExtension(item), Parent = parent }; group.ChildGroups = AllGroups(viewEngine, parent, item); group.CodeSnippets = AllCodeSnippet(group, item); yield return group; } } }
private IEnumerable <CodeSnippetGroup> AllGroups(string viewEngine, CodeSnippetGroup parent, string physicalPath) { var dir = Directory.GetDirectories(physicalPath); if (dir.Length != 0) { foreach (var item in dir.ExcludeSvn()) { var group = new CodeSnippetGroup() { ViewEngine = viewEngine, Name = Path.GetFileNameWithoutExtension(item), Parent = parent }; group.ChildGroups = AllGroups(viewEngine, parent, item); group.CodeSnippets = AllCodeSnippet(group, item); yield return(group); } } }
/// <summary> /// Starts the command /// </summary> public override void Run() { ITextEditor editor = SD.GetActiveViewContentService <ITextEditor>(); if (editor == null) { return; } CodeSnippetGroup group = SnippetManager.Instance.FindGroup(Path.GetExtension(editor.FileName)); if (group == null) { return; } DefaultCompletionItemList list = new DefaultCompletionItemList(); list.Items.AddRange(group.Snippets.Where(i => i.HasSelection).Select(item => item.CreateCompletionItem(editor))); new CodeSnippetCompletionWindow(editor, list).Show(); }
/// <summary> /// Starts the command /// </summary> public override void Run() { ICodeEditorProvider provider = WorkbenchSingleton.Workbench.ActiveViewContent as ICodeEditorProvider; if (provider == null) { return; } CodeSnippetGroup group = SnippetManager.Instance.FindGroup(Path.GetExtension(provider.TextEditor.FileName)); if (group == null) { return; } DefaultCompletionItemList list = new DefaultCompletionItemList(); list.Items.AddRange(group.Snippets.Where(i => i.HasSelection).Select(item => item.CreateCompletionItem(provider.TextEditor))); new CodeSnippetCompletionWindow(provider.TextEditor, list).Show(); }
//===================================================================== /// <summary> /// This is used to group and sort code snippets based on the order of the defined syntax generators /// </summary> /// <param name="sender">The sender of the event</param> /// <param name="e">The event arguments</param> /// <remarks>A two-phase approach is used to ensure that we don't have to be concerned about the /// placement of the syntax component in relation to other components that may insert code snippets. /// By running just prior to XSL transformation all other components that may insert code snippets will /// have been executed.</remarks> private void TransformComponent_TopicTransforming(object sender, EventArgs e) { XPathNavigator[] codeList; XmlAttribute attribute; XmlNode code, nextNode; CodeSnippetGroup snippetGroup; ISyntaxGeneratorMetadata metadata; string namespaceUri; int order; // Don't bother if not a transforming event or not in our group TransformingTopicEventArgs tt = e as TransformingTopicEventArgs; if (tt == null || ((BuildComponentCore)sender).GroupId != this.GroupId) { return; } XmlDocument document = tt.Document; XPathNavigator root, navDoc = document.CreateNavigator(); List <CodeSnippetGroup> allGroups = new List <CodeSnippetGroup>(); List <List <CodeSnippet> > extraGroups = new List <List <CodeSnippet> >(); // Select all code nodes. The location depends on the build type. root = navDoc.SelectSingleNode(referenceRoot); // If not null, it's a reference (API) build. If null, it's a conceptual build. if (root != null) { codeList = root.Select(referenceCode).ToArray(); namespaceUri = String.Empty; } else { root = navDoc.SelectSingleNode(conceptualRoot); if (root == null) { this.WriteMessage(tt.Key, MessageLevel.Warn, "Root content node not found. Cannot group " + "and sort code snippets."); return; } codeList = root.Select(conceptualCode).ToArray(); namespaceUri = "http://ddue.schemas.microsoft.com/authoring/2003/5"; } if (codeList.Length == 0) { return; } // In the first pass, group all consecutive code snippets foreach (XPathNavigator navCode in codeList) { code = ((IHasXmlNode)navCode).GetNode(); // If the parent is null, it was a sibling of a lead node and has already been handled. If // the parent is another code element, ignore it as it's a nested code element which will be // replaced later with actual code by the Code Block Component. if (code.ParentNode == null || code.ParentNode.LocalName == "code") { continue; } if (code.LocalName != "div") { snippetGroup = new CodeSnippetGroup(document.CreateElement(containerElementName, namespaceUri)); allGroups.Add(snippetGroup); code.ParentNode.InsertBefore(snippetGroup.SnippetGroupElement, code); } else { // A div indicates a syntax section so we must reuse the parent element rather than replace // it as they are already grouped. snippetGroup = new CodeSnippetGroup((XmlElement)code.ParentNode) { IsSyntaxSection = true }; allGroups.Add(snippetGroup); } // Keep going until we hit a text node or a non-code element while (code != null && (code.LocalName == "code" || code.LocalName == "snippet" || code.LocalName == "div")) { snippetGroup.CodeSnippets.Add(new CodeSnippet((XmlElement)code)); nextNode = code.NextSibling; code.ParentNode.RemoveChild(code); // Skip intervening comment nodes as they may incorrectly split a group while (nextNode is XmlComment) { nextNode = nextNode.NextSibling; } code = nextNode as XmlElement; } } foreach (var group in allGroups.ToList()) { // Get the group insertion point and snippet count up front as they may change below int groupInsertionPoint = allGroups.IndexOf(group) + 1, snippetCount = group.CodeSnippets.Count; CodeSnippetGroup insertionPoint = group; // For each group, move snippets with a title or a language not in the syntax set into a new // group by themselves following the containing group and remove them from the containing group. foreach (var snippet in group.CodeSnippets.ToList()) { if (!codeSnippetLanguages.TryGetValue(snippet.Language, out metadata)) { // If the language is not set or not recognized, put it in a standalone group without // a title if one hasn't been set already. snippet.LanguageElementName = snippet.KeywordStyleParameter = "Other"; if (snippet.CodeElement.Attributes["title"] == null) { string friendlyName = " "; if (!snippet.Language.Equals("none", StringComparison.OrdinalIgnoreCase) && !snippet.Language.Equals("other", StringComparison.OrdinalIgnoreCase)) { // If we have a set of shared language IDs, see if it's in there so that we can // get a human-readable name. If not, we'll use the language ID as the title. var languageIds = (IReadOnlyDictionary <string, string>)BuildComponentCore.Data["LanguageIds"]; if (languageIds == null || !languageIds.TryGetValue(snippet.Language, out friendlyName)) { friendlyName = snippet.Language; } } attribute = document.CreateAttribute("title"); attribute.Value = friendlyName; snippet.CodeElement.Attributes.Append(attribute); } } else { snippet.LanguageElementName = metadata.LanguageElementName; snippet.KeywordStyleParameter = metadata.KeywordStyleParameter; if (!languageOrder.TryGetValue(metadata.Id, out order)) { order = metadata.SortOrder; } snippet.SortOrder = order; } attribute = document.CreateAttribute("codeLanguage"); attribute.Value = snippet.LanguageElementName; snippet.CodeElement.Attributes.Append(attribute); attribute = document.CreateAttribute("style"); attribute.Value = snippet.KeywordStyleParameter; snippet.CodeElement.Attributes.Append(attribute); if (snippet.CodeElement.Attributes["title"] != null || !generatorLanguages.Contains(snippet.Language)) { if (snippetCount != 1) { snippetGroup = new CodeSnippetGroup(document.CreateElement(containerElementName, namespaceUri)) { IsStandalone = true }; allGroups.Insert(groupInsertionPoint++, snippetGroup); insertionPoint.SnippetGroupElement.ParentNode.InsertAfter(snippetGroup.SnippetGroupElement, insertionPoint.SnippetGroupElement); insertionPoint = snippetGroup; group.CodeSnippets.Remove(snippet); snippetGroup.CodeSnippets.Add(snippet); } else { group.IsStandalone = true; } } } // If all snippets were converted into standalone snippets, remove the group if (group.CodeSnippets.Count == 0) { group.SnippetGroupElement.ParentNode.RemoveChild(group.SnippetGroupElement); allGroups.Remove(group); continue; } extraGroups.Clear(); groupInsertionPoint = allGroups.IndexOf(group) + 1; insertionPoint = group; // If there are multiple snippets for a language in a group, move the duplicates into a new group if (!group.IsStandalone && group.CodeSnippets.Count != 1) { // There can be multiple language IDs that map to the same keyword style so group by that foreach (var langSet in group.CodeSnippets.GroupBy(c => c.KeywordStyleParameter).Where( g => g.Count() != 1).ToList()) { // For syntax sections, merge common language snippets into a single section if (group.IsSyntaxSection) { var firstSnippet = langSet.First(); foreach (var snippet in langSet.Skip(1)) { firstSnippet.CodeElement.InnerXml += "\r\n\r\n" + snippet.CodeElement.InnerXml; group.CodeSnippets.Remove(snippet); } continue; } // For each duplicate, find a group that doesn't contain the language or create // a new group and put it in that one. foreach (var snippet in langSet.Skip(1)) { var moveToGroup = extraGroups.FirstOrDefault(eg => !eg.Any( c => c.Language == snippet.Language)); if (moveToGroup == null) { moveToGroup = new List <CodeSnippet>(); extraGroups.Add(moveToGroup); } moveToGroup.Add(snippet); group.CodeSnippets.Remove(snippet); } } foreach (var set in extraGroups) { snippetGroup = new CodeSnippetGroup(document.CreateElement(containerElementName, namespaceUri)); allGroups.Insert(groupInsertionPoint++, snippetGroup); insertionPoint.SnippetGroupElement.ParentNode.InsertAfter(snippetGroup.SnippetGroupElement, insertionPoint.SnippetGroupElement); insertionPoint = snippetGroup; snippetGroup.CodeSnippets.AddRange(set); } } } // We've got all the necessary groups now so make the final pass foreach (var group in allGroups) { // If wanted, add "no example" snippets to groups that don't contain all of the syntax languages. // Standalone groups are always excluded. Single snippets are excluded if so indicated. if (addNoExampleTabs && !group.IsStandalone && (group.CodeSnippets.Count > 1 || includeOnSingleSnippets)) { foreach (var style in languageSet.Select(l => l.KeywordStyleParameter).Except( group.CodeSnippets.Select(c => c.KeywordStyleParameter))) { var language = languageSet.First(l => l.KeywordStyleParameter == style); var noExample = document.CreateElement(group.CodeSnippets[0].CodeElement.LocalName, namespaceUri); attribute = document.CreateAttribute("language"); attribute.Value = language.KeywordStyleParameter; noExample.Attributes.Append(attribute); attribute = document.CreateAttribute("codeLanguage"); attribute.Value = language.LanguageElementName; noExample.Attributes.Append(attribute); attribute = document.CreateAttribute("style"); attribute.Value = language.KeywordStyleParameter; noExample.Attributes.Append(attribute); attribute = document.CreateAttribute("phantom"); attribute.Value = "true"; noExample.Attributes.Append(attribute); if (!languageOrder.TryGetValue(language.Id, out order)) { order = language.SortOrder; } group.CodeSnippets.Add(new CodeSnippet(noExample) { SortOrder = order }); } } // And finally, add the snippets to the group container in sorted order foreach (var snippet in group.CodeSnippets.OrderBy(c => c.SortOrder)) { group.SnippetGroupElement.AppendChild(snippet.CodeElement); } } }