public BoundActiveElement(InsertionContext context, SnippetReplaceableTextElement targetSnippetElement, SnippetBoundElement boundElement, AnchorSegment segment) { this.context = context; this.targetSnippetElement = targetSnippetElement; this.boundElement = boundElement; this.segment = segment; }
/// <summary> /// Adds existing segments as snippet elements. /// </summary> public void Link(ISegment mainElement, ISegment[] boundElements) { var main = new SnippetReplaceableTextElement { Text = Document.GetText(mainElement) }; RegisterActiveElement(main, new ReplaceableActiveElement(this, mainElement.Offset, mainElement.EndOffset)); foreach (var boundElement in boundElements) { var bound = new SnippetBoundElement { TargetElement = main }; var start = Document.CreateAnchor(boundElement.Offset); start.MovementType = AnchorMovementType.BeforeInsertion; start.SurviveDeletion = true; var end = Document.CreateAnchor(boundElement.EndOffset); end.MovementType = AnchorMovementType.BeforeInsertion; end.SurviveDeletion = true; RegisterActiveElement(bound, new BoundActiveElement(this, main, bound, new AnchorSegment(start, end))); } }
public static Snippet CreateAvalonEditSnippet(RunbookModelProxy runbook, string snippetText) { if (snippetText == null) throw new ArgumentNullException("text"); var replaceableElements = new Dictionary<string, SnippetReplaceableTextElement>(StringComparer.OrdinalIgnoreCase); foreach (Match m in pattern.Matches(snippetText)) { string val = m.Groups[1].Value; int equalsSign = val.IndexOf('='); if (equalsSign > 0) { string name = val.Substring(0, equalsSign); replaceableElements[name] = new SnippetReplaceableTextElement(); } } Snippet snippet = new Snippet(); int pos = 0; foreach (Match m in pattern.Matches(snippetText)) { if (pos < m.Index) { snippet.Elements.Add(new SnippetTextElement { Text = snippetText.Substring(pos, m.Index - pos) }); pos = m.Index; } snippet.Elements.Add(CreateElementForValue(runbook, replaceableElements, m.Groups[1].Value, m.Index, snippetText)); pos = m.Index + m.Length; } if (pos < snippetText.Length) { snippet.Elements.Add(new SnippetTextElement { Text = snippetText.Substring(pos) }); } if (!snippet.Elements.Any(e => e is SnippetCaretElement)) { var obj = snippet.Elements.FirstOrDefault(e2 => e2 is SnippetSelectionElement); int index = snippet.Elements.IndexOf(obj); if (index > -1) snippet.Elements.Insert(index + 1, new SnippetCaretElement()); } return snippet; }
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; }
public void Complete(TextArea textArea, ISegment completionSegment, EventArgs insertionRequestEventArgs) { var context = new InsertionContext(textArea, completionSegment.Offset); var sni = new SnippetContainerElement(); sni.Elements.Add(new SnippetTextElement{Text = Name + "("}); foreach (var argument in Arguments) { var element = new SnippetReplaceableTextElement{Text = argument.ToString()}; sni.Elements.Add(element); sni.Elements.Add(new SnippetTextElement{Text = ", "}); } if (Arguments.Count > 0) sni.Elements.RemoveAt(sni.Elements.Count - 1); sni.Elements.Add(new SnippetTextElement{Text = ")"}); textArea.Document.Remove(completionSegment); sni.Insert(context); context.RaiseInsertionCompleted(null); }
/// <summary> /// Add Time Stamp to TextBox /// </summary> /// <param name="b"></param> // ReSharper disable UnusedParameter.Local private void AddTimeStamp(bool b) // ReSharper restore UnusedParameter.Local { var item = new SnippetTextElement { Text = "\r\n; * " }; var item2 = new SnippetTextElement { Text = "By : " }; var current = WindowsIdentity.GetCurrent(); if (current != null) { var item3 = new SnippetReplaceableTextElement { Text = current.Name }; var item4 = new SnippetTextElement { Text = DateTime.Now.ToString(((EditorOptions)Options).TimestampFormat) }; var snippet = new Snippet { Elements = { item, item2, item3, item, item4, item } }; snippet.Insert(TextArea); } }