InsertAtCaret() public method

public InsertAtCaret ( string text ) : void
text string
return void
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);
                        }
                    }
                }
            }
        }
Example #2
0
        public static void InsertNewLine(TextEditorData data)
        {
            if (!data.CanEditSelection)
            {
                return;
            }

            using (var undo = data.OpenUndoGroup())
            {
                if (data.IsSomethingSelected)
                {
                    data.DeleteSelectedText();
                }
                switch (data.Options.IndentStyle)
                {
                case IndentStyle.None:
                    data.InsertAtCaret(data.EolMarker);
                    break;

                case IndentStyle.Auto:
                    data.EnsureCaretIsNotVirtual();
                    var sb = new StringBuilder(data.EolMarker);
                    sb.Append(data.Document.GetLineIndent(data.Caret.Line));
                    data.InsertAtCaret(sb.ToString());
                    break;

                case IndentStyle.Smart:
                    if (!data.HasIndentationTracker)
                    {
                        goto case IndentStyle.Auto;
                    }
                    NewLineSmartIndent(data);
                    break;

                case IndentStyle.Virtual:
                    if (!data.HasIndentationTracker)
                    {
                        goto case IndentStyle.Auto;
                    }
                    var oldLine   = data.Caret.Line;
                    var curLine   = data.GetLine(oldLine);
                    var indentCol = data.GetVirtualIndentationColumn(data.Caret.Location);
                    if (curLine.Length >= data.Caret.Column)
                    {
                        NewLineSmartIndent(data);
                        data.FixVirtualIndentation();
                        data.FixVirtualIndentation(oldLine);
                        break;
                    }
                    data.Insert(data.Caret.Offset, data.EolMarker);
                    data.FixVirtualIndentation(oldLine);
                    data.Caret.Column = indentCol;
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
        }
Example #3
0
 static void NewLineSmartIndent(TextEditorData data)
 {
     using (var undo = data.OpenUndoGroup()) {
         data.EnsureCaretIsNotVirtual();
         data.InsertAtCaret(data.EolMarker);
         data.InsertAtCaret(data.GetIndentationString(data.Caret.Location));
     }
 }
 static void NewLineSmartIndent(TextEditorData data)
 {
     using (var undo = data.OpenUndoGroup()) {
         data.EnsureCaretIsNotVirtual();
         data.InsertAtCaret(data.EolMarker);
         var line = data.Document.GetLine(data.Caret.Line);
         data.InsertAtCaret(data.GetIndentationString(line.EndOffset));
     }
 }
Example #5
0
        static void SmartBackspace(TextEditorData data, DocumentLine line)
        {
            var prevLine        = line.PreviousLine;
            var prevLineIsEmpty = prevLine != null && prevLine.Length == 0;

            var startOffset = prevLine != null ? prevLine.EndOffset : 0;
            var count       = data.Caret.Offset - startOffset;

            if (count < 0 || startOffset >= data.Length)
            {
                return;
            }
            data.Remove(startOffset, count);
            if (prevLine != null)
            {
                if (prevLineIsEmpty)
                {
                    if (line.Length - data.Caret.Column - 1 > 0 && data.HasIndentationTracker)
                    {
                        data.InsertAtCaret(data.IndentationTracker.GetIndentationString(data.Caret.Line));
                    }
                    else
                    {
                        data.Caret.Column = data.GetVirtualIndentationColumn(prevLine.Offset);
                    }
                }
                data.FixVirtualIndentation();
            }
        }
Example #6
0
		public static void InsertTab (TextEditorData data)
		{
			if (!data.CanEditSelection)
				return;
			if (data.IsMultiLineSelection && data.MainSelection.SelectionMode != SelectionMode.Block) {
				IndentSelection (data);
				return;
			}
			using (var undo = data.OpenUndoGroup ()) {
				string indentationString = "\t";
				bool convertTabToSpaces = data.Options.TabsToSpaces;
				
				if (!convertTabToSpaces && !data.Options.AllowTabsAfterNonTabs) {
					for (int i = 1; i < data.Caret.Column; i++) {
						if (data.Document.GetCharAt (data.Caret.Offset - i) != '\t') {
							convertTabToSpaces = true;
							break;
						}
					}
				}
					
				if (convertTabToSpaces) {
					DocumentLocation visualLocation = data.LogicalToVisualLocation (data.Caret.Location);
					int tabWidth = TextViewMargin.GetNextTabstop (data, visualLocation.Column) - visualLocation.Column;
					indentationString = new string (' ', tabWidth);
				}
				if (!(data.IsMultiLineSelection && data.MainSelection.SelectionMode == SelectionMode.Block) && data.IsSomethingSelected)
					data.DeleteSelectedText ();
				data.InsertAtCaret (indentationString);
			}
		}
Example #7
0
 public static void DuplicateLine(TextEditorData data)
 {
     using (var undoGroup = data.OpenUndoGroup()) {
         if (data.IsSomethingSelected)
         {
             var selectedText = data.SelectedText;
             data.ClearSelection();
             data.InsertAtCaret(selectedText);
         }
         else
         {
             DocumentLine line = data.Document.GetLine(data.Caret.Line);
             if (line == null)
             {
                 return;
             }
             if (line.DelimiterLength == 0)
             {
                 data.Insert(line.Offset, data.GetTextAt(line.SegmentIncludingDelimiter) + data.EolMarker);
             }
             else
             {
                 data.Insert(line.Offset, data.GetTextAt(line.SegmentIncludingDelimiter));
             }
         }
     }
 }
        static void PastePlainText(TextEditorData data, string text)
        {
            data.DeleteSelectedText(!data.IsSomethingSelected || data.MainSelection.SelectionMode != SelectionMode.Block);
            int offset = data.Caret.Offset;

            data.InsertAtCaret(text);
            data.PasteText(offset, text, data.Caret.Offset - offset);
        }
Example #9
0
 static void NewLineSmartIndent(TextEditorData data)
 {
     using (var undo = data.OpenUndoGroup()) {
         data.EnsureCaretIsNotVirtual();
         string indentString;
         if (data.HasIndentationTracker)
         {
             indentString = data.IndentationTracker.GetIndentationString(data.Caret.Location);
         }
         else
         {
             indentString = data.GetIndentationString(data.Caret.Location);
         }
         data.InsertAtCaret(data.EolMarker);
         data.InsertAtCaret(indentString);
     }
 }
Example #10
0
        static void NewLineSmartIndent(TextEditorData data)
        {
            using (var undo = data.OpenUndoGroup()) {
                data.EnsureCaretIsNotVirtual();

                var oldCaretLine = data.Caret.Location.Line;

                string indentString = data.GetIndentationString(data.Caret.Location);
                data.InsertAtCaret(data.EolMarker);

                // 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)
                {
                    data.InsertAtCaret(indentString);
                }
            }
        }
Example #11
0
		/// <summary>
		/// Transpose characters (Emacs C-t)
		/// </summary>
		public static void TransposeCharacters (TextEditorData data)
		{
			if (data.Caret.Offset == 0)
				return;
			DocumentLine line = data.Document.GetLine (data.Caret.Line);
			if (line == null)
				return;
			int transposeOffset = data.Caret.Offset - 1;
			char ch;
			if (data.Caret.Column == 0) {
				DocumentLine lineAbove = data.Document.GetLine (data.Caret.Line - 1);
				if (lineAbove.Length == 0 && line.Length == 0) 
					return;
				
				if (line.Length != 0) {
					ch = data.Document.GetCharAt (data.Caret.Offset);
					data.Remove (data.Caret.Offset, 1);
					data.Insert (lineAbove.Offset + lineAbove.Length, ch.ToString ());
					data.Document.CommitLineUpdate (data.Caret.Line - 1);
					return;
				}
				
				int lastCharOffset = lineAbove.Offset + lineAbove.Length - 1;
				ch = data.Document.GetCharAt (lastCharOffset);
				data.Remove (lastCharOffset, 1);
				data.InsertAtCaret (ch.ToString ());
				return;
			}
			
			int offset = data.Caret.Offset;
			if (data.Caret.Column >= line.Length + 1) {
				offset = line.Offset + line.Length - 1;
				transposeOffset = offset - 1;
				// case one char in line:
				if (transposeOffset < line.Offset) {
					DocumentLine lineAbove = data.Document.GetLine (data.Caret.Line - 1);
					transposeOffset = lineAbove.Offset + lineAbove.Length;
					ch = data.Document.GetCharAt (offset);
					data.Remove (offset, 1);
					data.Insert (transposeOffset, ch.ToString ());
					data.Caret.Offset = line.Offset;
					data.Document.CommitLineUpdate (data.Caret.Line - 1);
					return;
				}
			}
			
			ch = data.Document.GetCharAt (offset);
			data.Replace (offset, 1, data.Document.GetCharAt (transposeOffset).ToString ());
			data.Replace (transposeOffset, 1, ch.ToString ());
			if (data.Caret.Column < line.Length + 1)
				data.Caret.Offset = offset + 1;
		}
Example #12
0
 public static void DuplicateLine(TextEditorData data)
 {
     if (data.IsSomethingSelected)
     {
         data.InsertAtCaret(data.SelectedText);
     }
     else
     {
         DocumentLine line = data.Document.GetLine(data.Caret.Line);
         if (line == null)
         {
             return;
         }
         data.Insert(line.Offset, data.GetTextAt(line.SegmentIncludingDelimiter));
     }
 }
		public static void InsertNewLine (TextEditorData data)
		{
			if (!data.CanEditSelection)
				return;
			
			using (var undo = data.OpenUndoGroup ()) {
				if (data.IsSomethingSelected)
					data.DeleteSelectedText ();
				switch (data.Options.IndentStyle) {
				case IndentStyle.None:
					data.InsertAtCaret (data.EolMarker);
					break;
				case IndentStyle.Auto:
					data.EnsureCaretIsNotVirtual ();
					var sb = new StringBuilder (data.EolMarker);
					sb.Append (data.Document.GetLineIndent (data.Caret.Line));
					data.InsertAtCaret (sb.ToString ());
					break;
				case IndentStyle.Smart:
					if (!data.HasIndentationTracker)
						goto case IndentStyle.Auto;
					NewLineSmartIndent (data);
					break;
				case IndentStyle.Virtual:
					if (!data.HasIndentationTracker)
						goto case IndentStyle.Auto;
					var oldLine = data.Caret.Line;
					var curLine = data.GetLine (oldLine);
					var indentCol = data.GetVirtualIndentationColumn (data.Caret.Location);
					if (curLine.Length >= data.Caret.Column) {
						NewLineSmartIndent (data);
						data.FixVirtualIndentation ();
						data.FixVirtualIndentation (oldLine);
						break;
					}
					data.Insert (data.Caret.Offset, data.EolMarker);
					data.FixVirtualIndentation (oldLine);
					data.Caret.Column = indentCol;
					break;
				default:
					throw new ArgumentOutOfRangeException ();
				}
			}
		}
		static void NewLineSmartIndent (TextEditorData data)
		{
			using (var undo = data.OpenUndoGroup ()) {
				data.EnsureCaretIsNotVirtual ();
				data.InsertAtCaret (data.EolMarker);
				data.InsertAtCaret (data.GetIndentationString (data.Caret.Location));
			}
		}
		public static void InsertTab (TextEditorData data)
		{
			if (!data.CanEditSelection)
				return;
			if (data.IsMultiLineSelection && data.MainSelection.SelectionMode != SelectionMode.Block) {
				IndentSelection (data);
				return;
			}
			using (var undo = data.OpenUndoGroup ()) {
				string indentationString = "\t";
				bool convertTabToSpaces = data.TabsToSpaces;
				
				if (!convertTabToSpaces && !data.Options.AllowTabsAfterNonTabs) {
					for (int i = 1; i < data.Caret.Column; i++) {
						if (data.Document.GetCharAt (data.Caret.Offset - i) != '\t') {
							convertTabToSpaces = true;
							break;
						}
					}
				}
					
				if (convertTabToSpaces) {
					DocumentLocation visualLocation = data.LogicalToVisualLocation (data.Caret.Location);
					int tabWidth = TextViewMargin.GetNextTabstop (data, visualLocation.Column) - visualLocation.Column;
					indentationString = new string (' ', tabWidth);
				}
				if (data.IsSomethingSelected)
					data.DeleteSelectedText (data.MainSelection.SelectionMode != SelectionMode.Block);
				data.InsertAtCaret (indentationString);
			}
		}
Example #16
0
        public static void InsertNewLine(TextEditorData data)
        {
            if (!data.CanEditSelection)
            {
                return;
            }

            using (var undo = data.OpenUndoGroup()) {
                if (data.IsSomethingSelected)
                {
                    var end = data.MainSelection.End;
                    data.DeleteSelectedText();
                    if (end.Column == 1)
                    {
                        CaretMoveActions.InternalCaretMoveHome(data, true, false);
                        return;
                    }
                }
                switch (data.Options.IndentStyle)
                {
                case IndentStyle.None:
                    data.InsertAtCaret(data.EolMarker);
                    break;

                case IndentStyle.Auto:
                    data.EnsureCaretIsNotVirtual();
                    var indent = data.Document.GetLineIndent(data.Caret.Line);
                    data.InsertAtCaret(data.EolMarker);
                    data.EnsureCaretIsNotVirtual();
                    if (data.GetLine(data.Caret.Line).Length == 0)
                    {
                        data.InsertAtCaret(indent);
                    }
                    break;

                case IndentStyle.Smart:
                    if (!data.HasIndentationTracker)
                    {
                        goto case IndentStyle.Auto;
                    }
                    NewLineSmartIndent(data);
                    break;

                case IndentStyle.Virtual:
                    if (!data.HasIndentationTracker)
                    {
                        goto case IndentStyle.Auto;
                    }
                    var oldLine   = data.Caret.Line;
                    var curLine   = data.GetLine(oldLine);
                    var indentCol = data.GetVirtualIndentationColumn(data.Caret.Location);
                    if (curLine.Length >= data.Caret.Column)
                    {
                        NewLineSmartIndent(data);
                        data.FixVirtualIndentation();
                        data.FixVirtualIndentation(oldLine);
                        break;
                    }
                    data.Insert(data.Caret.Offset, data.EolMarker);
                    data.FixVirtualIndentation(oldLine);
                    data.Caret.Column = indentCol;
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
        }
Example #17
0
		public static void DuplicateLine (TextEditorData data)
		{
			using (var undoGroup = data.OpenUndoGroup ()) {
				if (data.IsSomethingSelected) {
					var selectedText = data.SelectedText;
					data.ClearSelection ();
					data.InsertAtCaret (selectedText);
				} else {
					DocumentLine line = data.Document.GetLine (data.Caret.Line);
					if (line == null)
						return;
					if (line.DelimiterLength == 0) {
						data.Insert (line.Offset, data.GetTextAt (line.SegmentIncludingDelimiter) + data.EolMarker);
					} else {
						data.Insert (line.Offset, data.GetTextAt (line.SegmentIncludingDelimiter));
					}
				}
			}
		}
		public static void Backspace (TextEditorData data, Action<TextEditorData> removeCharBeforeCaret)
		{
			if (!data.CanEditSelection)
				return;
			using (var undo = data.OpenUndoGroup ()) {
				if (data.IsSomethingSelected) {
					var visualAnchorLocation = data.LogicalToVisualLocation (data.MainSelection.Anchor);
					var visualLeadLocation = data.LogicalToVisualLocation (data.MainSelection.Lead);
					// case: zero width block selection
					if (data.MainSelection.SelectionMode == SelectionMode.Block && visualAnchorLocation.Column == visualLeadLocation.Column) {
						var col = data.MainSelection.Lead.Column;
						if (col <= DocumentLocation.MinColumn) {
							data.ClearSelection ();
							return;
						}
						bool preserve = data.Caret.PreserveSelection;
						data.Caret.PreserveSelection = true;
						for (int lineNumber = data.MainSelection.MinLine; lineNumber <= data.MainSelection.MaxLine; lineNumber++) {
							var lineSegment = data.Document.GetLine (lineNumber);
							int insertOffset = lineSegment.GetLogicalColumn (data, visualAnchorLocation.Column - 1) - 1;
							data.Remove (lineSegment.Offset + insertOffset, 1);
						}

						var visualColumn = data.GetLine (data.Caret.Location.Line).GetVisualColumn (data, col - 1);
						data.MainSelection = new Selection (
							new DocumentLocation (data.MainSelection.Anchor.Line, data.GetLine (data.MainSelection.Anchor.Line).GetLogicalColumn (data, visualColumn)),
							new DocumentLocation (data.MainSelection.Lead.Line, data.GetLine (data.MainSelection.Lead.Line).GetLogicalColumn (data, visualColumn)),
							SelectionMode.Block
						);

						data.Caret.PreserveSelection = preserve;
						data.Document.CommitMultipleLineUpdate (data.MainSelection.MinLine, data.MainSelection.MaxLine);
						return;
					}
					data.DeleteSelectedText (data.MainSelection.SelectionMode != SelectionMode.Block);
					return;
				}

				if (data.Caret.Line == DocumentLocation.MinLine && data.Caret.Column == DocumentLocation.MinColumn)
					return;
			
				// Virtual indentation needs to be fixed before to have the same behavior
				// if it's there or not (otherwise user has to press multiple backspaces in some cases)
				data.EnsureCaretIsNotVirtual ();

				var line = data.Document.GetLine (data.Caret.Line);
				// smart backspace (delete indentation)
				if (data.HasIndentationTracker && (data.IndentationTracker.SupportedFeatures & IndentatitonTrackerFeatures.SmartBackspace) != 0 && (data.Options.IndentStyle == IndentStyle.Smart || data.Options.IndentStyle == IndentStyle.Virtual)) {
					if (data.Caret.Column == data.GetVirtualIndentationColumn (data.Caret.Location)) {
						bool isAllIndent = line.GetIndentation (data.Document).Length == data.Caret.Column - 1;

						if (isAllIndent) {
							var prevLine = line.PreviousLine;
							var prevLineIsEmpty = prevLine != null && prevLine.Length == 0;

							var startOffset = prevLine != null ? prevLine.EndOffset : 0;
							data.Remove (startOffset, data.Caret.Offset - startOffset);
							if (prevLine != null) {
								if (prevLineIsEmpty) {
									if (line.Length - data.Caret.Column - 1 > 0 && data.HasIndentationTracker) {
										data.InsertAtCaret (data.IndentationTracker.GetIndentationString (data.Caret.Offset));
									} else {
										data.Caret.Column = data.GetVirtualIndentationColumn (prevLine.Offset);
									}
								}
								data.FixVirtualIndentation ();
							}
							return;
						}
					}
				}

				// normal backspace.
				if (data.Caret.Column > line.Length + 1) {
					data.Caret.Column = line.Length + 1;
				} else if (data.Caret.Offset == line.Offset) {
					DocumentLine lineAbove = data.Document.GetLine (data.Caret.Line - 1);
					if (lineAbove.Length == 0 && data.HasIndentationTracker && data.Options.IndentStyle == IndentStyle.Virtual) {
						data.Caret.Location = new DocumentLocation (data.Caret.Line - 1, data.IndentationTracker.GetVirtualIndentationColumn (data.Caret.Line - 1, 1));
						data.Replace (lineAbove.EndOffsetIncludingDelimiter - lineAbove.DelimiterLength, lineAbove.DelimiterLength, data.IndentationTracker.GetIndentationString (data.Caret.Line - 1, 1));
					} else {
						data.Remove (lineAbove.EndOffsetIncludingDelimiter - lineAbove.DelimiterLength, lineAbove.DelimiterLength);
					}
				} else {
					removeCharBeforeCaret (data);
				}

				// Needs to be fixed after, the line may just contain the indentation
				data.FixVirtualIndentation ();
			}
		}
Example #19
0
		public static void InsertNewLine (TextEditorData data)
		{
			if (!data.CanEditSelection)
				return;
			
			using (var undo = data.OpenUndoGroup ()) {
				if (data.IsSomethingSelected) {
					var end = data.MainSelection.End;
					data.DeleteSelectedText ();
					if (end.Column == 1) {
						CaretMoveActions.InternalCaretMoveHome (data, true, false);
						return;
					}
				}
				switch (data.Options.IndentStyle) {
				case IndentStyle.None:
					data.InsertAtCaret (data.EolMarker);
					break;
				case IndentStyle.Auto:
					data.EnsureCaretIsNotVirtual ();
					var indent = data.Document.GetLineIndent (data.Caret.Line);
					data.InsertAtCaret (data.EolMarker);
					data.EnsureCaretIsNotVirtual ();
					if (data.GetLine (data.Caret.Line).Length == 0)
						data.InsertAtCaret (indent);
					break;
				case IndentStyle.Smart:
					if (!data.HasIndentationTracker)
						goto case IndentStyle.Auto;
					NewLineSmartIndent (data);
					break;
				case IndentStyle.Virtual:
					if (!data.HasIndentationTracker)
						goto case IndentStyle.Auto;
					var oldLine = data.Caret.Line;
					var curLine = data.GetLine (oldLine);
					var indentCol = data.GetVirtualIndentationColumn (data.Caret.Location);
					if (curLine.Length >= data.Caret.Column) {
						NewLineSmartIndent (data);
						data.FixVirtualIndentation ();
						data.FixVirtualIndentation (oldLine);
						break;
					}
					data.Insert (data.Caret.Offset, data.EolMarker);
					data.FixVirtualIndentation (oldLine);
					data.Caret.Column = indentCol;
					break;
				default:
					throw new ArgumentOutOfRangeException ();
				}
			}
		}
		static void SmartBackspace (TextEditorData data, DocumentLine line)
		{
			var prevLine = line.PreviousLine;
			var prevLineIsEmpty = prevLine != null && prevLine.Length == 0;

			var startOffset = prevLine != null ? prevLine.EndOffset : 0;
			var count = data.Caret.Offset - startOffset;
			if (count < 0)
				return;
			data.Remove (startOffset, count);
			if (prevLine != null) {
				if (prevLineIsEmpty) {
					if (line.Length - data.Caret.Column - 1 > 0 && data.HasIndentationTracker) {
						data.InsertAtCaret (data.IndentationTracker.GetIndentationString (data.Caret.Offset));
					} else {
						data.Caret.Column = data.GetVirtualIndentationColumn (prevLine.Offset);
					}
				}
				data.FixVirtualIndentation ();
			}
		}
Example #21
0
        public static void Backspace(TextEditorData data, Action <TextEditorData> removeCharBeforeCaret)
        {
            if (!data.CanEditSelection)
            {
                return;
            }
            using (var undo = data.OpenUndoGroup()) {
                if (data.IsSomethingSelected)
                {
                    var visualAnchorLocation = data.LogicalToVisualLocation(data.MainSelection.Anchor);
                    var visualLeadLocation   = data.LogicalToVisualLocation(data.MainSelection.Lead);
                    // case: zero width block selection
                    if (data.MainSelection.SelectionMode == SelectionMode.Block && visualAnchorLocation.Column == visualLeadLocation.Column)
                    {
                        var col = data.MainSelection.Lead.Column;
                        if (col <= DocumentLocation.MinColumn)
                        {
                            data.ClearSelection();
                            return;
                        }
                        bool preserve = data.Caret.PreserveSelection;
                        data.Caret.PreserveSelection = true;
                        for (int lineNumber = data.MainSelection.MinLine; lineNumber <= data.MainSelection.MaxLine; lineNumber++)
                        {
                            var lineSegment  = data.Document.GetLine(lineNumber);
                            int insertOffset = lineSegment.GetLogicalColumn(data, visualAnchorLocation.Column - 1) - 1;
                            data.Remove(lineSegment.Offset + insertOffset, 1);
                        }

                        var visualColumn = data.GetLine(data.Caret.Location.Line).GetVisualColumn(data, col - 1);
                        data.MainSelection = new Selection(
                            new DocumentLocation(data.MainSelection.Anchor.Line, data.GetLine(data.MainSelection.Anchor.Line).GetLogicalColumn(data, visualColumn)),
                            new DocumentLocation(data.MainSelection.Lead.Line, data.GetLine(data.MainSelection.Lead.Line).GetLogicalColumn(data, visualColumn)),
                            SelectionMode.Block
                            );

                        data.Caret.PreserveSelection = preserve;
                        data.Document.CommitMultipleLineUpdate(data.MainSelection.MinLine, data.MainSelection.MaxLine);
                        return;
                    }
                    data.DeleteSelectedText(data.MainSelection.SelectionMode != SelectionMode.Block);
                    return;
                }

                if (data.Caret.Line == DocumentLocation.MinLine && data.Caret.Column == DocumentLocation.MinColumn)
                {
                    return;
                }

                // Virtual indentation needs to be fixed before to have the same behavior
                // if it's there or not (otherwise user has to press multiple backspaces in some cases)
                data.EnsureCaretIsNotVirtual();

                var line = data.Document.GetLine(data.Caret.Line);
                // smart backspace (delete indentation)
                if (data.HasIndentationTracker && (data.IndentationTracker.SupportedFeatures & IndentatitonTrackerFeatures.SmartBackspace) != 0 && (data.Options.IndentStyle == IndentStyle.Smart || data.Options.IndentStyle == IndentStyle.Virtual))
                {
                    if (data.Caret.Column == data.GetVirtualIndentationColumn(data.Caret.Location))
                    {
                        bool isAllIndent = line.GetIndentation(data.Document).Length == data.Caret.Column - 1;

                        if (isAllIndent)
                        {
                            var prevLine        = line.PreviousLine;
                            var prevLineIsEmpty = prevLine != null && prevLine.Length == 0;

                            var startOffset = prevLine != null ? prevLine.EndOffset : 0;
                            data.Remove(startOffset, data.Caret.Offset - startOffset);
                            if (prevLine != null)
                            {
                                if (prevLineIsEmpty)
                                {
                                    if (line.Length - data.Caret.Column - 1 > 0 && data.HasIndentationTracker)
                                    {
                                        data.InsertAtCaret(data.IndentationTracker.GetIndentationString(data.Caret.Offset));
                                    }
                                    else
                                    {
                                        data.Caret.Column = data.GetVirtualIndentationColumn(prevLine.Offset);
                                    }
                                }
                                data.FixVirtualIndentation();
                            }
                            return;
                        }
                    }
                }

                // normal backspace.
                if (data.Caret.Column > line.Length + 1)
                {
                    data.Caret.Column = line.Length + 1;
                }
                else if (data.Caret.Offset == line.Offset)
                {
                    DocumentLine lineAbove = data.Document.GetLine(data.Caret.Line - 1);
                    if (lineAbove.Length == 0 && data.HasIndentationTracker && data.Options.IndentStyle == IndentStyle.Virtual)
                    {
                        data.Caret.Location = new DocumentLocation(data.Caret.Line - 1, data.IndentationTracker.GetVirtualIndentationColumn(data.Caret.Line - 1, 1));
                        data.Replace(lineAbove.EndOffsetIncludingDelimiter - lineAbove.DelimiterLength, lineAbove.DelimiterLength, data.IndentationTracker.GetIndentationString(data.Caret.Line - 1, 1));
                    }
                    else
                    {
                        data.Remove(lineAbove.EndOffsetIncludingDelimiter - lineAbove.DelimiterLength, lineAbove.DelimiterLength);
                    }
                }
                else
                {
                    removeCharBeforeCaret(data);
                }

                // Needs to be fixed after, the line may just contain the indentation
                data.FixVirtualIndentation();
            }
        }
		/// <summary>
		/// Transpose characters (Emacs C-t)
		/// </summary>
		public static void TransposeCharacters (TextEditorData data)
		{
			if (data.Caret.Offset == 0)
				return;
			DocumentLine line = data.Document.GetLine (data.Caret.Line);
			if (line == null)
				return;
			int transposeOffset = data.Caret.Offset - 1;
			char ch;
			if (data.Caret.Column == 0) {
				DocumentLine lineAbove = data.Document.GetLine (data.Caret.Line - 1);
				if (lineAbove.Length == 0 && line.Length == 0) 
					return;
				
				if (line.Length != 0) {
					ch = data.Document.GetCharAt (data.Caret.Offset);
					data.Remove (data.Caret.Offset, 1);
					data.Insert (lineAbove.Offset + lineAbove.Length, ch.ToString ());
					data.Document.CommitLineUpdate (data.Caret.Line - 1);
					return;
				}
				
				int lastCharOffset = lineAbove.Offset + lineAbove.Length - 1;
				ch = data.Document.GetCharAt (lastCharOffset);
				data.Remove (lastCharOffset, 1);
				data.InsertAtCaret (ch.ToString ());
				return;
			}
			
			int offset = data.Caret.Offset;
			if (data.Caret.Column >= line.Length + 1) {
				offset = line.Offset + line.Length - 1;
				transposeOffset = offset - 1;
				// case one char in line:
				if (transposeOffset < line.Offset) {
					DocumentLine lineAbove = data.Document.GetLine (data.Caret.Line - 1);
					transposeOffset = lineAbove.Offset + lineAbove.Length;
					ch = data.Document.GetCharAt (offset);
					data.Remove (offset, 1);
					data.Insert (transposeOffset, ch.ToString ());
					data.Caret.Offset = line.Offset;
					data.Document.CommitLineUpdate (data.Caret.Line - 1);
					return;
				}
			}
			
			ch = data.Document.GetCharAt (offset);
			data.Replace (offset, 1, data.Document.GetCharAt (transposeOffset).ToString ());
			data.Replace (transposeOffset, 1, ch.ToString ());
			if (data.Caret.Column < line.Length + 1)
				data.Caret.Offset = offset + 1;
		}
Example #23
0
		static void NewLineSmartIndent (TextEditorData data)
		{
			using (var undo = data.OpenUndoGroup ()) {
				data.EnsureCaretIsNotVirtual ();
				data.InsertAtCaret (data.EolMarker);
				var line = data.Document.GetLine (data.Caret.Line);
				data.InsertAtCaret (data.GetIndentationString (line.EndOffset));
			}
		}
		public static void DuplicateLine (TextEditorData data)
		{
			if (data.IsSomethingSelected) {
				var selectedText = data.SelectedText;
				data.ClearSelection ();
				data.InsertAtCaret (selectedText);
			}
			else {
				DocumentLine line = data.Document.GetLine (data.Caret.Line);
				if (line == null)
					return;
				data.Insert (line.Offset, data.GetTextAt (line.SegmentIncludingDelimiter));
			}
		}
Example #25
0
		static void PastePlainText (TextEditorData data, string text)
		{
			data.DeleteSelectedText (!data.IsSomethingSelected || data.MainSelection.SelectionMode != SelectionMode.Block);
			int offset = data.Caret.Offset;
			data.InsertAtCaret (text);
			data.PasteText (offset, text, data.Caret.Offset - offset);
		}
Example #26
0
		static int PasteFrom (Clipboard clipboard, TextEditorData data, bool preserveSelection, int insertionOffset, bool preserveState)
		{
			int result = -1;
			if (!data.CanEdit (data.Document.OffsetToLineNumber (insertionOffset)))
				return result;
			if (clipboard.WaitIsTargetAvailable (CopyOperation.MD_ATOM)) {
				clipboard.RequestContents (CopyOperation.MD_ATOM, delegate(Clipboard clp, SelectionData selectionData) {
					if (selectionData.Length > 0) {
						byte[] selBytes = selectionData.Data;
	
						string text = System.Text.Encoding.UTF8.GetString (selBytes, 1, selBytes.Length - 1);
						bool pasteBlock = (selBytes [0] & 1) == 1;
						bool pasteLine = (selBytes [0] & 2) == 2;
						
						using (var undo = data.OpenUndoGroup ()) {
							if (preserveSelection && data.IsSomethingSelected)
								data.DeleteSelectedText ();
							
							data.Caret.PreserveSelection = true;
							if (pasteBlock) {
								string[] lines = text.Split ('\r');
								int lineNr = data.Document.OffsetToLineNumber (insertionOffset);
								int col = insertionOffset - data.Document.GetLine (lineNr).Offset;
								int visCol = data.Document.GetLine (lineNr).GetVisualColumn (data, col);
								DocumentLine curLine;
								int lineCol = col;
								result = 0;
								for (int i = 0; i < lines.Length; i++) {
									while (data.Document.LineCount <= lineNr + i) {
										data.Insert (data.Document.TextLength, Environment.NewLine);
										result += Environment.NewLine.Length;
									}
									curLine = data.Document.GetLine (lineNr + i);
									if (lines [i].Length > 0) {
										lineCol = curLine.GetLogicalColumn (data, visCol);
										if (curLine.Length + 1 < lineCol) {
											result += lineCol - curLine.Length;
											data.Insert (curLine.Offset + curLine.Length, new string (' ', lineCol - curLine.Length));
										}
										data.Insert (curLine.Offset + lineCol, lines [i]);
										result += lines [i].Length;
									}
									if (!preserveState)
										data.Caret.Offset = curLine.Offset + lineCol + lines [i].Length;
								}
							} else if (pasteLine) {
								result = text.Length;
								DocumentLine curLine = data.Document.GetLine (data.Caret.Line);
								data.Insert (curLine.Offset, text + data.EolMarker);
							} else {
								int offset = data.Caret.Offset;
								data.InsertAtCaret (text);
								data.PasteText (offset, text, data.Caret.Offset - offset);
							}
							/*				data.MainSelection = new Selection (data.Document.OffsetToLocation (insertionOffset),
							                                    data.Caret.Location,
							                                    lines.Length > 1 ? SelectionMode.Block : SelectionMode.Normal);*/
							if (!preserveState)
								data.ClearSelection ();
							data.Caret.PreserveSelection = false;
						}
					}
				});
				// we got MD_ATOM text - no need to request text. (otherwise buffer may get copied twice).
				return result;
			}
			
			if (result < 0 && clipboard.WaitIsTextAvailable ()) {
				clipboard.RequestText (delegate(Clipboard clp, string text) {
					if (string.IsNullOrEmpty (text))
						return;
					using (var undo = data.OpenUndoGroup ()) {
						int caretPos = data.Caret.Offset;
						if (data.IsSomethingSelected && data.MainSelection.SelectionMode == SelectionMode.Block) {
							data.Caret.PreserveSelection = true;
							data.DeleteSelectedText (false);
							int textLength = 0;
							int minLine = data.MainSelection.MinLine;
							int maxLine = data.MainSelection.MaxLine;
							var visualInsertLocation = data.LogicalToVisualLocation (data.Caret.Location);
							for (int lineNumber = minLine; lineNumber <= maxLine; lineNumber++) {
								DocumentLine lineSegment = data.GetLine (lineNumber);
								int insertOffset = lineSegment.GetLogicalColumn (data, visualInsertLocation.Column) - 1;
								if (lineSegment.Length < insertOffset) {
									int visualLastColumn = lineSegment.GetVisualColumn (data, lineSegment.Length + 1);
									int charsToInsert = visualInsertLocation.Column - visualLastColumn;
									int spaceCount = charsToInsert % data.Options.TabSize;
									string textToInsert = new string ('\t', (charsToInsert - spaceCount) / data.Options.TabSize) + new string (' ', spaceCount) + text;
									insertOffset = lineSegment.Length;
									int insertedChars = data.Insert (lineSegment.Offset + insertOffset, textToInsert);
									data.PasteText (lineSegment.Offset + insertOffset, textToInsert, insertedChars);
								} else {
									textLength = data.Insert (lineSegment.Offset + insertOffset, text);
									data.PasteText (lineSegment.Offset + insertOffset, text, textLength);
								}
							}
							
							data.MainSelection.Anchor = new DocumentLocation (System.Math.Max (DocumentLocation.MinLine, data.Caret.Line == minLine ? maxLine : minLine), System.Math.Max (DocumentLocation.MinColumn, data.Caret.Column - textLength));
							data.MainSelection.Lead = new DocumentLocation (data.Caret.Line, data.Caret.Column);
							data.Caret.PreserveSelection = false;
							data.Document.CommitMultipleLineUpdate (data.MainSelection.MinLine, data.MainSelection.MaxLine);
						} else {
							TextSegment selection = data.SelectionRange;
							if (preserveSelection && data.IsSomethingSelected)
								data.DeleteSelectedText ();
							data.Caret.PreserveSelection = true;
							//int oldLine = data.Caret.Line;
							int textLength = data.Insert (insertionOffset, text);
							result = textLength;
		
							if (data.IsSomethingSelected && data.SelectionRange.Offset >= insertionOffset)
								data.SelectionRange = new TextSegment (data.SelectionRange.Offset + textLength, data.SelectionRange.Length);
							if (data.IsSomethingSelected && data.MainSelection.GetAnchorOffset (data) >= insertionOffset)
								data.MainSelection.Anchor = data.Document.OffsetToLocation (data.MainSelection.GetAnchorOffset (data) + textLength);
							
							data.Caret.PreserveSelection = false;
							if (!preserveState) {
							} else {
								if (!selection.IsInvalid) {
									int offset = selection.Offset;
									if (offset >= insertionOffset)
										offset += textLength;
									data.SelectionRange = new TextSegment (offset, selection.Length);
								}
							}
							data.PasteText (insertionOffset, text, textLength);
						}
					}
				});
			}
			
			return result;
		}
Example #27
0
		static void NewLineSmartIndent (TextEditorData data)
		{
			using (var undo = data.OpenUndoGroup ()) {
				data.EnsureCaretIsNotVirtual ();
				string indentString;
				if (data.HasIndentationTracker) {
					indentString = data.IndentationTracker.GetIndentationString (data.Caret.Location);
				} else {
					indentString = data.GetIndentationString (data.Caret.Location);
				}
				data.InsertAtCaret (data.EolMarker);
				data.InsertAtCaret (indentString);
			}
		}