Mono.TextEditor.TextEditorData.InsertAtCaret C# (CSharp) Method

InsertAtCaret() public method

public InsertAtCaret ( string text ) : void
text string
return void
		public void InsertAtCaret (string text)
		{
			if (String.IsNullOrEmpty (text))
				return;
			using (var undo = OpenUndoGroup ()) {
				DeleteSelectedText (IsSomethingSelected ? MainSelection.SelectionMode != SelectionMode.Block : true);
				// Needs to be called after delete text, delete text handles virtual caret postitions itself,
				// but afterwards the virtual position may need to be restored.
				EnsureCaretIsNotVirtual ();

				if (IsSomethingSelected && MainSelection.SelectionMode == SelectionMode.Block) {
					var visualInsertLocation = LogicalToVisualLocation (MainSelection.Anchor);
					var selection = MainSelection;
					Caret.PreserveSelection = true;
					for (int lineNumber = selection.MinLine; lineNumber <= selection.MaxLine; lineNumber++) {
						var lineSegment = GetLine (lineNumber);
						int insertOffset = lineSegment.GetLogicalColumn (this, visualInsertLocation.Column) - 1;
						string textToInsert;
						if (lineSegment.Length < insertOffset) {
							int visualLastColumn = lineSegment.GetVisualColumn (this, lineSegment.Length + 1);
							int charsToInsert = visualInsertLocation.Column - visualLastColumn;
							int spaceCount = charsToInsert % Options.TabSize;
							textToInsert = new string ('\t', (charsToInsert - spaceCount) / Options.TabSize) + new string (' ', spaceCount) + text;
							insertOffset = lineSegment.Length;
						} else {
							textToInsert = text;
						}
						Insert (lineSegment.Offset + insertOffset, textToInsert);
					}
					var visualColumn = GetLine (Caret.Location.Line).GetVisualColumn (this, Caret.Column);
					MainSelection = new Selection (
						new DocumentLocation (selection.Anchor.Line, GetLine (selection.Anchor.Line).GetLogicalColumn (this, visualColumn)),
						new DocumentLocation (selection.Lead.Line, GetLine (selection.Lead.Line).GetLogicalColumn (this, visualColumn)),
						SelectionMode.Block
					);
					Caret.PreserveSelection = false;
					Document.CommitMultipleLineUpdate (selection.MinLine, selection.MaxLine);
				} else {
					EnsureCaretIsNotVirtual ();
					Insert (Caret.Offset, text);
				}
			}
		}

Usage Example

Example #1
0
        static void NewLineSmartIndent(TextEditorData data)
        {
            using (var undo = data.OpenUndoGroup()) {
                data.EnsureCaretIsNotVirtual();

                var oldCaretLine = data.Caret.Location.Line;
                var indentString = data.IndentationTracker.GetIndentationString(data.Caret.Line);
                data.InsertAtCaret(data.EolMarker);

                if (data.HasIndentationTracker)
                {
                    // Don't insert the indent string if the EOL insertion modified the caret location in an unexpected fashion
                    //  (This likely means someone has custom logic regarding insertion of the EOL)
                    if (data.Caret.Location.Line == oldCaretLine + 1 && data.Caret.Location.Column == 1)
                    {
                        var line                    = data.GetLine(data.Caret.Line);
                        var currentIndent           = line.GetIndentation(data.Document);
                        var currentCalculatedIndent = data.IndentationTracker.GetIndentationString(data.Caret.Line);
                        if (!string.IsNullOrEmpty(currentCalculatedIndent))
                        {
                            indentString = currentCalculatedIndent;
                        }
                        if (indentString != currentIndent)
                        {
                            data.InsertAtCaret(indentString);
                        }
                    }
                }
            }
        }
All Usage Examples Of Mono.TextEditor.TextEditorData::InsertAtCaret