static int DeleteWordBeforeCaret (MonoDevelop.Ide.Gui.TextEditor editor) { int offset = editor.CursorPosition; int start = FindPrevWordStart (editor, offset); editor.DeleteText (start, offset - start); return start; }
/// <summary> /// Don't use this unless you're implementing ICodeTemplateWidget. Use Insert instead. /// </summary> public TemplateResult InsertTemplateContents (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 = document.TextEditorData; int offset = editor.CursorPosition; 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) { document.TextEditorData.Caret.Offset = offset + template.CaretEndOffset; } else { document.TextEditorData.Caret.Offset= offset + template.Code.Length; } return template; }
// todo: better code formatting !!! string GetIndent (MonoDevelop.Ide.Gui.TextEditor d, int lineNumber, int terminateIndex) { string lineText = d.GetLineText (lineNumber); if(terminateIndex > 0) lineText = lineText.Substring(0, terminateIndex); StringBuilder whitespaces = new StringBuilder (); foreach (char ch in lineText) { if (!char.IsWhiteSpace (ch)) break; whitespaces.Append (ch); } return whitespaces.ToString (); }
public static string GetWordBeforeCaret (MonoDevelop.Ide.Gui.TextEditor editor) { int offset = editor.CursorPosition; int start = FindPrevWordStart (editor, offset); return editor.GetText (start, offset); }
static int FindPrevWordStart (MonoDevelop.Ide.Gui.TextEditor editor, int offset) { while (--offset >= 0 && !Char.IsWhiteSpace (editor.GetCharAt (offset))) ; return ++offset; }