コード例 #1
0
ファイル: CodeTemplate.cs プロジェクト: yayanyang/monodevelop
        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);
        }
コード例 #2
0
		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;
		}