public TemplateResult FillVariables(TemplateContext context) { var expansion = CodeTemplateService.GetExpansionObject(this); var result = new TemplateResult(); var sb = new StringBuilder(); int lastOffset = 0; string code = context.Editor.FormatString(context.InsertPosition, context.TemplateCode); result.TextLinks = new List <TextLink> (); foreach (System.Text.RegularExpressions.Match match in variableRegEx.Matches(code)) { string name = match.Groups [1].Value; sb.Append(code, lastOffset, match.Index - lastOffset); lastOffset = match.Index + match.Length; if (string.IsNullOrEmpty(name)) // $$ is interpreted as $ { sb.Append("$"); } else if (name == "end") { result.CaretEndOffset = sb.Length; } else if (name == "selected") { if (!string.IsNullOrEmpty(context.SelectedText)) { string indent = GetIndent(sb); string selection = Reindent(context.SelectedText, indent); sb.Append(selection); } } if (!variableDecarations.ContainsKey(name)) { continue; } var link = result.TextLinks.Find(l => l.Name == name); bool isNew = link == null; if (isNew) { link = new TextLink(name); if (!string.IsNullOrEmpty(variableDecarations [name].ToolTip)) { link.Tooltip = GettextCatalog.GetString(variableDecarations [name].ToolTip); } link.Values = new CodeTemplateListDataProvider(variableDecarations [name].Values); if (!string.IsNullOrEmpty(variableDecarations [name].Function)) { link.Values = expansion.RunFunction(context, null, variableDecarations [name].Function); } result.TextLinks.Add(link); } link.IsEditable = variableDecarations [name].IsEditable; link.IsIdentifier = variableDecarations [name].IsIdentifier; if (!string.IsNullOrEmpty(variableDecarations [name].Function)) { var functionResult = expansion.RunFunction(context, null, variableDecarations [name].Function); if (functionResult != null && functionResult.Count > 0) { string s = (string)functionResult [functionResult.Count - 1]; if (s == null) { if (variableDecarations.ContainsKey(name)) { s = variableDecarations [name].Default; } } if (s != null) { link.AddLink(new TextSegment(sb.Length, s.Length)); if (isNew) { link.GetStringFunc = delegate(Func <string, string> callback) { return(expansion.RunFunction(context, callback, variableDecarations [name].Function)); }; } sb.Append(s); } } else { AddDefaultValue(sb, link, name); } } else { AddDefaultValue(sb, link, name); } } sb.Append(code, lastOffset, code.Length - lastOffset); // format & indent template code var data = TextEditorFactory.CreateNewDocument(); data.Text = sb.ToString(); data.TextChanged += delegate(object sender, MonoDevelop.Core.Text.TextChangeEventArgs e) { int delta = e.InsertionLength - e.RemovalLength; foreach (var link in result.TextLinks) { link.Links = link.Links.AdjustSegments(e).ToList(); } if (result.CaretEndOffset > e.Offset) { result.CaretEndOffset += delta; } }; IndentCode(data, context.LineIndent); result.Code = data.Text; return(result); }
public TemplateResult FillVariables(TemplateContext context) { ExpansionObject expansion = CodeTemplateService.GetExpansionObject(this); TemplateResult result = new TemplateResult(); StringBuilder sb = new StringBuilder(); int lastOffset = 0; string code = context.Document.Editor.FormatString(context.InsertPosition, context.TemplateCode); result.TextLinks = new List <TextLink> (); foreach (Match match in variableRegEx.Matches(code)) { string name = match.Groups [1].Value; sb.Append(code.Substring(lastOffset, match.Index - lastOffset)); lastOffset = match.Index + match.Length; if (string.IsNullOrEmpty(name)) // $$ is interpreted as $ { sb.Append("$"); } else if (name == "end") { result.CaretEndOffset = sb.Length; } else if (name == "selected") { if (!string.IsNullOrEmpty(context.SelectedText)) { string indent = GetIndent(sb); string selection = Reindent(context.SelectedText, indent); sb.Append(selection); } } if (!variableDecarations.ContainsKey(name)) { continue; } TextLink link = result.TextLinks.Find(l => l.Name == name); bool isNew = link == null; if (isNew) { link = new TextLink(name); if (!string.IsNullOrEmpty(variableDecarations [name].ToolTip)) { link.Tooltip = GettextCatalog.GetString(variableDecarations [name].ToolTip); } link.Values = new CodeTemplateListDataProvider(variableDecarations [name].Values); if (!string.IsNullOrEmpty(variableDecarations [name].Function)) { link.Values = expansion.RunFunction(context, null, variableDecarations [name].Function); } result.TextLinks.Add(link); } link.IsEditable = variableDecarations [name].IsEditable; link.IsIdentifier = variableDecarations [name].IsIdentifier; if (!string.IsNullOrEmpty(variableDecarations [name].Function)) { IListDataProvider <string> functionResult = expansion.RunFunction(context, null, variableDecarations [name].Function); if (functionResult != null && functionResult.Count > 0) { string s = (string)functionResult [functionResult.Count - 1]; if (s == null) { if (variableDecarations.ContainsKey(name)) { s = variableDecarations [name].Default; } } if (s != null) { link.AddLink(new Segment(sb.Length, s.Length)); if (isNew) { link.GetStringFunc = delegate(Func <string, string> callback) { return(expansion.RunFunction(context, callback, variableDecarations [name].Function)); }; } sb.Append(s); } } else { AddDefaultValue(sb, link, name); } } else { AddDefaultValue(sb, link, name); } } sb.Append(code.Substring(lastOffset, code.Length - lastOffset)); // format & indent template code TextEditorData data = new TextEditorData(); data.Text = sb.ToString(); data.Document.TextReplaced += delegate(object sender, ReplaceEventArgs e) { int delta = -e.Count + (e.Value != null ? e.Value.Length : 0); foreach (var link in result.TextLinks) { foreach (var segment in link.Links) { if (segment.Offset > e.Offset) { segment.Offset += delta; } } } if (result.CaretEndOffset > e.Offset) { result.CaretEndOffset += delta; } }; var formatter = CodeFormatterService.GetFormatter(context.Document.Editor.Document.MimeType); if (formatter != null && context.Document.HasProject) { formatter.OnTheFlyFormat(context.Document.Project.Policies, data, 0, data.Length); } IndentCode(data, context.LineIndent); result.Code = data.Text; data.Dispose(); return(result); }
public TemplateResult FillVariables(TemplateContext context) { var expansion = CodeTemplateService.GetExpansionObject(this); var result = new TemplateResult(); var sb = StringBuilderCache.Allocate(); int lastOffset = 0; string code = context.Editor.FormatString(context.InsertPosition, context.TemplateCode); result.TextLinks = new List <TextLink> (); foreach (System.Text.RegularExpressions.Match match in variableRegEx.Matches(code)) { string name = match.Groups [1].Value; sb.Append(code, lastOffset, match.Index - lastOffset); lastOffset = match.Index + match.Length; if (string.IsNullOrEmpty(name)) // $$ is interpreted as $ { sb.Append("$"); } else { switch (name) { case "end": result.CaretEndOffset = sb.Length; break; case "selected": if (!string.IsNullOrEmpty(context.SelectedText)) { string indent = GetIndent(sb); string selection = Reindent(context.SelectedText, indent); sb.Append(selection); } break; case "TM_CURRENT_LINE": sb.Append(context.Editor.CaretLine); break; case "TM_CURRENT_WORD": sb.Append(""); break; case "TM_FILENAME": sb.Append(context.Editor.FileName); break; case "TM_FILEPATH": sb.Append(Path.GetDirectoryName(context.Editor.FileName)); break; case "TM_FULLNAME": sb.Append(AuthorInformation.Default.Name); break; case "TM_LINE_INDEX": sb.Append(context.Editor.CaretColumn - 1); break; case "TM_LINE_NUMBER": sb.Append(context.Editor.CaretLine); break; case "TM_SOFT_TABS": sb.Append(context.Editor.Options.TabsToSpaces ? "YES" : "NO"); // Note: these strings need no translation. break; case "TM_TAB_SIZE": sb.Append(context.Editor.Options.TabSize); break; } } if (!variableDecarations.ContainsKey(name)) { continue; } var link = result.TextLinks.Find(l => l.Name == name); bool isNew = link == null; if (isNew) { link = new TextLink(name); if (!string.IsNullOrEmpty(variableDecarations [name].ToolTip)) { link.Tooltip = GettextCatalog.GetString(variableDecarations [name].ToolTip); } link.Values = new CodeTemplateListDataProvider(variableDecarations [name].Values); if (!string.IsNullOrEmpty(variableDecarations [name].Function)) { link.Values = expansion.RunFunction(context, null, variableDecarations [name].Function); } result.TextLinks.Add(link); } link.IsEditable = variableDecarations [name].IsEditable; link.IsIdentifier = variableDecarations [name].IsIdentifier; if (!string.IsNullOrEmpty(variableDecarations [name].Function)) { var functionResult = expansion.RunFunction(context, null, variableDecarations [name].Function); if (functionResult != null && functionResult.Count > 0) { string s = (string)functionResult [functionResult.Count - 1]; if (s == null) { if (variableDecarations.ContainsKey(name)) { s = variableDecarations [name].Default; } } if (s != null) { link.AddLink(new TextSegment(sb.Length, s.Length)); if (isNew) { link.GetStringFunc = delegate(Func <string, string> callback) { return(expansion.RunFunction(context, callback, variableDecarations [name].Function)); }; } sb.Append(s); } } else { AddDefaultValue(sb, link, name); } } else { AddDefaultValue(sb, link, name); } } sb.Append(code, lastOffset, code.Length - lastOffset); // format & indent template code var data = TextEditorFactory.CreateNewDocument(); data.Text = StringBuilderCache.ReturnAndFree(sb); data.TextChanged += delegate(object sender, MonoDevelop.Core.Text.TextChangeEventArgs e) { for (int i = 0; i < e.TextChanges.Count; ++i) { var change = e.TextChanges[i]; int delta = change.InsertionLength - change.RemovalLength; foreach (var link in result.TextLinks) { link.Links = link.Links.AdjustSegments(e).ToList(); } if (result.CaretEndOffset > change.Offset) { result.CaretEndOffset += delta; } } }; IndentCode(data, context.LineIndent); result.Code = data.Text; return(result); }
public TemplateResult FillVariables (TemplateContext context) { ExpansionObject expansion = CodeTemplateService.GetExpansionObject (this); TemplateResult result = new TemplateResult (); StringBuilder sb = new StringBuilder (); int lastOffset = 0; string code = context.TemplateCode; result.TextLinks = new List<TextLink> (); foreach (Match match in variableRegEx.Matches (code)) { string name = match.Groups[1].Value; sb.Append (code.Substring (lastOffset, match.Index - lastOffset)); lastOffset = match.Index + match.Length; if (string.IsNullOrEmpty (name)) { // $$ is interpreted as $ sb.Append ("$"); } else if (name == "end") { result.CaretEndOffset = sb.Length; } else if (name == "selected") { if (!string.IsNullOrEmpty (context.SelectedText)) { string indent = GetIndent (sb); string selection = Reindent (context.SelectedText, indent); sb.Append (selection); } } if (!variableDecarations.ContainsKey (name)) continue; TextLink link = result.TextLinks.Find (l => l.Name == name); bool isNew = link == null; if (isNew) { link = new TextLink (name); if (!string.IsNullOrEmpty (variableDecarations[name].ToolTip)) link.Tooltip = GettextCatalog.GetString (variableDecarations[name].ToolTip); link.Values = new CodeTemplateListDataProvider (variableDecarations[name].Values); if (!string.IsNullOrEmpty (variableDecarations[name].Function)) { link.Values = expansion.RunFunction (context, null, variableDecarations[name].Function); } result.TextLinks.Add (link); } link.IsEditable = variableDecarations[name].IsEditable; link.IsIdentifier = variableDecarations[name].IsIdentifier; if (!string.IsNullOrEmpty (variableDecarations[name].Function)) { IListDataProvider<string> functionResult = expansion.RunFunction (context, null, variableDecarations[name].Function); if (functionResult != null && functionResult.Count > 0) { string s = (string)functionResult[functionResult.Count - 1] ?? variableDecarations[name].Default; link.AddLink (new Segment (sb.Length, s.Length)); if (isNew) { link.GetStringFunc = delegate (Func<string, string> callback) { return expansion.RunFunction (context, callback, variableDecarations[name].Function); }; } sb.Append (s); } else { AddDefaultValue (sb, link, name); } } else { AddDefaultValue (sb, link, name); } } sb.Append (code.Substring (lastOffset, code.Length - lastOffset)); result.Code = sb.ToString (); return result; }