Пример #1
0
        //MonoDevelop.Ide.Gui.Document document)
        /// <summary>
        /// Don't use this unless you're implementing ICodeTemplateWidget. Use Insert instead.
        /// </summary>
        /*		public TemplateResult InsertTemplateContents (TextEditor editor)//(MonoDevelop.Ide.Gui.Document document)
        {
            //ProjectDom dom = document.Dom;
            //ParsedDocument doc = document.ParsedDocument ?? MonoDevelop.Projects.Dom.Parser.ProjectDomService.GetParsedDocument (dom, document.FileName);
            //MonoDevelop.Ide.Gui.TextEditor editor = document.TextEditor;

            Mono.TextEditor.TextEditorData data = editor.GetTextEditorData();

            int offset = data.Caret.Offset;
            int line, col;
            editor.GetLineColumnFromPosition (offset, out line, out col);
        //			string leadingWhiteSpace = GetLeadingWhiteSpace (editor, editor.CursorLine);

            TemplateContext context = new TemplateContext {
                Template = this,
                //Document = document,
                //ProjectDom = dom,
                //ParsedDocument = doc,
                InsertPosition = new DomLocation (line, col),
                LineIndent = GetIndent (editor, line, 0),
                TemplateCode = IndentCode (Code, document.TextEditorData.EolMarker, GetIndent (editor, line, 0))
            };

            if (data.IsSomethingSelected) {
                int start = data.SelectionRange.Offset;
                while (Char.IsWhiteSpace (data.Document.GetCharAt (start))) {
                    start++;
                }
                int end = data.SelectionRange.EndOffset;
                while (Char.IsWhiteSpace (data.Document.GetCharAt (end - 1))) {
                    end--;
                }
                context.LineIndent = GetIndent (editor, data.Document.OffsetToLineNumber (start) + 1, 0);
                context.SelectedText = RemoveIndent (data.Document.GetTextBetween (start, end), context.LineIndent);
                data.Remove (start, end - start);
                offset = start;
            } else {
                string word = GetWordBeforeCaret (editor).Trim ();
                if (word.Length > 0)
                    offset = DeleteWordBeforeCaret (editor);
            }

            TemplateResult template = FillVariables (context);
            template.InsertPosition = offset;
            document.TextEditorData.Insert (offset, template.Code);

            if (template.CaretEndOffset >= 0) {
                data.Caret.Offset = offset + template.CaretEndOffset;
            } else {
                data.Caret.Offset= offset + template.Code.Length;
            }
            return template;
        }*/
        public CodeTemplate.TemplateResult InsertTemplateContents(TextEditor editor)
        {
            //ProjectDom dom = document.Dom;
            //ParsedDocument doc = document.ParsedDocument ?? MonoDevelop.Projects.Dom.Parser.ProjectDomService.GetParsedDocument (dom, document.FileName);
            //Mono.TextEditor.TextEditorData data = document.Editor;
            Mono.TextEditor.TextEditorData data = editor.GetTextEditorData();

            int offset = data.Caret.Offset;
            //			string leadingWhiteSpace = GetLeadingWhiteSpace (editor, editor.CursorLine);

            TemplateContext context = new TemplateContext {
                Template = this,
                Document = data,
                //ProjectDom = dom,
                //ParsedDocument = doc,
                InsertPosition = data.Caret.Location,
                LineIndent = data.Document.GetLineIndent (data.Caret.Line),
                TemplateCode = Code
            };
            if (editor.IsSomethingSelected) {//data.IsSomethingSelected
                int start = data.SelectionRange.Offset;
                while (Char.IsWhiteSpace (data.Document.GetCharAt (start))) {
                    start++;
                }
                int end = data.SelectionRange.EndOffset;
                while (Char.IsWhiteSpace (data.Document.GetCharAt (end - 1))) {
                    end--;
                }
                context.LineIndent = data.Document.GetLineIndent (data.Document.OffsetToLineNumber (start));
                context.SelectedText = RemoveIndent (data.Document.GetTextBetween (start, end), context.LineIndent);
                data.Remove (start, end - start);
                offset = start;
            } else {
                string word = GetWordBeforeCaret (editor).Trim ();
                if (word.Length > 0)
                    offset = DeleteWordBeforeCaret (editor);
            }

            TemplateResult template = FillVariables (context);
            template.InsertPosition = offset;
            data.Insert (offset, template.Code);

            int newoffset;
            if (template.CaretEndOffset >= 0) {
                newoffset = offset + template.CaretEndOffset;
            } else {
                newoffset = offset + template.Code.Length;
            }
            editor.Caret.Location = editor.Document.OffsetToLocation (newoffset) ;

            //			if (PropertyService.Get ("OnTheFlyFormatting", false)) {
            //				string mt = DesktopService.GetMimeTypeForUri (document.FileName);
            //				var formatter = MonoDevelop.Ide.CodeFormatting.CodeFormatterService.GetFormatter (mt);
            //				if (formatter != null && formatter.SupportsOnTheFlyFormatting) {
            //					document.Editor.Document.BeginAtomicUndo ();
            //					formatter.OnTheFlyFormat (document.Project != null ? document.Project.Policies : null,
            //						document.Editor, offset, offset + length);
            //					document.Editor.Document.EndAtomicUndo ();
            //				}
            //			}
            return template;
        }
Пример #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 = 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;
 }